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