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_requestpool_object.h"
40 #include "php_http_exception_object.h"
46 ZEND_EXTERN_MODULE_GLOBALS(http
)
48 /* {{{ HttpResponse */
50 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
52 * Instantiates a new HttpResponse object, which can be used to send
53 * any data/resource/file to an HTTP client with caching and multiple
54 * ranges/resuming support.
56 * NOTE: GZIPping is not implemented yet.
58 PHP_METHOD(HttpResponse
, __construct
)
60 zend_bool do_cache
= 0, do_gzip
= 0;
61 getObject(http_response_object
, obj
);
64 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
65 UPD_PROP(obj
, long, cache
, do_cache
);
66 UPD_PROP(obj
, long, gzip
, do_gzip
);
72 /* {{{ proto bool HttpResponse::setCache(bool cache)
74 * Whether it sould be attempted to cache the entitity.
75 * This will result in necessary caching headers and checks of clients
76 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
77 * matches a "304 Not Modified" status code will be issued.
79 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
80 * to something more appropriate than "no-cache"!
82 PHP_METHOD(HttpResponse
, setCache
)
84 zend_bool do_cache
= 0;
85 getObject(http_response_object
, obj
);
87 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
91 UPD_PROP(obj
, long, cache
, do_cache
);
96 /* {{{ proto bool HttpResponse::getCache()
98 * Get current caching setting.
100 PHP_METHOD(HttpResponse
, getCache
)
105 zval
*do_cache
= NULL
;
106 getObject(http_response_object
, obj
);
108 do_cache
= GET_PROP(obj
, cache
);
109 RETURN_BOOL(Z_LVAL_P(do_cache
));
114 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
116 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
118 PHP_METHOD(HttpResponse
, setGzip
)
120 zend_bool do_gzip
= 0;
121 getObject(http_response_object
, obj
);
123 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
127 UPD_PROP(obj
, long, gzip
, do_gzip
);
132 /* {{{ proto bool HttpResponse::getGzip()
134 * Get current gzipping setting.
136 PHP_METHOD(HttpResponse
, getGzip
)
142 getObject(http_response_object
, obj
);
144 do_gzip
= GET_PROP(obj
, gzip
);
145 RETURN_BOOL(Z_LVAL_P(do_gzip
));
150 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
152 * Set a custom cache-control header, usually being "private" or "public"; if
153 * $raw is set to true the header will be sent as-is.
155 PHP_METHOD(HttpResponse
, setCacheControl
)
160 getObject(http_response_object
, obj
);
162 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
166 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
167 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
171 UPD_PROP(obj
, long, raw_cache_header
, raw
);
172 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
177 /* {{{ proto string HttpResponse::getCacheControl()
179 * Get current Cache-Control header setting.
181 PHP_METHOD(HttpResponse
, getCacheControl
)
187 getObject(http_response_object
, obj
);
189 ccontrol
= GET_PROP(obj
, cacheControl
);
190 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
195 /* {{{ proto bool HttpResponse::setContentType(string content_type)
197 * Set the content-type of the sent entity.
199 PHP_METHOD(HttpResponse
, setContentType
)
203 getObject(http_response_object
, obj
);
205 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
209 if (!strchr(ctype
, '/')) {
210 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
214 UPD_PROP(obj
, string
, contentType
, ctype
);
220 /* {{{ proto string HttpResponse::getContentType()
222 * Get current Content-Type header setting.
224 PHP_METHOD(HttpResponse
, getContentType
)
230 getObject(http_response_object
, obj
);
232 ctype
= GET_PROP(obj
, contentType
);
233 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
238 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
240 * Set the Content-Disposition of the sent entity. This setting aims to suggest
241 * the receiveing user agent how to handle the sent entity; usually the client
242 * will show the user a "Save As..." popup.
244 PHP_METHOD(HttpResponse
, setContentDisposition
)
248 zend_bool is_inline
= 0;
249 getObject(http_response_object
, obj
);
251 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
255 UPD_PROP(obj
, string
, dispoFile
, file
);
256 UPD_PROP(obj
, long, dispoInline
, is_inline
);
261 /* {{{ proto array HttpResponse::getContentDisposition()
263 * Get current Content-Disposition setting.
264 * Will return an associative array like:
267 * 'filename' => 'foo.bar',
272 PHP_METHOD(HttpResponse
, getContentDisposition
)
277 zval
*file
, *is_inline
;
278 getObject(http_response_object
, obj
);
280 file
= GET_PROP(obj
, dispoFile
);
281 is_inline
= GET_PROP(obj
, dispoInline
);
283 array_init(return_value
);
284 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
285 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
290 /* {{{ proto bool HttpResponse::setETag(string etag)
292 * Set a custom ETag. Use this only if you know what you're doing.
294 PHP_METHOD(HttpResponse
, setETag
)
298 getObject(http_response_object
, obj
);
300 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
304 UPD_PROP(obj
, string
, eTag
, etag
);
309 /* {{{ proto string HttpResponse::getETag()
311 * Get the previously set custom ETag.
313 PHP_METHOD(HttpResponse
, getETag
)
319 getObject(http_response_object
, obj
);
321 etag
= GET_PROP(obj
, eTag
);
322 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
327 /* {{{ proto void HttpResponse::setThrottleDelay(double seconds)
330 PHP_METHOD(HttpResponse
, setThrottleDelay
)
334 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d", &seconds
)) {
335 getObject(http_response_object
, obj
);
337 UPD_PROP(obj
, double, throttleDelay
, seconds
);
342 /* {{{ proto double HttpResponse::getThrottleDelay()
345 PHP_METHOD(HttpResponse
, getThrottleDelay
)
351 getObject(http_response_object
, obj
);
353 seconds
= GET_PROP(obj
, throttleDelay
);
354 RETURN_DOUBLE(Z_DVAL_P(seconds
));
359 /* {{{ proto void HttpResponse::setSendBuffersize(long bytes)
362 PHP_METHOD(HttpResponse
, setSendBuffersize
)
366 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &bytes
)) {
367 getObject(http_response_object
, obj
);
369 UPD_PROP(obj
, long, sendBuffersize
, bytes
);
374 /* {{{ proto long HttpResponse::getSendBuffersize()
377 PHP_METHOD(HttpResponse
, getSendBuffersize
)
383 getObject(http_response_object
, obj
);
385 bytes
= GET_PROP(obj
, sendBuffersize
);
386 RETURN_LONG(Z_LVAL_P(bytes
));
391 /* {{{ proto bool HttpResponse::setData(string data)
393 * Set the data to be sent.
395 PHP_METHOD(HttpResponse
, setData
)
398 getObject(http_response_object
, obj
);
400 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &the_data
)) {
404 convert_to_string_ex(&the_data
);
405 SET_PROP(obj
, data
, the_data
);
406 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_data
, SEND_DATA
));
407 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
412 /* {{{ proto string HttpResponse::getData()
414 * Get the previously set data to be sent.
416 PHP_METHOD(HttpResponse
, getData
)
422 getObject(http_response_object
, obj
);
424 the_data
= GET_PROP(obj
, data
);
425 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
430 /* {{{ proto bool HttpResponse::setStream(resource stream)
432 * Set the resource to be sent.
434 PHP_METHOD(HttpResponse
, setStream
)
437 php_stream
*the_real_stream
;
438 getObject(http_response_object
, obj
);
440 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
444 php_stream_from_zval(the_real_stream
, &the_stream
);
446 SET_PROP(obj
, stream
, the_stream
);
447 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_real_stream
, SEND_RSRC
));
448 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
453 /* {{{ proto resource HttpResponse::getStream()
455 * Get the previously set resource to be sent.
457 PHP_METHOD(HttpResponse
, getStream
)
463 getObject(http_response_object
, obj
);
465 the_stream
= GET_PROP(obj
, stream
);
466 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
471 /* {{{ proto bool HttpResponse::setFile(string file)
473 * Set the file to be sent.
475 PHP_METHOD(HttpResponse
, setFile
)
478 getObject(http_response_object
, obj
);
480 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
484 convert_to_string_ex(&the_file
);
486 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
487 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_file
, -1));
488 UPD_PROP(obj
, long, send_mode
, -1);
493 /* {{{ proto string HttpResponse::getFile()
495 * Get the previously set file to be sent.
497 PHP_METHOD(HttpResponse
, getFile
)
503 getObject(http_response_object
, obj
);
505 the_file
= GET_PROP(obj
, file
);
506 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
511 /* {{{ proto bool HttpResponse::send([bool clean_ob = true])
513 * Finally send the entity.
518 * $r = new HttpResponse(true);
519 * $r->setFile('../hidden/contract.pdf');
520 * $r->setContentType('application/pdf');
526 PHP_METHOD(HttpResponse
, send
)
528 zend_bool clean_ob
= 1;
529 zval
*do_cache
, *do_gzip
;
530 getObject(http_response_object
, obj
);
532 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &clean_ob
)) {
536 do_cache
= GET_PROP(obj
, cache
);
537 do_gzip
= GET_PROP(obj
, gzip
);
540 /* interrupt on-the-fly etag generation */
541 HTTP_G(etag
).started
= 0;
542 /* discard previous output buffers */
543 php_end_ob_buffers(0 TSRMLS_CC
);
547 if (Z_LVAL_P(do_gzip
)) {
548 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC
);
552 if (Z_LVAL_P(do_cache
)) {
555 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
557 etag
= GET_PROP(obj
, eTag
);
558 lmod
= GET_PROP(obj
, lastModified
);
559 cctrl
= GET_PROP(obj
, cacheControl
);
560 ccraw
= GET_PROP(obj
, raw_cache_header
);
562 if (Z_LVAL_P(ccraw
)) {
563 cc_hdr
= Z_STRVAL_P(cctrl
);
564 cc_len
= Z_STRLEN_P(cctrl
);
566 char cc_header
[42] = {0};
567 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
569 cc_len
= Z_STRLEN_P(cctrl
) + lenof(", must-revalidate, max-age=0");
572 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_hdr
, cc_len
);
573 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_hdr
, cc_len
);
578 zval
*ctype
= GET_PROP(obj
, contentType
);
579 if (Z_STRLEN_P(ctype
)) {
580 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
582 http_send_content_type("application/x-octetstream", lenof("application/x-octetstream"));
586 /* content disposition */
588 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
589 if (Z_STRLEN_P(dispo_file
)) {
590 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
591 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
597 zval
*send_buffersize
, *throttle_delay
;
598 send_buffersize
= GET_PROP(obj
, sendBuffersize
);
599 throttle_delay
= GET_PROP(obj
, throttleDelay
);
600 HTTP_G(send
).buffer_size
= Z_LVAL_P(send_buffersize
);
601 HTTP_G(send
).throttle_delay
= Z_DVAL_P(throttle_delay
);
606 zval
*send_mode
= GET_PROP(obj
, send_mode
);
607 switch (Z_LVAL_P(send_mode
))
611 zval
*zdata
= GET_PROP(obj
, data
);
612 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
617 php_stream
*the_real_stream
;
618 zval
*the_stream
= GET_PROP(obj
, stream
);
619 php_stream_from_zval(the_real_stream
, &the_stream
);
620 RETURN_SUCCESS(http_send_stream(the_real_stream
));
625 zval
*zfile
= GET_PROP(obj
, file
);
626 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
634 /* {{{ HttpMessage */
636 /* {{{ proto void HttpMessage::__construct([string message])
638 * Instantiate a new HttpMessage object.
640 PHP_METHOD(HttpMessage
, __construct
)
642 char *message
= NULL
;
644 getObject(http_message_object
, obj
);
647 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
648 if (obj
->message
= http_message_parse(message
, length
)) {
649 if (obj
->message
->parent
) {
650 obj
->parent
= http_message_object_from_msg(obj
->message
->parent
);
653 } else if (!obj
->message
) {
654 obj
->message
= http_message_new();
660 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
662 * Create an HttpMessage object from a string.
664 PHP_METHOD(HttpMessage
, fromString
)
668 http_message
*msg
= NULL
;
670 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
674 if (!(msg
= http_message_parse(string
, length
))) {
678 Z_TYPE_P(return_value
) = IS_OBJECT
;
679 return_value
->value
.obj
= http_message_object_from_msg(msg
);
683 /* {{{ proto string HttpMessage::getBody()
685 * Get the body of the parsed Message.
687 PHP_METHOD(HttpMessage
, getBody
)
692 getObject(http_message_object
, obj
);
693 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
698 /* {{{ proto array HttpMessage::getHeaders()
700 * Get Message Headers.
702 PHP_METHOD(HttpMessage
, getHeaders
)
708 getObject(http_message_object
, obj
);
710 Z_ARRVAL(headers
) = &obj
->message
->hdrs
;
711 array_init(return_value
);
712 array_copy(&headers
, return_value
);
717 /* {{{ proto void HttpMessage::setHeaders(array headers)
721 PHP_METHOD(HttpMessage
, setHeaders
)
723 zval
*new_headers
, old_headers
;
724 getObject(http_message_object
, obj
);
726 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
730 zend_hash_clean(&obj
->message
->hdrs
);
731 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
732 array_copy(new_headers
, &old_headers
);
736 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
738 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
740 PHP_METHOD(HttpMessage
, addHeaders
)
742 zval old_headers
, *new_headers
;
743 zend_bool append
= 0;
744 getObject(http_message_object
, obj
);
746 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
750 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
752 array_append(new_headers
, &old_headers
);
754 array_merge(new_headers
, &old_headers
);
759 /* {{{ proto long HttpMessage::getType()
761 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
763 PHP_METHOD(HttpMessage
, getType
)
768 getObject(http_message_object
, obj
);
769 RETURN_LONG(obj
->message
->type
);
774 /* {{{ proto void HttpMessage::setType(long type)
776 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
778 PHP_METHOD(HttpMessage
, setType
)
781 getObject(http_message_object
, obj
);
783 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
786 http_message_set_type(obj
->message
, type
);
790 /* {{{ proto long HttpMessage::getResponseCode()
792 * Get the Response Code of the Message.
794 PHP_METHOD(HttpMessage
, getResponseCode
)
799 getObject(http_message_object
, obj
);
801 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
802 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
806 RETURN_LONG(obj
->message
->info
.response
.code
);
811 /* {{{ proto bool HttpMessage::setResponseCode(long code)
813 * Set the response code of an HTTP Response Message.
814 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
815 * or if the response code is out of range (100-510).
817 PHP_METHOD(HttpMessage
, setResponseCode
)
820 getObject(http_message_object
, obj
);
822 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
823 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
827 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
830 if (code
< 100 || code
> 510) {
831 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid response code (100-510): %ld", code
);
835 obj
->message
->info
.response
.code
= code
;
840 /* {{{ proto string HttpMessage::getRequestMethod()
842 * Get the Request Method of the Message.
843 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
845 PHP_METHOD(HttpMessage
, getRequestMethod
)
850 getObject(http_message_object
, obj
);
852 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
853 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
857 RETURN_STRING(obj
->message
->info
.request
.method
, 1);
862 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
864 * Set the Request Method of the HTTP Message.
865 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
867 PHP_METHOD(HttpMessage
, setRequestMethod
)
871 getObject(http_message_object
, obj
);
873 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
874 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
878 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
881 if (method_len
< 1) {
882 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
885 if (SUCCESS
!= http_check_method(method
)) {
886 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Unkown request method: %s", method
);
890 STR_SET(obj
->message
->info
.request
.method
, estrndup(method
, method_len
));
895 /* {{{ proto string HttpMessage::getRequestUri()
897 * Get the Request URI of the Message.
899 PHP_METHOD(HttpMessage
, getRequestUri
)
904 getObject(http_message_object
, obj
);
906 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
907 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
911 RETURN_STRING(obj
->message
->info
.request
.URI
, 1);
916 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
918 * Set the Request URI of the HTTP Message.
919 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
920 * or if paramtere URI was empty.
922 PHP_METHOD(HttpMessage
, setRequestUri
)
926 getObject(http_message_object
, obj
);
928 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
929 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
932 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
936 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
940 STR_SET(obj
->message
->info
.request
.URI
, estrndup(URI
, URIlen
));
945 /* {{{ proto string HttpMessage::getHttpVersion()
947 * Get the HTTP Protocol Version of the Message.
949 PHP_METHOD(HttpMessage
, getHttpVersion
)
956 getObject(http_message_object
, obj
);
958 switch (obj
->message
->type
)
960 case HTTP_MSG_RESPONSE
:
961 version
= obj
->message
->info
.response
.http_version
;
964 case HTTP_MSG_REQUEST
:
965 version
= obj
->message
->info
.request
.http_version
;
972 sprintf(ver
, "%1.1f", version
);
973 RETURN_STRINGL(ver
, 3, 1);
978 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
980 * Set the HTTP Protocol version of the Message.
981 * Returns false if version is invalid (1.0 and 1.1).
983 PHP_METHOD(HttpMessage
, setHttpVersion
)
987 getObject(http_message_object
, obj
);
989 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
993 if (HTTP_MSG_TYPE(NONE
, obj
->message
)) {
994 http_error(E_WARNING
, HTTP_E_MSG
, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
998 convert_to_double(zv
);
999 sprintf(v
, "%1.1f", Z_DVAL_P(zv
));
1000 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
1001 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
1005 if (HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
1006 obj
->message
->info
.response
.http_version
= (float) Z_DVAL_P(zv
);
1008 obj
->message
->info
.request
.http_version
= (float) Z_DVAL_P(zv
);
1014 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1016 * Get parent Message.
1018 PHP_METHOD(HttpMessage
, getParentMessage
)
1023 getObject(http_message_object
, obj
);
1025 if (obj
->message
->parent
) {
1026 RETVAL_OBJVAL(obj
->parent
);
1034 /* {{{ proto bool HttpMessage::send()
1036 * Send the Message according to its type as Response or Request.
1038 PHP_METHOD(HttpMessage
, send
)
1040 getObject(http_message_object
, obj
);
1044 RETURN_SUCCESS(http_message_send(obj
->message
));
1048 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
1050 * Get the string representation of the Message.
1052 PHP_METHOD(HttpMessage
, toString
)
1057 zend_bool include_parent
= 1;
1058 getObject(http_message_object
, obj
);
1060 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1064 if (include_parent
) {
1065 http_message_serialize(obj
->message
, &string
, &length
);
1067 http_message_tostring(obj
->message
, &string
, &length
);
1069 RETURN_STRINGL(string
, length
, 0);
1076 #ifdef HTTP_HAVE_CURL
1077 /* {{{ HttpRequest */
1079 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
1081 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
1082 * and POST (including posting files) HTTP requests.
1084 PHP_METHOD(HttpRequest
, __construct
)
1089 getObject(http_request_object
, obj
);
1091 SET_EH_THROW_HTTP();
1092 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
1093 INIT_PARR(obj
, options
);
1094 INIT_PARR(obj
, responseInfo
);
1095 INIT_PARR(obj
, responseData
);
1096 INIT_PARR(obj
, postFields
);
1097 INIT_PARR(obj
, postFiles
);
1100 UPD_PROP(obj
, string
, url
, URL
);
1103 UPD_PROP(obj
, long, method
, meth
);
1110 /* {{{ proto void HttpRequest::__destruct()
1112 * Destroys the HttpRequest object.
1114 PHP_METHOD(HttpRequest
, __destruct
)
1116 getObject(http_request_object
, obj
);
1120 FREE_PARR(obj
, options
);
1121 FREE_PARR(obj
, responseInfo
);
1122 FREE_PARR(obj
, responseData
);
1123 FREE_PARR(obj
, postFields
);
1124 FREE_PARR(obj
, postFiles
);
1128 /* {{{ proto bool HttpRequest::setOptions(array options)
1130 * Set the request options to use. See http_get() for a full list of available options.
1132 PHP_METHOD(HttpRequest
, setOptions
)
1136 zval
*opts
, *old_opts
, **opt
;
1137 getObject(http_request_object
, obj
);
1139 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1143 old_opts
= GET_PROP(obj
, options
);
1145 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
1146 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
1148 if (!strcmp(key
, "headers")) {
1150 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1151 array_merge(*opt
, *headers
);
1154 } else if (!strcmp(key
, "cookies")) {
1156 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1157 array_merge(*opt
, *cookies
);
1162 add_assoc_zval(old_opts
, key
, *opt
);
1173 /* {{{ proto array HttpRequest::getOptions()
1175 * Get current set options.
1177 PHP_METHOD(HttpRequest
, getOptions
)
1183 getObject(http_request_object
, obj
);
1185 opts
= GET_PROP(obj
, options
);
1186 array_init(return_value
);
1187 array_copy(opts
, return_value
);
1192 /* {{{ proto void HttpRequest::unsetOptions()
1194 * Unset all options/headers/cookies.
1196 PHP_METHOD(HttpRequest
, unsetOptions
)
1198 getObject(http_request_object
, obj
);
1202 FREE_PARR(obj
, options
);
1203 INIT_PARR(obj
, options
);
1207 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1209 * Set additional SSL options.
1211 PHP_METHOD(HttpRequest
, setSslOptions
)
1213 zval
*opts
, *old_opts
, **ssl_options
;
1214 getObject(http_request_object
, obj
);
1216 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1220 old_opts
= GET_PROP(obj
, options
);
1222 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1223 array_merge(opts
, *ssl_options
);
1225 zval_add_ref(&opts
);
1226 add_assoc_zval(old_opts
, "ssl", opts
);
1233 /* {{{ proto array HttpRequest::getSslOtpions()
1235 * Get previously set SSL options.
1237 PHP_METHOD(HttpRequest
, getSslOptions
)
1242 zval
*opts
, **ssl_options
;
1243 getObject(http_request_object
, obj
);
1245 opts
= GET_PROP(obj
, options
);
1247 array_init(return_value
);
1249 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1250 array_copy(*ssl_options
, return_value
);
1256 /* {{{ proto void HttpRequest::unsetSslOptions()
1258 * Unset previously set SSL options.
1260 PHP_METHOD(HttpRequest
, unsetSslOptions
)
1263 getObject(http_request_object
, obj
);
1267 opts
= GET_PROP(obj
, options
);
1268 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
1272 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1274 * Add request header name/value pairs.
1276 PHP_METHOD(HttpRequest
, addHeaders
)
1278 zval
*opts
, **headers
, *new_headers
;
1279 getObject(http_request_object
, obj
);
1281 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
1285 opts
= GET_PROP(obj
, options
);
1287 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1288 array_merge(new_headers
, *headers
);
1290 zval_add_ref(&new_headers
);
1291 add_assoc_zval(opts
, "headers", new_headers
);
1298 /* {{{ proto array HttpRequest::getHeaders()
1300 * Get previously set request headers.
1302 PHP_METHOD(HttpRequest
, getHeaders
)
1307 zval
*opts
, **headers
;
1308 getObject(http_request_object
, obj
);
1310 opts
= GET_PROP(obj
, options
);
1312 array_init(return_value
);
1314 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1315 array_copy(*headers
, return_value
);
1321 /* {{{ proto void HttpRequest::unsetHeaders()
1323 * Unset previously set request headers.
1325 PHP_METHOD(HttpRequest
, unsetHeaders
)
1328 getObject(http_request_object
, obj
);
1332 opts
= GET_PROP(obj
, options
);
1333 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
1337 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1341 PHP_METHOD(HttpRequest
, addCookies
)
1343 zval
*opts
, **cookies
, *new_cookies
;
1344 getObject(http_request_object
, obj
);
1346 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
1350 opts
= GET_PROP(obj
, options
);
1352 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1353 array_merge(new_cookies
, *cookies
);
1355 zval_add_ref(&new_cookies
);
1356 add_assoc_zval(opts
, "cookies", new_cookies
);
1363 /* {{{ proto array HttpRequest::getCookies()
1365 * Get previously set cookies.
1367 PHP_METHOD(HttpRequest
, getCookies
)
1372 zval
*opts
, **cookies
;
1373 getObject(http_request_object
, obj
);
1375 opts
= GET_PROP(obj
, options
);
1377 array_init(return_value
);
1379 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1380 array_copy(*cookies
, return_value
);
1386 /* {{{ proto void HttpRequest::unsetCookies()
1389 PHP_METHOD(HttpRequest
, unsetCookies
)
1392 getObject(http_request_object
, obj
);
1396 opts
= GET_PROP(obj
, options
);
1397 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
1401 /* {{{ proto bool HttpRequest::setURL(string url)
1403 * Set the request URL.
1405 PHP_METHOD(HttpRequest
, setURL
)
1409 getObject(http_request_object
, obj
);
1411 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1415 UPD_PROP(obj
, string
, url
, URL
);
1420 /* {{{ proto string HttpRequest::getUrl()
1422 * Get the previously set request URL.
1424 PHP_METHOD(HttpRequest
, getURL
)
1430 getObject(http_request_object
, obj
);
1432 URL
= GET_PROP(obj
, url
);
1433 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
1438 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1440 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1441 * <tt>HTTP_POST</tt> constants.
1443 PHP_METHOD(HttpRequest
, setMethod
)
1446 getObject(http_request_object
, obj
);
1448 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1452 UPD_PROP(obj
, long, method
, meth
);
1457 /* {{{ proto long HttpRequest::getMethod()
1459 * Get the previously set request method.
1461 PHP_METHOD(HttpRequest
, getMethod
)
1467 getObject(http_request_object
, obj
);
1469 meth
= GET_PROP(obj
, method
);
1470 RETURN_LONG(Z_LVAL_P(meth
));
1475 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1477 * Set the content type the post request should have.
1478 * Use this only if you know what you're doing.
1480 PHP_METHOD(HttpRequest
, setContentType
)
1484 getObject(http_request_object
, obj
);
1486 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1490 if (!strchr(ctype
, '/')) {
1491 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
1495 UPD_PROP(obj
, string
, contentType
, ctype
);
1500 /* {{{ proto string HttpRequest::getContentType()
1502 * Get the previously content type.
1504 PHP_METHOD(HttpRequest
, getContentType
)
1510 getObject(http_request_object
, obj
);
1512 ctype
= GET_PROP(obj
, contentType
);
1513 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
1518 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1520 * Set the URL query parameters to use.
1521 * Overwrites previously set query parameters.
1522 * Affects any request types.
1524 PHP_METHOD(HttpRequest
, setQueryData
)
1527 char *query_data
= NULL
;
1528 getObject(http_request_object
, obj
);
1530 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
1534 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1535 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1538 UPD_PROP(obj
, string
, queryData
, query_data
);
1543 convert_to_string(qdata
);
1544 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
1549 /* {{{ proto string HttpRequest::getQueryData()
1551 * Get the current query data in form of an urlencoded query string.
1553 PHP_METHOD(HttpRequest
, getQueryData
)
1559 getObject(http_request_object
, obj
);
1561 qdata
= GET_PROP(obj
, queryData
);
1562 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
1567 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1569 * Add parameters to the query parameter list.
1570 * Affects any request type.
1572 PHP_METHOD(HttpRequest
, addQueryData
)
1574 zval
*qdata
, *old_qdata
;
1575 char *query_data
= NULL
;
1576 getObject(http_request_object
, obj
);
1578 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1582 old_qdata
= GET_PROP(obj
, queryData
);
1584 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1588 UPD_PROP(obj
, string
, queryData
, query_data
);
1595 /* {{{ proto void HttpRequest::unsetQueryData()
1597 * Clean the query parameters.
1598 * Affects any request type.
1600 PHP_METHOD(HttpRequest
, unsetQueryData
)
1602 getObject(http_request_object
, obj
);
1606 UPD_PROP(obj
, string
, queryData
, "");
1610 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1612 * Adds POST data entries.
1613 * Affects only POST requests.
1615 PHP_METHOD(HttpRequest
, addPostFields
)
1617 zval
*post
, *post_data
;
1618 getObject(http_request_object
, obj
);
1620 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1624 post
= GET_PROP(obj
, postFields
);
1625 array_merge(post_data
, post
);
1631 /* {{{ proto bool HttpRequest::setPostFields(array post_data)
1633 * Set the POST data entries.
1634 * Overwrites previously set POST data.
1635 * Affects only POST requests.
1637 PHP_METHOD(HttpRequest
, setPostFields
)
1639 zval
*post
, *post_data
;
1640 getObject(http_request_object
, obj
);
1642 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1646 post
= GET_PROP(obj
, postFields
);
1647 zend_hash_clean(Z_ARRVAL_P(post
));
1648 array_copy(post_data
, post
);
1654 /* {{{ proto array HttpRequest::getPostFields()
1656 * Get previously set POST data.
1658 PHP_METHOD(HttpRequest
, getPostFields
)
1664 getObject(http_request_object
, obj
);
1666 post_data
= GET_PROP(obj
, postFields
);
1667 array_init(return_value
);
1668 array_copy(post_data
, return_value
);
1673 /* {{{ proto void HttpRequest::unsetPostFields()
1675 * Clean POST data entires.
1676 * Affects only POST requests.
1678 PHP_METHOD(HttpRequest
, unsetPostFields
)
1681 getObject(http_request_object
, obj
);
1685 post_data
= GET_PROP(obj
, postFields
);
1686 zend_hash_clean(Z_ARRVAL_P(post_data
));
1690 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1692 * Add a file to the POST request.
1693 * Affects only POST requests.
1695 PHP_METHOD(HttpRequest
, addPostFile
)
1697 zval
*files
, *entry
;
1698 char *name
, *file
, *type
= NULL
;
1699 int name_len
, file_len
, type_len
= 0;
1700 getObject(http_request_object
, obj
);
1702 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1707 if (!strchr(type
, '/')) {
1708 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1712 type
= "application/x-octetstream";
1713 type_len
= sizeof("application/x-octetstream") - 1;
1716 MAKE_STD_ZVAL(entry
);
1719 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1720 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1721 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1723 files
= GET_PROP(obj
, postFiles
);
1724 add_next_index_zval(files
, entry
);
1730 /* {{{ proto bool HttpRequest::setPostFiles()
1732 * Set files to post.
1733 * Overwrites previously set post files.
1734 * Affects only POST requests.
1736 PHP_METHOD(HttpRequest
, setPostFiles
)
1738 zval
*files
, *pFiles
;
1739 getObject(http_request_object
, obj
);
1741 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &files
)) {
1745 pFiles
= GET_PROP(obj
, postFiles
);
1746 zend_hash_clean(Z_ARRVAL_P(pFiles
));
1747 array_copy(files
, pFiles
);
1753 /* {{{ proto array HttpRequest::getPostFiles()
1755 * Get all previously added POST files.
1757 PHP_METHOD(HttpRequest
, getPostFiles
)
1763 getObject(http_request_object
, obj
);
1765 files
= GET_PROP(obj
, postFiles
);
1767 array_init(return_value
);
1768 array_copy(files
, return_value
);
1773 /* {{{ proto void HttpRequest::unsetPostFiles()
1775 * Unset the POST files list.
1776 * Affects only POST requests.
1778 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1781 getObject(http_request_object
, obj
);
1785 files
= GET_PROP(obj
, postFiles
);
1786 zend_hash_clean(Z_ARRVAL_P(files
));
1790 /* {{{ proto bool HttpRequest::SetPutFile(string file)
1793 * Affects only PUT requests.
1795 PHP_METHOD(HttpRequest
, setPutFile
)
1799 getObject(http_request_object
, obj
);
1801 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &file_len
)) {
1805 UPD_PROP(obj
, string
, putFile
, file
);
1810 /* {{{ proto string HttpRequest::getPutFile()
1812 * Get previously set put file.
1814 PHP_METHOD(HttpRequest
, getPutFile
)
1820 getObject(http_request_object
, obj
);
1822 putfile
= GET_PROP(obj
, putFile
);
1823 RETVAL_STRINGL(Z_STRVAL_P(putfile
), Z_STRLEN_P(putfile
), 1);
1828 /* {{{ proto void HttpRequest::unsetPutFile()
1830 * Unset file to put.
1831 * Affects only PUT requests.
1833 PHP_METHOD(HttpRequest
, unsetPutFile
)
1835 getObject(http_request_object
, obj
);
1839 UPD_PROP(obj
, string
, putFile
, "");
1843 /* {{{ proto array HttpRequest::getResponseData()
1845 * Get all response data after the request has been sent.
1847 PHP_METHOD(HttpRequest
, getResponseData
)
1853 getObject(http_request_object
, obj
);
1855 data
= GET_PROP(obj
, responseData
);
1856 array_init(return_value
);
1857 array_copy(data
, return_value
);
1862 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1864 * Get response header(s) after the request has been sent.
1866 PHP_METHOD(HttpRequest
, getResponseHeader
)
1869 zval
*data
, **headers
, **header
;
1870 char *header_name
= NULL
;
1872 getObject(http_response_object
, obj
);
1874 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1878 data
= GET_PROP(obj
, responseData
);
1879 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1883 if (!header_len
|| !header_name
) {
1884 array_init(return_value
);
1885 array_copy(*headers
, return_value
);
1886 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1887 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1895 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1897 * Get response cookie(s) after the request has been sent.
1899 PHP_METHOD(HttpRequest
, getResponseCookie
)
1902 zval
*data
, **headers
;
1903 char *cookie_name
= NULL
;
1905 getObject(http_request_object
, obj
);
1907 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1911 array_init(return_value
);
1913 data
= GET_PROP(obj
, responseData
);
1914 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1917 zval
**header
= NULL
;
1919 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1920 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1921 /* several cookies? */
1922 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1925 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1927 MAKE_STD_ZVAL(cookie_hash
);
1928 array_init(cookie_hash
);
1930 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1932 add_next_index_zval(return_value
, cookie_hash
);
1936 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1937 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1938 add_next_index_zval(return_value
, cookie_hash
);
1939 return; /* <<< FOUND >>> */
1941 zval_dtor(cookie_hash
);
1946 zval_dtor(cookie_hash
);
1952 MAKE_STD_ZVAL(cookie_hash
);
1953 array_init(cookie_hash
);
1955 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1957 add_next_index_zval(return_value
, cookie_hash
);
1961 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1962 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1963 add_next_index_zval(return_value
, cookie_hash
);
1965 zval_dtor(cookie_hash
);
1970 zval_dtor(cookie_hash
);
1984 /* {{{ proto string HttpRequest::getResponseBody()
1986 * Get the response body after the request has been sent.
1988 PHP_METHOD(HttpRequest
, getResponseBody
)
1994 getObject(http_request_object
, obj
);
1996 data
= GET_PROP(obj
, responseData
);
1997 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1998 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
2006 /* {{{ proto int HttpRequest::getResponseCode()
2008 * Get the response code after the request has been sent.
2010 PHP_METHOD(HttpRequest
, getResponseCode
)
2016 getObject(http_request_object
, obj
);
2018 code
= GET_PROP(obj
, responseCode
);
2019 RETURN_LONG(Z_LVAL_P(code
));
2024 /* {{{ proto array HttpRequest::getResponseInfo([string name])
2026 * Get response info after the request has been sent.
2027 * See http_get() for a full list of returned info.
2029 PHP_METHOD(HttpRequest
, getResponseInfo
)
2032 zval
*info
, **infop
;
2033 char *info_name
= NULL
;
2035 getObject(http_request_object
, obj
);
2037 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
2041 info
= GET_PROP(obj
, responseInfo
);
2043 if (info_len
&& info_name
) {
2044 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
2045 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
2047 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Could not find response info named %s", info_name
);
2051 array_init(return_value
);
2052 array_copy(info
, return_value
);
2058 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
2060 * Get the full response as HttpMessage object.
2062 PHP_METHOD(HttpRequest
, getResponseMessage
)
2068 getObject(http_request_object
, obj
);
2070 message
= GET_PROP(obj
, responseMessage
);
2071 Z_TYPE_P(return_value
) = IS_OBJECT
;
2072 return_value
->is_ref
= 1;
2073 return_value
->value
.obj
= message
->value
.obj
;
2074 zval_add_ref(&return_value
);
2078 /* {{{ proto bool HttpRequest::send()
2080 * Send the HTTP request.
2085 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
2086 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
2087 * $r->addQueryData(array('category' => 3));
2090 * if ($r->getResponseCode() == 200) {
2091 * file_put_contents('local.rss', $r->getResponseBody());
2093 * } catch (HttpException $ex) {
2102 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
2103 * $r->setOptions(array('cookies' => array('lang' => 'de')));
2104 * $r->addpostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
2105 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
2107 * echo $r->getResponseBody();
2112 PHP_METHOD(HttpRequest
, send
)
2114 STATUS status
= FAILURE
;
2115 http_request_body body
= {0};
2116 getObject(http_request_object
, obj
);
2120 SET_EH_THROW_HTTP();
2123 http_error(E_WARNING
, HTTP_E_CURL
, "You cannot call HttpRequest::send() while attached to an HttpRequestPool");
2127 if (SUCCESS
== (status
= http_request_object_requesthandler(obj
, getThis(), &body
))) {
2128 zval
*info
= GET_PROP(obj
, responseInfo
);
2129 status
= http_request_exec(obj
->ch
, Z_ARRVAL_P(info
));
2130 SET_PROP(obj
, responseInfo
, info
);
2132 http_request_body_dtor(&body
);
2134 /* final data handling */
2135 if (SUCCESS
== status
) {
2136 status
= http_request_object_responsehandler(obj
, getThis(), NULL
);
2140 RETURN_SUCCESS(status
);
2144 /* {{{ HttpRequestPool */
2146 /* {{{ proto void HttpRequestPool::__construct()
2148 * Instantiate a new HttpRequestPool object.
2150 PHP_METHOD(HttpRequestPool
, __construct
)
2156 /* {{{ proto void HttpRequestPool::__destruct()
2158 * Clean up HttpRequestPool object.
2160 PHP_METHOD(HttpRequestPool
, __destruct
)
2162 getObject(http_requestpool_object
, obj
);
2166 http_request_pool_detach_all(&obj
->pool
);
2170 /* {{{ proto void HttpRequestPool::reset()
2172 * Detach all attached HttpRequest objects.
2174 PHP_METHOD(HttpRequestPool
, reset
)
2176 getObject(http_requestpool_object
, obj
);
2180 http_request_pool_detach_all(&obj
->pool
);
2183 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
2185 * Attach an HttpRequest object to this HttpRequestPool.
2186 * NOTE: set all options prior attaching!
2188 PHP_METHOD(HttpRequestPool
, attach
)
2191 STATUS status
= FAILURE
;
2192 getObject(http_requestpool_object
, obj
);
2194 SET_EH_THROW_HTTP();
2195 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
2196 status
= http_request_pool_attach(&obj
->pool
, request
);
2199 RETURN_SUCCESS(status
);
2203 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
2205 * Detach an HttpRequest object from this HttpRequestPool.
2207 PHP_METHOD(HttpRequestPool
, detach
)
2210 STATUS status
= FAILURE
;
2211 getObject(http_requestpool_object
, obj
);
2213 SET_EH_THROW_HTTP();
2214 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
2215 status
= http_request_pool_detach(&obj
->pool
, request
);
2218 RETURN_SUCCESS(status
);
2222 /* {{{ proto bool HttpRequestPool::send()
2224 * Send all attached HttpRequest objects in parallel.
2226 PHP_METHOD(HttpRequestPool
, send
)
2228 getObject(http_requestpool_object
, obj
);
2232 RETURN_SUCCESS(http_request_pool_send(&obj
->pool
));
2236 /* {{{ proto protected bool HttpRequestPool::socketSend()
2241 * while ($pool->socketSend()) {
2242 * do_something_else();
2243 * if (!$pool->socketSelect()) {
2244 * die('Socket error');
2247 * $pool->socketRead();
2251 PHP_METHOD(HttpRequestPool
, socketSend
)
2253 getObject(http_requestpool_object
, obj
);
2257 RETURN_BOOL(0 < http_request_pool_perform(&obj
->pool
));
2261 /* {{{ proto protected bool HttpRequestPool::socketSelect()
2263 * See HttpRequestPool::socketSend().
2265 PHP_METHOD(HttpRequestPool
, socketSelect
)
2267 getObject(http_requestpool_object
, obj
);
2271 RETURN_SUCCESS(http_request_pool_select(&obj
->pool
));
2275 /* {{{ proto protected void HttpRequestPool::socketRead()
2277 * See HttpRequestPool::socketSend().
2279 PHP_METHOD(HttpRequestPool
, socketRead
)
2281 getObject(http_requestpool_object
, obj
);
2285 zend_llist_apply(&obj
->pool
.handles
, (llist_apply_func_t
) http_request_pool_responsehandler TSRMLS_CC
);
2292 #endif /* HTTP_HAVE_CURL */
2294 #endif /* ZEND_ENGINE_2 */
2301 * vim600: noet sw=4 ts=4 fdm=marker
2302 * vim<600: noet sw=4 ts=4