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_request_pool_api.h"
31 #include "php_http_date_api.h"
32 #include "php_http_headers_api.h"
33 #include "php_http_message_api.h"
34 #include "php_http_send_api.h"
35 #include "php_http_url_api.h"
37 #include "php_http_message_object.h"
38 #include "php_http_response_object.h"
39 #include "php_http_request_object.h"
40 #include "php_http_requestpool_object.h"
41 #include "php_http_exception_object.h"
47 ZEND_EXTERN_MODULE_GLOBALS(http
)
49 /* {{{ HttpResponse */
51 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
53 * Instantiates a new HttpResponse object, which can be used to send
54 * any data/resource/file to an HTTP client with caching and multiple
55 * ranges/resuming support.
57 * NOTE: GZIPping is not implemented yet.
59 PHP_METHOD(HttpResponse
, __construct
)
61 zend_bool do_cache
= 0, do_gzip
= 0;
62 getObject(http_response_object
, obj
);
65 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
66 UPD_PROP(obj
, long, cache
, do_cache
);
67 UPD_PROP(obj
, long, gzip
, do_gzip
);
73 /* {{{ proto bool HttpResponse::setCache(bool cache)
75 * Whether it sould be attempted to cache the entitity.
76 * This will result in necessary caching headers and checks of clients
77 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
78 * matches a "304 Not Modified" status code will be issued.
80 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
81 * to something more appropriate than "no-cache"!
83 PHP_METHOD(HttpResponse
, setCache
)
85 zend_bool do_cache
= 0;
86 getObject(http_response_object
, obj
);
88 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
92 UPD_PROP(obj
, long, cache
, do_cache
);
97 /* {{{ proto bool HttpResponse::getCache()
99 * Get current caching setting.
101 PHP_METHOD(HttpResponse
, getCache
)
106 zval
*do_cache
= NULL
;
107 getObject(http_response_object
, obj
);
109 do_cache
= GET_PROP(obj
, cache
);
110 RETURN_BOOL(Z_LVAL_P(do_cache
));
115 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
117 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
119 PHP_METHOD(HttpResponse
, setGzip
)
121 zend_bool do_gzip
= 0;
122 getObject(http_response_object
, obj
);
124 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
128 UPD_PROP(obj
, long, gzip
, do_gzip
);
133 /* {{{ proto bool HttpResponse::getGzip()
135 * Get current gzipping setting.
137 PHP_METHOD(HttpResponse
, getGzip
)
143 getObject(http_response_object
, obj
);
145 do_gzip
= GET_PROP(obj
, gzip
);
146 RETURN_BOOL(Z_LVAL_P(do_gzip
));
151 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
153 * Set a custom cache-control header, usually being "private" or "public"; if
154 * $raw is set to true the header will be sent as-is.
156 PHP_METHOD(HttpResponse
, setCacheControl
)
161 getObject(http_response_object
, obj
);
163 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
167 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
168 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
172 UPD_PROP(obj
, long, raw_cache_header
, raw
);
173 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
178 /* {{{ proto string HttpResponse::getCacheControl()
180 * Get current Cache-Control header setting.
182 PHP_METHOD(HttpResponse
, getCacheControl
)
188 getObject(http_response_object
, obj
);
190 ccontrol
= GET_PROP(obj
, cacheControl
);
191 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
196 /* {{{ proto bool HttpResponse::setContentType(string content_type)
198 * Set the content-type of the sent entity.
200 PHP_METHOD(HttpResponse
, setContentType
)
204 getObject(http_response_object
, obj
);
206 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
210 if (!strchr(ctype
, '/')) {
211 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
215 UPD_PROP(obj
, string
, contentType
, ctype
);
221 /* {{{ proto string HttpResponse::getContentType()
223 * Get current Content-Type header setting.
225 PHP_METHOD(HttpResponse
, getContentType
)
231 getObject(http_response_object
, obj
);
233 ctype
= GET_PROP(obj
, contentType
);
234 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
239 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
241 * Set the Content-Disposition of the sent entity. This setting aims to suggest
242 * the receiveing user agent how to handle the sent entity; usually the client
243 * will show the user a "Save As..." popup.
245 PHP_METHOD(HttpResponse
, setContentDisposition
)
249 zend_bool is_inline
= 0;
250 getObject(http_response_object
, obj
);
252 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
256 UPD_PROP(obj
, string
, dispoFile
, file
);
257 UPD_PROP(obj
, long, dispoInline
, is_inline
);
262 /* {{{ proto array HttpResponse::getContentDisposition()
264 * Get current Content-Disposition setting.
265 * Will return an associative array like:
268 * 'filename' => 'foo.bar',
273 PHP_METHOD(HttpResponse
, getContentDisposition
)
278 zval
*file
, *is_inline
;
279 getObject(http_response_object
, obj
);
281 file
= GET_PROP(obj
, dispoFile
);
282 is_inline
= GET_PROP(obj
, dispoInline
);
284 array_init(return_value
);
285 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
286 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
291 /* {{{ proto bool HttpResponse::setETag(string etag)
293 * Set a custom ETag. Use this only if you know what you're doing.
295 PHP_METHOD(HttpResponse
, setETag
)
299 getObject(http_response_object
, obj
);
301 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
305 UPD_PROP(obj
, string
, eTag
, etag
);
310 /* {{{ proto string HttpResponse::getETag()
312 * Get the previously set custom ETag.
314 PHP_METHOD(HttpResponse
, getETag
)
320 getObject(http_response_object
, obj
);
322 etag
= GET_PROP(obj
, eTag
);
323 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
328 /* {{{ proto void HttpResponse::setThrottleDelay(double seconds)
331 PHP_METHOD(HttpResponse
, setThrottleDelay
)
335 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d", &seconds
)) {
336 getObject(http_response_object
, obj
);
338 UPD_PROP(obj
, double, throttleDelay
, seconds
);
343 /* {{{ proto double HttpResponse::getThrottleDelay()
346 PHP_METHOD(HttpResponse
, getThrottleDelay
)
352 getObject(http_response_object
, obj
);
354 seconds
= GET_PROP(obj
, throttleDelay
);
355 RETURN_DOUBLE(Z_DVAL_P(seconds
));
360 /* {{{ proto void HttpResponse::setSendBuffersize(long bytes)
363 PHP_METHOD(HttpResponse
, setSendBuffersize
)
367 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &bytes
)) {
368 getObject(http_response_object
, obj
);
370 UPD_PROP(obj
, long, sendBuffersize
, bytes
);
375 /* {{{ proto long HttpResponse::getSendBuffersize()
378 PHP_METHOD(HttpResponse
, getSendBuffersize
)
384 getObject(http_response_object
, obj
);
386 bytes
= GET_PROP(obj
, sendBuffersize
);
387 RETURN_LONG(Z_LVAL_P(bytes
));
392 /* {{{ proto bool HttpResponse::setData(string data)
394 * Set the data to be sent.
396 PHP_METHOD(HttpResponse
, setData
)
399 getObject(http_response_object
, obj
);
401 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &the_data
)) {
405 convert_to_string_ex(&the_data
);
406 SET_PROP(obj
, data
, the_data
);
407 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_data
, SEND_DATA
));
408 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
413 /* {{{ proto string HttpResponse::getData()
415 * Get the previously set data to be sent.
417 PHP_METHOD(HttpResponse
, getData
)
423 getObject(http_response_object
, obj
);
425 the_data
= GET_PROP(obj
, data
);
426 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
431 /* {{{ proto bool HttpResponse::setStream(resource stream)
433 * Set the resource to be sent.
435 PHP_METHOD(HttpResponse
, setStream
)
438 php_stream
*the_real_stream
;
439 getObject(http_response_object
, obj
);
441 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
445 php_stream_from_zval(the_real_stream
, &the_stream
);
447 SET_PROP(obj
, stream
, the_stream
);
448 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_real_stream
, SEND_RSRC
));
449 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
454 /* {{{ proto resource HttpResponse::getStream()
456 * Get the previously set resource to be sent.
458 PHP_METHOD(HttpResponse
, getStream
)
464 getObject(http_response_object
, obj
);
466 the_stream
= GET_PROP(obj
, stream
);
467 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
472 /* {{{ proto bool HttpResponse::setFile(string file)
474 * Set the file to be sent.
476 PHP_METHOD(HttpResponse
, setFile
)
479 getObject(http_response_object
, obj
);
481 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
485 convert_to_string_ex(&the_file
);
487 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
488 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_file
, -1));
489 UPD_PROP(obj
, long, send_mode
, -1);
494 /* {{{ proto string HttpResponse::getFile()
496 * Get the previously set file to be sent.
498 PHP_METHOD(HttpResponse
, getFile
)
504 getObject(http_response_object
, obj
);
506 the_file
= GET_PROP(obj
, file
);
507 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
512 /* {{{ proto bool HttpResponse::send([bool clean_ob = true])
514 * Finally send the entity.
519 * $r = new HttpResponse(true);
520 * $r->setFile('../hidden/contract.pdf');
521 * $r->setContentType('application/pdf');
527 PHP_METHOD(HttpResponse
, send
)
529 zend_bool clean_ob
= 1;
530 zval
*do_cache
, *do_gzip
;
531 getObject(http_response_object
, obj
);
533 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &clean_ob
)) {
537 do_cache
= GET_PROP(obj
, cache
);
538 do_gzip
= GET_PROP(obj
, gzip
);
541 /* interrupt on-the-fly etag generation */
542 HTTP_G(etag
).started
= 0;
543 /* discard previous output buffers */
544 php_end_ob_buffers(0 TSRMLS_CC
);
548 if (Z_LVAL_P(do_gzip
)) {
549 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC
);
553 if (Z_LVAL_P(do_cache
)) {
556 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
558 etag
= GET_PROP(obj
, eTag
);
559 lmod
= GET_PROP(obj
, lastModified
);
560 cctrl
= GET_PROP(obj
, cacheControl
);
561 ccraw
= GET_PROP(obj
, raw_cache_header
);
563 if (Z_LVAL_P(ccraw
)) {
564 cc_hdr
= Z_STRVAL_P(cctrl
);
565 cc_len
= Z_STRLEN_P(cctrl
);
567 char cc_header
[42] = {0};
568 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
570 cc_len
= Z_STRLEN_P(cctrl
) + lenof(", must-revalidate, max-age=0");
573 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_hdr
, cc_len
);
574 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_hdr
, cc_len
);
579 zval
*ctype
= GET_PROP(obj
, contentType
);
580 if (Z_STRLEN_P(ctype
)) {
581 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
583 http_send_content_type("application/x-octetstream", lenof("application/x-octetstream"));
587 /* content disposition */
589 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
590 if (Z_STRLEN_P(dispo_file
)) {
591 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
592 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
598 zval
*send_buffersize
, *throttle_delay
;
599 send_buffersize
= GET_PROP(obj
, sendBuffersize
);
600 throttle_delay
= GET_PROP(obj
, throttleDelay
);
601 HTTP_G(send
).buffer_size
= Z_LVAL_P(send_buffersize
);
602 HTTP_G(send
).throttle_delay
= Z_DVAL_P(throttle_delay
);
607 zval
*send_mode
= GET_PROP(obj
, send_mode
);
608 switch (Z_LVAL_P(send_mode
))
612 zval
*zdata
= GET_PROP(obj
, data
);
613 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
618 php_stream
*the_real_stream
;
619 zval
*the_stream
= GET_PROP(obj
, stream
);
620 php_stream_from_zval(the_real_stream
, &the_stream
);
621 RETURN_SUCCESS(http_send_stream(the_real_stream
));
626 zval
*zfile
= GET_PROP(obj
, file
);
627 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
635 /* {{{ HttpMessage */
637 /* {{{ proto void HttpMessage::__construct([string message])
639 * Instantiate a new HttpMessage object.
641 PHP_METHOD(HttpMessage
, __construct
)
643 char *message
= NULL
;
645 getObject(http_message_object
, obj
);
648 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
649 if (obj
->message
= http_message_parse(message
, length
)) {
650 if (obj
->message
->parent
) {
651 obj
->parent
= http_message_object_from_msg(obj
->message
->parent
);
654 } else if (!obj
->message
) {
655 obj
->message
= http_message_new();
661 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
663 * Create an HttpMessage object from a string.
665 PHP_METHOD(HttpMessage
, fromString
)
669 http_message
*msg
= NULL
;
671 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
675 if (!(msg
= http_message_parse(string
, length
))) {
679 Z_TYPE_P(return_value
) = IS_OBJECT
;
680 return_value
->value
.obj
= http_message_object_from_msg(msg
);
684 /* {{{ proto string HttpMessage::getBody()
686 * Get the body of the parsed Message.
688 PHP_METHOD(HttpMessage
, getBody
)
693 getObject(http_message_object
, obj
);
694 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
699 /* {{{ proto array HttpMessage::getHeaders()
701 * Get Message Headers.
703 PHP_METHOD(HttpMessage
, getHeaders
)
709 getObject(http_message_object
, obj
);
711 Z_ARRVAL(headers
) = &obj
->message
->hdrs
;
712 array_init(return_value
);
713 array_copy(&headers
, return_value
);
718 /* {{{ proto void HttpMessage::setHeaders(array headers)
722 PHP_METHOD(HttpMessage
, setHeaders
)
724 zval
*new_headers
, old_headers
;
725 getObject(http_message_object
, obj
);
727 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
731 zend_hash_clean(&obj
->message
->hdrs
);
732 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
733 array_copy(new_headers
, &old_headers
);
737 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
739 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
741 PHP_METHOD(HttpMessage
, addHeaders
)
743 zval old_headers
, *new_headers
;
744 zend_bool append
= 0;
745 getObject(http_message_object
, obj
);
747 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
751 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
753 array_append(new_headers
, &old_headers
);
755 array_merge(new_headers
, &old_headers
);
760 /* {{{ proto long HttpMessage::getType()
762 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
764 PHP_METHOD(HttpMessage
, getType
)
769 getObject(http_message_object
, obj
);
770 RETURN_LONG(obj
->message
->type
);
775 /* {{{ proto void HttpMessage::setType(long type)
777 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
779 PHP_METHOD(HttpMessage
, setType
)
782 getObject(http_message_object
, obj
);
784 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
787 http_message_set_type(obj
->message
, type
);
791 /* {{{ proto long HttpMessage::getResponseCode()
793 * Get the Response Code of the Message.
795 PHP_METHOD(HttpMessage
, getResponseCode
)
800 getObject(http_message_object
, obj
);
802 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
803 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
807 RETURN_LONG(obj
->message
->info
.response
.code
);
812 /* {{{ proto bool HttpMessage::setResponseCode(long code)
814 * Set the response code of an HTTP Response Message.
815 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
816 * or if the response code is out of range (100-510).
818 PHP_METHOD(HttpMessage
, setResponseCode
)
821 getObject(http_message_object
, obj
);
823 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
824 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
828 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
831 if (code
< 100 || code
> 510) {
832 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid response code (100-510): %ld", code
);
836 obj
->message
->info
.response
.code
= code
;
841 /* {{{ proto string HttpMessage::getRequestMethod()
843 * Get the Request Method of the Message.
844 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
846 PHP_METHOD(HttpMessage
, getRequestMethod
)
851 getObject(http_message_object
, obj
);
853 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
854 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
858 RETURN_STRING(obj
->message
->info
.request
.method
, 1);
863 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
865 * Set the Request Method of the HTTP Message.
866 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
868 PHP_METHOD(HttpMessage
, setRequestMethod
)
872 getObject(http_message_object
, obj
);
874 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
875 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
879 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
882 if (method_len
< 1) {
883 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
886 if (SUCCESS
!= http_check_method(method
)) {
887 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Unkown request method: %s", method
);
891 STR_SET(obj
->message
->info
.request
.method
, estrndup(method
, method_len
));
896 /* {{{ proto string HttpMessage::getRequestUri()
898 * Get the Request URI of the Message.
900 PHP_METHOD(HttpMessage
, getRequestUri
)
905 getObject(http_message_object
, obj
);
907 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
908 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
912 RETURN_STRING(obj
->message
->info
.request
.URI
, 1);
917 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
919 * Set the Request URI of the HTTP Message.
920 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
921 * or if paramtere URI was empty.
923 PHP_METHOD(HttpMessage
, setRequestUri
)
927 getObject(http_message_object
, obj
);
929 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
930 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
933 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
937 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
941 STR_SET(obj
->message
->info
.request
.URI
, estrndup(URI
, URIlen
));
946 /* {{{ proto string HttpMessage::getHttpVersion()
948 * Get the HTTP Protocol Version of the Message.
950 PHP_METHOD(HttpMessage
, getHttpVersion
)
957 getObject(http_message_object
, obj
);
959 switch (obj
->message
->type
)
961 case HTTP_MSG_RESPONSE
:
962 version
= obj
->message
->info
.response
.http_version
;
965 case HTTP_MSG_REQUEST
:
966 version
= obj
->message
->info
.request
.http_version
;
973 sprintf(ver
, "%1.1f", version
);
974 RETURN_STRINGL(ver
, 3, 1);
979 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
981 * Set the HTTP Protocol version of the Message.
982 * Returns false if version is invalid (1.0 and 1.1).
984 PHP_METHOD(HttpMessage
, setHttpVersion
)
988 getObject(http_message_object
, obj
);
990 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
994 if (HTTP_MSG_TYPE(NONE
, obj
->message
)) {
995 http_error(E_WARNING
, HTTP_E_MSG
, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
999 convert_to_double(zv
);
1000 sprintf(v
, "%1.1f", Z_DVAL_P(zv
));
1001 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
1002 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
1006 if (HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
1007 obj
->message
->info
.response
.http_version
= (float) Z_DVAL_P(zv
);
1009 obj
->message
->info
.request
.http_version
= (float) Z_DVAL_P(zv
);
1015 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1017 * Get parent Message.
1019 PHP_METHOD(HttpMessage
, getParentMessage
)
1024 getObject(http_message_object
, obj
);
1026 if (obj
->message
->parent
) {
1027 RETVAL_OBJVAL(obj
->parent
);
1035 /* {{{ proto bool HttpMessage::send()
1037 * Send the Message according to its type as Response or Request.
1039 PHP_METHOD(HttpMessage
, send
)
1041 getObject(http_message_object
, obj
);
1045 RETURN_SUCCESS(http_message_send(obj
->message
));
1049 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
1051 * Get the string representation of the Message.
1053 PHP_METHOD(HttpMessage
, toString
)
1058 zend_bool include_parent
= 1;
1059 getObject(http_message_object
, obj
);
1061 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1065 if (include_parent
) {
1066 http_message_serialize(obj
->message
, &string
, &length
);
1068 http_message_tostring(obj
->message
, &string
, &length
);
1070 RETURN_STRINGL(string
, length
, 0);
1077 #ifdef HTTP_HAVE_CURL
1078 /* {{{ HttpRequest */
1080 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
1082 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
1083 * and POST (including posting files) HTTP requests.
1085 PHP_METHOD(HttpRequest
, __construct
)
1090 getObject(http_request_object
, obj
);
1092 SET_EH_THROW_HTTP();
1093 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
1094 INIT_PARR(obj
, options
);
1095 INIT_PARR(obj
, responseInfo
);
1096 INIT_PARR(obj
, responseData
);
1097 INIT_PARR(obj
, postFields
);
1098 INIT_PARR(obj
, postFiles
);
1101 UPD_PROP(obj
, string
, url
, URL
);
1104 UPD_PROP(obj
, long, method
, meth
);
1111 /* {{{ proto void HttpRequest::__destruct()
1113 * Destroys the HttpRequest object.
1115 PHP_METHOD(HttpRequest
, __destruct
)
1117 getObject(http_request_object
, obj
);
1121 FREE_PARR(obj
, options
);
1122 FREE_PARR(obj
, responseInfo
);
1123 FREE_PARR(obj
, responseData
);
1124 FREE_PARR(obj
, postFields
);
1125 FREE_PARR(obj
, postFiles
);
1129 /* {{{ proto bool HttpRequest::setOptions(array options)
1131 * Set the request options to use. See http_get() for a full list of available options.
1133 PHP_METHOD(HttpRequest
, setOptions
)
1137 zval
*opts
, *old_opts
, **opt
;
1138 getObject(http_request_object
, obj
);
1140 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1144 old_opts
= GET_PROP(obj
, options
);
1146 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
1147 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
1149 if (!strcmp(key
, "headers")) {
1151 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1152 array_merge(*opt
, *headers
);
1155 } else if (!strcmp(key
, "cookies")) {
1157 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1158 array_merge(*opt
, *cookies
);
1163 add_assoc_zval(old_opts
, key
, *opt
);
1174 /* {{{ proto array HttpRequest::getOptions()
1176 * Get current set options.
1178 PHP_METHOD(HttpRequest
, getOptions
)
1184 getObject(http_request_object
, obj
);
1186 opts
= GET_PROP(obj
, options
);
1187 array_init(return_value
);
1188 array_copy(opts
, return_value
);
1193 /* {{{ proto void HttpRequest::unsetOptions()
1195 * Unset all options/headers/cookies.
1197 PHP_METHOD(HttpRequest
, unsetOptions
)
1199 getObject(http_request_object
, obj
);
1203 FREE_PARR(obj
, options
);
1204 INIT_PARR(obj
, options
);
1208 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1210 * Set additional SSL options.
1212 PHP_METHOD(HttpRequest
, setSslOptions
)
1214 zval
*opts
, *old_opts
, **ssl_options
;
1215 getObject(http_request_object
, obj
);
1217 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1221 old_opts
= GET_PROP(obj
, options
);
1223 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1224 array_merge(opts
, *ssl_options
);
1226 zval_add_ref(&opts
);
1227 add_assoc_zval(old_opts
, "ssl", opts
);
1234 /* {{{ proto array HttpRequest::getSslOtpions()
1236 * Get previously set SSL options.
1238 PHP_METHOD(HttpRequest
, getSslOptions
)
1243 zval
*opts
, **ssl_options
;
1244 getObject(http_request_object
, obj
);
1246 opts
= GET_PROP(obj
, options
);
1248 array_init(return_value
);
1250 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1251 array_copy(*ssl_options
, return_value
);
1257 /* {{{ proto void HttpRequest::unsetSslOptions()
1259 * Unset previously set SSL options.
1261 PHP_METHOD(HttpRequest
, unsetSslOptions
)
1264 getObject(http_request_object
, obj
);
1268 opts
= GET_PROP(obj
, options
);
1269 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
1273 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1275 * Add request header name/value pairs.
1277 PHP_METHOD(HttpRequest
, addHeaders
)
1279 zval
*opts
, **headers
, *new_headers
;
1280 getObject(http_request_object
, obj
);
1282 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
1286 opts
= GET_PROP(obj
, options
);
1288 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1289 array_merge(new_headers
, *headers
);
1291 zval_add_ref(&new_headers
);
1292 add_assoc_zval(opts
, "headers", new_headers
);
1299 /* {{{ proto array HttpRequest::getHeaders()
1301 * Get previously set request headers.
1303 PHP_METHOD(HttpRequest
, getHeaders
)
1308 zval
*opts
, **headers
;
1309 getObject(http_request_object
, obj
);
1311 opts
= GET_PROP(obj
, options
);
1313 array_init(return_value
);
1315 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1316 array_copy(*headers
, return_value
);
1322 /* {{{ proto void HttpRequest::unsetHeaders()
1324 * Unset previously set request headers.
1326 PHP_METHOD(HttpRequest
, unsetHeaders
)
1329 getObject(http_request_object
, obj
);
1333 opts
= GET_PROP(obj
, options
);
1334 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
1338 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1342 PHP_METHOD(HttpRequest
, addCookies
)
1344 zval
*opts
, **cookies
, *new_cookies
;
1345 getObject(http_request_object
, obj
);
1347 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
1351 opts
= GET_PROP(obj
, options
);
1353 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1354 array_merge(new_cookies
, *cookies
);
1356 zval_add_ref(&new_cookies
);
1357 add_assoc_zval(opts
, "cookies", new_cookies
);
1364 /* {{{ proto array HttpRequest::getCookies()
1366 * Get previously set cookies.
1368 PHP_METHOD(HttpRequest
, getCookies
)
1373 zval
*opts
, **cookies
;
1374 getObject(http_request_object
, obj
);
1376 opts
= GET_PROP(obj
, options
);
1378 array_init(return_value
);
1380 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1381 array_copy(*cookies
, return_value
);
1387 /* {{{ proto void HttpRequest::unsetCookies()
1390 PHP_METHOD(HttpRequest
, unsetCookies
)
1393 getObject(http_request_object
, obj
);
1397 opts
= GET_PROP(obj
, options
);
1398 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
1402 /* {{{ proto bool HttpRequest::setURL(string url)
1404 * Set the request URL.
1406 PHP_METHOD(HttpRequest
, setURL
)
1410 getObject(http_request_object
, obj
);
1412 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1416 UPD_PROP(obj
, string
, url
, URL
);
1421 /* {{{ proto string HttpRequest::getUrl()
1423 * Get the previously set request URL.
1425 PHP_METHOD(HttpRequest
, getURL
)
1431 getObject(http_request_object
, obj
);
1433 URL
= GET_PROP(obj
, url
);
1434 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
1439 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1441 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1442 * <tt>HTTP_POST</tt> constants.
1444 PHP_METHOD(HttpRequest
, setMethod
)
1447 getObject(http_request_object
, obj
);
1449 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1453 UPD_PROP(obj
, long, method
, meth
);
1458 /* {{{ proto long HttpRequest::getMethod()
1460 * Get the previously set request method.
1462 PHP_METHOD(HttpRequest
, getMethod
)
1468 getObject(http_request_object
, obj
);
1470 meth
= GET_PROP(obj
, method
);
1471 RETURN_LONG(Z_LVAL_P(meth
));
1476 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1478 * Set the content type the post request should have.
1479 * Use this only if you know what you're doing.
1481 PHP_METHOD(HttpRequest
, setContentType
)
1485 getObject(http_request_object
, obj
);
1487 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1491 if (!strchr(ctype
, '/')) {
1492 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
1496 UPD_PROP(obj
, string
, contentType
, ctype
);
1501 /* {{{ proto string HttpRequest::getContentType()
1503 * Get the previously content type.
1505 PHP_METHOD(HttpRequest
, getContentType
)
1511 getObject(http_request_object
, obj
);
1513 ctype
= GET_PROP(obj
, contentType
);
1514 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
1519 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1521 * Set the URL query parameters to use.
1522 * Overwrites previously set query parameters.
1523 * Affects any request types.
1525 PHP_METHOD(HttpRequest
, setQueryData
)
1528 char *query_data
= NULL
;
1529 getObject(http_request_object
, obj
);
1531 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
1535 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1536 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1539 UPD_PROP(obj
, string
, queryData
, query_data
);
1544 convert_to_string(qdata
);
1545 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
1550 /* {{{ proto string HttpRequest::getQueryData()
1552 * Get the current query data in form of an urlencoded query string.
1554 PHP_METHOD(HttpRequest
, getQueryData
)
1560 getObject(http_request_object
, obj
);
1562 qdata
= GET_PROP(obj
, queryData
);
1563 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
1568 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1570 * Add parameters to the query parameter list.
1571 * Affects any request type.
1573 PHP_METHOD(HttpRequest
, addQueryData
)
1575 zval
*qdata
, *old_qdata
;
1576 char *query_data
= NULL
;
1577 getObject(http_request_object
, obj
);
1579 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1583 old_qdata
= GET_PROP(obj
, queryData
);
1585 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1589 UPD_PROP(obj
, string
, queryData
, query_data
);
1596 /* {{{ proto void HttpRequest::unsetQueryData()
1598 * Clean the query parameters.
1599 * Affects any request type.
1601 PHP_METHOD(HttpRequest
, unsetQueryData
)
1603 getObject(http_request_object
, obj
);
1607 UPD_PROP(obj
, string
, queryData
, "");
1611 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1613 * Adds POST data entries.
1614 * Affects only POST requests.
1616 PHP_METHOD(HttpRequest
, addPostFields
)
1618 zval
*post
, *post_data
;
1619 getObject(http_request_object
, obj
);
1621 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1625 post
= GET_PROP(obj
, postFields
);
1626 array_merge(post_data
, post
);
1632 /* {{{ proto bool HttpRequest::setPostFields(array post_data)
1634 * Set the POST data entries.
1635 * Overwrites previously set POST data.
1636 * Affects only POST requests.
1638 PHP_METHOD(HttpRequest
, setPostFields
)
1640 zval
*post
, *post_data
;
1641 getObject(http_request_object
, obj
);
1643 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1647 post
= GET_PROP(obj
, postFields
);
1648 zend_hash_clean(Z_ARRVAL_P(post
));
1649 array_copy(post_data
, post
);
1655 /* {{{ proto array HttpRequest::getPostFields()
1657 * Get previously set POST data.
1659 PHP_METHOD(HttpRequest
, getPostFields
)
1665 getObject(http_request_object
, obj
);
1667 post_data
= GET_PROP(obj
, postFields
);
1668 array_init(return_value
);
1669 array_copy(post_data
, return_value
);
1674 /* {{{ proto void HttpRequest::unsetPostFields()
1676 * Clean POST data entires.
1677 * Affects only POST requests.
1679 PHP_METHOD(HttpRequest
, unsetPostFields
)
1682 getObject(http_request_object
, obj
);
1686 post_data
= GET_PROP(obj
, postFields
);
1687 zend_hash_clean(Z_ARRVAL_P(post_data
));
1691 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1693 * Add a file to the POST request.
1694 * Affects only POST requests.
1696 PHP_METHOD(HttpRequest
, addPostFile
)
1698 zval
*files
, *entry
;
1699 char *name
, *file
, *type
= NULL
;
1700 int name_len
, file_len
, type_len
= 0;
1701 getObject(http_request_object
, obj
);
1703 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1708 if (!strchr(type
, '/')) {
1709 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1713 type
= "application/x-octetstream";
1714 type_len
= sizeof("application/x-octetstream") - 1;
1717 MAKE_STD_ZVAL(entry
);
1720 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1721 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1722 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1724 files
= GET_PROP(obj
, postFiles
);
1725 add_next_index_zval(files
, entry
);
1731 /* {{{ proto bool HttpRequest::setPostFiles()
1733 * Set files to post.
1734 * Overwrites previously set post files.
1735 * Affects only POST requests.
1737 PHP_METHOD(HttpRequest
, setPostFiles
)
1739 zval
*files
, *pFiles
;
1740 getObject(http_request_object
, obj
);
1742 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &files
)) {
1746 pFiles
= GET_PROP(obj
, postFiles
);
1747 zend_hash_clean(Z_ARRVAL_P(pFiles
));
1748 array_copy(files
, pFiles
);
1754 /* {{{ proto array HttpRequest::getPostFiles()
1756 * Get all previously added POST files.
1758 PHP_METHOD(HttpRequest
, getPostFiles
)
1764 getObject(http_request_object
, obj
);
1766 files
= GET_PROP(obj
, postFiles
);
1768 array_init(return_value
);
1769 array_copy(files
, return_value
);
1774 /* {{{ proto void HttpRequest::unsetPostFiles()
1776 * Unset the POST files list.
1777 * Affects only POST requests.
1779 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1782 getObject(http_request_object
, obj
);
1786 files
= GET_PROP(obj
, postFiles
);
1787 zend_hash_clean(Z_ARRVAL_P(files
));
1791 /* {{{ proto bool HttpRequest::SetPutFile(string file)
1794 * Affects only PUT requests.
1796 PHP_METHOD(HttpRequest
, setPutFile
)
1800 getObject(http_request_object
, obj
);
1802 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &file_len
)) {
1806 UPD_PROP(obj
, string
, putFile
, file
);
1811 /* {{{ proto string HttpRequest::getPutFile()
1813 * Get previously set put file.
1815 PHP_METHOD(HttpRequest
, getPutFile
)
1821 getObject(http_request_object
, obj
);
1823 putfile
= GET_PROP(obj
, putFile
);
1824 RETVAL_STRINGL(Z_STRVAL_P(putfile
), Z_STRLEN_P(putfile
), 1);
1829 /* {{{ proto void HttpRequest::unsetPutFile()
1831 * Unset file to put.
1832 * Affects only PUT requests.
1834 PHP_METHOD(HttpRequest
, unsetPutFile
)
1836 getObject(http_request_object
, obj
);
1840 UPD_PROP(obj
, string
, putFile
, "");
1844 /* {{{ proto array HttpRequest::getResponseData()
1846 * Get all response data after the request has been sent.
1848 PHP_METHOD(HttpRequest
, getResponseData
)
1854 getObject(http_request_object
, obj
);
1856 data
= GET_PROP(obj
, responseData
);
1857 array_init(return_value
);
1858 array_copy(data
, return_value
);
1863 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1865 * Get response header(s) after the request has been sent.
1867 PHP_METHOD(HttpRequest
, getResponseHeader
)
1870 zval
*data
, **headers
, **header
;
1871 char *header_name
= NULL
;
1873 getObject(http_response_object
, obj
);
1875 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1879 data
= GET_PROP(obj
, responseData
);
1880 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1884 if (!header_len
|| !header_name
) {
1885 array_init(return_value
);
1886 array_copy(*headers
, return_value
);
1887 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1888 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1896 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1898 * Get response cookie(s) after the request has been sent.
1900 PHP_METHOD(HttpRequest
, getResponseCookie
)
1903 zval
*data
, **headers
;
1904 char *cookie_name
= NULL
;
1906 getObject(http_request_object
, obj
);
1908 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1912 array_init(return_value
);
1914 data
= GET_PROP(obj
, responseData
);
1915 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1918 zval
**header
= NULL
;
1920 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1921 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1922 /* several cookies? */
1923 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1926 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1928 MAKE_STD_ZVAL(cookie_hash
);
1929 array_init(cookie_hash
);
1931 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1933 add_next_index_zval(return_value
, cookie_hash
);
1937 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1938 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1939 add_next_index_zval(return_value
, cookie_hash
);
1940 return; /* <<< FOUND >>> */
1942 zval_dtor(cookie_hash
);
1947 zval_dtor(cookie_hash
);
1953 MAKE_STD_ZVAL(cookie_hash
);
1954 array_init(cookie_hash
);
1956 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1958 add_next_index_zval(return_value
, cookie_hash
);
1962 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1963 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1964 add_next_index_zval(return_value
, cookie_hash
);
1966 zval_dtor(cookie_hash
);
1971 zval_dtor(cookie_hash
);
1985 /* {{{ proto string HttpRequest::getResponseBody()
1987 * Get the response body after the request has been sent.
1989 PHP_METHOD(HttpRequest
, getResponseBody
)
1995 getObject(http_request_object
, obj
);
1997 data
= GET_PROP(obj
, responseData
);
1998 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1999 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
2007 /* {{{ proto int HttpRequest::getResponseCode()
2009 * Get the response code after the request has been sent.
2011 PHP_METHOD(HttpRequest
, getResponseCode
)
2017 getObject(http_request_object
, obj
);
2019 code
= GET_PROP(obj
, responseCode
);
2020 RETURN_LONG(Z_LVAL_P(code
));
2025 /* {{{ proto array HttpRequest::getResponseInfo([string name])
2027 * Get response info after the request has been sent.
2028 * See http_get() for a full list of returned info.
2030 PHP_METHOD(HttpRequest
, getResponseInfo
)
2033 zval
*info
, **infop
;
2034 char *info_name
= NULL
;
2036 getObject(http_request_object
, obj
);
2038 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
2042 info
= GET_PROP(obj
, responseInfo
);
2044 if (info_len
&& info_name
) {
2045 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
2046 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
2048 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Could not find response info named %s", info_name
);
2052 array_init(return_value
);
2053 array_copy(info
, return_value
);
2059 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
2061 * Get the full response as HttpMessage object.
2063 PHP_METHOD(HttpRequest
, getResponseMessage
)
2069 getObject(http_request_object
, obj
);
2071 message
= GET_PROP(obj
, responseMessage
);
2072 if (Z_TYPE_P(message
) == IS_OBJECT
) {
2073 RETVAL_OBJECT(message
);
2081 /* {{{ proto bool HttpRequest::send()
2083 * Send the HTTP request.
2088 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
2089 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
2090 * $r->addQueryData(array('category' => 3));
2093 * if ($r->getResponseCode() == 200) {
2094 * file_put_contents('local.rss', $r->getResponseBody());
2096 * } catch (HttpException $ex) {
2105 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
2106 * $r->setOptions(array('cookies' => array('lang' => 'de')));
2107 * $r->addpostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
2108 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
2110 * echo $r->getResponseBody();
2115 PHP_METHOD(HttpRequest
, send
)
2117 STATUS status
= FAILURE
;
2118 http_request_body body
= {0};
2119 getObject(http_request_object
, obj
);
2123 SET_EH_THROW_HTTP();
2126 http_error(E_WARNING
, HTTP_E_CURL
, "You cannot call HttpRequest::send() while attached to an HttpRequestPool");
2130 if (SUCCESS
== (status
= http_request_object_requesthandler(obj
, getThis(), &body
))) {
2131 status
= http_request_exec(obj
->ch
, NULL
);
2133 http_request_body_dtor(&body
);
2135 /* final data handling */
2136 if (SUCCESS
== status
) {
2137 status
= http_request_object_responsehandler(obj
, getThis());
2141 RETURN_SUCCESS(status
);
2145 /* {{{ HttpRequestPool */
2147 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
2149 * Instantiate a new HttpRequestPool object. An HttpRequestPool is
2150 * able to send several HttpRequests in parallel.
2155 * $urls = array('www.php.net', 'pecl.php.net', 'pear.php.net')
2156 * $pool = new HttpRequestPool;
2157 * foreach ($urls as $url) {
2158 * $req[$url] = new HttpRequest("http://$url", HTTP_HEAD);
2159 * $pool->attach($req[$url]);
2162 * foreach ($urls as $url) {
2163 * printf("%s (%s) is %s\n",
2164 * $url, $req[$url]->getResponseInfo('effective_url'),
2165 * $r->getResponseCode() == 200 ? 'alive' : 'not alive'
2171 PHP_METHOD(HttpRequestPool
, __construct
)
2173 int argc
= ZEND_NUM_ARGS();
2174 zval
***argv
= safe_emalloc(argc
, sizeof(zval
*), 0);
2175 getObject(http_requestpool_object
, obj
);
2177 if (SUCCESS
== zend_get_parameters_array_ex(argc
, argv
)) {
2180 for (i
= 0; i
< argc
; ++i
) {
2181 if (Z_TYPE_PP(argv
[i
]) == IS_OBJECT
&& instanceof_function(Z_OBJCE_PP(argv
[i
]), http_request_object_ce TSRMLS_CC
)) {
2182 http_request_pool_attach(&obj
->pool
, *(argv
[i
]));
2190 /* {{{ proto void HttpRequestPool::__destruct()
2192 * Clean up HttpRequestPool object.
2194 PHP_METHOD(HttpRequestPool
, __destruct
)
2196 getObject(http_requestpool_object
, obj
);
2200 http_request_pool_detach_all(&obj
->pool
);
2204 /* {{{ proto void HttpRequestPool::reset()
2206 * Detach all attached HttpRequest objects.
2208 PHP_METHOD(HttpRequestPool
, reset
)
2210 getObject(http_requestpool_object
, obj
);
2214 http_request_pool_detach_all(&obj
->pool
);
2217 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
2219 * Attach an HttpRequest object to this HttpRequestPool.
2220 * NOTE: set all options prior attaching!
2222 PHP_METHOD(HttpRequestPool
, attach
)
2225 STATUS status
= FAILURE
;
2226 getObject(http_requestpool_object
, obj
);
2228 SET_EH_THROW_HTTP();
2229 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
2230 status
= http_request_pool_attach(&obj
->pool
, request
);
2233 RETURN_SUCCESS(status
);
2237 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
2239 * Detach an HttpRequest object from this HttpRequestPool.
2241 PHP_METHOD(HttpRequestPool
, detach
)
2244 STATUS status
= FAILURE
;
2245 getObject(http_requestpool_object
, obj
);
2247 SET_EH_THROW_HTTP();
2248 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
2249 status
= http_request_pool_detach(&obj
->pool
, request
);
2252 RETURN_SUCCESS(status
);
2256 /* {{{ proto bool HttpRequestPool::send()
2258 * Send all attached HttpRequest objects in parallel.
2260 PHP_METHOD(HttpRequestPool
, send
)
2263 getObject(http_requestpool_object
, obj
);
2267 SET_EH_THROW_HTTP();
2268 status
= http_request_pool_send(&obj
->pool
);
2271 RETURN_SUCCESS(status
);
2275 /* {{{ proto protected bool HttpRequestPool::socketSend()
2280 * while ($pool->socketSend()) {
2281 * do_something_else();
2282 * if (!$pool->socketSelect()) {
2283 * die('Socket error');
2286 * $pool->socketRead();
2290 PHP_METHOD(HttpRequestPool
, socketSend
)
2292 getObject(http_requestpool_object
, obj
);
2296 RETURN_BOOL(0 < http_request_pool_perform(&obj
->pool
));
2300 /* {{{ proto protected bool HttpRequestPool::socketSelect()
2302 * See HttpRequestPool::socketSend().
2304 PHP_METHOD(HttpRequestPool
, socketSelect
)
2306 getObject(http_requestpool_object
, obj
);
2310 RETURN_SUCCESS(http_request_pool_select(&obj
->pool
));
2314 /* {{{ proto protected void HttpRequestPool::socketRead()
2316 * See HttpRequestPool::socketSend().
2318 PHP_METHOD(HttpRequestPool
, socketRead
)
2320 getObject(http_requestpool_object
, obj
);
2324 zend_llist_apply(&obj
->pool
.handles
, (llist_apply_func_t
) http_request_pool_responsehandler TSRMLS_CC
);
2331 #endif /* HTTP_HAVE_CURL */
2333 #endif /* ZEND_ENGINE_2 */
2340 * vim600: noet sw=4 ts=4 fdm=marker
2341 * vim<600: noet sw=4 ts=4