- use exceptions in constructors and HttpRequest::send()
[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 #include "php_http_message_object.h"
34 #include "php_http_response_object.h"
35 #include "php_http_request_object.h"
36 #include "php_http_exception_object.h"
37
38 #ifdef ZEND_ENGINE_2
39
40 /* {{{ HttpResponse */
41
42 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
43 *
44 * Instantiates a new HttpResponse object, which can be used to send
45 * any data/resource/file to an HTTP client with caching and multiple
46 * ranges/resuming support.
47 *
48 * NOTE: GZIPping is not implemented yet.
49 */
50 PHP_METHOD(HttpResponse, __construct)
51 {
52 zend_bool do_cache = 0, do_gzip = 0;
53 getObject(http_response_object, obj);
54
55 SET_EH_THROW_HTTP();
56 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
57 UPD_PROP(obj, long, cache, do_cache);
58 UPD_PROP(obj, long, gzip, do_gzip);
59 }
60 SET_EH_NORMAL();
61 }
62 /* }}} */
63
64 /* {{{ proto bool HttpResponse::setCache(bool cache)
65 *
66 * Whether it sould be attempted to cache the entitity.
67 * This will result in necessary caching headers and checks of clients
68 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
69 * matches a "304 Not Modified" status code will be issued.
70 *
71 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
72 * to something more appropriate than "no-cache"!
73 */
74 PHP_METHOD(HttpResponse, setCache)
75 {
76 zend_bool do_cache = 0;
77 getObject(http_response_object, obj);
78
79 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
80 RETURN_FALSE;
81 }
82
83 UPD_PROP(obj, long, cache, do_cache);
84 RETURN_TRUE;
85 }
86 /* }}} */
87
88 /* {{{ proto bool HttpResponse::getCache()
89 *
90 * Get current caching setting.
91 */
92 PHP_METHOD(HttpResponse, getCache)
93 {
94 zval *do_cache = NULL;
95 getObject(http_response_object, obj);
96
97 NO_ARGS;
98
99 do_cache = GET_PROP(obj, cache);
100 RETURN_BOOL(Z_LVAL_P(do_cache));
101 }
102 /* }}}*/
103
104 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
105 *
106 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
107 */
108 PHP_METHOD(HttpResponse, setGzip)
109 {
110 zend_bool do_gzip = 0;
111 getObject(http_response_object, obj);
112
113 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
114 RETURN_FALSE;
115 }
116
117 UPD_PROP(obj, long, gzip, do_gzip);
118 RETURN_TRUE;
119 }
120 /* }}} */
121
122 /* {{{ proto bool HttpResponse::getGzip()
123 *
124 * Get current gzipping setting.
125 */
126 PHP_METHOD(HttpResponse, getGzip)
127 {
128 zval *do_gzip = NULL;
129 getObject(http_response_object, obj);
130
131 NO_ARGS;
132
133 do_gzip = GET_PROP(obj, gzip);
134 RETURN_BOOL(Z_LVAL_P(do_gzip));
135 }
136 /* }}} */
137
138 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
139 *
140 * Set a custom cache-control header, usually being "private" or "public"; if
141 * $raw is set to true the header will be sent as-is.
142 */
143 PHP_METHOD(HttpResponse, setCacheControl)
144 {
145 char *ccontrol;
146 int cc_len;
147 zend_bool raw = 0;
148 getObject(http_response_object, obj);
149
150 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
151 RETURN_FALSE;
152 }
153
154 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
155 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
156 RETURN_FALSE;
157 }
158
159 UPD_PROP(obj, long, raw_cache_header, raw);
160 UPD_PROP(obj, string, cacheControl, ccontrol);
161 RETURN_TRUE;
162 }
163 /* }}} */
164
165 /* {{{ proto string HttpResponse::getCacheControl()
166 *
167 * Get current Cache-Control header setting.
168 */
169 PHP_METHOD(HttpResponse, getCacheControl)
170 {
171 zval *ccontrol;
172 getObject(http_response_object, obj);
173
174 NO_ARGS;
175
176 ccontrol = GET_PROP(obj, cacheControl);
177 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
178 }
179 /* }}} */
180
181 /* {{{ proto bool HttpResponse::setContentType(string content_type)
182 *
183 * Set the content-type of the sent entity.
184 */
185 PHP_METHOD(HttpResponse, setContentType)
186 {
187 char *ctype;
188 int ctype_len;
189 getObject(http_response_object, obj);
190
191 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
192 RETURN_FALSE;
193 }
194
195 if (!strchr(ctype, '/')) {
196 php_error_docref(NULL TSRMLS_CC, E_WARNING,
197 "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_lmod(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_lmod(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_lmod(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 /* {{{ void HttpMessage::__construct([string raw_message])
527 *
528 * Instantiate a new HttpMessage object based on the optionally provided
529 * raw message. An HTTP Message can be either a response or a request.
530 */
531 PHP_METHOD(HttpMessage, __construct)
532 {
533 zval *message = NULL;
534 int message_len;
535 getObject(http_message_object, obj);
536
537 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/", &message)) {
538 return;
539 }
540
541 if (message) {
542 convert_to_string(message);
543 SET_PROP(obj, raw, message);
544 }
545 }
546 /* }}} */
547
548 /* {{{ void HttpMessage::setRaw(string raw_message)
549 *
550 * Parse a new raw message.
551 */
552 PHP_METHOD(HttpMessage, setRaw)
553 {
554 zval *message;
555 getObject(http_message_object, obj);
556
557 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &message)) {
558 return;
559 }
560
561 convert_to_string(message);
562 SET_PROP(obj, raw, message);
563 }
564 /* }}} */
565
566 /* {{{ string HttpMessage::getBody()
567 *
568 * Get the body of the parsed Message.
569 */
570 PHP_METHOD(HttpMessage, getBody)
571 {
572 zval *body;
573 getObject(http_message_object, obj);
574
575 NO_ARGS;
576
577 body = GET_PROP(obj, body);
578 RETURN_STRINGL(Z_STRVAL_P(body), Z_STRLEN_P(body), 1);
579 }
580 /* }}} */
581
582 /* {{{ array HttpMessage::getHeaders()
583 *
584 * Get Message Headers.
585 */
586 PHP_METHOD(HttpMessage, getHeaders)
587 {
588 zval *headers;
589 getObject(http_message_object, obj);
590
591 NO_ARGS;
592
593 headers = GET_PROP(obj, headers);
594 array_init(return_value);
595 array_copy(headers, return_value);
596 }
597 /* }}} */
598
599 /* }}} */
600
601 #ifdef HTTP_HAVE_CURL
602 /* {{{ HttpRequest */
603
604 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
605 *
606 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
607 * and POST (including posting files) HTTP requests.
608 */
609 PHP_METHOD(HttpRequest, __construct)
610 {
611 char *URL = NULL;
612 int URL_len;
613 long meth = -1;
614 getObject(http_request_object, obj);
615
616 SET_EH_THROW_HTTP();
617 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
618 INIT_PARR(obj, options);
619 INIT_PARR(obj, responseInfo);
620 INIT_PARR(obj, responseData);
621 INIT_PARR(obj, postData);
622 INIT_PARR(obj, postFiles);
623
624 if (URL) {
625 UPD_PROP(obj, string, url, URL);
626 }
627 if (meth > -1) {
628 UPD_PROP(obj, long, method, meth);
629 }
630 }
631 SET_EH_NORMAL();
632 }
633 /* }}} */
634
635 /* {{{ proto void HttpRequest::__destruct()
636 *
637 * Destroys the HttpRequest object.
638 */
639 PHP_METHOD(HttpRequest, __destruct)
640 {
641 getObject(http_request_object, obj);
642
643 NO_ARGS;
644
645 FREE_PARR(obj, options);
646 FREE_PARR(obj, responseInfo);
647 FREE_PARR(obj, responseData);
648 FREE_PARR(obj, postData);
649 FREE_PARR(obj, postFiles);
650 }
651 /* }}} */
652
653 /* {{{ proto bool HttpRequest::setOptions(array options)
654 *
655 * Set the request options to use. See http_get() for a full list of available options.
656 */
657 PHP_METHOD(HttpRequest, setOptions)
658 {
659 char *key = NULL;
660 long idx = 0;
661 zval *opts, *old_opts, **opt;
662 getObject(http_request_object, obj);
663
664 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
665 RETURN_FALSE;
666 }
667
668 old_opts = GET_PROP(obj, options);
669
670 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
671 FOREACH_KEYVAL(opts, key, idx, opt) {
672 if (key) {
673 if (!strcmp(key, "headers")) {
674 zval **headers;
675 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
676 array_merge(*opt, *headers);
677 continue;
678 }
679 } else if (!strcmp(key, "cookies")) {
680 zval **cookies;
681 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
682 array_merge(*opt, *cookies);
683 continue;
684 }
685 }
686 zval_add_ref(opt);
687 add_assoc_zval(old_opts, key, *opt);
688
689 /* reset */
690 key = NULL;
691 }
692 }
693
694 RETURN_TRUE;
695 }
696 /* }}} */
697
698 /* {{{ proto array HttpRequest::getOptions()
699 *
700 * Get current set options.
701 */
702 PHP_METHOD(HttpRequest, getOptions)
703 {
704 zval *opts;
705 getObject(http_request_object, obj);
706
707 NO_ARGS;
708
709 opts = GET_PROP(obj, options);
710 array_init(return_value);
711 array_copy(opts, return_value);
712 }
713 /* }}} */
714
715 /* {{{ proto void HttpRequest::unsetOptions()
716 *
717 * Unset all options/headers/cookies.
718 */
719 PHP_METHOD(HttpRequest, unsetOptions)
720 {
721 getObject(http_request_object, obj);
722
723 NO_ARGS;
724
725 FREE_PARR(obj, options);
726 INIT_PARR(obj, options);
727 }
728 /* }}} */
729
730 /* {{{ proto bool HttpRequest::setSslOptions(array options)
731 *
732 * Set additional SSL options.
733 */
734 PHP_METHOD(HttpRequest, setSslOptions)
735 {
736 zval *opts, *old_opts, **ssl_options;
737 getObject(http_request_object, obj);
738
739 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
740 RETURN_FALSE;
741 }
742
743 old_opts = GET_PROP(obj, options);
744
745 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
746 array_merge(opts, *ssl_options);
747 } else {
748 zval_add_ref(&opts);
749 add_assoc_zval(old_opts, "ssl", opts);
750 }
751
752 RETURN_TRUE;
753 }
754 /* }}} */
755
756 /* {{{ proto array HttpRequest::getSslOtpions()
757 *
758 * Get previously set SSL options.
759 */
760 PHP_METHOD(HttpRequest, getSslOptions)
761 {
762 zval *opts, **ssl_options;
763 getObject(http_request_object, obj);
764
765 NO_ARGS;
766
767 opts = GET_PROP(obj, options);
768
769 array_init(return_value);
770
771 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
772 array_copy(*ssl_options, return_value);
773 }
774 }
775 /* }}} */
776
777 /* {{{ proto void HttpRequest::unsetSslOptions()
778 *
779 * Unset previously set SSL options.
780 */
781 PHP_METHOD(HttpRequest, unsetSslOptions)
782 {
783 zval *opts;
784 getObject(http_request_object, obj);
785
786 NO_ARGS;
787
788 opts = GET_PROP(obj, options);
789 zend_hash_del(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"));
790 }
791 /* }}} */
792
793 /* {{{ proto bool HttpRequest::addHeaders(array headers)
794 *
795 * Add request header name/value pairs.
796 */
797 PHP_METHOD(HttpRequest, addHeaders)
798 {
799 zval *opts, **headers, *new_headers;
800 getObject(http_request_object, obj);
801
802 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
803 RETURN_FALSE;
804 }
805
806 opts = GET_PROP(obj, options);
807
808 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
809 array_merge(new_headers, *headers);
810 } else {
811 zval_add_ref(&new_headers);
812 add_assoc_zval(opts, "headers", new_headers);
813 }
814
815 RETURN_TRUE;
816 }
817 /* }}} */
818
819 /* {{{ proto array HttpRequest::getHeaders()
820 *
821 * Get previously set request headers.
822 */
823 PHP_METHOD(HttpRequest, getHeaders)
824 {
825 zval *opts, **headers;
826 getObject(http_request_object, obj);
827
828 NO_ARGS;
829
830 opts = GET_PROP(obj, options);
831
832 array_init(return_value);
833
834 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
835 array_copy(*headers, return_value);
836 }
837 }
838 /* }}} */
839
840 /* {{{ proto void HttpRequest::unsetHeaders()
841 *
842 * Unset previously set request headers.
843 */
844 PHP_METHOD(HttpRequest, unsetHeaders)
845 {
846 zval *opts;
847 getObject(http_request_object, obj);
848
849 NO_ARGS;
850
851 opts = GET_PROP(obj, options);
852 zend_hash_del(Z_ARRVAL_P(opts), "headers", sizeof("headers"));
853 }
854 /* }}} */
855
856 /* {{{ proto bool HttpRequest::addCookies(array cookies)
857 *
858 * Add cookies.
859 */
860 PHP_METHOD(HttpRequest, addCookies)
861 {
862 zval *opts, **cookies, *new_cookies;
863 getObject(http_request_object, obj);
864
865 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_cookies)) {
866 RETURN_FALSE;
867 }
868
869 opts = GET_PROP(obj, options);
870
871 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
872 array_merge(new_cookies, *cookies);
873 } else {
874 zval_add_ref(&new_cookies);
875 add_assoc_zval(opts, "cookies", new_cookies);
876 }
877
878 RETURN_TRUE;
879 }
880 /* }}} */
881
882 /* {{{ proto array HttpRequest::getCookies()
883 *
884 * Get previously set cookies.
885 */
886 PHP_METHOD(HttpRequest, getCookies)
887 {
888 zval *opts, **cookies;
889 getObject(http_request_object, obj);
890
891 NO_ARGS;
892
893 opts = GET_PROP(obj, options);
894
895 array_init(return_value);
896
897 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
898 array_copy(*cookies, return_value);
899 }
900 }
901 /* }}} */
902
903 /* {{{ proto void HttpRequest::unsetCookies()
904 *
905 */
906 PHP_METHOD(HttpRequest, unsetCookies)
907 {
908 zval *opts;
909 getObject(http_request_object, obj);
910
911 NO_ARGS;
912
913 opts = GET_PROP(obj, options);
914 zend_hash_del(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"));
915 }
916 /* }}} */
917
918 /* {{{ proto bool HttpRequest::setURL(string url)
919 *
920 * Set the request URL.
921 */
922 PHP_METHOD(HttpRequest, setURL)
923 {
924 char *URL = NULL;
925 int URL_len;
926 getObject(http_request_object, obj);
927
928 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
929 RETURN_FALSE;
930 }
931
932 UPD_PROP(obj, string, url, URL);
933 RETURN_TRUE;
934 }
935 /* }}} */
936
937 /* {{{ proto string HttpRequest::getUrl()
938 *
939 * Get the previously set request URL.
940 */
941 PHP_METHOD(HttpRequest, getURL)
942 {
943 zval *URL;
944 getObject(http_request_object, obj);
945
946 NO_ARGS;
947
948 URL = GET_PROP(obj, url);
949 RETURN_STRINGL(Z_STRVAL_P(URL), Z_STRLEN_P(URL), 1);
950 }
951 /* }}} */
952
953 /* {{{ proto bool HttpRequest::setMethod(long request_method)
954 *
955 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
956 * <tt>HTTP_POST</tt> constants.
957 */
958 PHP_METHOD(HttpRequest, setMethod)
959 {
960 long meth;
961 getObject(http_request_object, obj);
962
963 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
964 RETURN_FALSE;
965 }
966
967 UPD_PROP(obj, long, method, meth);
968 RETURN_TRUE;
969 }
970 /* }}} */
971
972 /* {{{ proto long HttpRequest::getMethod()
973 *
974 * Get the previously set request method.
975 */
976 PHP_METHOD(HttpRequest, getMethod)
977 {
978 zval *meth;
979 getObject(http_request_object, obj);
980
981 NO_ARGS;
982
983 meth = GET_PROP(obj, method);
984 RETURN_LONG(Z_LVAL_P(meth));
985 }
986 /* }}} */
987
988 /* {{{ proto bool HttpRequest::setContentType(string content_type)
989 *
990 * Set the content type the post request should have.
991 * Use this only if you know what you're doing.
992 */
993 PHP_METHOD(HttpRequest, setContentType)
994 {
995 char *ctype;
996 int ct_len;
997 getObject(http_request_object, obj);
998
999 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1000 RETURN_FALSE;
1001 }
1002
1003 if (!strchr(ctype, '/')) {
1004 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1005 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
1006 ctype);
1007 RETURN_FALSE;
1008 }
1009
1010 UPD_PROP(obj, string, contentType, ctype);
1011 RETURN_TRUE;
1012 }
1013 /* }}} */
1014
1015 /* {{{ proto string HttpRequest::getContentType()
1016 *
1017 * Get the previously content type.
1018 */
1019 PHP_METHOD(HttpRequest, getContentType)
1020 {
1021 zval *ctype;
1022 getObject(http_request_object, obj);
1023
1024 NO_ARGS;
1025
1026 ctype = GET_PROP(obj, contentType);
1027 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
1028 }
1029 /* }}} */
1030
1031 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1032 *
1033 * Set the URL query parameters to use.
1034 * Overwrites previously set query parameters.
1035 * Affects any request types.
1036 */
1037 PHP_METHOD(HttpRequest, setQueryData)
1038 {
1039 zval *qdata;
1040 char *query_data = NULL;
1041 getObject(http_request_object, obj);
1042
1043 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
1044 RETURN_FALSE;
1045 }
1046
1047 if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1048 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1049 RETURN_FALSE;
1050 }
1051 UPD_PROP(obj, string, queryData, query_data);
1052 efree(query_data);
1053 RETURN_TRUE;
1054 }
1055
1056 convert_to_string(qdata);
1057 UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
1058 RETURN_TRUE;
1059 }
1060 /* }}} */
1061
1062 /* {{{ proto string HttpRequest::getQueryData()
1063 *
1064 * Get the current query data in form of an urlencoded query string.
1065 */
1066 PHP_METHOD(HttpRequest, getQueryData)
1067 {
1068 zval *qdata;
1069 getObject(http_request_object, obj);
1070
1071 NO_ARGS;
1072
1073 qdata = GET_PROP(obj, queryData);
1074 RETURN_STRINGL(Z_STRVAL_P(qdata), Z_STRLEN_P(qdata), 1);
1075 }
1076 /* }}} */
1077
1078 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1079 *
1080 * Add parameters to the query parameter list.
1081 * Affects any request type.
1082 */
1083 PHP_METHOD(HttpRequest, addQueryData)
1084 {
1085 zval *qdata, *old_qdata;
1086 char *query_data = NULL;
1087 getObject(http_request_object, obj);
1088
1089 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1090 RETURN_FALSE;
1091 }
1092
1093 old_qdata = GET_PROP(obj, queryData);
1094
1095 if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, NULL)) {
1096 RETURN_FALSE;
1097 }
1098
1099 UPD_PROP(obj, string, queryData, query_data);
1100 efree(query_data);
1101
1102 RETURN_TRUE;
1103 }
1104 /* }}} */
1105
1106 /* {{{ proto void HttpRequest::unsetQueryData()
1107 *
1108 * Clean the query parameters.
1109 * Affects any request type.
1110 */
1111 PHP_METHOD(HttpRequest, unsetQueryData)
1112 {
1113 getObject(http_request_object, obj);
1114
1115 NO_ARGS;
1116
1117 UPD_PROP(obj, string, queryData, "");
1118 }
1119 /* }}} */
1120
1121 /* {{{ proto bool HttpRequest::addPostData(array post_data)
1122 *
1123 * Adds POST data entries.
1124 * Affects only POST requests.
1125 */
1126 PHP_METHOD(HttpRequest, addPostData)
1127 {
1128 zval *post, *post_data;
1129 getObject(http_request_object, obj);
1130
1131 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1132 RETURN_FALSE;
1133 }
1134
1135 post = GET_PROP(obj, postData);
1136 array_merge(post_data, post);
1137
1138 RETURN_TRUE;
1139 }
1140 /* }}} */
1141
1142 /* {{{ proto bool HttpRequest::setPostData(array post_data)
1143 *
1144 * Set the POST data entries.
1145 * Overwrites previously set POST data.
1146 * Affects only POST requests.
1147 */
1148 PHP_METHOD(HttpRequest, setPostData)
1149 {
1150 zval *post, *post_data;
1151 getObject(http_request_object, obj);
1152
1153 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1154 RETURN_FALSE;
1155 }
1156
1157 post = GET_PROP(obj, postData);
1158 zend_hash_clean(Z_ARRVAL_P(post));
1159 array_copy(post_data, post);
1160
1161 RETURN_TRUE;
1162 }
1163 /* }}}*/
1164
1165 /* {{{ proto array HttpRequest::getPostData()
1166 *
1167 * Get previously set POST data.
1168 */
1169 PHP_METHOD(HttpRequest, getPostData)
1170 {
1171 zval *post_data;
1172 getObject(http_request_object, obj);
1173
1174 NO_ARGS;
1175
1176 post_data = GET_PROP(obj, postData);
1177 array_init(return_value);
1178 array_copy(post_data, return_value);
1179 }
1180 /* }}} */
1181
1182 /* {{{ proto void HttpRequest::unsetPostData()
1183 *
1184 * Clean POST data entires.
1185 * Affects only POST requests.
1186 */
1187 PHP_METHOD(HttpRequest, unsetPostData)
1188 {
1189 zval *post_data;
1190 getObject(http_request_object, obj);
1191
1192 NO_ARGS;
1193
1194 post_data = GET_PROP(obj, postData);
1195 zend_hash_clean(Z_ARRVAL_P(post_data));
1196 }
1197 /* }}} */
1198
1199 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1200 *
1201 * Add a file to the POST request.
1202 * Affects only POST requests.
1203 */
1204 PHP_METHOD(HttpRequest, addPostFile)
1205 {
1206 zval *files, *entry;
1207 char *name, *file, *type = NULL;
1208 int name_len, file_len, type_len = 0;
1209 getObject(http_request_object, obj);
1210
1211 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1212 RETURN_FALSE;
1213 }
1214
1215 if (type_len) {
1216 if (!strchr(type, '/')) {
1217 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
1218 RETURN_FALSE;
1219 }
1220 } else {
1221 type = "application/x-octetstream";
1222 type_len = sizeof("application/x-octetstream") - 1;
1223 }
1224
1225 MAKE_STD_ZVAL(entry);
1226 array_init(entry);
1227
1228 add_assoc_stringl(entry, "name", name, name_len, 1);
1229 add_assoc_stringl(entry, "type", type, type_len, 1);
1230 add_assoc_stringl(entry, "file", file, file_len, 1);
1231
1232 files = GET_PROP(obj, postFiles);
1233 add_next_index_zval(files, entry);
1234
1235 RETURN_TRUE;
1236 }
1237 /* }}} */
1238
1239 /* {{{ proto array HttpRequest::getPostFiles()
1240 *
1241 * Get all previously added POST files.
1242 */
1243 PHP_METHOD(HttpRequest, getPostFiles)
1244 {
1245 zval *files;
1246 getObject(http_request_object, obj);
1247
1248 NO_ARGS;
1249
1250 files = GET_PROP(obj, postFiles);
1251
1252 array_init(return_value);
1253 array_copy(files, return_value);
1254 }
1255 /* }}} */
1256
1257 /* {{{ proto void HttpRequest::unsetPostFiles()
1258 *
1259 * Unset the POST files list.
1260 * Affects only POST requests.
1261 */
1262 PHP_METHOD(HttpRequest, unsetPostFiles)
1263 {
1264 zval *files;
1265 getObject(http_request_object, obj);
1266
1267 NO_ARGS;
1268
1269 files = GET_PROP(obj, postFiles);
1270 zend_hash_clean(Z_ARRVAL_P(files));
1271 }
1272 /* }}} */
1273
1274 /* {{{ proto array HttpRequest::getResponseData()
1275 *
1276 * Get all response data after the request has been sent.
1277 */
1278 PHP_METHOD(HttpRequest, getResponseData)
1279 {
1280 zval *data;
1281 getObject(http_request_object, obj);
1282
1283 NO_ARGS;
1284
1285 data = GET_PROP(obj, responseData);
1286 array_init(return_value);
1287 array_copy(data, return_value);
1288 }
1289 /* }}} */
1290
1291 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1292 *
1293 * Get response header(s) after the request has been sent.
1294 */
1295 PHP_METHOD(HttpRequest, getResponseHeader)
1296 {
1297 zval *data, **headers, **header;
1298 char *header_name = NULL;
1299 int header_len = 0;
1300 getObject(http_response_object, obj);
1301
1302 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1303 RETURN_FALSE;
1304 }
1305
1306 data = GET_PROP(obj, responseData);
1307 if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1308 RETURN_FALSE;
1309 }
1310
1311 if (!header_len || !header_name) {
1312 array_init(return_value);
1313 array_copy(*headers, return_value);
1314 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1315 RETURN_STRINGL(Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
1316 } else {
1317 RETURN_FALSE;
1318 }
1319 }
1320 /* }}} */
1321
1322 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1323 *
1324 * Get response cookie(s) after the request has been sent.
1325 */
1326 PHP_METHOD(HttpRequest, getResponseCookie)
1327 {
1328 zval *data, **headers;
1329 char *cookie_name = NULL;
1330 int cookie_len = 0;
1331 getObject(http_request_object, obj);
1332
1333 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
1334 RETURN_FALSE;
1335 }
1336
1337 array_init(return_value);
1338
1339 data = GET_PROP(obj, responseData);
1340 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1341 ulong idx = 0;
1342 char *key = NULL;
1343 zval **header = NULL;
1344
1345 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers), key, idx, header) {
1346 if (key && !strcasecmp(key, "Set-Cookie")) {
1347 /* several cookies? */
1348 if (Z_TYPE_PP(header) == IS_ARRAY) {
1349 zval **cookie;
1350
1351 FOREACH_HASH_VAL(Z_ARRVAL_PP(header), cookie) {
1352 zval *cookie_hash;
1353 MAKE_STD_ZVAL(cookie_hash);
1354 array_init(cookie_hash);
1355
1356 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
1357 if (!cookie_len) {
1358 add_next_index_zval(return_value, cookie_hash);
1359 } else {
1360 zval **name;
1361
1362 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1363 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1364 add_next_index_zval(return_value, cookie_hash);
1365 return; /* <<< FOUND >>> */
1366 } else {
1367 zval_dtor(cookie_hash);
1368 efree(cookie_hash);
1369 }
1370 }
1371 } else {
1372 zval_dtor(cookie_hash);
1373 efree(cookie_hash);
1374 }
1375 }
1376 } else {
1377 zval *cookie_hash;
1378 MAKE_STD_ZVAL(cookie_hash);
1379 array_init(cookie_hash);
1380
1381 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
1382 if (!cookie_len) {
1383 add_next_index_zval(return_value, cookie_hash);
1384 } else {
1385 zval **name;
1386
1387 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1388 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1389 add_next_index_zval(return_value, cookie_hash);
1390 } else {
1391 zval_dtor(cookie_hash);
1392 efree(cookie_hash);
1393 }
1394 }
1395 } else {
1396 zval_dtor(cookie_hash);
1397 efree(cookie_hash);
1398 }
1399 }
1400 break;
1401 }
1402 /* reset key */
1403 key = NULL;
1404 }
1405 }
1406 }
1407 /* }}} */
1408
1409 /* {{{ proto string HttpRequest::getResponseBody()
1410 *
1411 * Get the response body after the request has been sent.
1412 */
1413 PHP_METHOD(HttpRequest, getResponseBody)
1414 {
1415 zval *data, **body;
1416 getObject(http_request_object, obj);
1417
1418 NO_ARGS;
1419
1420 data = GET_PROP(obj, responseData);
1421 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1422 RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
1423 } else {
1424 RETURN_FALSE;
1425 }
1426 }
1427 /* }}} */
1428
1429 /* {{{ proto int HttpRequest::getResponseCode()
1430 *
1431 * Get the response code after the request has been sent.
1432 */
1433 PHP_METHOD(HttpRequest, getResponseCode)
1434 {
1435 zval **code, **hdrs, *data;
1436 getObject(http_request_object, obj);
1437
1438 NO_ARGS;
1439
1440 data = GET_PROP(obj, responseData);
1441 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &hdrs)) &&
1442 (SUCCESS == zend_hash_find(Z_ARRVAL_PP(hdrs), "Response Status", sizeof("Response Status"), (void **) &code))) {
1443 RETVAL_STRINGL(Z_STRVAL_PP(code), Z_STRLEN_PP(code), 1);
1444 convert_to_long(return_value);
1445 } else {
1446 RETURN_FALSE;
1447 }
1448 }
1449 /* }}} */
1450
1451 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1452 *
1453 * Get response info after the request has been sent.
1454 * See http_get() for a full list of returned info.
1455 */
1456 PHP_METHOD(HttpRequest, getResponseInfo)
1457 {
1458 zval *info, **infop;
1459 char *info_name = NULL;
1460 int info_len = 0;
1461 getObject(http_request_object, obj);
1462
1463 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1464 RETURN_FALSE;
1465 }
1466
1467 info = GET_PROP(obj, responseInfo);
1468
1469 if (info_len && info_name) {
1470 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1471 RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
1472 } else {
1473 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not find response info named %s", info_name);
1474 RETURN_FALSE;
1475 }
1476 } else {
1477 array_init(return_value);
1478 array_copy(info, return_value);
1479 }
1480 }
1481 /* }}}*/
1482
1483 /* {{{ proto bool HttpRequest::send()
1484 *
1485 * Send the HTTP request.
1486 *
1487 * GET example:
1488 * <pre>
1489 * <?php
1490 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1491 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1492 * $r->addQueryData(array('category' => 3));
1493 * if ($r->send() && $r->getResponseCode() == 200) {
1494 * file_put_contents('local.rss', $r->getResponseBody());
1495 * }
1496 * ?>
1497 * </pre>
1498 *
1499 * POST example:
1500 * <pre>
1501 * <?php
1502 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1503 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1504 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1505 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1506 * if ($r->send()) {
1507 * echo $r->getResponseBody();
1508 * }
1509 * ?>
1510 * </pre>
1511 */
1512 PHP_METHOD(HttpRequest, send)
1513 {
1514 STATUS status = FAILURE;
1515 zval *meth, *URL, *qdata, *opts, *info, *resp;
1516 char *response_data, *request_uri;
1517 size_t response_len;
1518 getObject(http_request_object, obj);
1519
1520 NO_ARGS;
1521
1522 SET_EH_THROW_HTTP();
1523
1524 if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
1525 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initilaize cURL");
1526 RETURN_FALSE;
1527 }
1528
1529 meth = GET_PROP(obj, method);
1530 URL = GET_PROP(obj, url);
1531 qdata = GET_PROP(obj, queryData);
1532 opts = GET_PROP(obj, options);
1533 info = GET_PROP(obj, responseInfo);
1534 resp = GET_PROP(obj, responseData);
1535
1536 // HTTP_URI_MAXLEN+1 long char *
1537 request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0);
1538
1539 if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
1540 if (!strchr(request_uri, '?')) {
1541 strcat(request_uri, "?");
1542 } else {
1543 strcat(request_uri, "&");
1544 }
1545 strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
1546 }
1547
1548 switch (Z_LVAL_P(meth))
1549 {
1550 case HTTP_GET:
1551 status = http_get_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1552 break;
1553
1554 case HTTP_HEAD:
1555 status = http_head_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1556 break;
1557
1558 case HTTP_POST:
1559 {
1560 zval *post_files, *post_data;
1561
1562 post_files = GET_PROP(obj, postFiles);
1563 post_data = GET_PROP(obj, postData);
1564
1565 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files))) {
1566
1567 /* urlencoded post */
1568 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);
1569
1570 } else {
1571
1572 /*
1573 * multipart post
1574 */
1575 char *key = NULL;
1576 long idx;
1577 zval **data;
1578 struct curl_httppost *http_post_data[2] = {NULL, NULL};
1579
1580 /* normal data */
1581 FOREACH_KEYVAL(post_data, key, idx, data) {
1582 if (key) {
1583 convert_to_string_ex(data);
1584 curl_formadd(&http_post_data[0], &http_post_data[1],
1585 CURLFORM_COPYNAME, key,
1586 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
1587 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
1588 CURLFORM_END
1589 );
1590
1591 /* reset */
1592 key = NULL;
1593 }
1594 }
1595
1596 /* file data */
1597 FOREACH_VAL(post_files, data) {
1598 zval **file, **type, **name;
1599
1600 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
1601 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
1602 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
1603
1604 curl_formadd(&http_post_data[0], &http_post_data[1],
1605 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
1606 CURLFORM_FILE, Z_STRVAL_PP(file),
1607 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
1608 CURLFORM_END
1609 );
1610 }
1611 }
1612
1613 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);
1614 curl_formfree(http_post_data[0]);
1615 }
1616 }
1617 break;
1618
1619 default:
1620 break;
1621 }
1622
1623 efree(request_uri);
1624
1625 /* final data handling */
1626 if (status == SUCCESS) {
1627 char *body = NULL;
1628 size_t body_len = 0;
1629 zval *zheaders;
1630
1631 MAKE_STD_ZVAL(zheaders)
1632 array_init(zheaders);
1633
1634 if (SUCCESS != http_split_response_ex(response_data, response_len, Z_ARRVAL_P(zheaders), &body, &body_len)) {
1635 zval_dtor(zheaders);
1636 efree(zheaders),
1637 efree(response_data);
1638 RETURN_FALSE;
1639 }
1640
1641 add_assoc_zval(resp, "headers", zheaders);
1642 add_assoc_stringl(resp, "body", body, body_len, 0);
1643
1644 efree(response_data);
1645
1646 RETURN_TRUE;
1647 }
1648
1649 SET_EH_NORMAL();
1650 RETURN_SUCCESS(status);
1651 }
1652 /* }}} */
1653 /* }}} */
1654 #endif /* HTTP_HAVE_CURL */
1655
1656 #endif /* ZEND_ENGINE_2 */
1657
1658 /*
1659 * Local variables:
1660 * tab-width: 4
1661 * c-basic-offset: 4
1662 * End:
1663 * vim600: noet sw=4 ts=4 fdm=marker
1664 * vim<600: noet sw=4 ts=4
1665 */
1666