- unroll loop
[m6w6/ext-http] / http_methods.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "php.h"
23 #include "php_http.h"
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_cache_api.h"
27 #include "php_http_curl_api.h"
28 #include "php_http_date_api.h"
29 #include "php_http_headers_api.h"
30 #include "php_http_message_api.h"
31 #include "php_http_send_api.h"
32 #include "php_http_url_api.h"
33
34 #include "php_http_message_object.h"
35 #include "php_http_response_object.h"
36 #include "php_http_request_object.h"
37 #include "php_http_exception_object.h"
38
39 #ifdef ZEND_ENGINE_2
40
41 /* {{{ HttpResponse */
42
43 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
44 *
45 * Instantiates a new HttpResponse object, which can be used to send
46 * any data/resource/file to an HTTP client with caching and multiple
47 * ranges/resuming support.
48 *
49 * NOTE: GZIPping is not implemented yet.
50 */
51 PHP_METHOD(HttpResponse, __construct)
52 {
53 zend_bool do_cache = 0, do_gzip = 0;
54 getObject(http_response_object, obj);
55
56 SET_EH_THROW_HTTP();
57 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
58 UPD_PROP(obj, long, cache, do_cache);
59 UPD_PROP(obj, long, gzip, do_gzip);
60 }
61 SET_EH_NORMAL();
62 }
63 /* }}} */
64
65 /* {{{ proto bool HttpResponse::setCache(bool cache)
66 *
67 * Whether it sould be attempted to cache the entitity.
68 * This will result in necessary caching headers and checks of clients
69 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
70 * matches a "304 Not Modified" status code will be issued.
71 *
72 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
73 * to something more appropriate than "no-cache"!
74 */
75 PHP_METHOD(HttpResponse, setCache)
76 {
77 zend_bool do_cache = 0;
78 getObject(http_response_object, obj);
79
80 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
81 RETURN_FALSE;
82 }
83
84 UPD_PROP(obj, long, cache, do_cache);
85 RETURN_TRUE;
86 }
87 /* }}} */
88
89 /* {{{ proto bool HttpResponse::getCache()
90 *
91 * Get current caching setting.
92 */
93 PHP_METHOD(HttpResponse, getCache)
94 {
95 zval *do_cache = NULL;
96 getObject(http_response_object, obj);
97
98 NO_ARGS;
99
100 do_cache = GET_PROP(obj, cache);
101 RETURN_BOOL(Z_LVAL_P(do_cache));
102 }
103 /* }}}*/
104
105 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
106 *
107 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
108 */
109 PHP_METHOD(HttpResponse, setGzip)
110 {
111 zend_bool do_gzip = 0;
112 getObject(http_response_object, obj);
113
114 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
115 RETURN_FALSE;
116 }
117
118 UPD_PROP(obj, long, gzip, do_gzip);
119 RETURN_TRUE;
120 }
121 /* }}} */
122
123 /* {{{ proto bool HttpResponse::getGzip()
124 *
125 * Get current gzipping setting.
126 */
127 PHP_METHOD(HttpResponse, getGzip)
128 {
129 zval *do_gzip = NULL;
130 getObject(http_response_object, obj);
131
132 NO_ARGS;
133
134 do_gzip = GET_PROP(obj, gzip);
135 RETURN_BOOL(Z_LVAL_P(do_gzip));
136 }
137 /* }}} */
138
139 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
140 *
141 * Set a custom cache-control header, usually being "private" or "public"; if
142 * $raw is set to true the header will be sent as-is.
143 */
144 PHP_METHOD(HttpResponse, setCacheControl)
145 {
146 char *ccontrol;
147 int cc_len;
148 zend_bool raw = 0;
149 getObject(http_response_object, obj);
150
151 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
152 RETURN_FALSE;
153 }
154
155 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
156 http_error_ex(E_WARNING, HTTP_E_PARAM, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
157 RETURN_FALSE;
158 }
159
160 UPD_PROP(obj, long, raw_cache_header, raw);
161 UPD_PROP(obj, string, cacheControl, ccontrol);
162 RETURN_TRUE;
163 }
164 /* }}} */
165
166 /* {{{ proto string HttpResponse::getCacheControl()
167 *
168 * Get current Cache-Control header setting.
169 */
170 PHP_METHOD(HttpResponse, getCacheControl)
171 {
172 zval *ccontrol;
173 getObject(http_response_object, obj);
174
175 NO_ARGS;
176
177 ccontrol = GET_PROP(obj, cacheControl);
178 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
179 }
180 /* }}} */
181
182 /* {{{ proto bool HttpResponse::setContentType(string content_type)
183 *
184 * Set the content-type of the sent entity.
185 */
186 PHP_METHOD(HttpResponse, setContentType)
187 {
188 char *ctype;
189 int ctype_len;
190 getObject(http_response_object, obj);
191
192 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
193 RETURN_FALSE;
194 }
195
196 if (!strchr(ctype, '/')) {
197 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
198 RETURN_FALSE;
199 }
200
201 UPD_PROP(obj, string, contentType, ctype);
202
203 RETURN_TRUE;
204 }
205 /* }}} */
206
207 /* {{{ proto string HttpResponse::getContentType()
208 *
209 * Get current Content-Type header setting.
210 */
211 PHP_METHOD(HttpResponse, getContentType)
212 {
213 zval *ctype;
214 getObject(http_response_object, obj);
215
216 NO_ARGS;
217
218 ctype = GET_PROP(obj, contentType);
219 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
220 }
221 /* }}} */
222
223 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
224 *
225 * Set the Content-Disposition of the sent entity. This setting aims to suggest
226 * the receiveing user agent how to handle the sent entity; usually the client
227 * will show the user a "Save As..." popup.
228 */
229 PHP_METHOD(HttpResponse, setContentDisposition)
230 {
231 char *file;
232 int file_len;
233 zend_bool is_inline = 0;
234 getObject(http_response_object, obj);
235
236 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_inline)) {
237 RETURN_FALSE;
238 }
239
240 UPD_PROP(obj, string, dispoFile, file);
241 UPD_PROP(obj, long, dispoInline, is_inline);
242 RETURN_TRUE;
243 }
244 /* }}} */
245
246 /* {{{ proto array HttpResponse::getContentDisposition()
247 *
248 * Get current Content-Disposition setting.
249 * Will return an associative array like:
250 * <pre>
251 * array(
252 * 'filename' => 'foo.bar',
253 * 'inline' => false
254 * )
255 * </pre>
256 */
257 PHP_METHOD(HttpResponse, getContentDisposition)
258 {
259 zval *file;
260 zval *is_inline;
261 getObject(http_response_object, obj);
262
263 if (ZEND_NUM_ARGS()) {
264 WRONG_PARAM_COUNT;
265 }
266
267 file = GET_PROP(obj, dispoFile);
268 is_inline = GET_PROP(obj, dispoInline);
269
270 array_init(return_value);
271 add_assoc_stringl(return_value, "filename", Z_STRVAL_P(file), Z_STRLEN_P(file), 1);
272 add_assoc_bool(return_value, "inline", Z_LVAL_P(is_inline));
273 }
274 /* }}} */
275
276 /* {{{ proto bool HttpResponse::setETag(string etag)
277 *
278 * Set a custom ETag. Use this only if you know what you're doing.
279 */
280 PHP_METHOD(HttpResponse, setETag)
281 {
282 char *etag;
283 int etag_len;
284 getObject(http_response_object, obj);
285
286 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
287 RETURN_FALSE;
288 }
289
290 UPD_PROP(obj, string, eTag, etag);
291 RETURN_TRUE;
292 }
293 /* }}} */
294
295 /* {{{ proto string HttpResponse::getETag()
296 *
297 * Get the previously set custom ETag.
298 */
299 PHP_METHOD(HttpResponse, getETag)
300 {
301 zval *etag;
302 getObject(http_response_object, obj);
303
304 NO_ARGS;
305
306 etag = GET_PROP(obj, eTag);
307 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
308 }
309 /* }}} */
310
311 /* {{{ proto bool HttpResponse::setData(string data)
312 *
313 * Set the data to be sent.
314 */
315 PHP_METHOD(HttpResponse, setData)
316 {
317 zval *the_data;
318 getObject(http_response_object, obj);
319
320 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &the_data)) {
321 RETURN_FALSE;
322 }
323
324 convert_to_string_ex(&the_data);
325 SET_PROP(obj, data, the_data);
326 UPD_PROP(obj, long, lastModified, http_last_modified(the_data, SEND_DATA));
327 UPD_PROP(obj, long, send_mode, SEND_DATA);
328 RETURN_TRUE;
329 }
330 /* }}} */
331
332 /* {{{ proto string HttpResponse::getData()
333 *
334 * Get the previously set data to be sent.
335 */
336 PHP_METHOD(HttpResponse, getData)
337 {
338 zval *the_data;
339 getObject(http_response_object, obj);
340
341 NO_ARGS;
342
343 the_data = GET_PROP(obj, data);
344 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
345 }
346 /* }}} */
347
348 /* {{{ proto bool HttpResponse::setStream(resource stream)
349 *
350 * Set the resource to be sent.
351 */
352 PHP_METHOD(HttpResponse, setStream)
353 {
354 zval *the_stream;
355 php_stream *the_real_stream;
356 getObject(http_response_object, obj);
357
358 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
359 RETURN_FALSE;
360 }
361
362 php_stream_from_zval(the_real_stream, &the_stream);
363
364 SET_PROP(obj, stream, the_stream);
365 UPD_PROP(obj, long, lastModified, http_last_modified(the_real_stream, SEND_RSRC));
366 UPD_PROP(obj, long, send_mode, SEND_RSRC);
367 RETURN_TRUE;
368 }
369 /* }}} */
370
371 /* {{{ proto resource HttpResponse::getStream()
372 *
373 * Get the previously set resource to be sent.
374 */
375 PHP_METHOD(HttpResponse, getStream)
376 {
377 zval *the_stream;
378 getObject(http_response_object, obj);
379
380 NO_ARGS;
381
382 the_stream = GET_PROP(obj, stream);
383 RETURN_RESOURCE(Z_LVAL_P(the_stream));
384 }
385 /* }}} */
386
387 /* {{{ proto bool HttpResponse::setFile(string file)
388 *
389 * Set the file to be sent.
390 */
391 PHP_METHOD(HttpResponse, setFile)
392 {
393 zval *the_file;
394 getObject(http_response_object, obj);
395
396 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
397 RETURN_FALSE;
398 }
399
400 convert_to_string_ex(&the_file);
401
402 UPD_PROP(obj, string, file, Z_STRVAL_P(the_file));
403 UPD_PROP(obj, long, lastModified, http_last_modified(the_file, -1));
404 UPD_PROP(obj, long, send_mode, -1);
405 RETURN_TRUE;
406 }
407 /* }}} */
408
409 /* {{{ proto string HttpResponse::getFile()
410 *
411 * Get the previously set file to be sent.
412 */
413 PHP_METHOD(HttpResponse, getFile)
414 {
415 zval *the_file;
416 getObject(http_response_object, obj);
417
418 NO_ARGS;
419
420 the_file = GET_PROP(obj, file);
421 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
422 }
423 /* }}} */
424
425 /* {{{ proto bool HttpResponse::send()
426 *
427 * Finally send the entity.
428 *
429 * Example:
430 * <pre>
431 * <?php
432 * $r = new HttpResponse(true);
433 * $r->setFile('../hidden/contract.pdf');
434 * $r->setContentType('application/pdf');
435 * $r->send();
436 * ?>
437 * </pre>
438 *
439 */
440 PHP_METHOD(HttpResponse, send)
441 {
442 zval *do_cache, *do_gzip;
443 getObject(http_response_object, obj);
444
445 NO_ARGS;
446
447 do_cache = GET_PROP(obj, cache);
448 do_gzip = GET_PROP(obj, gzip);
449
450 /* gzip */
451 if (Z_LVAL_P(do_gzip)) {
452 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC);
453 }
454
455 /* caching */
456 if (Z_LVAL_P(do_cache)) {
457 zval *cctrl, *etag, *lmod, *ccraw;
458
459 etag = GET_PROP(obj, eTag);
460 lmod = GET_PROP(obj, lastModified);
461 cctrl = GET_PROP(obj, cacheControl);
462 ccraw = GET_PROP(obj, raw_cache_header);
463
464 if (Z_LVAL_P(ccraw)) {
465 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
466 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
467 } else {
468 char cc_header[42] = {0};
469 sprintf(cc_header, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl));
470 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), cc_header, strlen(cc_header));
471 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), cc_header, strlen(cc_header));
472 }
473 }
474
475 /* content type */
476 {
477 zval *ctype = GET_PROP(obj, contentType);
478 if (Z_STRLEN_P(ctype)) {
479 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
480 } else {
481 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
482 }
483 }
484
485 /* content disposition */
486 {
487 zval *dispo_file = GET_PROP(obj, dispoFile);
488 if (Z_STRLEN_P(dispo_file)) {
489 zval *dispo_inline = GET_PROP(obj, dispoInline);
490 http_send_content_disposition(Z_STRVAL_P(dispo_file), Z_STRLEN_P(dispo_file), (zend_bool) Z_LVAL_P(dispo_inline));
491 }
492 }
493
494 /* send */
495 {
496 zval *send_mode = GET_PROP(obj, send_mode);
497 switch (Z_LVAL_P(send_mode))
498 {
499 case SEND_DATA:
500 {
501 zval *zdata = GET_PROP(obj, data);
502 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
503 }
504
505 case SEND_RSRC:
506 {
507 php_stream *the_real_stream;
508 zval *the_stream = GET_PROP(obj, stream);
509 php_stream_from_zval(the_real_stream, &the_stream);
510 RETURN_SUCCESS(http_send_stream(the_real_stream));
511 }
512
513 default:
514 {
515 zval *zfile = GET_PROP(obj, file);
516 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile)));
517 }
518 }
519 }
520 }
521 /* }}} */
522 /* }}} */
523
524 /* {{{ HttpMessage */
525
526 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
527 *
528 * Create an HttpMessage object from a string.
529 */
530 PHP_METHOD(HttpMessage, fromString)
531 {
532 char *string = NULL;
533 int length = 0;
534 http_message *msg = NULL;
535 http_message_object obj;
536
537 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
538 RETURN_NULL();
539 }
540
541 if (!(msg = http_message_parse(string, length))) {
542 RETURN_NULL();
543 }
544
545 Z_TYPE_P(return_value) = IS_OBJECT;
546 return_value->value.obj = http_message_object_from_msg(msg);
547 }
548 /* }}} */
549
550 /* {{{ proto void HttpMessage::__construct([string message])
551 *
552 * Instantiate a new HttpMessage object.
553 */
554 PHP_METHOD(HttpMessage, __construct)
555 {
556 char *message = NULL;
557 int length = 0;
558 getObject(http_message_object, obj);
559
560 SET_EH_THROW_HTTP();
561 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
562 if (obj->message = http_message_parse(message, length)) {
563 if (obj->message->parent) {
564 obj->parent = http_message_object_from_msg(obj->message->parent);
565 }
566 }
567 } else if (!obj->message) {
568 obj->message = http_message_new();
569 }
570 SET_EH_NORMAL();
571 }
572 /* }}} */
573
574 /* {{{ proto string HttpMessage::getBody()
575 *
576 * Get the body of the parsed Message.
577 */
578 PHP_METHOD(HttpMessage, getBody)
579 {
580 zval *body;
581 getObject(http_message_object, obj);
582
583 NO_ARGS;
584
585 RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
586 }
587 /* }}} */
588
589 /* {{{ proto array HttpMessage::getHeaders()
590 *
591 * Get Message Headers.
592 */
593 PHP_METHOD(HttpMessage, getHeaders)
594 {
595 zval headers;
596 getObject(http_message_object, obj);
597
598 NO_ARGS;
599
600 Z_ARRVAL(headers) = &obj->message->hdrs;
601 array_init(return_value);
602 array_copy(&headers, return_value);
603 }
604 /* }}} */
605
606 /* {{{ proto void HttpMessage::setHeaders(array headers)
607 *
608 * Sets new headers.
609 */
610 PHP_METHOD(HttpMessage, setHeaders)
611 {
612 zval *new_headers, old_headers;
613 getObject(http_message_object, obj);
614
615 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
616 return;
617 }
618
619 zend_hash_clean(&obj->message->hdrs);
620 Z_ARRVAL(old_headers) = &obj->message->hdrs;
621 array_copy(new_headers, &old_headers);
622 }
623 /* }}} */
624
625 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
626 *
627 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
628 */
629 PHP_METHOD(HttpMessage, addHeaders)
630 {
631 zval old_headers, *new_headers;
632 zend_bool append = 0;
633 getObject(http_message_object, obj);
634
635 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
636 return;
637 }
638
639 Z_ARRVAL(old_headers) = &obj->message->hdrs;
640 if (append) {
641 array_append(new_headers, &old_headers);
642 } else {
643 array_merge(new_headers, &old_headers);
644 }
645 }
646 /* }}} */
647
648 /* {{{ proto long HttpMessage::getType()
649 *
650 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
651 */
652 PHP_METHOD(HttpMessage, getType)
653 {
654 getObject(http_message_object, obj);
655
656 NO_ARGS;
657
658 RETURN_LONG(obj->message->type);
659 }
660 /* }}} */
661
662 /* {{{ proto void HttpMessage::setType(long type)
663 *
664 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
665 */
666 PHP_METHOD(HttpMessage, setType)
667 {
668 long type;
669 getObject(http_message_object, obj);
670
671 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
672 return;
673 }
674 http_message_set_type(obj->message, type);
675 }
676 /* }}} */
677
678 /* {{{ proto long HttpMessage::getResponseCode()
679 *
680 * Get the Response Code of the Message.
681 */
682 PHP_METHOD(HttpMessage, getResponseCode)
683 {
684 getObject(http_message_object, obj);
685
686 NO_ARGS;
687
688 if (!HTTP_MSG_TYPE(RESPONSE, obj->message)) {
689 http_error(E_NOTICE, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_RESPONSE");
690 RETURN_NULL();
691 }
692
693 RETURN_LONG(obj->message->info.response.code);
694 }
695 /* }}} */
696
697 /* {{{ proto bool HttpMessage::setResponseCode(long code)
698 *
699 * Set the response code of an HTTP Response Message.
700 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
701 * or if the response code is out of range (100-510).
702 */
703 PHP_METHOD(HttpMessage, setResponseCode)
704 {
705 long code;
706 getObject(http_message_object, obj);
707
708 if (obj->message->type != HTTP_MSG_RESPONSE) {
709 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_RESPONSE");
710 RETURN_FALSE;
711 }
712
713 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
714 RETURN_FALSE;
715 }
716 if (code < 100 || code > 510) {
717 http_error_ex(E_WARNING, HTTP_E_PARAM, "Invalid response code (100-510): %ld", code);
718 RETURN_FALSE;
719 }
720
721 obj->message->info.response.code = code;
722 RETURN_TRUE;
723 }
724 /* }}} */
725
726 /* {{{ proto string HttpMessage::getRequestMethod()
727 *
728 * Get the Request Method of the Message.
729 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
730 */
731 PHP_METHOD(HttpMessage, getRequestMethod)
732 {
733 getObject(http_message_object, obj);
734
735 NO_ARGS;
736
737 if (obj->message->type != HTTP_MSG_REQUEST) {
738 http_error(E_NOTICE, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
739 RETURN_NULL();
740 }
741
742 RETURN_STRING(obj->message->info.request.method, 1);
743 }
744 /* }}} */
745
746 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
747 *
748 * Set the Request Method of the HTTP Message.
749 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
750 */
751 PHP_METHOD(HttpMessage, setRequestMethod)
752 {
753 char *method;
754 int method_len;
755 getObject(http_message_object, obj);
756
757 if (obj->message->type != HTTP_MSG_REQUEST) {
758 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
759 RETURN_FALSE;
760 }
761
762 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
763 RETURN_FALSE;
764 }
765 if (method_len < 1) {
766 http_error(E_WARNING, HTTP_E_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
767 RETURN_FALSE;
768 }
769 if (SUCCESS != http_check_method(method)) {
770 http_error_ex(E_WARNING, HTTP_E_PARAM, "Unkown request method: %s", method);
771 RETURN_FALSE;
772 }
773
774 if (obj->message->info.request.method) {
775 efree(obj->message->info.request.method);
776 }
777 obj->message->info.request.method = estrndup(method, method_len);
778 RETURN_TRUE;
779 }
780 /* }}} */
781
782 /* {{{ proto string HttpMessage::getRequestUri()
783 *
784 * Get the Request URI of the Message.
785 */
786 PHP_METHOD(HttpMessage, getRequestUri)
787 {
788 zval *uri;
789 getObject(http_message_object, obj);
790
791 NO_ARGS;
792
793 if (obj->message->type != HTTP_MSG_REQUEST) {
794 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
795 RETURN_NULL();
796 }
797
798 RETURN_STRING(obj->message->info.request.URI, 1);
799 }
800 /* }}} */
801
802 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
803 *
804 * Set the Request URI of the HTTP Message.
805 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
806 * or if paramtere URI was empty.
807 */
808 PHP_METHOD(HttpMessage, setRequestUri)
809 {
810 char *URI;
811 int URIlen;
812 getObject(http_message_object, obj);
813
814 if (obj->message->type != HTTP_MSG_REQUEST) {
815 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
816 RETURN_FALSE;
817 }
818 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
819 RETURN_FALSE;
820 }
821 if (URIlen < 1) {
822 http_error(E_WARNING, HTTP_E_PARAM, "Cannot set HttpMessage::requestUri to an empty string");
823 RETURN_FALSE;
824 }
825
826 if (obj->message->info.request.URI) {
827 efree(obj->message->info.request.URI);
828 }
829 obj->message->info.request.URI = estrndup(URI, URIlen);
830 RETURN_TRUE;
831 }
832 /* }}} */
833
834 /* {{{ proto string HttpMessage::getHttpVersion()
835 *
836 * Get the HTTP Protocol Version of the Message.
837 */
838 PHP_METHOD(HttpMessage, getHttpVersion)
839 {
840 char ver[4] = {0};
841 float version;
842 getObject(http_message_object, obj);
843
844 NO_ARGS;
845
846 switch (obj->message->type)
847 {
848 case HTTP_MSG_RESPONSE:
849 version = obj->message->info.response.http_version;
850 break;
851
852 case HTTP_MSG_REQUEST:
853 version = obj->message->info.request.http_version;
854 break;
855
856 case HTTP_MSG_NONE:
857 default:
858 RETURN_NULL();
859 }
860 sprintf(ver, "%1.1f", version);
861 RETURN_STRINGL(ver, 3, 1);
862 }
863 /* }}} */
864
865 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
866 *
867 * Set the HTTP Protocol version of the Message.
868 * Returns false if version is invalid (1.0 and 1.1).
869 */
870 PHP_METHOD(HttpMessage, setHttpVersion)
871 {
872 char v[4];
873 zval *zv, *version;
874 getObject(http_message_object, obj);
875
876 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
877 return;
878 }
879
880 if (obj->message->type == HTTP_MSG_NONE) {
881 http_error(E_WARNING, HTTP_E_MSG, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
882 RETURN_FALSE;
883 }
884
885 convert_to_double_ex(&zv);
886 sprintf(v, "%1.1f", Z_DVAL_P(zv));
887 if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
888 http_error_ex(E_WARNING, HTTP_E_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %s", v);
889 RETURN_FALSE;
890 }
891
892 if (obj->message->type == HTTP_MSG_RESPONSE) {
893 obj->message->info.response.http_version = (float) Z_DVAL_P(zv);
894 } else {
895 obj->message->info.request.http_version = (float) Z_DVAL_P(zv);
896 }
897 RETURN_TRUE;
898 }
899 /* }}} */
900
901 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
902 *
903 * Get parent Message.
904 */
905 PHP_METHOD(HttpMessage, getParentMessage)
906 {
907 getObject(http_message_object, obj);
908
909 NO_ARGS;
910
911 if (obj->message->parent) {
912 RETVAL_OBJVAL(obj->parent);
913 } else {
914 RETVAL_NULL();
915 }
916 }
917 /* }}} */
918
919 /* {{{ proto bool HttpMessage::send()
920 *
921 * Send the Message according to its type as Response or Request.
922 */
923 PHP_METHOD(HttpMessage, send)
924 {
925 getObject(http_message_object, obj);
926
927 NO_ARGS;
928
929 RETURN_SUCCESS(http_message_send(obj->message));
930 }
931 /* }}} */
932
933 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
934 *
935 * Get the string representation of the Message.
936 */
937 PHP_METHOD(HttpMessage, toString)
938 {
939 char *string;
940 size_t length;
941 zend_bool include_parent = 1;
942 getObject(http_message_object, obj);
943
944 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
945 RETURN_FALSE;
946 }
947
948 if (include_parent) {
949 http_message_serialize(obj->message, &string, &length);
950 } else {
951 http_message_tostring(obj->message, &string, &length);
952 }
953 RETURN_STRINGL(string, length, 0);
954 }
955 /* }}} */
956
957 /* }}} */
958
959 #ifdef HTTP_HAVE_CURL
960 /* {{{ HttpRequest */
961
962 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
963 *
964 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
965 * and POST (including posting files) HTTP requests.
966 */
967 PHP_METHOD(HttpRequest, __construct)
968 {
969 char *URL = NULL;
970 int URL_len;
971 long meth = -1;
972 getObject(http_request_object, obj);
973
974 SET_EH_THROW_HTTP();
975 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
976 INIT_PARR(obj, options);
977 INIT_PARR(obj, responseInfo);
978 INIT_PARR(obj, responseData);
979 INIT_PARR(obj, postData);
980 INIT_PARR(obj, postFiles);
981
982 if (URL) {
983 UPD_PROP(obj, string, url, URL);
984 }
985 if (meth > -1) {
986 UPD_PROP(obj, long, method, meth);
987 }
988 }
989 SET_EH_NORMAL();
990 }
991 /* }}} */
992
993 /* {{{ proto void HttpRequest::__destruct()
994 *
995 * Destroys the HttpRequest object.
996 */
997 PHP_METHOD(HttpRequest, __destruct)
998 {
999 getObject(http_request_object, obj);
1000
1001 NO_ARGS;
1002
1003 FREE_PARR(obj, options);
1004 FREE_PARR(obj, responseInfo);
1005 FREE_PARR(obj, responseData);
1006 FREE_PARR(obj, postData);
1007 FREE_PARR(obj, postFiles);
1008 }
1009 /* }}} */
1010
1011 /* {{{ proto bool HttpRequest::setOptions(array options)
1012 *
1013 * Set the request options to use. See http_get() for a full list of available options.
1014 */
1015 PHP_METHOD(HttpRequest, setOptions)
1016 {
1017 char *key = NULL;
1018 long idx = 0;
1019 zval *opts, *old_opts, **opt;
1020 getObject(http_request_object, obj);
1021
1022 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
1023 RETURN_FALSE;
1024 }
1025
1026 old_opts = GET_PROP(obj, options);
1027
1028 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
1029 FOREACH_KEYVAL(opts, key, idx, opt) {
1030 if (key) {
1031 if (!strcmp(key, "headers")) {
1032 zval **headers;
1033 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
1034 array_merge(*opt, *headers);
1035 continue;
1036 }
1037 } else if (!strcmp(key, "cookies")) {
1038 zval **cookies;
1039 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1040 array_merge(*opt, *cookies);
1041 continue;
1042 }
1043 }
1044 zval_add_ref(opt);
1045 add_assoc_zval(old_opts, key, *opt);
1046
1047 /* reset */
1048 key = NULL;
1049 }
1050 }
1051
1052 RETURN_TRUE;
1053 }
1054 /* }}} */
1055
1056 /* {{{ proto array HttpRequest::getOptions()
1057 *
1058 * Get current set options.
1059 */
1060 PHP_METHOD(HttpRequest, getOptions)
1061 {
1062 zval *opts;
1063 getObject(http_request_object, obj);
1064
1065 NO_ARGS;
1066
1067 opts = GET_PROP(obj, options);
1068 array_init(return_value);
1069 array_copy(opts, return_value);
1070 }
1071 /* }}} */
1072
1073 /* {{{ proto void HttpRequest::unsetOptions()
1074 *
1075 * Unset all options/headers/cookies.
1076 */
1077 PHP_METHOD(HttpRequest, unsetOptions)
1078 {
1079 getObject(http_request_object, obj);
1080
1081 NO_ARGS;
1082
1083 FREE_PARR(obj, options);
1084 INIT_PARR(obj, options);
1085 }
1086 /* }}} */
1087
1088 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1089 *
1090 * Set additional SSL options.
1091 */
1092 PHP_METHOD(HttpRequest, setSslOptions)
1093 {
1094 zval *opts, *old_opts, **ssl_options;
1095 getObject(http_request_object, obj);
1096
1097 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
1098 RETURN_FALSE;
1099 }
1100
1101 old_opts = GET_PROP(obj, options);
1102
1103 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
1104 array_merge(opts, *ssl_options);
1105 } else {
1106 zval_add_ref(&opts);
1107 add_assoc_zval(old_opts, "ssl", opts);
1108 }
1109
1110 RETURN_TRUE;
1111 }
1112 /* }}} */
1113
1114 /* {{{ proto array HttpRequest::getSslOtpions()
1115 *
1116 * Get previously set SSL options.
1117 */
1118 PHP_METHOD(HttpRequest, getSslOptions)
1119 {
1120 zval *opts, **ssl_options;
1121 getObject(http_request_object, obj);
1122
1123 NO_ARGS;
1124
1125 opts = GET_PROP(obj, options);
1126
1127 array_init(return_value);
1128
1129 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
1130 array_copy(*ssl_options, return_value);
1131 }
1132 }
1133 /* }}} */
1134
1135 /* {{{ proto void HttpRequest::unsetSslOptions()
1136 *
1137 * Unset previously set SSL options.
1138 */
1139 PHP_METHOD(HttpRequest, unsetSslOptions)
1140 {
1141 zval *opts;
1142 getObject(http_request_object, obj);
1143
1144 NO_ARGS;
1145
1146 opts = GET_PROP(obj, options);
1147 zend_hash_del(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"));
1148 }
1149 /* }}} */
1150
1151 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1152 *
1153 * Add request header name/value pairs.
1154 */
1155 PHP_METHOD(HttpRequest, addHeaders)
1156 {
1157 zval *opts, **headers, *new_headers;
1158 getObject(http_request_object, obj);
1159
1160 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
1161 RETURN_FALSE;
1162 }
1163
1164 opts = GET_PROP(obj, options);
1165
1166 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
1167 array_merge(new_headers, *headers);
1168 } else {
1169 zval_add_ref(&new_headers);
1170 add_assoc_zval(opts, "headers", new_headers);
1171 }
1172
1173 RETURN_TRUE;
1174 }
1175 /* }}} */
1176
1177 /* {{{ proto array HttpRequest::getHeaders()
1178 *
1179 * Get previously set request headers.
1180 */
1181 PHP_METHOD(HttpRequest, getHeaders)
1182 {
1183 zval *opts, **headers;
1184 getObject(http_request_object, obj);
1185
1186 NO_ARGS;
1187
1188 opts = GET_PROP(obj, options);
1189
1190 array_init(return_value);
1191
1192 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
1193 array_copy(*headers, return_value);
1194 }
1195 }
1196 /* }}} */
1197
1198 /* {{{ proto void HttpRequest::unsetHeaders()
1199 *
1200 * Unset previously set request headers.
1201 */
1202 PHP_METHOD(HttpRequest, unsetHeaders)
1203 {
1204 zval *opts;
1205 getObject(http_request_object, obj);
1206
1207 NO_ARGS;
1208
1209 opts = GET_PROP(obj, options);
1210 zend_hash_del(Z_ARRVAL_P(opts), "headers", sizeof("headers"));
1211 }
1212 /* }}} */
1213
1214 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1215 *
1216 * Add cookies.
1217 */
1218 PHP_METHOD(HttpRequest, addCookies)
1219 {
1220 zval *opts, **cookies, *new_cookies;
1221 getObject(http_request_object, obj);
1222
1223 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_cookies)) {
1224 RETURN_FALSE;
1225 }
1226
1227 opts = GET_PROP(obj, options);
1228
1229 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1230 array_merge(new_cookies, *cookies);
1231 } else {
1232 zval_add_ref(&new_cookies);
1233 add_assoc_zval(opts, "cookies", new_cookies);
1234 }
1235
1236 RETURN_TRUE;
1237 }
1238 /* }}} */
1239
1240 /* {{{ proto array HttpRequest::getCookies()
1241 *
1242 * Get previously set cookies.
1243 */
1244 PHP_METHOD(HttpRequest, getCookies)
1245 {
1246 zval *opts, **cookies;
1247 getObject(http_request_object, obj);
1248
1249 NO_ARGS;
1250
1251 opts = GET_PROP(obj, options);
1252
1253 array_init(return_value);
1254
1255 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1256 array_copy(*cookies, return_value);
1257 }
1258 }
1259 /* }}} */
1260
1261 /* {{{ proto void HttpRequest::unsetCookies()
1262 *
1263 */
1264 PHP_METHOD(HttpRequest, unsetCookies)
1265 {
1266 zval *opts;
1267 getObject(http_request_object, obj);
1268
1269 NO_ARGS;
1270
1271 opts = GET_PROP(obj, options);
1272 zend_hash_del(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"));
1273 }
1274 /* }}} */
1275
1276 /* {{{ proto bool HttpRequest::setURL(string url)
1277 *
1278 * Set the request URL.
1279 */
1280 PHP_METHOD(HttpRequest, setURL)
1281 {
1282 char *URL = NULL;
1283 int URL_len;
1284 getObject(http_request_object, obj);
1285
1286 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
1287 RETURN_FALSE;
1288 }
1289
1290 UPD_PROP(obj, string, url, URL);
1291 RETURN_TRUE;
1292 }
1293 /* }}} */
1294
1295 /* {{{ proto string HttpRequest::getUrl()
1296 *
1297 * Get the previously set request URL.
1298 */
1299 PHP_METHOD(HttpRequest, getURL)
1300 {
1301 zval *URL;
1302 getObject(http_request_object, obj);
1303
1304 NO_ARGS;
1305
1306 URL = GET_PROP(obj, url);
1307 RETURN_STRINGL(Z_STRVAL_P(URL), Z_STRLEN_P(URL), 1);
1308 }
1309 /* }}} */
1310
1311 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1312 *
1313 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1314 * <tt>HTTP_POST</tt> constants.
1315 */
1316 PHP_METHOD(HttpRequest, setMethod)
1317 {
1318 long meth;
1319 getObject(http_request_object, obj);
1320
1321 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
1322 RETURN_FALSE;
1323 }
1324
1325 UPD_PROP(obj, long, method, meth);
1326 RETURN_TRUE;
1327 }
1328 /* }}} */
1329
1330 /* {{{ proto long HttpRequest::getMethod()
1331 *
1332 * Get the previously set request method.
1333 */
1334 PHP_METHOD(HttpRequest, getMethod)
1335 {
1336 zval *meth;
1337 getObject(http_request_object, obj);
1338
1339 NO_ARGS;
1340
1341 meth = GET_PROP(obj, method);
1342 RETURN_LONG(Z_LVAL_P(meth));
1343 }
1344 /* }}} */
1345
1346 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1347 *
1348 * Set the content type the post request should have.
1349 * Use this only if you know what you're doing.
1350 */
1351 PHP_METHOD(HttpRequest, setContentType)
1352 {
1353 char *ctype;
1354 int ct_len;
1355 getObject(http_request_object, obj);
1356
1357 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1358 RETURN_FALSE;
1359 }
1360
1361 if (!strchr(ctype, '/')) {
1362 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
1363 RETURN_FALSE;
1364 }
1365
1366 UPD_PROP(obj, string, contentType, ctype);
1367 RETURN_TRUE;
1368 }
1369 /* }}} */
1370
1371 /* {{{ proto string HttpRequest::getContentType()
1372 *
1373 * Get the previously content type.
1374 */
1375 PHP_METHOD(HttpRequest, getContentType)
1376 {
1377 zval *ctype;
1378 getObject(http_request_object, obj);
1379
1380 NO_ARGS;
1381
1382 ctype = GET_PROP(obj, contentType);
1383 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
1384 }
1385 /* }}} */
1386
1387 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1388 *
1389 * Set the URL query parameters to use.
1390 * Overwrites previously set query parameters.
1391 * Affects any request types.
1392 */
1393 PHP_METHOD(HttpRequest, setQueryData)
1394 {
1395 zval *qdata;
1396 char *query_data = NULL;
1397 getObject(http_request_object, obj);
1398
1399 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
1400 RETURN_FALSE;
1401 }
1402
1403 if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1404 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1405 RETURN_FALSE;
1406 }
1407 UPD_PROP(obj, string, queryData, query_data);
1408 efree(query_data);
1409 RETURN_TRUE;
1410 }
1411
1412 convert_to_string(qdata);
1413 UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
1414 RETURN_TRUE;
1415 }
1416 /* }}} */
1417
1418 /* {{{ proto string HttpRequest::getQueryData()
1419 *
1420 * Get the current query data in form of an urlencoded query string.
1421 */
1422 PHP_METHOD(HttpRequest, getQueryData)
1423 {
1424 zval *qdata;
1425 getObject(http_request_object, obj);
1426
1427 NO_ARGS;
1428
1429 qdata = GET_PROP(obj, queryData);
1430 RETURN_STRINGL(Z_STRVAL_P(qdata), Z_STRLEN_P(qdata), 1);
1431 }
1432 /* }}} */
1433
1434 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1435 *
1436 * Add parameters to the query parameter list.
1437 * Affects any request type.
1438 */
1439 PHP_METHOD(HttpRequest, addQueryData)
1440 {
1441 zval *qdata, *old_qdata;
1442 char *query_data = NULL;
1443 getObject(http_request_object, obj);
1444
1445 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1446 RETURN_FALSE;
1447 }
1448
1449 old_qdata = GET_PROP(obj, queryData);
1450
1451 if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, NULL)) {
1452 RETURN_FALSE;
1453 }
1454
1455 UPD_PROP(obj, string, queryData, query_data);
1456 efree(query_data);
1457
1458 RETURN_TRUE;
1459 }
1460 /* }}} */
1461
1462 /* {{{ proto void HttpRequest::unsetQueryData()
1463 *
1464 * Clean the query parameters.
1465 * Affects any request type.
1466 */
1467 PHP_METHOD(HttpRequest, unsetQueryData)
1468 {
1469 getObject(http_request_object, obj);
1470
1471 NO_ARGS;
1472
1473 UPD_PROP(obj, string, queryData, "");
1474 }
1475 /* }}} */
1476
1477 /* {{{ proto bool HttpRequest::addPostData(array post_data)
1478 *
1479 * Adds POST data entries.
1480 * Affects only POST requests.
1481 */
1482 PHP_METHOD(HttpRequest, addPostData)
1483 {
1484 zval *post, *post_data;
1485 getObject(http_request_object, obj);
1486
1487 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1488 RETURN_FALSE;
1489 }
1490
1491 post = GET_PROP(obj, postData);
1492 array_merge(post_data, post);
1493
1494 RETURN_TRUE;
1495 }
1496 /* }}} */
1497
1498 /* {{{ proto bool HttpRequest::setPostData(array post_data)
1499 *
1500 * Set the POST data entries.
1501 * Overwrites previously set POST data.
1502 * Affects only POST requests.
1503 */
1504 PHP_METHOD(HttpRequest, setPostData)
1505 {
1506 zval *post, *post_data;
1507 getObject(http_request_object, obj);
1508
1509 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1510 RETURN_FALSE;
1511 }
1512
1513 post = GET_PROP(obj, postData);
1514 zend_hash_clean(Z_ARRVAL_P(post));
1515 array_copy(post_data, post);
1516
1517 RETURN_TRUE;
1518 }
1519 /* }}}*/
1520
1521 /* {{{ proto array HttpRequest::getPostData()
1522 *
1523 * Get previously set POST data.
1524 */
1525 PHP_METHOD(HttpRequest, getPostData)
1526 {
1527 zval *post_data;
1528 getObject(http_request_object, obj);
1529
1530 NO_ARGS;
1531
1532 post_data = GET_PROP(obj, postData);
1533 array_init(return_value);
1534 array_copy(post_data, return_value);
1535 }
1536 /* }}} */
1537
1538 /* {{{ proto void HttpRequest::unsetPostData()
1539 *
1540 * Clean POST data entires.
1541 * Affects only POST requests.
1542 */
1543 PHP_METHOD(HttpRequest, unsetPostData)
1544 {
1545 zval *post_data;
1546 getObject(http_request_object, obj);
1547
1548 NO_ARGS;
1549
1550 post_data = GET_PROP(obj, postData);
1551 zend_hash_clean(Z_ARRVAL_P(post_data));
1552 }
1553 /* }}} */
1554
1555 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1556 *
1557 * Add a file to the POST request.
1558 * Affects only POST requests.
1559 */
1560 PHP_METHOD(HttpRequest, addPostFile)
1561 {
1562 zval *files, *entry;
1563 char *name, *file, *type = NULL;
1564 int name_len, file_len, type_len = 0;
1565 getObject(http_request_object, obj);
1566
1567 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1568 RETURN_FALSE;
1569 }
1570
1571 if (type_len) {
1572 if (!strchr(type, '/')) {
1573 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
1574 RETURN_FALSE;
1575 }
1576 } else {
1577 type = "application/x-octetstream";
1578 type_len = sizeof("application/x-octetstream") - 1;
1579 }
1580
1581 MAKE_STD_ZVAL(entry);
1582 array_init(entry);
1583
1584 add_assoc_stringl(entry, "name", name, name_len, 1);
1585 add_assoc_stringl(entry, "type", type, type_len, 1);
1586 add_assoc_stringl(entry, "file", file, file_len, 1);
1587
1588 files = GET_PROP(obj, postFiles);
1589 add_next_index_zval(files, entry);
1590
1591 RETURN_TRUE;
1592 }
1593 /* }}} */
1594
1595 /* {{{ proto array HttpRequest::getPostFiles()
1596 *
1597 * Get all previously added POST files.
1598 */
1599 PHP_METHOD(HttpRequest, getPostFiles)
1600 {
1601 zval *files;
1602 getObject(http_request_object, obj);
1603
1604 NO_ARGS;
1605
1606 files = GET_PROP(obj, postFiles);
1607
1608 array_init(return_value);
1609 array_copy(files, return_value);
1610 }
1611 /* }}} */
1612
1613 /* {{{ proto void HttpRequest::unsetPostFiles()
1614 *
1615 * Unset the POST files list.
1616 * Affects only POST requests.
1617 */
1618 PHP_METHOD(HttpRequest, unsetPostFiles)
1619 {
1620 zval *files;
1621 getObject(http_request_object, obj);
1622
1623 NO_ARGS;
1624
1625 files = GET_PROP(obj, postFiles);
1626 zend_hash_clean(Z_ARRVAL_P(files));
1627 }
1628 /* }}} */
1629
1630 /* {{{ proto array HttpRequest::getResponseData()
1631 *
1632 * Get all response data after the request has been sent.
1633 */
1634 PHP_METHOD(HttpRequest, getResponseData)
1635 {
1636 zval *data;
1637 getObject(http_request_object, obj);
1638
1639 NO_ARGS;
1640
1641 data = GET_PROP(obj, responseData);
1642 array_init(return_value);
1643 array_copy(data, return_value);
1644 }
1645 /* }}} */
1646
1647 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1648 *
1649 * Get response header(s) after the request has been sent.
1650 */
1651 PHP_METHOD(HttpRequest, getResponseHeader)
1652 {
1653 zval *data, **headers, **header;
1654 char *header_name = NULL;
1655 int header_len = 0;
1656 getObject(http_response_object, obj);
1657
1658 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1659 RETURN_FALSE;
1660 }
1661
1662 data = GET_PROP(obj, responseData);
1663 if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1664 RETURN_FALSE;
1665 }
1666
1667 if (!header_len || !header_name) {
1668 array_init(return_value);
1669 array_copy(*headers, return_value);
1670 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1671 RETURN_STRINGL(Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
1672 } else {
1673 RETURN_FALSE;
1674 }
1675 }
1676 /* }}} */
1677
1678 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1679 *
1680 * Get response cookie(s) after the request has been sent.
1681 */
1682 PHP_METHOD(HttpRequest, getResponseCookie)
1683 {
1684 zval *data, **headers;
1685 char *cookie_name = NULL;
1686 int cookie_len = 0;
1687 getObject(http_request_object, obj);
1688
1689 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
1690 RETURN_FALSE;
1691 }
1692
1693 array_init(return_value);
1694
1695 data = GET_PROP(obj, responseData);
1696 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1697 ulong idx = 0;
1698 char *key = NULL;
1699 zval **header = NULL;
1700
1701 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers), key, idx, header) {
1702 if (key && !strcasecmp(key, "Set-Cookie")) {
1703 /* several cookies? */
1704 if (Z_TYPE_PP(header) == IS_ARRAY) {
1705 zval **cookie;
1706
1707 FOREACH_HASH_VAL(Z_ARRVAL_PP(header), cookie) {
1708 zval *cookie_hash;
1709 MAKE_STD_ZVAL(cookie_hash);
1710 array_init(cookie_hash);
1711
1712 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
1713 if (!cookie_len) {
1714 add_next_index_zval(return_value, cookie_hash);
1715 } else {
1716 zval **name;
1717
1718 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1719 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1720 add_next_index_zval(return_value, cookie_hash);
1721 return; /* <<< FOUND >>> */
1722 } else {
1723 zval_dtor(cookie_hash);
1724 efree(cookie_hash);
1725 }
1726 }
1727 } else {
1728 zval_dtor(cookie_hash);
1729 efree(cookie_hash);
1730 }
1731 }
1732 } else {
1733 zval *cookie_hash;
1734 MAKE_STD_ZVAL(cookie_hash);
1735 array_init(cookie_hash);
1736
1737 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
1738 if (!cookie_len) {
1739 add_next_index_zval(return_value, cookie_hash);
1740 } else {
1741 zval **name;
1742
1743 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1744 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1745 add_next_index_zval(return_value, cookie_hash);
1746 } else {
1747 zval_dtor(cookie_hash);
1748 efree(cookie_hash);
1749 }
1750 }
1751 } else {
1752 zval_dtor(cookie_hash);
1753 efree(cookie_hash);
1754 }
1755 }
1756 break;
1757 }
1758 /* reset key */
1759 key = NULL;
1760 }
1761 }
1762 }
1763 /* }}} */
1764
1765 /* {{{ proto string HttpRequest::getResponseBody()
1766 *
1767 * Get the response body after the request has been sent.
1768 */
1769 PHP_METHOD(HttpRequest, getResponseBody)
1770 {
1771 zval *data, **body;
1772 getObject(http_request_object, obj);
1773
1774 NO_ARGS;
1775
1776 data = GET_PROP(obj, responseData);
1777 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1778 RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
1779 } else {
1780 RETURN_FALSE;
1781 }
1782 }
1783 /* }}} */
1784
1785 /* {{{ proto int HttpRequest::getResponseCode()
1786 *
1787 * Get the response code after the request has been sent.
1788 */
1789 PHP_METHOD(HttpRequest, getResponseCode)
1790 {
1791 zval *code;
1792 getObject(http_request_object, obj);
1793
1794 NO_ARGS;
1795
1796 code = GET_PROP(obj, responseCode);
1797 RETURN_LONG(Z_LVAL_P(code));
1798 }
1799 /* }}} */
1800
1801 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1802 *
1803 * Get response info after the request has been sent.
1804 * See http_get() for a full list of returned info.
1805 */
1806 PHP_METHOD(HttpRequest, getResponseInfo)
1807 {
1808 zval *info, **infop;
1809 char *info_name = NULL;
1810 int info_len = 0;
1811 getObject(http_request_object, obj);
1812
1813 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1814 RETURN_FALSE;
1815 }
1816
1817 info = GET_PROP(obj, responseInfo);
1818
1819 if (info_len && info_name) {
1820 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1821 RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
1822 } else {
1823 http_error_ex(E_NOTICE, HTTP_E_PARAM, "Could not find response info named %s", info_name);
1824 RETURN_FALSE;
1825 }
1826 } else {
1827 array_init(return_value);
1828 array_copy(info, return_value);
1829 }
1830 }
1831 /* }}}*/
1832
1833 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1834 *
1835 * Get the full response as HttpMessage object.
1836 */
1837 PHP_METHOD(HttpRequest, getResponseMessage)
1838 {
1839 zval *message;
1840 getObject(http_request_object, obj);
1841
1842 NO_ARGS;
1843
1844 message = GET_PROP(obj, responseMessage);
1845 Z_TYPE_P(return_value) = IS_OBJECT;
1846 return_value->is_ref = 1;
1847 return_value->value.obj = message->value.obj;
1848 zval_add_ref(&return_value);
1849 }
1850
1851 /* {{{ proto bool HttpRequest::send()
1852 *
1853 * Send the HTTP request.
1854 *
1855 * GET example:
1856 * <pre>
1857 * <?php
1858 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1859 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1860 * $r->addQueryData(array('category' => 3));
1861 * try {
1862 * $r->send();
1863 * if ($r->getResponseCode() == 200) {
1864 * file_put_contents('local.rss', $r->getResponseBody());
1865 * }
1866 * } catch (HttpException $ex) {
1867 * echo $ex;
1868 * }
1869 * ?>
1870 * </pre>
1871 *
1872 * POST example:
1873 * <pre>
1874 * <?php
1875 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1876 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1877 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1878 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1879 * if ($r->send()) {
1880 * echo $r->getResponseBody();
1881 * }
1882 * ?>
1883 * </pre>
1884 */
1885 PHP_METHOD(HttpRequest, send)
1886 {
1887 STATUS status = FAILURE;
1888 zval *meth, *URL, *qdata, *opts, *info, *resp;
1889 char *request_uri;
1890 getObject(http_request_object, obj);
1891
1892 NO_ARGS;
1893
1894 SET_EH_THROW_HTTP();
1895
1896 if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
1897 http_error(E_WARNING, HTTP_E_CURL, "Could not initilaize curl");
1898 RETURN_FALSE;
1899 }
1900
1901 meth = GET_PROP(obj, method);
1902 URL = GET_PROP(obj, url);
1903 qdata = GET_PROP(obj, queryData);
1904 opts = GET_PROP(obj, options);
1905 info = GET_PROP(obj, responseInfo);
1906 resp = GET_PROP(obj, responseData);
1907
1908 // HTTP_URI_MAXLEN+1 long char *
1909 request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0);
1910
1911 if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
1912 if (!strchr(request_uri, '?')) {
1913 strcat(request_uri, "?");
1914 } else {
1915 strcat(request_uri, "&");
1916 }
1917 strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
1918 }
1919
1920 switch (Z_LVAL_P(meth))
1921 {
1922 case HTTP_GET:
1923 status = http_get_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1924 break;
1925
1926 case HTTP_HEAD:
1927 status = http_head_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1928 break;
1929
1930 case HTTP_POST:
1931 {
1932 zval *post_files, *post_data;
1933
1934 post_files = GET_PROP(obj, postFiles);
1935 post_data = GET_PROP(obj, postData);
1936
1937 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files))) {
1938
1939 /* urlencoded post */
1940 status = http_post_array_ex(obj->ch, request_uri, Z_ARRVAL_P(post_data), Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1941
1942 } else {
1943
1944 /*
1945 * multipart post
1946 */
1947 char *key = NULL;
1948 long idx;
1949 zval **data;
1950 struct curl_httppost *http_post_data[2] = {NULL, NULL};
1951
1952 /* normal data */
1953 FOREACH_KEYVAL(post_data, key, idx, data) {
1954 if (key) {
1955 convert_to_string_ex(data);
1956 curl_formadd(&http_post_data[0], &http_post_data[1],
1957 CURLFORM_COPYNAME, key,
1958 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
1959 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
1960 CURLFORM_END
1961 );
1962
1963 /* reset */
1964 key = NULL;
1965 }
1966 }
1967
1968 /* file data */
1969 FOREACH_VAL(post_files, data) {
1970 zval **file, **type, **name;
1971
1972 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
1973 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
1974 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
1975
1976 curl_formadd(&http_post_data[0], &http_post_data[1],
1977 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
1978 CURLFORM_FILE, Z_STRVAL_PP(file),
1979 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
1980 CURLFORM_END
1981 );
1982 }
1983 }
1984
1985 status = http_post_curldata_ex(obj->ch, request_uri, http_post_data[0], Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1986 curl_formfree(http_post_data[0]);
1987 }
1988 }
1989 break;
1990
1991 default:
1992 break;
1993 }
1994
1995 efree(request_uri);
1996
1997 /* final data handling */
1998 if (status == SUCCESS) {
1999 http_message *msg;
2000
2001 if (msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response))) {
2002 zval *headers, *message;
2003 char *body;
2004 size_t body_len;
2005
2006 UPD_PROP(obj, long, responseCode, msg->info.response.code);
2007
2008 MAKE_STD_ZVAL(headers)
2009 array_init(headers);
2010
2011 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
2012 phpstr_data(PHPSTR(msg), &body, &body_len);
2013
2014 add_assoc_zval(resp, "headers", headers);
2015 add_assoc_stringl(resp, "body", body, body_len, 0);
2016
2017 message = GET_PROP(obj, responseMessage);
2018 zval_dtor(message);
2019 Z_TYPE_P(message) = IS_OBJECT;
2020 message->value.obj = http_message_object_from_msg(msg);
2021 SET_PROP(obj, responseMessage, message);
2022 } else {
2023 status = FAILURE;
2024 }
2025 }
2026
2027 SET_EH_NORMAL();
2028 RETURN_SUCCESS(status);
2029 }
2030 /* }}} */
2031 /* }}} */
2032 #endif /* HTTP_HAVE_CURL */
2033
2034 #endif /* ZEND_ENGINE_2 */
2035
2036 /*
2037 * Local variables:
2038 * tab-width: 4
2039 * c-basic-offset: 4
2040 * End:
2041 * vim600: noet sw=4 ts=4 fdm=marker
2042 * vim<600: noet sw=4 ts=4
2043 */
2044