- preparations for HttpMessage
[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_send_api.h"
31 #include "php_http_url_api.h"
32
33 #ifdef ZEND_ENGINE_2
34
35 /* {{{ HttpResponse */
36
37 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
38 *
39 * Instantiates a new HttpResponse object, which can be used to send
40 * any data/resource/file to an HTTP client with caching and multiple
41 * ranges/resuming support.
42 *
43 * NOTE: GZIPping is not implemented yet.
44 */
45 PHP_METHOD(HttpResponse, __construct)
46 {
47 zend_bool do_cache = 0, do_gzip = 0;
48 getObject(http_response_object, obj);
49
50 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
51 // throw exception
52 return;
53 }
54
55 UPD_PROP(obj, long, cache, do_cache);
56 UPD_PROP(obj, long, gzip, do_gzip);
57 }
58 /* }}} */
59
60 /* {{{ proto bool HttpResponse::setCache(bool cache)
61 *
62 * Whether it sould be attempted to cache the entitity.
63 * This will result in necessary caching headers and checks of clients
64 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
65 * matches a "304 Not Modified" status code will be issued.
66 *
67 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
68 * to something more appropriate than "no-cache"!
69 */
70 PHP_METHOD(HttpResponse, setCache)
71 {
72 zend_bool do_cache = 0;
73 getObject(http_response_object, obj);
74
75 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
76 RETURN_FALSE;
77 }
78
79 UPD_PROP(obj, long, cache, do_cache);
80 RETURN_TRUE;
81 }
82 /* }}} */
83
84 /* {{{ proto bool HttpResponse::getCache()
85 *
86 * Get current caching setting.
87 */
88 PHP_METHOD(HttpResponse, getCache)
89 {
90 zval *do_cache = NULL;
91 getObject(http_response_object, obj);
92
93 NO_ARGS;
94
95 do_cache = GET_PROP(obj, cache);
96 RETURN_BOOL(Z_LVAL_P(do_cache));
97 }
98 /* }}}*/
99
100 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
101 *
102 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
103 */
104 PHP_METHOD(HttpResponse, setGzip)
105 {
106 zend_bool do_gzip = 0;
107 getObject(http_response_object, obj);
108
109 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
110 RETURN_FALSE;
111 }
112
113 UPD_PROP(obj, long, gzip, do_gzip);
114 RETURN_TRUE;
115 }
116 /* }}} */
117
118 /* {{{ proto bool HttpResponse::getGzip()
119 *
120 * Get current gzipping setting.
121 */
122 PHP_METHOD(HttpResponse, getGzip)
123 {
124 zval *do_gzip = NULL;
125 getObject(http_response_object, obj);
126
127 NO_ARGS;
128
129 do_gzip = GET_PROP(obj, gzip);
130 RETURN_BOOL(Z_LVAL_P(do_gzip));
131 }
132 /* }}} */
133
134 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
135 *
136 * Set a custom cache-control header, usually being "private" or "public"; if
137 * $raw is set to true the header will be sent as-is.
138 */
139 PHP_METHOD(HttpResponse, setCacheControl)
140 {
141 char *ccontrol;
142 int cc_len;
143 zend_bool raw = 0;
144 getObject(http_response_object, obj);
145
146 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
147 RETURN_FALSE;
148 }
149
150 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
151 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
152 RETURN_FALSE;
153 }
154
155 UPD_PROP(obj, long, raw_cache_header, raw);
156 UPD_PROP(obj, string, cacheControl, ccontrol);
157 RETURN_TRUE;
158 }
159 /* }}} */
160
161 /* {{{ proto string HttpResponse::getCacheControl()
162 *
163 * Get current Cache-Control header setting.
164 */
165 PHP_METHOD(HttpResponse, getCacheControl)
166 {
167 zval *ccontrol;
168 getObject(http_response_object, obj);
169
170 NO_ARGS;
171
172 ccontrol = GET_PROP(obj, cacheControl);
173 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
174 }
175 /* }}} */
176
177 /* {{{ proto bool HttpResponse::setContentType(string content_type)
178 *
179 * Set the content-type of the sent entity.
180 */
181 PHP_METHOD(HttpResponse, setContentType)
182 {
183 char *ctype;
184 int ctype_len;
185 getObject(http_response_object, obj);
186
187 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
188 RETURN_FALSE;
189 }
190
191 if (!strchr(ctype, '/')) {
192 php_error_docref(NULL TSRMLS_CC, E_WARNING,
193 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
194 RETURN_FALSE;
195 }
196
197 UPD_PROP(obj, string, contentType, ctype);
198
199 RETURN_TRUE;
200 }
201 /* }}} */
202
203 /* {{{ proto string HttpResponse::getContentType()
204 *
205 * Get current Content-Type header setting.
206 */
207 PHP_METHOD(HttpResponse, getContentType)
208 {
209 zval *ctype;
210 getObject(http_response_object, obj);
211
212 NO_ARGS;
213
214 ctype = GET_PROP(obj, contentType);
215 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
216 }
217 /* }}} */
218
219 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
220 *
221 * Set the Content-Disposition of the sent entity. This setting aims to suggest
222 * the receiveing user agent how to handle the sent entity; usually the client
223 * will show the user a "Save As..." popup.
224 */
225 PHP_METHOD(HttpResponse, setContentDisposition)
226 {
227 char *file;
228 int file_len;
229 zend_bool is_inline = 0;
230 getObject(http_response_object, obj);
231
232 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_inline)) {
233 RETURN_FALSE;
234 }
235
236 UPD_PROP(obj, string, dispoFile, file);
237 UPD_PROP(obj, long, dispoInline, is_inline);
238 RETURN_TRUE;
239 }
240 /* }}} */
241
242 /* {{{ proto array HttpResponse::getContentDisposition()
243 *
244 * Get current Content-Disposition setting.
245 * Will return an associative array like:
246 * <pre>
247 * array(
248 * 'filename' => 'foo.bar',
249 * 'inline' => false
250 * )
251 * </pre>
252 */
253 PHP_METHOD(HttpResponse, getContentDisposition)
254 {
255 zval *file;
256 zval *is_inline;
257 getObject(http_response_object, obj);
258
259 if (ZEND_NUM_ARGS()) {
260 WRONG_PARAM_COUNT;
261 }
262
263 file = GET_PROP(obj, dispoFile);
264 is_inline = GET_PROP(obj, dispoInline);
265
266 array_init(return_value);
267 add_assoc_stringl(return_value, "filename", Z_STRVAL_P(file), Z_STRLEN_P(file), 1);
268 add_assoc_bool(return_value, "inline", Z_LVAL_P(is_inline));
269 }
270 /* }}} */
271
272 /* {{{ proto bool HttpResponse::setETag(string etag)
273 *
274 * Set a custom ETag. Use this only if you know what you're doing.
275 */
276 PHP_METHOD(HttpResponse, setETag)
277 {
278 char *etag;
279 int etag_len;
280 getObject(http_response_object, obj);
281
282 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
283 RETURN_FALSE;
284 }
285
286 UPD_PROP(obj, string, eTag, etag);
287 RETURN_TRUE;
288 }
289 /* }}} */
290
291 /* {{{ proto string HttpResponse::getETag()
292 *
293 * Get the previously set custom ETag.
294 */
295 PHP_METHOD(HttpResponse, getETag)
296 {
297 zval *etag;
298 getObject(http_response_object, obj);
299
300 NO_ARGS;
301
302 etag = GET_PROP(obj, eTag);
303 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
304 }
305 /* }}} */
306
307 /* {{{ proto bool HttpResponse::setData(string data)
308 *
309 * Set the data to be sent.
310 */
311 PHP_METHOD(HttpResponse, setData)
312 {
313 zval *the_data;
314 getObject(http_response_object, obj);
315
316 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_data)) {
317 RETURN_FALSE;
318 }
319
320 convert_to_string_ex(&the_data);
321 SET_PROP(obj, data, the_data);
322 UPD_PROP(obj, long, lastModified, http_lmod(the_data, SEND_DATA));
323 UPD_PROP(obj, long, send_mode, SEND_DATA);
324 RETURN_TRUE;
325 }
326 /* }}} */
327
328 /* {{{ proto string HttpResponse::getData()
329 *
330 * Get the previously set data to be sent.
331 */
332 PHP_METHOD(HttpResponse, getData)
333 {
334 zval *the_data;
335 getObject(http_response_object, obj);
336
337 NO_ARGS;
338
339 the_data = GET_PROP(obj, data);
340 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
341 }
342 /* }}} */
343
344 /* {{{ proto bool HttpResponse::setStream(resource stream)
345 *
346 * Set the resource to be sent.
347 */
348 PHP_METHOD(HttpResponse, setStream)
349 {
350 zval *the_stream;
351 php_stream *the_real_stream;
352 getObject(http_response_object, obj);
353
354 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
355 RETURN_FALSE;
356 }
357
358 php_stream_from_zval(the_real_stream, &the_stream);
359
360 SET_PROP(obj, stream, the_stream);
361 UPD_PROP(obj, long, lastModified, http_lmod(the_real_stream, SEND_RSRC));
362 UPD_PROP(obj, long, send_mode, SEND_RSRC);
363 RETURN_TRUE;
364 }
365 /* }}} */
366
367 /* {{{ proto resource HttpResponse::getStream()
368 *
369 * Get the previously set resource to be sent.
370 */
371 PHP_METHOD(HttpResponse, getStream)
372 {
373 zval *the_stream;
374 getObject(http_response_object, obj);
375
376 NO_ARGS;
377
378 the_stream = GET_PROP(obj, stream);
379 RETURN_RESOURCE(Z_LVAL_P(the_stream));
380 }
381 /* }}} */
382
383 /* {{{ proto bool HttpResponse::setFile(string file)
384 *
385 * Set the file to be sent.
386 */
387 PHP_METHOD(HttpResponse, setFile)
388 {
389 zval *the_file;
390 getObject(http_response_object, obj);
391
392 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
393 RETURN_FALSE;
394 }
395
396 convert_to_string_ex(&the_file);
397
398 UPD_PROP(obj, string, file, Z_STRVAL_P(the_file));
399 UPD_PROP(obj, long, lastModified, http_lmod(the_file, -1));
400 UPD_PROP(obj, long, send_mode, -1);
401 RETURN_TRUE;
402 }
403 /* }}} */
404
405 /* {{{ proto string HttpResponse::getFile()
406 *
407 * Get the previously set file to be sent.
408 */
409 PHP_METHOD(HttpResponse, getFile)
410 {
411 zval *the_file;
412 getObject(http_response_object, obj);
413
414 NO_ARGS;
415
416 the_file = GET_PROP(obj, file);
417 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
418 }
419 /* }}} */
420
421 /* {{{ proto bool HttpResponse::send()
422 *
423 * Finally send the entity.
424 *
425 * Example:
426 * <pre>
427 * <?php
428 * $r = new HttpResponse(true);
429 * $r->setFile('../hidden/contract.pdf');
430 * $r->setContentType('application/pdf');
431 * $r->send();
432 * ?>
433 * </pre>
434 *
435 */
436 PHP_METHOD(HttpResponse, send)
437 {
438 zval *do_cache, *do_gzip;
439 getObject(http_response_object, obj);
440
441 NO_ARGS;
442
443 do_cache = GET_PROP(obj, cache);
444 do_gzip = GET_PROP(obj, gzip);
445
446 /* gzip */
447 if (Z_LVAL_P(do_gzip)) {
448 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC);
449 }
450
451 /* caching */
452 if (Z_LVAL_P(do_cache)) {
453 zval *cctrl, *etag, *lmod, *ccraw;
454
455 etag = GET_PROP(obj, eTag);
456 lmod = GET_PROP(obj, lastModified);
457 cctrl = GET_PROP(obj, cacheControl);
458 ccraw = GET_PROP(obj, raw_cache_header);
459
460 if (Z_LVAL_P(ccraw)) {
461 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
462 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));
463 } else {
464 char cc_header[42] = {0};
465 sprintf(cc_header, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl));
466 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), cc_header, strlen(cc_header));
467 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), cc_header, strlen(cc_header));
468 }
469 }
470
471 /* content type */
472 {
473 zval *ctype = GET_PROP(obj, contentType);
474 if (Z_STRLEN_P(ctype)) {
475 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
476 } else {
477 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
478 }
479 }
480
481 /* content disposition */
482 {
483 zval *dispo_file = GET_PROP(obj, dispoFile);
484 if (Z_STRLEN_P(dispo_file)) {
485 zval *dispo_inline = GET_PROP(obj, dispoInline);
486 http_send_content_disposition(Z_STRVAL_P(dispo_file), Z_STRLEN_P(dispo_file), (zend_bool) Z_LVAL_P(dispo_inline));
487 }
488 }
489
490 /* send */
491 {
492 zval *send_mode = GET_PROP(obj, send_mode);
493 switch (Z_LVAL_P(send_mode))
494 {
495 case SEND_DATA:
496 {
497 zval *zdata = GET_PROP(obj, data);
498 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
499 }
500
501 case SEND_RSRC:
502 {
503 php_stream *the_real_stream;
504 zval *the_stream = GET_PROP(obj, stream);
505 php_stream_from_zval(the_real_stream, &the_stream);
506 RETURN_SUCCESS(http_send_stream(the_real_stream));
507 }
508
509 default:
510 {
511 zval *zfile = GET_PROP(obj, file);
512 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile)));
513 }
514 }
515 }
516 }
517 /* }}} */
518 /* }}} */
519
520 /* {{{ HttpMessage */
521
522 /* {{{ void HttpMessage::__construct([string raw_message]) */
523 PHP_METHOD(HttpMessage, __construct)
524 {
525 zval *message = NULL;
526 int message_len;
527 getObject(http_message_object, obj);
528 http_message *msg = obj->message;
529
530 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/", &message)) {
531 return;
532 }
533
534 if (message) {
535 SET_PROP(obj, raw, message);
536 }
537 }
538 /* }}} */
539
540 /* {{{ void HttpMessage::__destruct() */
541 PHP_METHOD(HttpMessage, __destruct)
542 {
543 getObject(http_message_object, obj);
544
545 NO_ARGS;
546
547 }
548 /* }}} */
549
550 /* }}} */
551
552 #ifdef HTTP_HAVE_CURL
553 /* {{{ HttpRequest */
554
555 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
556 *
557 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
558 * and POST (including posting files) HTTP requests.
559 */
560 PHP_METHOD(HttpRequest, __construct)
561 {
562 char *URL = NULL;
563 int URL_len;
564 long meth = -1;
565 getObject(http_request_object, obj);
566
567 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
568 return;
569 }
570
571 INIT_PARR(obj, options);
572 INIT_PARR(obj, responseInfo);
573 INIT_PARR(obj, responseData);
574 INIT_PARR(obj, postData);
575 INIT_PARR(obj, postFiles);
576
577 if (URL) {
578 UPD_PROP(obj, string, url, URL);
579 }
580 if (meth > -1) {
581 UPD_PROP(obj, long, method, meth);
582 }
583 }
584 /* }}} */
585
586 /* {{{ proto void HttpRequest::__destruct()
587 *
588 * Destroys the HttpRequest object.
589 */
590 PHP_METHOD(HttpRequest, __destruct)
591 {
592 getObject(http_request_object, obj);
593
594 NO_ARGS;
595
596 FREE_PARR(obj, options);
597 FREE_PARR(obj, responseInfo);
598 FREE_PARR(obj, responseData);
599 FREE_PARR(obj, postData);
600 FREE_PARR(obj, postFiles);
601 }
602 /* }}} */
603
604 /* {{{ proto bool HttpRequest::setOptions(array options)
605 *
606 * Set the request options to use. See http_get() for a full list of available options.
607 */
608 PHP_METHOD(HttpRequest, setOptions)
609 {
610 char *key = NULL;
611 long idx = 0;
612 zval *opts, *old_opts, **opt;
613 getObject(http_request_object, obj);
614
615 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
616 RETURN_FALSE;
617 }
618
619 old_opts = GET_PROP(obj, options);
620
621 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
622 FOREACH_KEYVAL(opts, key, idx, opt) {
623 if (key) {
624 if (!strcmp(key, "headers")) {
625 zval **headers;
626 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
627 array_merge(*opt, *headers);
628 continue;
629 }
630 } else if (!strcmp(key, "cookies")) {
631 zval **cookies;
632 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
633 array_merge(*opt, *cookies);
634 continue;
635 }
636 }
637 zval_add_ref(opt);
638 add_assoc_zval(old_opts, key, *opt);
639
640 /* reset */
641 key = NULL;
642 }
643 }
644
645 RETURN_TRUE;
646 }
647 /* }}} */
648
649 /* {{{ proto array HttpRequest::getOptions()
650 *
651 * Get current set options.
652 */
653 PHP_METHOD(HttpRequest, getOptions)
654 {
655 zval *opts;
656 getObject(http_request_object, obj);
657
658 NO_ARGS;
659
660 opts = GET_PROP(obj, options);
661 array_init(return_value);
662 array_copy(opts, return_value);
663 }
664 /* }}} */
665
666 /* {{{ proto void HttpRequest::unsetOptions()
667 *
668 * Unset all options/headers/cookies.
669 */
670 PHP_METHOD(HttpRequest, unsetOptions)
671 {
672 getObject(http_request_object, obj);
673
674 NO_ARGS;
675
676 FREE_PARR(obj, options);
677 INIT_PARR(obj, options);
678 }
679 /* }}} */
680
681 /* {{{ proto bool HttpRequest::setSslOptions(array options)
682 *
683 * Set additional SSL options.
684 */
685 PHP_METHOD(HttpRequest, setSslOptions)
686 {
687 zval *opts, *old_opts, **ssl_options;
688 getObject(http_request_object, obj);
689
690 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
691 RETURN_FALSE;
692 }
693
694 old_opts = GET_PROP(obj, options);
695
696 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
697 array_merge(opts, *ssl_options);
698 } else {
699 zval_add_ref(&opts);
700 add_assoc_zval(old_opts, "ssl", opts);
701 }
702
703 RETURN_TRUE;
704 }
705 /* }}} */
706
707 /* {{{ proto array HttpRequest::getSslOtpions()
708 *
709 * Get previously set SSL options.
710 */
711 PHP_METHOD(HttpRequest, getSslOptions)
712 {
713 zval *opts, **ssl_options;
714 getObject(http_request_object, obj);
715
716 NO_ARGS;
717
718 opts = GET_PROP(obj, options);
719
720 array_init(return_value);
721
722 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
723 array_copy(*ssl_options, return_value);
724 }
725 }
726 /* }}} */
727
728 /* {{{ proto void HttpRequest::unsetSslOptions()
729 *
730 * Unset previously set SSL options.
731 */
732 PHP_METHOD(HttpRequest, unsetSslOptions)
733 {
734 zval *opts;
735 getObject(http_request_object, obj);
736
737 NO_ARGS;
738
739 opts = GET_PROP(obj, options);
740 zend_hash_del(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"));
741 }
742 /* }}} */
743
744 /* {{{ proto bool HttpRequest::addHeaders(array headers)
745 *
746 * Add request header name/value pairs.
747 */
748 PHP_METHOD(HttpRequest, addHeaders)
749 {
750 zval *opts, **headers, *new_headers;
751 getObject(http_request_object, obj);
752
753 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
754 RETURN_FALSE;
755 }
756
757 opts = GET_PROP(obj, options);
758
759 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
760 array_merge(new_headers, *headers);
761 } else {
762 zval_add_ref(&new_headers);
763 add_assoc_zval(opts, "headers", new_headers);
764 }
765
766 RETURN_TRUE;
767 }
768 /* }}} */
769
770 /* {{{ proto array HttpRequest::getHeaders()
771 *
772 * Get previously set request headers.
773 */
774 PHP_METHOD(HttpRequest, getHeaders)
775 {
776 zval *opts, **headers;
777 getObject(http_request_object, obj);
778
779 NO_ARGS;
780
781 opts = GET_PROP(obj, options);
782
783 array_init(return_value);
784
785 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
786 array_copy(*headers, return_value);
787 }
788 }
789 /* }}} */
790
791 /* {{{ proto void HttpRequest::unsetHeaders()
792 *
793 * Unset previously set request headers.
794 */
795 PHP_METHOD(HttpRequest, unsetHeaders)
796 {
797 zval *opts;
798 getObject(http_request_object, obj);
799
800 NO_ARGS;
801
802 opts = GET_PROP(obj, options);
803 zend_hash_del(Z_ARRVAL_P(opts), "headers", sizeof("headers"));
804 }
805 /* }}} */
806
807 /* {{{ proto bool HttpRequest::addCookies(array cookies)
808 *
809 * Add cookies.
810 */
811 PHP_METHOD(HttpRequest, addCookies)
812 {
813 zval *opts, **cookies, *new_cookies;
814 getObject(http_request_object, obj);
815
816 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_cookies)) {
817 RETURN_FALSE;
818 }
819
820 opts = GET_PROP(obj, options);
821
822 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
823 array_merge(new_cookies, *cookies);
824 } else {
825 zval_add_ref(&new_cookies);
826 add_assoc_zval(opts, "cookies", new_cookies);
827 }
828
829 RETURN_TRUE;
830 }
831 /* }}} */
832
833 /* {{{ proto array HttpRequest::getCookies()
834 *
835 * Get previously set cookies.
836 */
837 PHP_METHOD(HttpRequest, getCookies)
838 {
839 zval *opts, **cookies;
840 getObject(http_request_object, obj);
841
842 NO_ARGS;
843
844 opts = GET_PROP(obj, options);
845
846 array_init(return_value);
847
848 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
849 array_copy(*cookies, return_value);
850 }
851 }
852 /* }}} */
853
854 /* {{{ proto void HttpRequest::unsetCookies()
855 *
856 */
857 PHP_METHOD(HttpRequest, unsetCookies)
858 {
859 zval *opts;
860 getObject(http_request_object, obj);
861
862 NO_ARGS;
863
864 opts = GET_PROP(obj, options);
865 zend_hash_del(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"));
866 }
867 /* }}} */
868
869 /* {{{ proto bool HttpRequest::setURL(string url)
870 *
871 * Set the request URL.
872 */
873 PHP_METHOD(HttpRequest, setURL)
874 {
875 char *URL = NULL;
876 int URL_len;
877 getObject(http_request_object, obj);
878
879 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
880 RETURN_FALSE;
881 }
882
883 UPD_PROP(obj, string, url, URL);
884 RETURN_TRUE;
885 }
886 /* }}} */
887
888 /* {{{ proto string HttpRequest::getUrl()
889 *
890 * Get the previously set request URL.
891 */
892 PHP_METHOD(HttpRequest, getURL)
893 {
894 zval *URL;
895 getObject(http_request_object, obj);
896
897 NO_ARGS;
898
899 URL = GET_PROP(obj, url);
900 RETURN_STRINGL(Z_STRVAL_P(URL), Z_STRLEN_P(URL), 1);
901 }
902 /* }}} */
903
904 /* {{{ proto bool HttpRequest::setMethod(long request_method)
905 *
906 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
907 * <tt>HTTP_POST</tt> constants.
908 */
909 PHP_METHOD(HttpRequest, setMethod)
910 {
911 long meth;
912 getObject(http_request_object, obj);
913
914 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
915 RETURN_FALSE;
916 }
917
918 UPD_PROP(obj, long, method, meth);
919 RETURN_TRUE;
920 }
921 /* }}} */
922
923 /* {{{ proto long HttpRequest::getMethod()
924 *
925 * Get the previously set request method.
926 */
927 PHP_METHOD(HttpRequest, getMethod)
928 {
929 zval *meth;
930 getObject(http_request_object, obj);
931
932 NO_ARGS;
933
934 meth = GET_PROP(obj, method);
935 RETURN_LONG(Z_LVAL_P(meth));
936 }
937 /* }}} */
938
939 /* {{{ proto bool HttpRequest::setContentType(string content_type)
940 *
941 * Set the content type the post request should have.
942 * Use this only if you know what you're doing.
943 */
944 PHP_METHOD(HttpRequest, setContentType)
945 {
946 char *ctype;
947 int ct_len;
948 getObject(http_request_object, obj);
949
950 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
951 RETURN_FALSE;
952 }
953
954 if (!strchr(ctype, '/')) {
955 php_error_docref(NULL TSRMLS_CC, E_WARNING,
956 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
957 ctype);
958 RETURN_FALSE;
959 }
960
961 UPD_PROP(obj, string, contentType, ctype);
962 RETURN_TRUE;
963 }
964 /* }}} */
965
966 /* {{{ proto string HttpRequest::getContentType()
967 *
968 * Get the previously content type.
969 */
970 PHP_METHOD(HttpRequest, getContentType)
971 {
972 zval *ctype;
973 getObject(http_request_object, obj);
974
975 NO_ARGS;
976
977 ctype = GET_PROP(obj, contentType);
978 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
979 }
980 /* }}} */
981
982 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
983 *
984 * Set the URL query parameters to use.
985 * Overwrites previously set query parameters.
986 * Affects any request types.
987 */
988 PHP_METHOD(HttpRequest, setQueryData)
989 {
990 zval *qdata;
991 char *query_data = NULL;
992 getObject(http_request_object, obj);
993
994 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
995 RETURN_FALSE;
996 }
997
998 if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
999 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1000 RETURN_FALSE;
1001 }
1002 UPD_PROP(obj, string, queryData, query_data);
1003 efree(query_data);
1004 RETURN_TRUE;
1005 }
1006
1007 convert_to_string(qdata);
1008 UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
1009 RETURN_TRUE;
1010 }
1011 /* }}} */
1012
1013 /* {{{ proto string HttpRequest::getQueryData()
1014 *
1015 * Get the current query data in form of an urlencoded query string.
1016 */
1017 PHP_METHOD(HttpRequest, getQueryData)
1018 {
1019 zval *qdata;
1020 getObject(http_request_object, obj);
1021
1022 NO_ARGS;
1023
1024 qdata = GET_PROP(obj, queryData);
1025 RETURN_STRINGL(Z_STRVAL_P(qdata), Z_STRLEN_P(qdata), 1);
1026 }
1027 /* }}} */
1028
1029 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1030 *
1031 * Add parameters to the query parameter list.
1032 * Affects any request type.
1033 */
1034 PHP_METHOD(HttpRequest, addQueryData)
1035 {
1036 zval *qdata, *old_qdata;
1037 char *query_data = NULL;
1038 getObject(http_request_object, obj);
1039
1040 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1041 RETURN_FALSE;
1042 }
1043
1044 old_qdata = GET_PROP(obj, queryData);
1045
1046 if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, NULL)) {
1047 RETURN_FALSE;
1048 }
1049
1050 UPD_PROP(obj, string, queryData, query_data);
1051 efree(query_data);
1052
1053 RETURN_TRUE;
1054 }
1055 /* }}} */
1056
1057 /* {{{ proto void HttpRequest::unsetQueryData()
1058 *
1059 * Clean the query parameters.
1060 * Affects any request type.
1061 */
1062 PHP_METHOD(HttpRequest, unsetQueryData)
1063 {
1064 getObject(http_request_object, obj);
1065
1066 NO_ARGS;
1067
1068 UPD_PROP(obj, string, queryData, "");
1069 }
1070 /* }}} */
1071
1072 /* {{{ proto bool HttpRequest::addPostData(array post_data)
1073 *
1074 * Adds POST data entries.
1075 * Affects only POST requests.
1076 */
1077 PHP_METHOD(HttpRequest, addPostData)
1078 {
1079 zval *post, *post_data;
1080 getObject(http_request_object, obj);
1081
1082 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1083 RETURN_FALSE;
1084 }
1085
1086 post = GET_PROP(obj, postData);
1087 array_merge(post_data, post);
1088
1089 RETURN_TRUE;
1090 }
1091 /* }}} */
1092
1093 /* {{{ proto bool HttpRequest::setPostData(array post_data)
1094 *
1095 * Set the POST data entries.
1096 * Overwrites previously set POST data.
1097 * Affects only POST requests.
1098 */
1099 PHP_METHOD(HttpRequest, setPostData)
1100 {
1101 zval *post, *post_data;
1102 getObject(http_request_object, obj);
1103
1104 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1105 RETURN_FALSE;
1106 }
1107
1108 post = GET_PROP(obj, postData);
1109 zend_hash_clean(Z_ARRVAL_P(post));
1110 array_copy(post_data, post);
1111
1112 RETURN_TRUE;
1113 }
1114 /* }}}*/
1115
1116 /* {{{ proto array HttpRequest::getPostData()
1117 *
1118 * Get previously set POST data.
1119 */
1120 PHP_METHOD(HttpRequest, getPostData)
1121 {
1122 zval *post_data;
1123 getObject(http_request_object, obj);
1124
1125 NO_ARGS;
1126
1127 post_data = GET_PROP(obj, postData);
1128 array_init(return_value);
1129 array_copy(post_data, return_value);
1130 }
1131 /* }}} */
1132
1133 /* {{{ proto void HttpRequest::unsetPostData()
1134 *
1135 * Clean POST data entires.
1136 * Affects only POST requests.
1137 */
1138 PHP_METHOD(HttpRequest, unsetPostData)
1139 {
1140 zval *post_data;
1141 getObject(http_request_object, obj);
1142
1143 NO_ARGS;
1144
1145 post_data = GET_PROP(obj, postData);
1146 zend_hash_clean(Z_ARRVAL_P(post_data));
1147 }
1148 /* }}} */
1149
1150 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1151 *
1152 * Add a file to the POST request.
1153 * Affects only POST requests.
1154 */
1155 PHP_METHOD(HttpRequest, addPostFile)
1156 {
1157 zval *files, *entry;
1158 char *name, *file, *type = NULL;
1159 int name_len, file_len, type_len = 0;
1160 getObject(http_request_object, obj);
1161
1162 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1163 RETURN_FALSE;
1164 }
1165
1166 if (type_len) {
1167 if (!strchr(type, '/')) {
1168 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
1169 RETURN_FALSE;
1170 }
1171 } else {
1172 type = "application/x-octetstream";
1173 type_len = sizeof("application/x-octetstream") - 1;
1174 }
1175
1176 MAKE_STD_ZVAL(entry);
1177 array_init(entry);
1178
1179 add_assoc_stringl(entry, "name", name, name_len, 1);
1180 add_assoc_stringl(entry, "type", type, type_len, 1);
1181 add_assoc_stringl(entry, "file", file, file_len, 1);
1182
1183 files = GET_PROP(obj, postFiles);
1184 add_next_index_zval(files, entry);
1185
1186 RETURN_TRUE;
1187 }
1188 /* }}} */
1189
1190 /* {{{ proto array HttpRequest::getPostFiles()
1191 *
1192 * Get all previously added POST files.
1193 */
1194 PHP_METHOD(HttpRequest, getPostFiles)
1195 {
1196 zval *files;
1197 getObject(http_request_object, obj);
1198
1199 NO_ARGS;
1200
1201 files = GET_PROP(obj, postFiles);
1202
1203 array_init(return_value);
1204 array_copy(files, return_value);
1205 }
1206 /* }}} */
1207
1208 /* {{{ proto void HttpRequest::unsetPostFiles()
1209 *
1210 * Unset the POST files list.
1211 * Affects only POST requests.
1212 */
1213 PHP_METHOD(HttpRequest, unsetPostFiles)
1214 {
1215 zval *files;
1216 getObject(http_request_object, obj);
1217
1218 NO_ARGS;
1219
1220 files = GET_PROP(obj, postFiles);
1221 zend_hash_clean(Z_ARRVAL_P(files));
1222 }
1223 /* }}} */
1224
1225 /* {{{ proto array HttpRequest::getResponseData()
1226 *
1227 * Get all response data after the request has been sent.
1228 */
1229 PHP_METHOD(HttpRequest, getResponseData)
1230 {
1231 zval *data;
1232 getObject(http_request_object, obj);
1233
1234 NO_ARGS;
1235
1236 data = GET_PROP(obj, responseData);
1237 array_init(return_value);
1238 array_copy(data, return_value);
1239 }
1240 /* }}} */
1241
1242 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1243 *
1244 * Get response header(s) after the request has been sent.
1245 */
1246 PHP_METHOD(HttpRequest, getResponseHeader)
1247 {
1248 zval *data, **headers, **header;
1249 char *header_name = NULL;
1250 int header_len = 0;
1251 getObject(http_response_object, obj);
1252
1253 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1254 RETURN_FALSE;
1255 }
1256
1257 data = GET_PROP(obj, responseData);
1258 if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1259 RETURN_FALSE;
1260 }
1261
1262 if (!header_len || !header_name) {
1263 array_init(return_value);
1264 array_copy(*headers, return_value);
1265 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1266 RETURN_STRINGL(Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
1267 } else {
1268 RETURN_FALSE;
1269 }
1270 }
1271 /* }}} */
1272
1273 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1274 *
1275 * Get response cookie(s) after the request has been sent.
1276 */
1277 PHP_METHOD(HttpRequest, getResponseCookie)
1278 {
1279 zval *data, **headers;
1280 char *cookie_name = NULL;
1281 int cookie_len = 0;
1282 getObject(http_request_object, obj);
1283
1284 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
1285 RETURN_FALSE;
1286 }
1287
1288 array_init(return_value);
1289
1290 data = GET_PROP(obj, responseData);
1291 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1292 ulong idx = 0;
1293 char *key = NULL;
1294 zval **header = NULL;
1295
1296 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers), key, idx, header) {
1297 if (key && !strcasecmp(key, "Set-Cookie")) {
1298 /* several cookies? */
1299 if (Z_TYPE_PP(header) == IS_ARRAY) {
1300 zval **cookie;
1301
1302 FOREACH_HASH_VAL(Z_ARRVAL_PP(header), cookie) {
1303 zval *cookie_hash;
1304 MAKE_STD_ZVAL(cookie_hash);
1305 array_init(cookie_hash);
1306
1307 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
1308 if (!cookie_len) {
1309 add_next_index_zval(return_value, cookie_hash);
1310 } else {
1311 zval **name;
1312
1313 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1314 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1315 add_next_index_zval(return_value, cookie_hash);
1316 return; /* <<< FOUND >>> */
1317 } else {
1318 zval_dtor(cookie_hash);
1319 efree(cookie_hash);
1320 }
1321 }
1322 } else {
1323 zval_dtor(cookie_hash);
1324 efree(cookie_hash);
1325 }
1326 }
1327 } else {
1328 zval *cookie_hash;
1329 MAKE_STD_ZVAL(cookie_hash);
1330 array_init(cookie_hash);
1331
1332 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
1333 if (!cookie_len) {
1334 add_next_index_zval(return_value, cookie_hash);
1335 } else {
1336 zval **name;
1337
1338 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1339 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1340 add_next_index_zval(return_value, cookie_hash);
1341 } else {
1342 zval_dtor(cookie_hash);
1343 efree(cookie_hash);
1344 }
1345 }
1346 } else {
1347 zval_dtor(cookie_hash);
1348 efree(cookie_hash);
1349 }
1350 }
1351 break;
1352 }
1353 /* reset key */
1354 key = NULL;
1355 }
1356 }
1357 }
1358 /* }}} */
1359
1360 /* {{{ proto string HttpRequest::getResponseBody()
1361 *
1362 * Get the response body after the request has been sent.
1363 */
1364 PHP_METHOD(HttpRequest, getResponseBody)
1365 {
1366 zval *data, **body;
1367 getObject(http_request_object, obj);
1368
1369 NO_ARGS;
1370
1371 data = GET_PROP(obj, responseData);
1372 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1373 RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
1374 } else {
1375 RETURN_FALSE;
1376 }
1377 }
1378 /* }}} */
1379
1380 /* {{{ proto int HttpRequest::getResponseCode()
1381 *
1382 * Get the response code after the request has been sent.
1383 */
1384 PHP_METHOD(HttpRequest, getResponseCode)
1385 {
1386 zval **code, **hdrs, *data;
1387 getObject(http_request_object, obj);
1388
1389 NO_ARGS;
1390
1391 data = GET_PROP(obj, responseData);
1392 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &hdrs)) &&
1393 (SUCCESS == zend_hash_find(Z_ARRVAL_PP(hdrs), "Response Status", sizeof("Response Status"), (void **) &code))) {
1394 RETVAL_STRINGL(Z_STRVAL_PP(code), Z_STRLEN_PP(code), 1);
1395 convert_to_long(return_value);
1396 } else {
1397 RETURN_FALSE;
1398 }
1399 }
1400 /* }}} */
1401
1402 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1403 *
1404 * Get response info after the request has been sent.
1405 * See http_get() for a full list of returned info.
1406 */
1407 PHP_METHOD(HttpRequest, getResponseInfo)
1408 {
1409 zval *info, **infop;
1410 char *info_name = NULL;
1411 int info_len = 0;
1412 getObject(http_request_object, obj);
1413
1414 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1415 RETURN_FALSE;
1416 }
1417
1418 info = GET_PROP(obj, responseInfo);
1419
1420 if (info_len && info_name) {
1421 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1422 RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
1423 } else {
1424 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not find response info named %s", info_name);
1425 RETURN_FALSE;
1426 }
1427 } else {
1428 array_init(return_value);
1429 array_copy(info, return_value);
1430 }
1431 }
1432 /* }}}*/
1433
1434 /* {{{ proto bool HttpRequest::send()
1435 *
1436 * Send the HTTP request.
1437 *
1438 * GET example:
1439 * <pre>
1440 * <?php
1441 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1442 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1443 * $r->addQueryData(array('category' => 3));
1444 * if ($r->send() && $r->getResponseCode() == 200) {
1445 * file_put_contents('local.rss', $r->getResponseBody());
1446 * }
1447 * ?>
1448 * </pre>
1449 *
1450 * POST example:
1451 * <pre>
1452 * <?php
1453 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1454 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1455 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1456 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1457 * if ($r->send()) {
1458 * echo $r->getResponseBody();
1459 * }
1460 * ?>
1461 * </pre>
1462 */
1463 PHP_METHOD(HttpRequest, send)
1464 {
1465 STATUS status = FAILURE;
1466 zval *meth, *URL, *qdata, *opts, *info, *resp;
1467 char *response_data, *request_uri;
1468 size_t response_len;
1469 getObject(http_request_object, obj);
1470
1471 NO_ARGS;
1472
1473 if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
1474 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initilaize cURL");
1475 RETURN_FALSE;
1476 }
1477
1478 meth = GET_PROP(obj, method);
1479 URL = GET_PROP(obj, url);
1480 qdata = GET_PROP(obj, queryData);
1481 opts = GET_PROP(obj, options);
1482 info = GET_PROP(obj, responseInfo);
1483 resp = GET_PROP(obj, responseData);
1484
1485 // HTTP_URI_MAXLEN+1 long char *
1486 request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0);
1487
1488 if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
1489 if (!strchr(request_uri, '?')) {
1490 strcat(request_uri, "?");
1491 } else {
1492 strcat(request_uri, "&");
1493 }
1494 strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
1495 }
1496
1497 switch (Z_LVAL_P(meth))
1498 {
1499 case HTTP_GET:
1500 status = http_get_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1501 break;
1502
1503 case HTTP_HEAD:
1504 status = http_head_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1505 break;
1506
1507 case HTTP_POST:
1508 {
1509 zval *post_files, *post_data;
1510
1511 post_files = GET_PROP(obj, postFiles);
1512 post_data = GET_PROP(obj, postData);
1513
1514 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files))) {
1515
1516 /* urlencoded post */
1517 status = http_post_array_ex(obj->ch, request_uri, Z_ARRVAL_P(post_data), Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1518
1519 } else {
1520
1521 /*
1522 * multipart post
1523 */
1524 char *key = NULL;
1525 long idx;
1526 zval **data;
1527 struct curl_httppost *http_post_data[2] = {NULL, NULL};
1528
1529 /* normal data */
1530 FOREACH_KEYVAL(post_data, key, idx, data) {
1531 if (key) {
1532 convert_to_string_ex(data);
1533 curl_formadd(&http_post_data[0], &http_post_data[1],
1534 CURLFORM_COPYNAME, key,
1535 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
1536 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
1537 CURLFORM_END
1538 );
1539
1540 /* reset */
1541 key = NULL;
1542 }
1543 }
1544
1545 /* file data */
1546 FOREACH_VAL(post_files, data) {
1547 zval **file, **type, **name;
1548
1549 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
1550 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
1551 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
1552
1553 curl_formadd(&http_post_data[0], &http_post_data[1],
1554 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
1555 CURLFORM_FILE, Z_STRVAL_PP(file),
1556 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
1557 CURLFORM_END
1558 );
1559 }
1560 }
1561
1562 status = http_post_curldata_ex(obj->ch, request_uri, http_post_data[0], Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1563 curl_formfree(http_post_data[0]);
1564 }
1565 }
1566 break;
1567
1568 default:
1569 break;
1570 }
1571
1572 efree(request_uri);
1573
1574 /* final data handling */
1575 if (status != SUCCESS) {
1576 RETURN_FALSE;
1577 } else {
1578 char *body = NULL;
1579 size_t body_len = 0;
1580 zval *zheaders;
1581
1582 MAKE_STD_ZVAL(zheaders)
1583 array_init(zheaders);
1584
1585 if (SUCCESS != http_split_response_ex(response_data, response_len, Z_ARRVAL_P(zheaders), &body, &body_len)) {
1586 zval_dtor(zheaders);
1587 efree(zheaders),
1588 efree(response_data);
1589 RETURN_FALSE;
1590 }
1591
1592 add_assoc_zval(resp, "headers", zheaders);
1593 add_assoc_stringl(resp, "body", body, body_len, 0);
1594
1595 efree(response_data);
1596
1597 RETURN_TRUE;
1598 }
1599 /* */
1600 }
1601 /* }}} */
1602 /* }}} */
1603 #endif /* HTTP_HAVE_CURL */
1604
1605 #endif /* ZEND_ENGINE_2 */
1606
1607 /*
1608 * Local variables:
1609 * tab-width: 4
1610 * c-basic-offset: 4
1611 * End:
1612 * vim600: noet sw=4 ts=4 fdm=marker
1613 * vim<600: noet sw=4 ts=4
1614 */
1615