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 +----------------------------------------------------------------------+
25 #include "php_streams.h"
28 #include "php_http_std_defs.h"
29 #include "php_http_api.h"
30 #include "php_http_cache_api.h"
31 #include "php_http_request_api.h"
32 #include "php_http_request_pool_api.h"
33 #include "php_http_date_api.h"
34 #include "php_http_headers_api.h"
35 #include "php_http_message_api.h"
36 #include "php_http_send_api.h"
37 #include "php_http_url_api.h"
39 #include "php_http_message_object.h"
40 #include "php_http_response_object.h"
41 #include "php_http_request_object.h"
42 #include "php_http_requestpool_object.h"
43 #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 PHP_METHOD(HttpResponse
, __construct
)
59 zend_bool do_cache
= 0, do_gzip
= 0;
60 getObject(http_response_object
, obj
);
63 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
64 UPD_PROP(obj
, long, cache
, do_cache
);
65 UPD_PROP(obj
, long, gzip
, do_gzip
);
71 /* {{{ proto void HttpResponse::__destruct()
75 PHP_METHOD(HttpResponse
, __destruct
)
77 getObject(http_response_object
, obj
);
78 zval
*catch_ob
= GET_PROP(obj
, catch_ob
), *sent
= GET_PROP(obj
, sent
);
79 fprintf(stderr
, "DTOR!\n"); fflush(stderr
);
80 if (!Z_LVAL_P(sent
) && Z_LVAL_P(catch_ob
)) {
83 /* fetch catched output buffer */
84 if (SUCCESS
== php_ob_get_buffer(&ob_data TSRMLS_CC
)) {
85 zval
*lmod
= GET_PROP(obj
, lastModified
);
87 SET_PROP(obj
, data
, &ob_data
);
88 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
90 if (!Z_LVAL_P(lmod
)) {
91 UPD_PROP(obj
, long, lastModified
, http_last_modified(&ob_data
, SEND_DATA
));
95 http_response_object_sendhandler(getThis(), obj
, 1, return_value
);
102 /* {{{ proto bool HttpResponse::setCache(bool cache)
104 * Whether it sould be attempted to cache the entitity.
105 * This will result in necessary caching headers and checks of clients
106 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
107 * matches a "304 Not Modified" status code will be issued.
109 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
110 * to something more appropriate than "no-cache"!
112 PHP_METHOD(HttpResponse
, setCache
)
114 zend_bool do_cache
= 0;
115 getObject(http_response_object
, obj
);
117 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
121 UPD_PROP(obj
, long, cache
, do_cache
);
126 /* {{{ proto bool HttpResponse::getCache()
128 * Get current caching setting.
130 PHP_METHOD(HttpResponse
, getCache
)
135 zval
*do_cache
= NULL
;
136 getObject(http_response_object
, obj
);
138 do_cache
= GET_PROP(obj
, cache
);
139 RETURN_BOOL(Z_LVAL_P(do_cache
));
144 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
146 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
148 PHP_METHOD(HttpResponse
, setGzip
)
150 zend_bool do_gzip
= 0;
151 getObject(http_response_object
, obj
);
153 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
157 UPD_PROP(obj
, long, gzip
, do_gzip
);
162 /* {{{ proto bool HttpResponse::getGzip()
164 * Get current gzipping setting.
166 PHP_METHOD(HttpResponse
, getGzip
)
172 getObject(http_response_object
, obj
);
174 do_gzip
= GET_PROP(obj
, gzip
);
175 RETURN_BOOL(Z_LVAL_P(do_gzip
));
180 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
182 * Set a custom cache-control header, usually being "private" or "public"; if
183 * $raw is set to true the header will be sent as-is.
185 PHP_METHOD(HttpResponse
, setCacheControl
)
190 getObject(http_response_object
, obj
);
192 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
196 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
197 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
201 UPD_PROP(obj
, long, raw_cache_header
, raw
);
202 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
207 /* {{{ proto string HttpResponse::getCacheControl()
209 * Get current Cache-Control header setting.
211 PHP_METHOD(HttpResponse
, getCacheControl
)
217 getObject(http_response_object
, obj
);
219 ccontrol
= GET_PROP(obj
, cacheControl
);
220 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
225 /* {{{ proto bool HttpResponse::setContentType(string content_type)
227 * Set the content-type of the sent entity.
229 PHP_METHOD(HttpResponse
, setContentType
)
233 getObject(http_response_object
, obj
);
235 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
239 if (!strchr(ctype
, '/')) {
240 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
244 UPD_PROP(obj
, string
, contentType
, ctype
);
250 /* {{{ proto string HttpResponse::getContentType()
252 * Get current Content-Type header setting.
254 PHP_METHOD(HttpResponse
, getContentType
)
260 getObject(http_response_object
, obj
);
262 ctype
= GET_PROP(obj
, contentType
);
263 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
268 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
270 * Set the Content-Disposition of the sent entity. This setting aims to suggest
271 * the receiveing user agent how to handle the sent entity; usually the client
272 * will show the user a "Save As..." popup.
274 PHP_METHOD(HttpResponse
, setContentDisposition
)
278 zend_bool is_inline
= 0;
279 getObject(http_response_object
, obj
);
281 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
285 UPD_PROP(obj
, string
, dispoFile
, file
);
286 UPD_PROP(obj
, long, dispoInline
, is_inline
);
291 /* {{{ proto array HttpResponse::getContentDisposition()
293 * Get current Content-Disposition setting.
294 * Will return an associative array like:
297 * 'filename' => 'foo.bar',
302 PHP_METHOD(HttpResponse
, getContentDisposition
)
307 zval
*file
, *is_inline
;
308 getObject(http_response_object
, obj
);
310 file
= GET_PROP(obj
, dispoFile
);
311 is_inline
= GET_PROP(obj
, dispoInline
);
313 array_init(return_value
);
314 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
315 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
320 /* {{{ proto bool HttpResponse::setETag(string etag)
322 * Set a custom ETag. Use this only if you know what you're doing.
324 PHP_METHOD(HttpResponse
, setETag
)
328 getObject(http_response_object
, obj
);
330 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
334 UPD_PROP(obj
, string
, eTag
, etag
);
339 /* {{{ proto string HttpResponse::getETag()
341 * Get the previously set custom ETag.
343 PHP_METHOD(HttpResponse
, getETag
)
349 getObject(http_response_object
, obj
);
351 etag
= GET_PROP(obj
, eTag
);
352 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
357 /* {{{ proto void HttpResponse::setThrottleDelay(double seconds)
360 PHP_METHOD(HttpResponse
, setThrottleDelay
)
364 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d", &seconds
)) {
365 getObject(http_response_object
, obj
);
367 UPD_PROP(obj
, double, throttleDelay
, seconds
);
372 /* {{{ proto double HttpResponse::getThrottleDelay()
375 PHP_METHOD(HttpResponse
, getThrottleDelay
)
381 getObject(http_response_object
, obj
);
383 seconds
= GET_PROP(obj
, throttleDelay
);
384 RETURN_DOUBLE(Z_DVAL_P(seconds
));
389 /* {{{ proto void HttpResponse::setSendBuffersize(long bytes)
392 PHP_METHOD(HttpResponse
, setSendBuffersize
)
396 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &bytes
)) {
397 getObject(http_response_object
, obj
);
399 UPD_PROP(obj
, long, sendBuffersize
, bytes
);
404 /* {{{ proto long HttpResponse::getSendBuffersize()
407 PHP_METHOD(HttpResponse
, getSendBuffersize
)
413 getObject(http_response_object
, obj
);
415 bytes
= GET_PROP(obj
, sendBuffersize
);
416 RETURN_LONG(Z_LVAL_P(bytes
));
421 /* {{{ proto bool HttpResponse::setData(string data)
423 * Set the data to be sent.
425 PHP_METHOD(HttpResponse
, setData
)
428 getObject(http_response_object
, obj
);
430 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &the_data
)) {
434 convert_to_string_ex(&the_data
);
435 SET_PROP(obj
, data
, the_data
);
436 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_data
, SEND_DATA
));
437 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
442 /* {{{ proto string HttpResponse::getData()
444 * Get the previously set data to be sent.
446 PHP_METHOD(HttpResponse
, getData
)
452 getObject(http_response_object
, obj
);
454 the_data
= GET_PROP(obj
, data
);
455 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
460 /* {{{ proto bool HttpResponse::setStream(resource stream)
462 * Set the resource to be sent.
464 PHP_METHOD(HttpResponse
, setStream
)
467 php_stream
*the_real_stream
;
468 getObject(http_response_object
, obj
);
470 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
474 php_stream_from_zval(the_real_stream
, &the_stream
);
476 SET_PROP(obj
, stream
, the_stream
);
477 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_real_stream
, SEND_RSRC
));
478 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
483 /* {{{ proto resource HttpResponse::getStream()
485 * Get the previously set resource to be sent.
487 PHP_METHOD(HttpResponse
, getStream
)
493 getObject(http_response_object
, obj
);
495 the_stream
= GET_PROP(obj
, stream
);
496 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
501 /* {{{ proto bool HttpResponse::setFile(string file)
503 * Set the file to be sent.
505 PHP_METHOD(HttpResponse
, setFile
)
508 getObject(http_response_object
, obj
);
510 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
514 convert_to_string_ex(&the_file
);
516 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
517 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_file
, -1));
518 UPD_PROP(obj
, long, send_mode
, -1);
523 /* {{{ proto string HttpResponse::getFile()
525 * Get the previously set file to be sent.
527 PHP_METHOD(HttpResponse
, getFile
)
533 getObject(http_response_object
, obj
);
535 the_file
= GET_PROP(obj
, file
);
536 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
541 /* {{{ proto bool HttpResponse::send([bool clean_ob = true])
543 * Finally send the entity.
548 * $r = new HttpResponse(true);
549 * $r->setFile('../hidden/contract.pdf');
550 * $r->setContentType('application/pdf');
556 PHP_METHOD(HttpResponse
, send
)
558 zend_bool clean_ob
= 1;
560 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &clean_ob
)) {
564 http_response_object_sendhandler(getThis(), NULL
, clean_ob
, return_value
);
568 /* {{{ proto bool HttpResponse::catchOutput([bool clean_ob = false])
570 * Use this method instead of HttpResponse::set*() and HttpResponse::send()
571 * to let HttpResponse catch and handle the scripts output (i.e. echo/print).
576 * $r = new HttpResponse(true, true);
577 * $r->setContentType('text/html; charset=utf-8');
578 * $r->catchOutput(true);
583 PHP_METHOD(HttpResponse
, catchOutput
)
585 zend_bool clean_ob
= 0;
586 getObject(http_response_object
, obj
);
588 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &clean_ob
)) {
592 UPD_PROP(obj
, long, catch_ob
, 1);
595 /* interrupt on-the-fly etag generation */
596 HTTP_G(etag
).started
= 0;
597 /* discard previous output buffers */
598 php_end_ob_buffers(0 TSRMLS_CC
);
601 if (SUCCESS
!= php_start_ob_buffer(NULL
, 0, 1 TSRMLS_CC
)) {
609 /* {{{ HttpMessage */
611 /* {{{ proto void HttpMessage::__construct([string message])
613 * Instantiate a new HttpMessage object.
615 PHP_METHOD(HttpMessage
, __construct
)
617 char *message
= NULL
;
619 getObject(http_message_object
, obj
);
622 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
623 if (obj
->message
= http_message_parse(message
, length
)) {
624 if (obj
->message
->parent
) {
625 obj
->parent
= http_message_object_from_msg(obj
->message
->parent
);
628 } else if (!obj
->message
) {
629 obj
->message
= http_message_new();
635 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
637 * Create an HttpMessage object from a string.
639 PHP_METHOD(HttpMessage
, fromString
)
643 http_message
*msg
= NULL
;
645 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
649 if (!(msg
= http_message_parse(string
, length
))) {
653 Z_TYPE_P(return_value
) = IS_OBJECT
;
654 return_value
->value
.obj
= http_message_object_from_msg(msg
);
658 /* {{{ proto string HttpMessage::getBody()
660 * Get the body of the parsed Message.
662 PHP_METHOD(HttpMessage
, getBody
)
667 getObject(http_message_object
, obj
);
668 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
673 /* {{{ proto array HttpMessage::getHeaders()
675 * Get Message Headers.
677 PHP_METHOD(HttpMessage
, getHeaders
)
683 getObject(http_message_object
, obj
);
685 Z_ARRVAL(headers
) = &obj
->message
->hdrs
;
686 array_init(return_value
);
687 array_copy(&headers
, return_value
);
692 /* {{{ proto void HttpMessage::setHeaders(array headers)
696 PHP_METHOD(HttpMessage
, setHeaders
)
698 zval
*new_headers
, old_headers
;
699 getObject(http_message_object
, obj
);
701 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
705 zend_hash_clean(&obj
->message
->hdrs
);
706 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
707 array_copy(new_headers
, &old_headers
);
711 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
713 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
715 PHP_METHOD(HttpMessage
, addHeaders
)
717 zval old_headers
, *new_headers
;
718 zend_bool append
= 0;
719 getObject(http_message_object
, obj
);
721 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
725 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
727 array_append(new_headers
, &old_headers
);
729 array_merge(new_headers
, &old_headers
);
734 /* {{{ proto long HttpMessage::getType()
736 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
738 PHP_METHOD(HttpMessage
, getType
)
743 getObject(http_message_object
, obj
);
744 RETURN_LONG(obj
->message
->type
);
749 /* {{{ proto void HttpMessage::setType(long type)
751 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
753 PHP_METHOD(HttpMessage
, setType
)
756 getObject(http_message_object
, obj
);
758 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
761 http_message_set_type(obj
->message
, type
);
765 /* {{{ proto long HttpMessage::getResponseCode()
767 * Get the Response Code of the Message.
769 PHP_METHOD(HttpMessage
, getResponseCode
)
774 getObject(http_message_object
, obj
);
776 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
777 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
781 RETURN_LONG(obj
->message
->info
.response
.code
);
786 /* {{{ proto bool HttpMessage::setResponseCode(long code)
788 * Set the response code of an HTTP Response Message.
789 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
790 * or if the response code is out of range (100-510).
792 PHP_METHOD(HttpMessage
, setResponseCode
)
795 getObject(http_message_object
, obj
);
797 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
798 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
802 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
805 if (code
< 100 || code
> 510) {
806 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid response code (100-510): %ld", code
);
810 obj
->message
->info
.response
.code
= code
;
815 /* {{{ proto string HttpMessage::getRequestMethod()
817 * Get the Request Method of the Message.
818 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
820 PHP_METHOD(HttpMessage
, getRequestMethod
)
825 getObject(http_message_object
, obj
);
827 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
828 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
832 RETURN_STRING(obj
->message
->info
.request
.method
, 1);
837 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
839 * Set the Request Method of the HTTP Message.
840 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
842 PHP_METHOD(HttpMessage
, setRequestMethod
)
846 getObject(http_message_object
, obj
);
848 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
849 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
853 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
856 if (method_len
< 1) {
857 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
860 if (SUCCESS
!= http_check_method(method
)) {
861 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Unkown request method: %s", method
);
865 STR_SET(obj
->message
->info
.request
.method
, estrndup(method
, method_len
));
870 /* {{{ proto string HttpMessage::getRequestUri()
872 * Get the Request URI of the Message.
874 PHP_METHOD(HttpMessage
, getRequestUri
)
879 getObject(http_message_object
, obj
);
881 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
882 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
886 RETURN_STRING(obj
->message
->info
.request
.URI
, 1);
891 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
893 * Set the Request URI of the HTTP Message.
894 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
895 * or if paramtere URI was empty.
897 PHP_METHOD(HttpMessage
, setRequestUri
)
901 getObject(http_message_object
, obj
);
903 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
904 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
907 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
911 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
915 STR_SET(obj
->message
->info
.request
.URI
, estrndup(URI
, URIlen
));
920 /* {{{ proto string HttpMessage::getHttpVersion()
922 * Get the HTTP Protocol Version of the Message.
924 PHP_METHOD(HttpMessage
, getHttpVersion
)
931 getObject(http_message_object
, obj
);
933 switch (obj
->message
->type
)
935 case HTTP_MSG_RESPONSE
:
936 version
= obj
->message
->info
.response
.http_version
;
939 case HTTP_MSG_REQUEST
:
940 version
= obj
->message
->info
.request
.http_version
;
947 sprintf(ver
, "%1.1f", version
);
948 RETURN_STRINGL(ver
, 3, 1);
953 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
955 * Set the HTTP Protocol version of the Message.
956 * Returns false if version is invalid (1.0 and 1.1).
958 PHP_METHOD(HttpMessage
, setHttpVersion
)
962 getObject(http_message_object
, obj
);
964 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
968 if (HTTP_MSG_TYPE(NONE
, obj
->message
)) {
969 http_error(E_WARNING
, HTTP_E_MSG
, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
973 convert_to_double(zv
);
974 sprintf(v
, "%1.1f", Z_DVAL_P(zv
));
975 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
976 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
980 if (HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
981 obj
->message
->info
.response
.http_version
= (float) Z_DVAL_P(zv
);
983 obj
->message
->info
.request
.http_version
= (float) Z_DVAL_P(zv
);
989 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
991 * Get parent Message.
993 PHP_METHOD(HttpMessage
, getParentMessage
)
998 getObject(http_message_object
, obj
);
1000 if (obj
->message
->parent
) {
1001 RETVAL_OBJVAL(obj
->parent
);
1009 /* {{{ proto bool HttpMessage::send()
1011 * Send the Message according to its type as Response or Request.
1013 PHP_METHOD(HttpMessage
, send
)
1015 getObject(http_message_object
, obj
);
1019 RETURN_SUCCESS(http_message_send(obj
->message
));
1023 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
1025 * Get the string representation of the Message.
1027 PHP_METHOD(HttpMessage
, toString
)
1032 zend_bool include_parent
= 1;
1033 getObject(http_message_object
, obj
);
1035 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1039 if (include_parent
) {
1040 http_message_serialize(obj
->message
, &string
, &length
);
1042 http_message_tostring(obj
->message
, &string
, &length
);
1044 RETURN_STRINGL(string
, length
, 0);
1051 #ifdef HTTP_HAVE_CURL
1052 /* {{{ HttpRequest */
1054 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
1056 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
1057 * and POST (including posting files) HTTP requests.
1059 PHP_METHOD(HttpRequest
, __construct
)
1064 getObject(http_request_object
, obj
);
1066 SET_EH_THROW_HTTP();
1067 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
1068 INIT_PARR(obj
, options
);
1069 INIT_PARR(obj
, responseInfo
);
1070 INIT_PARR(obj
, responseData
);
1071 INIT_PARR(obj
, postFields
);
1072 INIT_PARR(obj
, postFiles
);
1075 UPD_PROP(obj
, string
, url
, URL
);
1078 UPD_PROP(obj
, long, method
, meth
);
1085 /* {{{ proto void HttpRequest::__destruct()
1087 * Destroys the HttpRequest object.
1089 PHP_METHOD(HttpRequest
, __destruct
)
1091 getObject(http_request_object
, obj
);
1095 FREE_PARR(obj
, options
);
1096 FREE_PARR(obj
, responseInfo
);
1097 FREE_PARR(obj
, responseData
);
1098 FREE_PARR(obj
, postFields
);
1099 FREE_PARR(obj
, postFiles
);
1103 /* {{{ proto bool HttpRequest::setOptions(array options)
1105 * Set the request options to use. See http_get() for a full list of available options.
1107 PHP_METHOD(HttpRequest
, setOptions
)
1111 zval
*opts
, *old_opts
, **opt
;
1112 getObject(http_request_object
, obj
);
1114 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1118 old_opts
= GET_PROP(obj
, options
);
1120 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
1121 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
1123 if (!strcmp(key
, "headers")) {
1125 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1126 array_merge(*opt
, *headers
);
1129 } else if (!strcmp(key
, "cookies")) {
1131 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1132 array_merge(*opt
, *cookies
);
1137 add_assoc_zval(old_opts
, key
, *opt
);
1148 /* {{{ proto array HttpRequest::getOptions()
1150 * Get current set options.
1152 PHP_METHOD(HttpRequest
, getOptions
)
1158 getObject(http_request_object
, obj
);
1160 opts
= GET_PROP(obj
, options
);
1161 array_init(return_value
);
1162 array_copy(opts
, return_value
);
1167 /* {{{ proto void HttpRequest::unsetOptions()
1169 * Unset all options/headers/cookies.
1171 PHP_METHOD(HttpRequest
, unsetOptions
)
1173 getObject(http_request_object
, obj
);
1177 FREE_PARR(obj
, options
);
1178 INIT_PARR(obj
, options
);
1182 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1184 * Set additional SSL options.
1186 PHP_METHOD(HttpRequest
, setSslOptions
)
1188 zval
*opts
, *old_opts
, **ssl_options
;
1189 getObject(http_request_object
, obj
);
1191 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1195 old_opts
= GET_PROP(obj
, options
);
1197 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1198 array_merge(opts
, *ssl_options
);
1200 zval_add_ref(&opts
);
1201 add_assoc_zval(old_opts
, "ssl", opts
);
1208 /* {{{ proto array HttpRequest::getSslOtpions()
1210 * Get previously set SSL options.
1212 PHP_METHOD(HttpRequest
, getSslOptions
)
1217 zval
*opts
, **ssl_options
;
1218 getObject(http_request_object
, obj
);
1220 opts
= GET_PROP(obj
, options
);
1222 array_init(return_value
);
1224 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1225 array_copy(*ssl_options
, return_value
);
1231 /* {{{ proto void HttpRequest::unsetSslOptions()
1233 * Unset previously set SSL options.
1235 PHP_METHOD(HttpRequest
, unsetSslOptions
)
1238 getObject(http_request_object
, obj
);
1242 opts
= GET_PROP(obj
, options
);
1243 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
1247 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1249 * Add request header name/value pairs.
1251 PHP_METHOD(HttpRequest
, addHeaders
)
1253 zval
*opts
, **headers
, *new_headers
;
1254 getObject(http_request_object
, obj
);
1256 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
1260 opts
= GET_PROP(obj
, options
);
1262 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1263 array_merge(new_headers
, *headers
);
1265 zval_add_ref(&new_headers
);
1266 add_assoc_zval(opts
, "headers", new_headers
);
1273 /* {{{ proto array HttpRequest::getHeaders()
1275 * Get previously set request headers.
1277 PHP_METHOD(HttpRequest
, getHeaders
)
1282 zval
*opts
, **headers
;
1283 getObject(http_request_object
, obj
);
1285 opts
= GET_PROP(obj
, options
);
1287 array_init(return_value
);
1289 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1290 array_copy(*headers
, return_value
);
1296 /* {{{ proto void HttpRequest::unsetHeaders()
1298 * Unset previously set request headers.
1300 PHP_METHOD(HttpRequest
, unsetHeaders
)
1303 getObject(http_request_object
, obj
);
1307 opts
= GET_PROP(obj
, options
);
1308 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
1312 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1316 PHP_METHOD(HttpRequest
, addCookies
)
1318 zval
*opts
, **cookies
, *new_cookies
;
1319 getObject(http_request_object
, obj
);
1321 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
1325 opts
= GET_PROP(obj
, options
);
1327 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1328 array_merge(new_cookies
, *cookies
);
1330 zval_add_ref(&new_cookies
);
1331 add_assoc_zval(opts
, "cookies", new_cookies
);
1338 /* {{{ proto array HttpRequest::getCookies()
1340 * Get previously set cookies.
1342 PHP_METHOD(HttpRequest
, getCookies
)
1347 zval
*opts
, **cookies
;
1348 getObject(http_request_object
, obj
);
1350 opts
= GET_PROP(obj
, options
);
1352 array_init(return_value
);
1354 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1355 array_copy(*cookies
, return_value
);
1361 /* {{{ proto void HttpRequest::unsetCookies()
1364 PHP_METHOD(HttpRequest
, unsetCookies
)
1367 getObject(http_request_object
, obj
);
1371 opts
= GET_PROP(obj
, options
);
1372 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
1376 /* {{{ proto bool HttpRequest::setUrl(string url)
1378 * Set the request URL.
1380 PHP_METHOD(HttpRequest
, setUrl
)
1384 getObject(http_request_object
, obj
);
1386 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1390 UPD_PROP(obj
, string
, url
, URL
);
1395 /* {{{ proto string HttpRequest::getUrl()
1397 * Get the previously set request URL.
1399 PHP_METHOD(HttpRequest
, getUrl
)
1405 getObject(http_request_object
, obj
);
1407 URL
= GET_PROP(obj
, url
);
1408 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
1413 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1415 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1416 * <tt>HTTP_POST</tt> constants.
1418 PHP_METHOD(HttpRequest
, setMethod
)
1421 getObject(http_request_object
, obj
);
1423 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1427 UPD_PROP(obj
, long, method
, meth
);
1432 /* {{{ proto long HttpRequest::getMethod()
1434 * Get the previously set request method.
1436 PHP_METHOD(HttpRequest
, getMethod
)
1442 getObject(http_request_object
, obj
);
1444 meth
= GET_PROP(obj
, method
);
1445 RETURN_LONG(Z_LVAL_P(meth
));
1450 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1452 * Set the content type the post request should have.
1453 * Use this only if you know what you're doing.
1455 PHP_METHOD(HttpRequest
, setContentType
)
1459 getObject(http_request_object
, obj
);
1461 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1465 if (!strchr(ctype
, '/')) {
1466 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
1470 UPD_PROP(obj
, string
, contentType
, ctype
);
1475 /* {{{ proto string HttpRequest::getContentType()
1477 * Get the previously content type.
1479 PHP_METHOD(HttpRequest
, getContentType
)
1485 getObject(http_request_object
, obj
);
1487 ctype
= GET_PROP(obj
, contentType
);
1488 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
1493 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1495 * Set the URL query parameters to use.
1496 * Overwrites previously set query parameters.
1497 * Affects any request types.
1499 PHP_METHOD(HttpRequest
, setQueryData
)
1502 char *query_data
= NULL
;
1503 getObject(http_request_object
, obj
);
1505 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
1509 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1510 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1513 UPD_PROP(obj
, string
, queryData
, query_data
);
1518 convert_to_string(qdata
);
1519 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
1524 /* {{{ proto string HttpRequest::getQueryData()
1526 * Get the current query data in form of an urlencoded query string.
1528 PHP_METHOD(HttpRequest
, getQueryData
)
1534 getObject(http_request_object
, obj
);
1536 qdata
= GET_PROP(obj
, queryData
);
1537 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
1542 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1544 * Add parameters to the query parameter list.
1545 * Affects any request type.
1547 PHP_METHOD(HttpRequest
, addQueryData
)
1549 zval
*qdata
, *old_qdata
;
1550 char *query_data
= NULL
;
1551 getObject(http_request_object
, obj
);
1553 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1557 old_qdata
= GET_PROP(obj
, queryData
);
1559 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1563 UPD_PROP(obj
, string
, queryData
, query_data
);
1570 /* {{{ proto void HttpRequest::unsetQueryData()
1572 * Clean the query parameters.
1573 * Affects any request type.
1575 PHP_METHOD(HttpRequest
, unsetQueryData
)
1577 getObject(http_request_object
, obj
);
1581 UPD_PROP(obj
, string
, queryData
, "");
1585 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1587 * Adds POST data entries.
1588 * Affects only POST requests.
1590 PHP_METHOD(HttpRequest
, addPostFields
)
1592 zval
*post
, *post_data
;
1593 getObject(http_request_object
, obj
);
1595 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1599 post
= GET_PROP(obj
, postFields
);
1600 array_merge(post_data
, post
);
1606 /* {{{ proto bool HttpRequest::setPostFields(array post_data)
1608 * Set the POST data entries.
1609 * Overwrites previously set POST data.
1610 * Affects only POST requests.
1612 PHP_METHOD(HttpRequest
, setPostFields
)
1614 zval
*post
, *post_data
;
1615 getObject(http_request_object
, obj
);
1617 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1621 post
= GET_PROP(obj
, postFields
);
1622 zend_hash_clean(Z_ARRVAL_P(post
));
1623 array_copy(post_data
, post
);
1629 /* {{{ proto array HttpRequest::getPostFields()
1631 * Get previously set POST data.
1633 PHP_METHOD(HttpRequest
, getPostFields
)
1639 getObject(http_request_object
, obj
);
1641 post_data
= GET_PROP(obj
, postFields
);
1642 array_init(return_value
);
1643 array_copy(post_data
, return_value
);
1648 /* {{{ proto void HttpRequest::unsetPostFields()
1650 * Clean POST data entires.
1651 * Affects only POST requests.
1653 PHP_METHOD(HttpRequest
, unsetPostFields
)
1656 getObject(http_request_object
, obj
);
1660 post_data
= GET_PROP(obj
, postFields
);
1661 zend_hash_clean(Z_ARRVAL_P(post_data
));
1665 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1667 * Add a file to the POST request.
1668 * Affects only POST requests.
1670 PHP_METHOD(HttpRequest
, addPostFile
)
1672 zval
*files
, *entry
;
1673 char *name
, *file
, *type
= NULL
;
1674 int name_len
, file_len
, type_len
= 0;
1675 getObject(http_request_object
, obj
);
1677 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1682 if (!strchr(type
, '/')) {
1683 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1687 type
= "application/x-octetstream";
1688 type_len
= sizeof("application/x-octetstream") - 1;
1691 MAKE_STD_ZVAL(entry
);
1694 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1695 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1696 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1698 files
= GET_PROP(obj
, postFiles
);
1699 add_next_index_zval(files
, entry
);
1705 /* {{{ proto bool HttpRequest::setPostFiles(array post_files)
1707 * Set files to post.
1708 * Overwrites previously set post files.
1709 * Affects only POST requests.
1711 PHP_METHOD(HttpRequest
, setPostFiles
)
1713 zval
*files
, *pFiles
;
1714 getObject(http_request_object
, obj
);
1716 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &files
)) {
1720 pFiles
= GET_PROP(obj
, postFiles
);
1721 zend_hash_clean(Z_ARRVAL_P(pFiles
));
1722 array_copy(files
, pFiles
);
1728 /* {{{ proto array HttpRequest::getPostFiles()
1730 * Get all previously added POST files.
1732 PHP_METHOD(HttpRequest
, getPostFiles
)
1738 getObject(http_request_object
, obj
);
1740 files
= GET_PROP(obj
, postFiles
);
1742 array_init(return_value
);
1743 array_copy(files
, return_value
);
1748 /* {{{ proto void HttpRequest::unsetPostFiles()
1750 * Unset the POST files list.
1751 * Affects only POST requests.
1753 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1756 getObject(http_request_object
, obj
);
1760 files
= GET_PROP(obj
, postFiles
);
1761 zend_hash_clean(Z_ARRVAL_P(files
));
1765 /* {{{ proto bool HttpRequest::SetPutFile(string file)
1768 * Affects only PUT requests.
1770 PHP_METHOD(HttpRequest
, setPutFile
)
1774 getObject(http_request_object
, obj
);
1776 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &file_len
)) {
1780 UPD_PROP(obj
, string
, putFile
, file
);
1785 /* {{{ proto string HttpRequest::getPutFile()
1787 * Get previously set put file.
1789 PHP_METHOD(HttpRequest
, getPutFile
)
1795 getObject(http_request_object
, obj
);
1797 putfile
= GET_PROP(obj
, putFile
);
1798 RETVAL_STRINGL(Z_STRVAL_P(putfile
), Z_STRLEN_P(putfile
), 1);
1803 /* {{{ proto void HttpRequest::unsetPutFile()
1805 * Unset file to put.
1806 * Affects only PUT requests.
1808 PHP_METHOD(HttpRequest
, unsetPutFile
)
1810 getObject(http_request_object
, obj
);
1814 UPD_PROP(obj
, string
, putFile
, "");
1818 /* {{{ proto array HttpRequest::getResponseData()
1820 * Get all response data after the request has been sent.
1822 PHP_METHOD(HttpRequest
, getResponseData
)
1828 getObject(http_request_object
, obj
);
1830 data
= GET_PROP(obj
, responseData
);
1831 array_init(return_value
);
1832 array_copy(data
, return_value
);
1837 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1839 * Get response header(s) after the request has been sent.
1841 PHP_METHOD(HttpRequest
, getResponseHeader
)
1844 zval
*data
, **headers
, **header
;
1845 char *header_name
= NULL
;
1847 getObject(http_response_object
, obj
);
1849 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1853 data
= GET_PROP(obj
, responseData
);
1854 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1858 if (!header_len
|| !header_name
) {
1859 array_init(return_value
);
1860 array_copy(*headers
, return_value
);
1861 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1862 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1870 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1872 * Get response cookie(s) after the request has been sent.
1874 PHP_METHOD(HttpRequest
, getResponseCookie
)
1877 zval
*data
, **headers
;
1878 char *cookie_name
= NULL
;
1880 getObject(http_request_object
, obj
);
1882 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1886 array_init(return_value
);
1888 data
= GET_PROP(obj
, responseData
);
1889 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1892 zval
**header
= NULL
;
1894 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1895 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1896 /* several cookies? */
1897 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1900 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1902 MAKE_STD_ZVAL(cookie_hash
);
1903 array_init(cookie_hash
);
1905 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1907 add_next_index_zval(return_value
, cookie_hash
);
1911 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1912 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1913 add_next_index_zval(return_value
, cookie_hash
);
1914 return; /* <<< FOUND >>> */
1916 zval_dtor(cookie_hash
);
1921 zval_dtor(cookie_hash
);
1927 MAKE_STD_ZVAL(cookie_hash
);
1928 array_init(cookie_hash
);
1930 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), 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
);
1940 zval_dtor(cookie_hash
);
1945 zval_dtor(cookie_hash
);
1959 /* {{{ proto string HttpRequest::getResponseBody()
1961 * Get the response body after the request has been sent.
1963 PHP_METHOD(HttpRequest
, getResponseBody
)
1969 getObject(http_request_object
, obj
);
1971 data
= GET_PROP(obj
, responseData
);
1972 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1973 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
1981 /* {{{ proto int HttpRequest::getResponseCode()
1983 * Get the response code after the request has been sent.
1985 PHP_METHOD(HttpRequest
, getResponseCode
)
1991 getObject(http_request_object
, obj
);
1993 code
= GET_PROP(obj
, responseCode
);
1994 RETURN_LONG(Z_LVAL_P(code
));
1999 /* {{{ proto array HttpRequest::getResponseInfo([string name])
2001 * Get response info after the request has been sent.
2002 * See http_get() for a full list of returned info.
2004 PHP_METHOD(HttpRequest
, getResponseInfo
)
2007 zval
*info
, **infop
;
2008 char *info_name
= NULL
;
2010 getObject(http_request_object
, obj
);
2012 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
2016 info
= GET_PROP(obj
, responseInfo
);
2018 if (info_len
&& info_name
) {
2019 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
2020 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
2022 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Could not find response info named %s", info_name
);
2026 array_init(return_value
);
2027 array_copy(info
, return_value
);
2033 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
2035 * Get the full response as HttpMessage object.
2037 PHP_METHOD(HttpRequest
, getResponseMessage
)
2043 getObject(http_request_object
, obj
);
2045 message
= GET_PROP(obj
, responseMessage
);
2046 if (Z_TYPE_P(message
) == IS_OBJECT
) {
2047 RETVAL_OBJECT(message
);
2055 /* {{{ proto bool HttpRequest::send()
2057 * Send the HTTP request.
2062 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
2063 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
2064 * $r->addQueryData(array('category' => 3));
2067 * if ($r->getResponseCode() == 200) {
2068 * file_put_contents('local.rss', $r->getResponseBody());
2070 * } catch (HttpException $ex) {
2079 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
2080 * $r->setOptions(array('cookies' => array('lang' => 'de')));
2081 * $r->addpostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
2082 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
2084 * echo $r->getResponseBody();
2089 PHP_METHOD(HttpRequest
, send
)
2091 STATUS status
= FAILURE
;
2092 http_request_body body
= {0};
2093 getObject(http_request_object
, obj
);
2097 SET_EH_THROW_HTTP();
2100 http_error(E_WARNING
, HTTP_E_CURL
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
2105 if (SUCCESS
== (status
= http_request_object_requesthandler(obj
, getThis(), &body
))) {
2106 status
= http_request_exec(obj
->ch
, NULL
);
2108 http_request_body_dtor(&body
);
2110 /* final data handling */
2111 if (SUCCESS
== status
) {
2112 status
= http_request_object_responsehandler(obj
, getThis());
2116 RETURN_SUCCESS(status
);
2120 /* {{{ HttpRequestPool */
2122 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
2124 * Instantiate a new HttpRequestPool object. An HttpRequestPool is
2125 * able to send several HttpRequests in parallel.
2130 * $urls = array('www.php.net', 'pecl.php.net', 'pear.php.net')
2131 * $pool = new HttpRequestPool;
2132 * foreach ($urls as $url) {
2133 * $req[$url] = new HttpRequest("http://$url", HTTP_HEAD);
2134 * $pool->attach($req[$url]);
2137 * foreach ($urls as $url) {
2138 * printf("%s (%s) is %s\n",
2139 * $url, $req[$url]->getResponseInfo('effective_url'),
2140 * $r->getResponseCode() == 200 ? 'alive' : 'not alive'
2146 PHP_METHOD(HttpRequestPool
, __construct
)
2148 int argc
= ZEND_NUM_ARGS();
2149 zval
***argv
= safe_emalloc(argc
, sizeof(zval
*), 0);
2150 getObject(http_requestpool_object
, obj
);
2152 if (SUCCESS
== zend_get_parameters_array_ex(argc
, argv
)) {
2155 for (i
= 0; i
< argc
; ++i
) {
2156 if (Z_TYPE_PP(argv
[i
]) == IS_OBJECT
&& instanceof_function(Z_OBJCE_PP(argv
[i
]), http_request_object_ce TSRMLS_CC
)) {
2157 http_request_pool_attach(&obj
->pool
, *(argv
[i
]));
2165 /* {{{ proto void HttpRequestPool::__destruct()
2167 * Clean up HttpRequestPool object.
2169 PHP_METHOD(HttpRequestPool
, __destruct
)
2171 getObject(http_requestpool_object
, obj
);
2175 http_request_pool_detach_all(&obj
->pool
);
2179 /* {{{ proto void HttpRequestPool::reset()
2181 * Detach all attached HttpRequest objects.
2183 PHP_METHOD(HttpRequestPool
, reset
)
2185 getObject(http_requestpool_object
, obj
);
2189 http_request_pool_detach_all(&obj
->pool
);
2192 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
2194 * Attach an HttpRequest object to this HttpRequestPool.
2195 * NOTE: set all options prior attaching!
2197 PHP_METHOD(HttpRequestPool
, attach
)
2200 STATUS status
= FAILURE
;
2201 getObject(http_requestpool_object
, obj
);
2203 SET_EH_THROW_HTTP();
2204 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
2205 status
= http_request_pool_attach(&obj
->pool
, request
);
2208 RETURN_SUCCESS(status
);
2212 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
2214 * Detach an HttpRequest object from this HttpRequestPool.
2216 PHP_METHOD(HttpRequestPool
, detach
)
2219 STATUS status
= FAILURE
;
2220 getObject(http_requestpool_object
, obj
);
2222 SET_EH_THROW_HTTP();
2223 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
2224 status
= http_request_pool_detach(&obj
->pool
, request
);
2227 RETURN_SUCCESS(status
);
2231 /* {{{ proto bool HttpRequestPool::send()
2233 * Send all attached HttpRequest objects in parallel.
2235 PHP_METHOD(HttpRequestPool
, send
)
2238 getObject(http_requestpool_object
, obj
);
2242 SET_EH_THROW_HTTP();
2243 status
= http_request_pool_send(&obj
->pool
);
2246 RETURN_SUCCESS(status
);
2250 /* {{{ proto protected bool HttpRequestPool::socketSend()
2255 * while ($pool->socketSend()) {
2256 * do_something_else();
2257 * if (!$pool->socketSelect()) {
2258 * die('Socket error');
2261 * $pool->socketRead();
2265 PHP_METHOD(HttpRequestPool
, socketSend
)
2267 getObject(http_requestpool_object
, obj
);
2271 RETURN_BOOL(0 < http_request_pool_perform(&obj
->pool
));
2275 /* {{{ proto protected bool HttpRequestPool::socketSelect()
2277 * See HttpRequestPool::socketSend().
2279 PHP_METHOD(HttpRequestPool
, socketSelect
)
2281 getObject(http_requestpool_object
, obj
);
2285 RETURN_SUCCESS(http_request_pool_select(&obj
->pool
));
2289 /* {{{ proto protected void HttpRequestPool::socketRead()
2291 * See HttpRequestPool::socketSend().
2293 PHP_METHOD(HttpRequestPool
, socketRead
)
2295 getObject(http_requestpool_object
, obj
);
2299 zend_llist_apply(&obj
->pool
.handles
, (llist_apply_func_t
) http_request_pool_responsehandler TSRMLS_CC
);
2306 #endif /* HTTP_HAVE_CURL */
2308 #endif /* ZEND_ENGINE_2 */
2315 * vim600: noet sw=4 ts=4 fdm=marker
2316 * vim<600: noet sw=4 ts=4