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