2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
23 #include "php_streams.h"
26 #include "php_http_std_defs.h"
27 #include "php_http_api.h"
28 #include "php_http_cache_api.h"
29 #include "php_http_request_api.h"
30 #include "php_http_date_api.h"
31 #include "php_http_headers_api.h"
32 #include "php_http_message_api.h"
33 #include "php_http_send_api.h"
34 #include "php_http_url_api.h"
36 #include "php_http_message_object.h"
37 #include "php_http_response_object.h"
38 #include "php_http_request_object.h"
39 #include "php_http_exception_object.h"
43 ZEND_EXTERN_MODULE_GLOBALS(http
)
45 /* {{{ HttpResponse */
47 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
49 * Instantiates a new HttpResponse object, which can be used to send
50 * any data/resource/file to an HTTP client with caching and multiple
51 * ranges/resuming support.
53 * NOTE: GZIPping is not implemented yet.
55 PHP_METHOD(HttpResponse
, __construct
)
57 zend_bool do_cache
= 0, do_gzip
= 0;
58 getObject(http_response_object
, obj
);
61 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
62 UPD_PROP(obj
, long, cache
, do_cache
);
63 UPD_PROP(obj
, long, gzip
, do_gzip
);
69 /* {{{ proto bool HttpResponse::setCache(bool cache)
71 * Whether it sould be attempted to cache the entitity.
72 * This will result in necessary caching headers and checks of clients
73 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
74 * matches a "304 Not Modified" status code will be issued.
76 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
77 * to something more appropriate than "no-cache"!
79 PHP_METHOD(HttpResponse
, setCache
)
81 zend_bool do_cache
= 0;
82 getObject(http_response_object
, obj
);
84 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
88 UPD_PROP(obj
, long, cache
, do_cache
);
93 /* {{{ proto bool HttpResponse::getCache()
95 * Get current caching setting.
97 PHP_METHOD(HttpResponse
, getCache
)
102 zval
*do_cache
= NULL
;
103 getObject(http_response_object
, obj
);
105 do_cache
= GET_PROP(obj
, cache
);
106 RETURN_BOOL(Z_LVAL_P(do_cache
));
111 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
113 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
115 PHP_METHOD(HttpResponse
, setGzip
)
117 zend_bool do_gzip
= 0;
118 getObject(http_response_object
, obj
);
120 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
124 UPD_PROP(obj
, long, gzip
, do_gzip
);
129 /* {{{ proto bool HttpResponse::getGzip()
131 * Get current gzipping setting.
133 PHP_METHOD(HttpResponse
, getGzip
)
139 getObject(http_response_object
, obj
);
141 do_gzip
= GET_PROP(obj
, gzip
);
142 RETURN_BOOL(Z_LVAL_P(do_gzip
));
147 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
149 * Set a custom cache-control header, usually being "private" or "public"; if
150 * $raw is set to true the header will be sent as-is.
152 PHP_METHOD(HttpResponse
, setCacheControl
)
157 getObject(http_response_object
, obj
);
159 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
163 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
164 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
168 UPD_PROP(obj
, long, raw_cache_header
, raw
);
169 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
174 /* {{{ proto string HttpResponse::getCacheControl()
176 * Get current Cache-Control header setting.
178 PHP_METHOD(HttpResponse
, getCacheControl
)
184 getObject(http_response_object
, obj
);
186 ccontrol
= GET_PROP(obj
, cacheControl
);
187 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
192 /* {{{ proto bool HttpResponse::setContentType(string content_type)
194 * Set the content-type of the sent entity.
196 PHP_METHOD(HttpResponse
, setContentType
)
200 getObject(http_response_object
, obj
);
202 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
206 if (!strchr(ctype
, '/')) {
207 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
211 UPD_PROP(obj
, string
, contentType
, ctype
);
217 /* {{{ proto string HttpResponse::getContentType()
219 * Get current Content-Type header setting.
221 PHP_METHOD(HttpResponse
, getContentType
)
227 getObject(http_response_object
, obj
);
229 ctype
= GET_PROP(obj
, contentType
);
230 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
235 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
237 * Set the Content-Disposition of the sent entity. This setting aims to suggest
238 * the receiveing user agent how to handle the sent entity; usually the client
239 * will show the user a "Save As..." popup.
241 PHP_METHOD(HttpResponse
, setContentDisposition
)
245 zend_bool is_inline
= 0;
246 getObject(http_response_object
, obj
);
248 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
252 UPD_PROP(obj
, string
, dispoFile
, file
);
253 UPD_PROP(obj
, long, dispoInline
, is_inline
);
258 /* {{{ proto array HttpResponse::getContentDisposition()
260 * Get current Content-Disposition setting.
261 * Will return an associative array like:
264 * 'filename' => 'foo.bar',
269 PHP_METHOD(HttpResponse
, getContentDisposition
)
274 zval
*file
, *is_inline
;
275 getObject(http_response_object
, obj
);
277 file
= GET_PROP(obj
, dispoFile
);
278 is_inline
= GET_PROP(obj
, dispoInline
);
280 array_init(return_value
);
281 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
282 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
287 /* {{{ proto bool HttpResponse::setETag(string etag)
289 * Set a custom ETag. Use this only if you know what you're doing.
291 PHP_METHOD(HttpResponse
, setETag
)
295 getObject(http_response_object
, obj
);
297 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
301 UPD_PROP(obj
, string
, eTag
, etag
);
306 /* {{{ proto string HttpResponse::getETag()
308 * Get the previously set custom ETag.
310 PHP_METHOD(HttpResponse
, getETag
)
316 getObject(http_response_object
, obj
);
318 etag
= GET_PROP(obj
, eTag
);
319 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
324 /* {{{ proto void HttpResponse::setThrottleDelay(double seconds)
327 PHP_METHOD(HttpResponse
, setThrottleDelay
)
331 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d", &seconds
)) {
332 getObject(http_response_object
, obj
);
334 UPD_PROP(obj
, double, throttleDelay
, seconds
);
339 /* {{{ proto double HttpResponse::getThrottleDelay()
342 PHP_METHOD(HttpResponse
, getThrottleDelay
)
348 getObject(http_response_object
, obj
);
350 seconds
= GET_PROP(obj
, throttleDelay
);
351 RETURN_DOUBLE(Z_DVAL_P(seconds
));
356 /* {{{ proto void HttpResponse::setSendBuffersize(long bytes)
359 PHP_METHOD(HttpResponse
, setSendBuffersize
)
363 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &bytes
)) {
364 getObject(http_response_object
, obj
);
366 UPD_PROP(obj
, long, sendBuffersize
, bytes
);
371 /* {{{ proto long HttpResponse::getSendBuffersize()
374 PHP_METHOD(HttpResponse
, getSendBuffersize
)
380 getObject(http_response_object
, obj
);
382 bytes
= GET_PROP(obj
, sendBuffersize
);
383 RETURN_LONG(Z_LVAL_P(bytes
));
388 /* {{{ proto bool HttpResponse::setData(string data)
390 * Set the data to be sent.
392 PHP_METHOD(HttpResponse
, setData
)
395 getObject(http_response_object
, obj
);
397 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &the_data
)) {
401 convert_to_string_ex(&the_data
);
402 SET_PROP(obj
, data
, the_data
);
403 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_data
, SEND_DATA
));
404 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
409 /* {{{ proto string HttpResponse::getData()
411 * Get the previously set data to be sent.
413 PHP_METHOD(HttpResponse
, getData
)
419 getObject(http_response_object
, obj
);
421 the_data
= GET_PROP(obj
, data
);
422 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
427 /* {{{ proto bool HttpResponse::setStream(resource stream)
429 * Set the resource to be sent.
431 PHP_METHOD(HttpResponse
, setStream
)
434 php_stream
*the_real_stream
;
435 getObject(http_response_object
, obj
);
437 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
441 php_stream_from_zval(the_real_stream
, &the_stream
);
443 SET_PROP(obj
, stream
, the_stream
);
444 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_real_stream
, SEND_RSRC
));
445 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
450 /* {{{ proto resource HttpResponse::getStream()
452 * Get the previously set resource to be sent.
454 PHP_METHOD(HttpResponse
, getStream
)
460 getObject(http_response_object
, obj
);
462 the_stream
= GET_PROP(obj
, stream
);
463 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
468 /* {{{ proto bool HttpResponse::setFile(string file)
470 * Set the file to be sent.
472 PHP_METHOD(HttpResponse
, setFile
)
475 getObject(http_response_object
, obj
);
477 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
481 convert_to_string_ex(&the_file
);
483 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
484 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_file
, -1));
485 UPD_PROP(obj
, long, send_mode
, -1);
490 /* {{{ proto string HttpResponse::getFile()
492 * Get the previously set file to be sent.
494 PHP_METHOD(HttpResponse
, getFile
)
500 getObject(http_response_object
, obj
);
502 the_file
= GET_PROP(obj
, file
);
503 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
508 /* {{{ proto bool HttpResponse::send()
510 * Finally send the entity.
515 * $r = new HttpResponse(true);
516 * $r->setFile('../hidden/contract.pdf');
517 * $r->setContentType('application/pdf');
523 PHP_METHOD(HttpResponse
, send
)
525 zval
*do_cache
, *do_gzip
;
526 getObject(http_response_object
, obj
);
530 do_cache
= GET_PROP(obj
, cache
);
531 do_gzip
= GET_PROP(obj
, gzip
);
534 if (Z_LVAL_P(do_gzip
)) {
535 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC
);
539 if (Z_LVAL_P(do_cache
)) {
542 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
544 etag
= GET_PROP(obj
, eTag
);
545 lmod
= GET_PROP(obj
, lastModified
);
546 cctrl
= GET_PROP(obj
, cacheControl
);
547 ccraw
= GET_PROP(obj
, raw_cache_header
);
549 if (Z_LVAL_P(ccraw
)) {
550 cc_hdr
= Z_STRVAL_P(cctrl
);
551 cc_len
= Z_STRLEN_P(cctrl
);
553 char cc_header
[42] = {0};
554 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
556 cc_len
= Z_STRLEN_P(cctrl
) + lenof(", must-revalidate, max-age=0");
559 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_hdr
, cc_len
);
560 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_hdr
, cc_len
);
565 zval
*ctype
= GET_PROP(obj
, contentType
);
566 if (Z_STRLEN_P(ctype
)) {
567 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
569 http_send_content_type("application/x-octetstream", lenof("application/x-octetstream"));
573 /* content disposition */
575 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
576 if (Z_STRLEN_P(dispo_file
)) {
577 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
578 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
584 zval
*send_buffersize
, *throttle_delay
;
585 send_buffersize
= GET_PROP(obj
, sendBuffersize
);
586 throttle_delay
= GET_PROP(obj
, throttleDelay
);
587 HTTP_G(send
).buffer_size
= Z_LVAL_P(send_buffersize
);
588 HTTP_G(send
).throttle_delay
= Z_DVAL_P(throttle_delay
);
593 zval
*send_mode
= GET_PROP(obj
, send_mode
);
594 switch (Z_LVAL_P(send_mode
))
598 zval
*zdata
= GET_PROP(obj
, data
);
599 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
604 php_stream
*the_real_stream
;
605 zval
*the_stream
= GET_PROP(obj
, stream
);
606 php_stream_from_zval(the_real_stream
, &the_stream
);
607 RETURN_SUCCESS(http_send_stream(the_real_stream
));
612 zval
*zfile
= GET_PROP(obj
, file
);
613 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
621 /* {{{ HttpMessage */
623 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
625 * Create an HttpMessage object from a string.
627 PHP_METHOD(HttpMessage
, fromString
)
631 http_message
*msg
= NULL
;
632 http_message_object obj
;
634 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
638 if (!(msg
= http_message_parse(string
, length
))) {
642 Z_TYPE_P(return_value
) = IS_OBJECT
;
643 return_value
->value
.obj
= http_message_object_from_msg(msg
);
647 /* {{{ proto void HttpMessage::__construct([string message])
649 * Instantiate a new HttpMessage object.
651 PHP_METHOD(HttpMessage
, __construct
)
653 char *message
= NULL
;
655 getObject(http_message_object
, obj
);
658 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
659 if (obj
->message
= http_message_parse(message
, length
)) {
660 if (obj
->message
->parent
) {
661 obj
->parent
= http_message_object_from_msg(obj
->message
->parent
);
664 } else if (!obj
->message
) {
665 obj
->message
= http_message_new();
671 /* {{{ proto string HttpMessage::getBody()
673 * Get the body of the parsed Message.
675 PHP_METHOD(HttpMessage
, getBody
)
680 getObject(http_message_object
, obj
);
681 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
686 /* {{{ proto array HttpMessage::getHeaders()
688 * Get Message Headers.
690 PHP_METHOD(HttpMessage
, getHeaders
)
696 getObject(http_message_object
, obj
);
698 Z_ARRVAL(headers
) = &obj
->message
->hdrs
;
699 array_init(return_value
);
700 array_copy(&headers
, return_value
);
705 /* {{{ proto void HttpMessage::setHeaders(array headers)
709 PHP_METHOD(HttpMessage
, setHeaders
)
711 zval
*new_headers
, old_headers
;
712 getObject(http_message_object
, obj
);
714 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
718 zend_hash_clean(&obj
->message
->hdrs
);
719 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
720 array_copy(new_headers
, &old_headers
);
724 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
726 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
728 PHP_METHOD(HttpMessage
, addHeaders
)
730 zval old_headers
, *new_headers
;
731 zend_bool append
= 0;
732 getObject(http_message_object
, obj
);
734 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
738 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
740 array_append(new_headers
, &old_headers
);
742 array_merge(new_headers
, &old_headers
);
747 /* {{{ proto long HttpMessage::getType()
749 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
751 PHP_METHOD(HttpMessage
, getType
)
756 getObject(http_message_object
, obj
);
757 RETURN_LONG(obj
->message
->type
);
762 /* {{{ proto void HttpMessage::setType(long type)
764 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
766 PHP_METHOD(HttpMessage
, setType
)
769 getObject(http_message_object
, obj
);
771 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
774 http_message_set_type(obj
->message
, type
);
778 /* {{{ proto long HttpMessage::getResponseCode()
780 * Get the Response Code of the Message.
782 PHP_METHOD(HttpMessage
, getResponseCode
)
787 getObject(http_message_object
, obj
);
789 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
790 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
794 RETURN_LONG(obj
->message
->info
.response
.code
);
799 /* {{{ proto bool HttpMessage::setResponseCode(long code)
801 * Set the response code of an HTTP Response Message.
802 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
803 * or if the response code is out of range (100-510).
805 PHP_METHOD(HttpMessage
, setResponseCode
)
808 getObject(http_message_object
, obj
);
810 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
811 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
815 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
818 if (code
< 100 || code
> 510) {
819 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid response code (100-510): %ld", code
);
823 obj
->message
->info
.response
.code
= code
;
828 /* {{{ proto string HttpMessage::getRequestMethod()
830 * Get the Request Method of the Message.
831 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
833 PHP_METHOD(HttpMessage
, getRequestMethod
)
838 getObject(http_message_object
, obj
);
840 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
841 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
845 RETURN_STRING(obj
->message
->info
.request
.method
, 1);
850 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
852 * Set the Request Method of the HTTP Message.
853 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
855 PHP_METHOD(HttpMessage
, setRequestMethod
)
859 getObject(http_message_object
, obj
);
861 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
862 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
866 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
869 if (method_len
< 1) {
870 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
873 if (SUCCESS
!= http_check_method(method
)) {
874 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Unkown request method: %s", method
);
878 STR_SET(obj
->message
->info
.request
.method
, estrndup(method
, method_len
));
883 /* {{{ proto string HttpMessage::getRequestUri()
885 * Get the Request URI of the Message.
887 PHP_METHOD(HttpMessage
, getRequestUri
)
893 getObject(http_message_object
, obj
);
895 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
896 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
900 RETURN_STRING(obj
->message
->info
.request
.URI
, 1);
905 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
907 * Set the Request URI of the HTTP Message.
908 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
909 * or if paramtere URI was empty.
911 PHP_METHOD(HttpMessage
, setRequestUri
)
915 getObject(http_message_object
, obj
);
917 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
918 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
921 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
925 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
929 STR_SET(obj
->message
->info
.request
.URI
, estrndup(URI
, URIlen
));
934 /* {{{ proto string HttpMessage::getHttpVersion()
936 * Get the HTTP Protocol Version of the Message.
938 PHP_METHOD(HttpMessage
, getHttpVersion
)
945 getObject(http_message_object
, obj
);
947 switch (obj
->message
->type
)
949 case HTTP_MSG_RESPONSE
:
950 version
= obj
->message
->info
.response
.http_version
;
953 case HTTP_MSG_REQUEST
:
954 version
= obj
->message
->info
.request
.http_version
;
961 sprintf(ver
, "%1.1f", version
);
962 RETURN_STRINGL(ver
, 3, 1);
967 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
969 * Set the HTTP Protocol version of the Message.
970 * Returns false if version is invalid (1.0 and 1.1).
972 PHP_METHOD(HttpMessage
, setHttpVersion
)
976 getObject(http_message_object
, obj
);
978 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
982 if (HTTP_MSG_TYPE(NONE
, obj
->message
)) {
983 http_error(E_WARNING
, HTTP_E_MSG
, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
987 convert_to_double(zv
);
988 sprintf(v
, "%1.1f", Z_DVAL_P(zv
));
989 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
990 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
994 if (HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
995 obj
->message
->info
.response
.http_version
= (float) Z_DVAL_P(zv
);
997 obj
->message
->info
.request
.http_version
= (float) Z_DVAL_P(zv
);
1003 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1005 * Get parent Message.
1007 PHP_METHOD(HttpMessage
, getParentMessage
)
1012 getObject(http_message_object
, obj
);
1014 if (obj
->message
->parent
) {
1015 RETVAL_OBJVAL(obj
->parent
);
1023 /* {{{ proto bool HttpMessage::send()
1025 * Send the Message according to its type as Response or Request.
1027 PHP_METHOD(HttpMessage
, send
)
1029 getObject(http_message_object
, obj
);
1033 RETURN_SUCCESS(http_message_send(obj
->message
));
1037 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
1039 * Get the string representation of the Message.
1041 PHP_METHOD(HttpMessage
, toString
)
1046 zend_bool include_parent
= 1;
1047 getObject(http_message_object
, obj
);
1049 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1053 if (include_parent
) {
1054 http_message_serialize(obj
->message
, &string
, &length
);
1056 http_message_tostring(obj
->message
, &string
, &length
);
1058 RETURN_STRINGL(string
, length
, 0);
1065 #ifdef HTTP_HAVE_CURL
1066 /* {{{ HttpRequest */
1068 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
1070 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
1071 * and POST (including posting files) HTTP requests.
1073 PHP_METHOD(HttpRequest
, __construct
)
1078 getObject(http_request_object
, obj
);
1080 SET_EH_THROW_HTTP();
1081 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
1082 INIT_PARR(obj
, options
);
1083 INIT_PARR(obj
, responseInfo
);
1084 INIT_PARR(obj
, responseData
);
1085 INIT_PARR(obj
, postFields
);
1086 INIT_PARR(obj
, postFiles
);
1089 UPD_PROP(obj
, string
, url
, URL
);
1092 UPD_PROP(obj
, long, method
, meth
);
1099 /* {{{ proto void HttpRequest::__destruct()
1101 * Destroys the HttpRequest object.
1103 PHP_METHOD(HttpRequest
, __destruct
)
1105 getObject(http_request_object
, obj
);
1109 FREE_PARR(obj
, options
);
1110 FREE_PARR(obj
, responseInfo
);
1111 FREE_PARR(obj
, responseData
);
1112 FREE_PARR(obj
, postFields
);
1113 FREE_PARR(obj
, postFiles
);
1117 /* {{{ proto bool HttpRequest::setOptions(array options)
1119 * Set the request options to use. See http_get() for a full list of available options.
1121 PHP_METHOD(HttpRequest
, setOptions
)
1125 zval
*opts
, *old_opts
, **opt
;
1126 getObject(http_request_object
, obj
);
1128 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1132 old_opts
= GET_PROP(obj
, options
);
1134 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
1135 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
1137 if (!strcmp(key
, "headers")) {
1139 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1140 array_merge(*opt
, *headers
);
1143 } else if (!strcmp(key
, "cookies")) {
1145 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1146 array_merge(*opt
, *cookies
);
1151 add_assoc_zval(old_opts
, key
, *opt
);
1162 /* {{{ proto array HttpRequest::getOptions()
1164 * Get current set options.
1166 PHP_METHOD(HttpRequest
, getOptions
)
1172 getObject(http_request_object
, obj
);
1174 opts
= GET_PROP(obj
, options
);
1175 array_init(return_value
);
1176 array_copy(opts
, return_value
);
1181 /* {{{ proto void HttpRequest::unsetOptions()
1183 * Unset all options/headers/cookies.
1185 PHP_METHOD(HttpRequest
, unsetOptions
)
1187 getObject(http_request_object
, obj
);
1191 FREE_PARR(obj
, options
);
1192 INIT_PARR(obj
, options
);
1196 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1198 * Set additional SSL options.
1200 PHP_METHOD(HttpRequest
, setSslOptions
)
1202 zval
*opts
, *old_opts
, **ssl_options
;
1203 getObject(http_request_object
, obj
);
1205 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1209 old_opts
= GET_PROP(obj
, options
);
1211 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1212 array_merge(opts
, *ssl_options
);
1214 zval_add_ref(&opts
);
1215 add_assoc_zval(old_opts
, "ssl", opts
);
1222 /* {{{ proto array HttpRequest::getSslOtpions()
1224 * Get previously set SSL options.
1226 PHP_METHOD(HttpRequest
, getSslOptions
)
1231 zval
*opts
, **ssl_options
;
1232 getObject(http_request_object
, obj
);
1234 opts
= GET_PROP(obj
, options
);
1236 array_init(return_value
);
1238 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1239 array_copy(*ssl_options
, return_value
);
1245 /* {{{ proto void HttpRequest::unsetSslOptions()
1247 * Unset previously set SSL options.
1249 PHP_METHOD(HttpRequest
, unsetSslOptions
)
1252 getObject(http_request_object
, obj
);
1256 opts
= GET_PROP(obj
, options
);
1257 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
1261 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1263 * Add request header name/value pairs.
1265 PHP_METHOD(HttpRequest
, addHeaders
)
1267 zval
*opts
, **headers
, *new_headers
;
1268 getObject(http_request_object
, obj
);
1270 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
1274 opts
= GET_PROP(obj
, options
);
1276 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1277 array_merge(new_headers
, *headers
);
1279 zval_add_ref(&new_headers
);
1280 add_assoc_zval(opts
, "headers", new_headers
);
1287 /* {{{ proto array HttpRequest::getHeaders()
1289 * Get previously set request headers.
1291 PHP_METHOD(HttpRequest
, getHeaders
)
1296 zval
*opts
, **headers
;
1297 getObject(http_request_object
, obj
);
1299 opts
= GET_PROP(obj
, options
);
1301 array_init(return_value
);
1303 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1304 array_copy(*headers
, return_value
);
1310 /* {{{ proto void HttpRequest::unsetHeaders()
1312 * Unset previously set request headers.
1314 PHP_METHOD(HttpRequest
, unsetHeaders
)
1317 getObject(http_request_object
, obj
);
1321 opts
= GET_PROP(obj
, options
);
1322 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
1326 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1330 PHP_METHOD(HttpRequest
, addCookies
)
1332 zval
*opts
, **cookies
, *new_cookies
;
1333 getObject(http_request_object
, obj
);
1335 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
1339 opts
= GET_PROP(obj
, options
);
1341 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1342 array_merge(new_cookies
, *cookies
);
1344 zval_add_ref(&new_cookies
);
1345 add_assoc_zval(opts
, "cookies", new_cookies
);
1352 /* {{{ proto array HttpRequest::getCookies()
1354 * Get previously set cookies.
1356 PHP_METHOD(HttpRequest
, getCookies
)
1361 zval
*opts
, **cookies
;
1362 getObject(http_request_object
, obj
);
1364 opts
= GET_PROP(obj
, options
);
1366 array_init(return_value
);
1368 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1369 array_copy(*cookies
, return_value
);
1375 /* {{{ proto void HttpRequest::unsetCookies()
1378 PHP_METHOD(HttpRequest
, unsetCookies
)
1381 getObject(http_request_object
, obj
);
1385 opts
= GET_PROP(obj
, options
);
1386 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
1390 /* {{{ proto bool HttpRequest::setURL(string url)
1392 * Set the request URL.
1394 PHP_METHOD(HttpRequest
, setURL
)
1398 getObject(http_request_object
, obj
);
1400 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1404 UPD_PROP(obj
, string
, url
, URL
);
1409 /* {{{ proto string HttpRequest::getUrl()
1411 * Get the previously set request URL.
1413 PHP_METHOD(HttpRequest
, getURL
)
1419 getObject(http_request_object
, obj
);
1421 URL
= GET_PROP(obj
, url
);
1422 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
1427 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1429 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1430 * <tt>HTTP_POST</tt> constants.
1432 PHP_METHOD(HttpRequest
, setMethod
)
1435 getObject(http_request_object
, obj
);
1437 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1441 UPD_PROP(obj
, long, method
, meth
);
1446 /* {{{ proto long HttpRequest::getMethod()
1448 * Get the previously set request method.
1450 PHP_METHOD(HttpRequest
, getMethod
)
1456 getObject(http_request_object
, obj
);
1458 meth
= GET_PROP(obj
, method
);
1459 RETURN_LONG(Z_LVAL_P(meth
));
1464 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1466 * Set the content type the post request should have.
1467 * Use this only if you know what you're doing.
1469 PHP_METHOD(HttpRequest
, setContentType
)
1473 getObject(http_request_object
, obj
);
1475 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1479 if (!strchr(ctype
, '/')) {
1480 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
1484 UPD_PROP(obj
, string
, contentType
, ctype
);
1489 /* {{{ proto string HttpRequest::getContentType()
1491 * Get the previously content type.
1493 PHP_METHOD(HttpRequest
, getContentType
)
1499 getObject(http_request_object
, obj
);
1501 ctype
= GET_PROP(obj
, contentType
);
1502 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
1507 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1509 * Set the URL query parameters to use.
1510 * Overwrites previously set query parameters.
1511 * Affects any request types.
1513 PHP_METHOD(HttpRequest
, setQueryData
)
1516 char *query_data
= NULL
;
1517 getObject(http_request_object
, obj
);
1519 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
1523 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1524 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1527 UPD_PROP(obj
, string
, queryData
, query_data
);
1532 convert_to_string(qdata
);
1533 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
1538 /* {{{ proto string HttpRequest::getQueryData()
1540 * Get the current query data in form of an urlencoded query string.
1542 PHP_METHOD(HttpRequest
, getQueryData
)
1548 getObject(http_request_object
, obj
);
1550 qdata
= GET_PROP(obj
, queryData
);
1551 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
1556 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1558 * Add parameters to the query parameter list.
1559 * Affects any request type.
1561 PHP_METHOD(HttpRequest
, addQueryData
)
1563 zval
*qdata
, *old_qdata
;
1564 char *query_data
= NULL
;
1565 getObject(http_request_object
, obj
);
1567 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1571 old_qdata
= GET_PROP(obj
, queryData
);
1573 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1577 UPD_PROP(obj
, string
, queryData
, query_data
);
1584 /* {{{ proto void HttpRequest::unsetQueryData()
1586 * Clean the query parameters.
1587 * Affects any request type.
1589 PHP_METHOD(HttpRequest
, unsetQueryData
)
1591 getObject(http_request_object
, obj
);
1595 UPD_PROP(obj
, string
, queryData
, "");
1599 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1601 * Adds POST data entries.
1602 * Affects only POST requests.
1604 PHP_METHOD(HttpRequest
, addPostFields
)
1606 zval
*post
, *post_data
;
1607 getObject(http_request_object
, obj
);
1609 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1613 post
= GET_PROP(obj
, postFields
);
1614 array_merge(post_data
, post
);
1620 /* {{{ proto bool HttpRequest::setPostFields(array post_data)
1622 * Set the POST data entries.
1623 * Overwrites previously set POST data.
1624 * Affects only POST requests.
1626 PHP_METHOD(HttpRequest
, setPostFields
)
1628 zval
*post
, *post_data
;
1629 getObject(http_request_object
, obj
);
1631 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1635 post
= GET_PROP(obj
, postFields
);
1636 zend_hash_clean(Z_ARRVAL_P(post
));
1637 array_copy(post_data
, post
);
1643 /* {{{ proto array HttpRequest::getPostFields()
1645 * Get previously set POST data.
1647 PHP_METHOD(HttpRequest
, getPostFields
)
1653 getObject(http_request_object
, obj
);
1655 post_data
= GET_PROP(obj
, postFields
);
1656 array_init(return_value
);
1657 array_copy(post_data
, return_value
);
1662 /* {{{ proto void HttpRequest::unsetPostFields()
1664 * Clean POST data entires.
1665 * Affects only POST requests.
1667 PHP_METHOD(HttpRequest
, unsetPostFields
)
1670 getObject(http_request_object
, obj
);
1674 post_data
= GET_PROP(obj
, postFields
);
1675 zend_hash_clean(Z_ARRVAL_P(post_data
));
1679 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1681 * Add a file to the POST request.
1682 * Affects only POST requests.
1684 PHP_METHOD(HttpRequest
, addPostFile
)
1686 zval
*files
, *entry
;
1687 char *name
, *file
, *type
= NULL
;
1688 int name_len
, file_len
, type_len
= 0;
1689 getObject(http_request_object
, obj
);
1691 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1696 if (!strchr(type
, '/')) {
1697 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1701 type
= "application/x-octetstream";
1702 type_len
= sizeof("application/x-octetstream") - 1;
1705 MAKE_STD_ZVAL(entry
);
1708 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1709 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1710 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1712 files
= GET_PROP(obj
, postFiles
);
1713 add_next_index_zval(files
, entry
);
1719 /* {{{ proto array HttpRequest::getPostFiles()
1721 * Get all previously added POST files.
1723 PHP_METHOD(HttpRequest
, getPostFiles
)
1729 getObject(http_request_object
, obj
);
1731 files
= GET_PROP(obj
, postFiles
);
1733 array_init(return_value
);
1734 array_copy(files
, return_value
);
1739 /* {{{ proto void HttpRequest::unsetPostFiles()
1741 * Unset the POST files list.
1742 * Affects only POST requests.
1744 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1747 getObject(http_request_object
, obj
);
1751 files
= GET_PROP(obj
, postFiles
);
1752 zend_hash_clean(Z_ARRVAL_P(files
));
1756 /* {{{ proto array HttpRequest::getResponseData()
1758 * Get all response data after the request has been sent.
1760 PHP_METHOD(HttpRequest
, getResponseData
)
1766 getObject(http_request_object
, obj
);
1768 data
= GET_PROP(obj
, responseData
);
1769 array_init(return_value
);
1770 array_copy(data
, return_value
);
1775 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1777 * Get response header(s) after the request has been sent.
1779 PHP_METHOD(HttpRequest
, getResponseHeader
)
1782 zval
*data
, **headers
, **header
;
1783 char *header_name
= NULL
;
1785 getObject(http_response_object
, obj
);
1787 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1791 data
= GET_PROP(obj
, responseData
);
1792 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1796 if (!header_len
|| !header_name
) {
1797 array_init(return_value
);
1798 array_copy(*headers
, return_value
);
1799 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1800 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1808 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1810 * Get response cookie(s) after the request has been sent.
1812 PHP_METHOD(HttpRequest
, getResponseCookie
)
1815 zval
*data
, **headers
;
1816 char *cookie_name
= NULL
;
1818 getObject(http_request_object
, obj
);
1820 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1824 array_init(return_value
);
1826 data
= GET_PROP(obj
, responseData
);
1827 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1830 zval
**header
= NULL
;
1832 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1833 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1834 /* several cookies? */
1835 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1838 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1840 MAKE_STD_ZVAL(cookie_hash
);
1841 array_init(cookie_hash
);
1843 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1845 add_next_index_zval(return_value
, cookie_hash
);
1849 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1850 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1851 add_next_index_zval(return_value
, cookie_hash
);
1852 return; /* <<< FOUND >>> */
1854 zval_dtor(cookie_hash
);
1859 zval_dtor(cookie_hash
);
1865 MAKE_STD_ZVAL(cookie_hash
);
1866 array_init(cookie_hash
);
1868 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1870 add_next_index_zval(return_value
, cookie_hash
);
1874 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1875 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1876 add_next_index_zval(return_value
, cookie_hash
);
1878 zval_dtor(cookie_hash
);
1883 zval_dtor(cookie_hash
);
1897 /* {{{ proto string HttpRequest::getResponseBody()
1899 * Get the response body after the request has been sent.
1901 PHP_METHOD(HttpRequest
, getResponseBody
)
1907 getObject(http_request_object
, obj
);
1909 data
= GET_PROP(obj
, responseData
);
1910 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1911 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
1919 /* {{{ proto int HttpRequest::getResponseCode()
1921 * Get the response code after the request has been sent.
1923 PHP_METHOD(HttpRequest
, getResponseCode
)
1929 getObject(http_request_object
, obj
);
1931 code
= GET_PROP(obj
, responseCode
);
1932 RETURN_LONG(Z_LVAL_P(code
));
1937 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1939 * Get response info after the request has been sent.
1940 * See http_get() for a full list of returned info.
1942 PHP_METHOD(HttpRequest
, getResponseInfo
)
1945 zval
*info
, **infop
;
1946 char *info_name
= NULL
;
1948 getObject(http_request_object
, obj
);
1950 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1954 info
= GET_PROP(obj
, responseInfo
);
1956 if (info_len
&& info_name
) {
1957 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1958 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
1960 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Could not find response info named %s", info_name
);
1964 array_init(return_value
);
1965 array_copy(info
, return_value
);
1971 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1973 * Get the full response as HttpMessage object.
1975 PHP_METHOD(HttpRequest
, getResponseMessage
)
1981 getObject(http_request_object
, obj
);
1983 message
= GET_PROP(obj
, responseMessage
);
1984 Z_TYPE_P(return_value
) = IS_OBJECT
;
1985 return_value
->is_ref
= 1;
1986 return_value
->value
.obj
= message
->value
.obj
;
1987 zval_add_ref(&return_value
);
1991 /* {{{ proto bool HttpRequest::send()
1993 * Send the HTTP request.
1998 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1999 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
2000 * $r->addQueryData(array('category' => 3));
2003 * if ($r->getResponseCode() == 200) {
2004 * file_put_contents('local.rss', $r->getResponseBody());
2006 * } catch (HttpException $ex) {
2015 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
2016 * $r->setOptions(array('cookies' => array('lang' => 'de')));
2017 * $r->addpostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
2018 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
2020 * echo $r->getResponseBody();
2025 PHP_METHOD(HttpRequest
, send
)
2027 STATUS status
= FAILURE
;
2028 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
2030 getObject(http_request_object
, obj
);
2034 SET_EH_THROW_HTTP();
2036 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
2037 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initilaize curl");
2041 meth
= GET_PROP(obj
, method
);
2042 URL
= GET_PROP(obj
, url
);
2043 qdata
= GET_PROP(obj
, queryData
);
2044 opts
= GET_PROP(obj
, options
);
2045 info
= GET_PROP(obj
, responseInfo
);
2046 resp
= GET_PROP(obj
, responseData
);
2048 // HTTP_URI_MAXLEN+1 long char *
2049 if (!(request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0))) {
2053 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
2054 if (!strchr(request_uri
, '?')) {
2055 strcat(request_uri
, "?");
2057 strcat(request_uri
, "&");
2059 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
2062 switch (Z_LVAL_P(meth
))
2066 status
= http_request_ex(obj
->ch
, Z_LVAL_P(meth
), request_uri
, NULL
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2071 http_request_body body
;
2073 php_stream_statbuf ssb
;
2074 zval
*file
= GET_PROP(obj
, putFile
);
2076 if ( (stream
= php_stream_open_wrapper(Z_STRVAL_P(file
), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
)) &&
2077 !php_stream_stat(stream
, &ssb
)) {
2078 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
2080 body
.size
= ssb
.sb
.st_size
;
2082 status
= http_put_ex(obj
->ch
, request_uri
, &body
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2083 http_request_body_dtor(&body
);
2092 http_request_body body
;
2093 zval
*fields
= GET_PROP(obj
, postFields
), *files
= GET_PROP(obj
, postFiles
);
2095 if (SUCCESS
== (status
= http_request_body_fill(&body
, Z_ARRVAL_P(fields
), Z_ARRVAL_P(files
)))) {
2096 status
= http_post_ex(obj
->ch
, request_uri
, &body
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2097 http_request_body_dtor(&body
);
2104 http_request_body body
;
2105 zval
*post
= GET_PROP(obj
, postData
);
2107 body
.type
= HTTP_REQUEST_BODY_CSTRING
;
2108 body
.data
= Z_STRVAL_P(post
);
2109 body
.size
= Z_STRLEN_P(post
);
2111 status
= http_request_ex(obj
->ch
, Z_LVAL_P(meth
), request_uri
, &body
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2118 /* final data handling */
2119 if (status
== SUCCESS
) {
2122 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->response
), PHPSTR_LEN(&obj
->response
))) {
2123 zval
*headers
, *message
;
2127 UPD_PROP(obj
, long, responseCode
, msg
->info
.response
.code
);
2129 MAKE_STD_ZVAL(headers
)
2130 array_init(headers
);
2132 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
2133 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
2135 add_assoc_zval(resp
, "headers", headers
);
2136 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
2138 message
= GET_PROP(obj
, responseMessage
);
2140 Z_TYPE_P(message
) = IS_OBJECT
;
2141 message
->value
.obj
= http_message_object_from_msg(msg
);
2142 SET_PROP(obj
, responseMessage
, message
);
2149 RETURN_SUCCESS(status
);
2153 #endif /* HTTP_HAVE_CURL */
2155 #endif /* ZEND_ENGINE_2 */
2162 * vim600: noet sw=4 ts=4 fdm=marker
2163 * vim<600: noet sw=4 ts=4