2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
23 #include "php_streams.h"
26 #include "php_http_std_defs.h"
27 #include "php_http_api.h"
28 #include "php_http_cache_api.h"
29 #include "php_http_request_api.h"
30 #include "php_http_date_api.h"
31 #include "php_http_headers_api.h"
32 #include "php_http_message_api.h"
33 #include "php_http_send_api.h"
34 #include "php_http_url_api.h"
36 #include "php_http_message_object.h"
37 #include "php_http_response_object.h"
38 #include "php_http_request_object.h"
39 #include "php_http_exception_object.h"
45 ZEND_EXTERN_MODULE_GLOBALS(http
)
47 /* {{{ HttpResponse */
49 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
51 * Instantiates a new HttpResponse object, which can be used to send
52 * any data/resource/file to an HTTP client with caching and multiple
53 * ranges/resuming support.
55 * NOTE: GZIPping is not implemented yet.
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 bool HttpResponse::setCache(bool cache)
73 * Whether it sould be attempted to cache the entitity.
74 * This will result in necessary caching headers and checks of clients
75 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
76 * matches a "304 Not Modified" status code will be issued.
78 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
79 * to something more appropriate than "no-cache"!
81 PHP_METHOD(HttpResponse
, setCache
)
83 zend_bool do_cache
= 0;
84 getObject(http_response_object
, obj
);
86 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
90 UPD_PROP(obj
, long, cache
, do_cache
);
95 /* {{{ proto bool HttpResponse::getCache()
97 * Get current caching setting.
99 PHP_METHOD(HttpResponse
, getCache
)
104 zval
*do_cache
= NULL
;
105 getObject(http_response_object
, obj
);
107 do_cache
= GET_PROP(obj
, cache
);
108 RETURN_BOOL(Z_LVAL_P(do_cache
));
113 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
115 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
117 PHP_METHOD(HttpResponse
, setGzip
)
119 zend_bool do_gzip
= 0;
120 getObject(http_response_object
, obj
);
122 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
126 UPD_PROP(obj
, long, gzip
, do_gzip
);
131 /* {{{ proto bool HttpResponse::getGzip()
133 * Get current gzipping setting.
135 PHP_METHOD(HttpResponse
, getGzip
)
141 getObject(http_response_object
, obj
);
143 do_gzip
= GET_PROP(obj
, gzip
);
144 RETURN_BOOL(Z_LVAL_P(do_gzip
));
149 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
151 * Set a custom cache-control header, usually being "private" or "public"; if
152 * $raw is set to true the header will be sent as-is.
154 PHP_METHOD(HttpResponse
, setCacheControl
)
159 getObject(http_response_object
, obj
);
161 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
165 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
166 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
170 UPD_PROP(obj
, long, raw_cache_header
, raw
);
171 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
176 /* {{{ proto string HttpResponse::getCacheControl()
178 * Get current Cache-Control header setting.
180 PHP_METHOD(HttpResponse
, getCacheControl
)
186 getObject(http_response_object
, obj
);
188 ccontrol
= GET_PROP(obj
, cacheControl
);
189 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
194 /* {{{ proto bool HttpResponse::setContentType(string content_type)
196 * Set the content-type of the sent entity.
198 PHP_METHOD(HttpResponse
, setContentType
)
202 getObject(http_response_object
, obj
);
204 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
208 if (!strchr(ctype
, '/')) {
209 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
213 UPD_PROP(obj
, string
, contentType
, ctype
);
219 /* {{{ proto string HttpResponse::getContentType()
221 * Get current Content-Type header setting.
223 PHP_METHOD(HttpResponse
, getContentType
)
229 getObject(http_response_object
, obj
);
231 ctype
= GET_PROP(obj
, contentType
);
232 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
237 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
239 * Set the Content-Disposition of the sent entity. This setting aims to suggest
240 * the receiveing user agent how to handle the sent entity; usually the client
241 * will show the user a "Save As..." popup.
243 PHP_METHOD(HttpResponse
, setContentDisposition
)
247 zend_bool is_inline
= 0;
248 getObject(http_response_object
, obj
);
250 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
254 UPD_PROP(obj
, string
, dispoFile
, file
);
255 UPD_PROP(obj
, long, dispoInline
, is_inline
);
260 /* {{{ proto array HttpResponse::getContentDisposition()
262 * Get current Content-Disposition setting.
263 * Will return an associative array like:
266 * 'filename' => 'foo.bar',
271 PHP_METHOD(HttpResponse
, getContentDisposition
)
276 zval
*file
, *is_inline
;
277 getObject(http_response_object
, obj
);
279 file
= GET_PROP(obj
, dispoFile
);
280 is_inline
= GET_PROP(obj
, dispoInline
);
282 array_init(return_value
);
283 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
284 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
289 /* {{{ proto bool HttpResponse::setETag(string etag)
291 * Set a custom ETag. Use this only if you know what you're doing.
293 PHP_METHOD(HttpResponse
, setETag
)
297 getObject(http_response_object
, obj
);
299 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
303 UPD_PROP(obj
, string
, eTag
, etag
);
308 /* {{{ proto string HttpResponse::getETag()
310 * Get the previously set custom ETag.
312 PHP_METHOD(HttpResponse
, getETag
)
318 getObject(http_response_object
, obj
);
320 etag
= GET_PROP(obj
, eTag
);
321 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
326 /* {{{ proto void HttpResponse::setThrottleDelay(double seconds)
329 PHP_METHOD(HttpResponse
, setThrottleDelay
)
333 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d", &seconds
)) {
334 getObject(http_response_object
, obj
);
336 UPD_PROP(obj
, double, throttleDelay
, seconds
);
341 /* {{{ proto double HttpResponse::getThrottleDelay()
344 PHP_METHOD(HttpResponse
, getThrottleDelay
)
350 getObject(http_response_object
, obj
);
352 seconds
= GET_PROP(obj
, throttleDelay
);
353 RETURN_DOUBLE(Z_DVAL_P(seconds
));
358 /* {{{ proto void HttpResponse::setSendBuffersize(long bytes)
361 PHP_METHOD(HttpResponse
, setSendBuffersize
)
365 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &bytes
)) {
366 getObject(http_response_object
, obj
);
368 UPD_PROP(obj
, long, sendBuffersize
, bytes
);
373 /* {{{ proto long HttpResponse::getSendBuffersize()
376 PHP_METHOD(HttpResponse
, getSendBuffersize
)
382 getObject(http_response_object
, obj
);
384 bytes
= GET_PROP(obj
, sendBuffersize
);
385 RETURN_LONG(Z_LVAL_P(bytes
));
390 /* {{{ proto bool HttpResponse::setData(string data)
392 * Set the data to be sent.
394 PHP_METHOD(HttpResponse
, setData
)
397 getObject(http_response_object
, obj
);
399 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &the_data
)) {
403 convert_to_string_ex(&the_data
);
404 SET_PROP(obj
, data
, the_data
);
405 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_data
, SEND_DATA
));
406 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
411 /* {{{ proto string HttpResponse::getData()
413 * Get the previously set data to be sent.
415 PHP_METHOD(HttpResponse
, getData
)
421 getObject(http_response_object
, obj
);
423 the_data
= GET_PROP(obj
, data
);
424 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
429 /* {{{ proto bool HttpResponse::setStream(resource stream)
431 * Set the resource to be sent.
433 PHP_METHOD(HttpResponse
, setStream
)
436 php_stream
*the_real_stream
;
437 getObject(http_response_object
, obj
);
439 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
443 php_stream_from_zval(the_real_stream
, &the_stream
);
445 SET_PROP(obj
, stream
, the_stream
);
446 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_real_stream
, SEND_RSRC
));
447 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
452 /* {{{ proto resource HttpResponse::getStream()
454 * Get the previously set resource to be sent.
456 PHP_METHOD(HttpResponse
, getStream
)
462 getObject(http_response_object
, obj
);
464 the_stream
= GET_PROP(obj
, stream
);
465 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
470 /* {{{ proto bool HttpResponse::setFile(string file)
472 * Set the file to be sent.
474 PHP_METHOD(HttpResponse
, setFile
)
477 getObject(http_response_object
, obj
);
479 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
483 convert_to_string_ex(&the_file
);
485 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
486 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_file
, -1));
487 UPD_PROP(obj
, long, send_mode
, -1);
492 /* {{{ proto string HttpResponse::getFile()
494 * Get the previously set file to be sent.
496 PHP_METHOD(HttpResponse
, getFile
)
502 getObject(http_response_object
, obj
);
504 the_file
= GET_PROP(obj
, file
);
505 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
510 /* {{{ proto bool HttpResponse::send([bool clean_ob = true])
512 * Finally send the entity.
517 * $r = new HttpResponse(true);
518 * $r->setFile('../hidden/contract.pdf');
519 * $r->setContentType('application/pdf');
525 PHP_METHOD(HttpResponse
, send
)
527 zend_bool clean_ob
= 1;
528 zval
*do_cache
, *do_gzip
;
529 getObject(http_response_object
, obj
);
531 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &clean_ob
)) {
535 do_cache
= GET_PROP(obj
, cache
);
536 do_gzip
= GET_PROP(obj
, gzip
);
539 /* interrupt on-the-fly etag generation */
540 HTTP_G(etag
).started
= 0;
541 /* discard previous output buffers */
542 php_end_ob_buffers(0 TSRMLS_CC
);
546 if (Z_LVAL_P(do_gzip
)) {
547 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC
);
551 if (Z_LVAL_P(do_cache
)) {
554 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
556 etag
= GET_PROP(obj
, eTag
);
557 lmod
= GET_PROP(obj
, lastModified
);
558 cctrl
= GET_PROP(obj
, cacheControl
);
559 ccraw
= GET_PROP(obj
, raw_cache_header
);
561 if (Z_LVAL_P(ccraw
)) {
562 cc_hdr
= Z_STRVAL_P(cctrl
);
563 cc_len
= Z_STRLEN_P(cctrl
);
565 char cc_header
[42] = {0};
566 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
568 cc_len
= Z_STRLEN_P(cctrl
) + lenof(", must-revalidate, max-age=0");
571 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_hdr
, cc_len
);
572 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_hdr
, cc_len
);
577 zval
*ctype
= GET_PROP(obj
, contentType
);
578 if (Z_STRLEN_P(ctype
)) {
579 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
581 http_send_content_type("application/x-octetstream", lenof("application/x-octetstream"));
585 /* content disposition */
587 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
588 if (Z_STRLEN_P(dispo_file
)) {
589 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
590 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
596 zval
*send_buffersize
, *throttle_delay
;
597 send_buffersize
= GET_PROP(obj
, sendBuffersize
);
598 throttle_delay
= GET_PROP(obj
, throttleDelay
);
599 HTTP_G(send
).buffer_size
= Z_LVAL_P(send_buffersize
);
600 HTTP_G(send
).throttle_delay
= Z_DVAL_P(throttle_delay
);
605 zval
*send_mode
= GET_PROP(obj
, send_mode
);
606 switch (Z_LVAL_P(send_mode
))
610 zval
*zdata
= GET_PROP(obj
, data
);
611 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
616 php_stream
*the_real_stream
;
617 zval
*the_stream
= GET_PROP(obj
, stream
);
618 php_stream_from_zval(the_real_stream
, &the_stream
);
619 RETURN_SUCCESS(http_send_stream(the_real_stream
));
624 zval
*zfile
= GET_PROP(obj
, file
);
625 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
633 /* {{{ HttpMessage */
635 /* {{{ proto void HttpMessage::__construct([string message])
637 * Instantiate a new HttpMessage object.
639 PHP_METHOD(HttpMessage
, __construct
)
641 char *message
= NULL
;
643 getObject(http_message_object
, obj
);
646 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
647 if (obj
->message
= http_message_parse(message
, length
)) {
648 if (obj
->message
->parent
) {
649 obj
->parent
= http_message_object_from_msg(obj
->message
->parent
);
652 } else if (!obj
->message
) {
653 obj
->message
= http_message_new();
659 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
661 * Create an HttpMessage object from a string.
663 PHP_METHOD(HttpMessage
, fromString
)
667 http_message
*msg
= NULL
;
669 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
673 if (!(msg
= http_message_parse(string
, length
))) {
677 Z_TYPE_P(return_value
) = IS_OBJECT
;
678 return_value
->value
.obj
= http_message_object_from_msg(msg
);
682 /* {{{ proto string HttpMessage::getBody()
684 * Get the body of the parsed Message.
686 PHP_METHOD(HttpMessage
, getBody
)
691 getObject(http_message_object
, obj
);
692 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
697 /* {{{ proto array HttpMessage::getHeaders()
699 * Get Message Headers.
701 PHP_METHOD(HttpMessage
, getHeaders
)
707 getObject(http_message_object
, obj
);
709 Z_ARRVAL(headers
) = &obj
->message
->hdrs
;
710 array_init(return_value
);
711 array_copy(&headers
, return_value
);
716 /* {{{ proto void HttpMessage::setHeaders(array headers)
720 PHP_METHOD(HttpMessage
, setHeaders
)
722 zval
*new_headers
, old_headers
;
723 getObject(http_message_object
, obj
);
725 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
729 zend_hash_clean(&obj
->message
->hdrs
);
730 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
731 array_copy(new_headers
, &old_headers
);
735 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
737 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
739 PHP_METHOD(HttpMessage
, addHeaders
)
741 zval old_headers
, *new_headers
;
742 zend_bool append
= 0;
743 getObject(http_message_object
, obj
);
745 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
749 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
751 array_append(new_headers
, &old_headers
);
753 array_merge(new_headers
, &old_headers
);
758 /* {{{ proto long HttpMessage::getType()
760 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
762 PHP_METHOD(HttpMessage
, getType
)
767 getObject(http_message_object
, obj
);
768 RETURN_LONG(obj
->message
->type
);
773 /* {{{ proto void HttpMessage::setType(long type)
775 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
777 PHP_METHOD(HttpMessage
, setType
)
780 getObject(http_message_object
, obj
);
782 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
785 http_message_set_type(obj
->message
, type
);
789 /* {{{ proto long HttpMessage::getResponseCode()
791 * Get the Response Code of the Message.
793 PHP_METHOD(HttpMessage
, getResponseCode
)
798 getObject(http_message_object
, obj
);
800 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
801 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
805 RETURN_LONG(obj
->message
->info
.response
.code
);
810 /* {{{ proto bool HttpMessage::setResponseCode(long code)
812 * Set the response code of an HTTP Response Message.
813 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
814 * or if the response code is out of range (100-510).
816 PHP_METHOD(HttpMessage
, setResponseCode
)
819 getObject(http_message_object
, obj
);
821 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
822 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
826 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
829 if (code
< 100 || code
> 510) {
830 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid response code (100-510): %ld", code
);
834 obj
->message
->info
.response
.code
= code
;
839 /* {{{ proto string HttpMessage::getRequestMethod()
841 * Get the Request Method of the Message.
842 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
844 PHP_METHOD(HttpMessage
, getRequestMethod
)
849 getObject(http_message_object
, obj
);
851 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
852 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
856 RETURN_STRING(obj
->message
->info
.request
.method
, 1);
861 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
863 * Set the Request Method of the HTTP Message.
864 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
866 PHP_METHOD(HttpMessage
, setRequestMethod
)
870 getObject(http_message_object
, obj
);
872 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
873 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
877 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
880 if (method_len
< 1) {
881 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
884 if (SUCCESS
!= http_check_method(method
)) {
885 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Unkown request method: %s", method
);
889 STR_SET(obj
->message
->info
.request
.method
, estrndup(method
, method_len
));
894 /* {{{ proto string HttpMessage::getRequestUri()
896 * Get the Request URI of the Message.
898 PHP_METHOD(HttpMessage
, getRequestUri
)
903 getObject(http_message_object
, obj
);
905 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
906 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
910 RETURN_STRING(obj
->message
->info
.request
.URI
, 1);
915 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
917 * Set the Request URI of the HTTP Message.
918 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
919 * or if paramtere URI was empty.
921 PHP_METHOD(HttpMessage
, setRequestUri
)
925 getObject(http_message_object
, obj
);
927 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
928 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
931 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
935 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
939 STR_SET(obj
->message
->info
.request
.URI
, estrndup(URI
, URIlen
));
944 /* {{{ proto string HttpMessage::getHttpVersion()
946 * Get the HTTP Protocol Version of the Message.
948 PHP_METHOD(HttpMessage
, getHttpVersion
)
955 getObject(http_message_object
, obj
);
957 switch (obj
->message
->type
)
959 case HTTP_MSG_RESPONSE
:
960 version
= obj
->message
->info
.response
.http_version
;
963 case HTTP_MSG_REQUEST
:
964 version
= obj
->message
->info
.request
.http_version
;
971 sprintf(ver
, "%1.1f", version
);
972 RETURN_STRINGL(ver
, 3, 1);
977 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
979 * Set the HTTP Protocol version of the Message.
980 * Returns false if version is invalid (1.0 and 1.1).
982 PHP_METHOD(HttpMessage
, setHttpVersion
)
986 getObject(http_message_object
, obj
);
988 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
992 if (HTTP_MSG_TYPE(NONE
, obj
->message
)) {
993 http_error(E_WARNING
, HTTP_E_MSG
, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
997 convert_to_double(zv
);
998 sprintf(v
, "%1.1f", Z_DVAL_P(zv
));
999 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
1000 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
1004 if (HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
1005 obj
->message
->info
.response
.http_version
= (float) Z_DVAL_P(zv
);
1007 obj
->message
->info
.request
.http_version
= (float) Z_DVAL_P(zv
);
1013 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1015 * Get parent Message.
1017 PHP_METHOD(HttpMessage
, getParentMessage
)
1022 getObject(http_message_object
, obj
);
1024 if (obj
->message
->parent
) {
1025 RETVAL_OBJVAL(obj
->parent
);
1033 /* {{{ proto bool HttpMessage::send()
1035 * Send the Message according to its type as Response or Request.
1037 PHP_METHOD(HttpMessage
, send
)
1039 getObject(http_message_object
, obj
);
1043 RETURN_SUCCESS(http_message_send(obj
->message
));
1047 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
1049 * Get the string representation of the Message.
1051 PHP_METHOD(HttpMessage
, toString
)
1056 zend_bool include_parent
= 1;
1057 getObject(http_message_object
, obj
);
1059 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1063 if (include_parent
) {
1064 http_message_serialize(obj
->message
, &string
, &length
);
1066 http_message_tostring(obj
->message
, &string
, &length
);
1068 RETURN_STRINGL(string
, length
, 0);
1075 #ifdef HTTP_HAVE_CURL
1076 /* {{{ HttpRequest */
1078 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
1080 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
1081 * and POST (including posting files) HTTP requests.
1083 PHP_METHOD(HttpRequest
, __construct
)
1088 getObject(http_request_object
, obj
);
1090 SET_EH_THROW_HTTP();
1091 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
1092 INIT_PARR(obj
, options
);
1093 INIT_PARR(obj
, responseInfo
);
1094 INIT_PARR(obj
, responseData
);
1095 INIT_PARR(obj
, postFields
);
1096 INIT_PARR(obj
, postFiles
);
1099 UPD_PROP(obj
, string
, url
, URL
);
1102 UPD_PROP(obj
, long, method
, meth
);
1109 /* {{{ proto void HttpRequest::__destruct()
1111 * Destroys the HttpRequest object.
1113 PHP_METHOD(HttpRequest
, __destruct
)
1115 getObject(http_request_object
, obj
);
1119 FREE_PARR(obj
, options
);
1120 FREE_PARR(obj
, responseInfo
);
1121 FREE_PARR(obj
, responseData
);
1122 FREE_PARR(obj
, postFields
);
1123 FREE_PARR(obj
, postFiles
);
1127 /* {{{ proto bool HttpRequest::setOptions(array options)
1129 * Set the request options to use. See http_get() for a full list of available options.
1131 PHP_METHOD(HttpRequest
, setOptions
)
1135 zval
*opts
, *old_opts
, **opt
;
1136 getObject(http_request_object
, obj
);
1138 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1142 old_opts
= GET_PROP(obj
, options
);
1144 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
1145 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
1147 if (!strcmp(key
, "headers")) {
1149 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1150 array_merge(*opt
, *headers
);
1153 } else if (!strcmp(key
, "cookies")) {
1155 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1156 array_merge(*opt
, *cookies
);
1161 add_assoc_zval(old_opts
, key
, *opt
);
1172 /* {{{ proto array HttpRequest::getOptions()
1174 * Get current set options.
1176 PHP_METHOD(HttpRequest
, getOptions
)
1182 getObject(http_request_object
, obj
);
1184 opts
= GET_PROP(obj
, options
);
1185 array_init(return_value
);
1186 array_copy(opts
, return_value
);
1191 /* {{{ proto void HttpRequest::unsetOptions()
1193 * Unset all options/headers/cookies.
1195 PHP_METHOD(HttpRequest
, unsetOptions
)
1197 getObject(http_request_object
, obj
);
1201 FREE_PARR(obj
, options
);
1202 INIT_PARR(obj
, options
);
1206 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1208 * Set additional SSL options.
1210 PHP_METHOD(HttpRequest
, setSslOptions
)
1212 zval
*opts
, *old_opts
, **ssl_options
;
1213 getObject(http_request_object
, obj
);
1215 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1219 old_opts
= GET_PROP(obj
, options
);
1221 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1222 array_merge(opts
, *ssl_options
);
1224 zval_add_ref(&opts
);
1225 add_assoc_zval(old_opts
, "ssl", opts
);
1232 /* {{{ proto array HttpRequest::getSslOtpions()
1234 * Get previously set SSL options.
1236 PHP_METHOD(HttpRequest
, getSslOptions
)
1241 zval
*opts
, **ssl_options
;
1242 getObject(http_request_object
, obj
);
1244 opts
= GET_PROP(obj
, options
);
1246 array_init(return_value
);
1248 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1249 array_copy(*ssl_options
, return_value
);
1255 /* {{{ proto void HttpRequest::unsetSslOptions()
1257 * Unset previously set SSL options.
1259 PHP_METHOD(HttpRequest
, unsetSslOptions
)
1262 getObject(http_request_object
, obj
);
1266 opts
= GET_PROP(obj
, options
);
1267 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
1271 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1273 * Add request header name/value pairs.
1275 PHP_METHOD(HttpRequest
, addHeaders
)
1277 zval
*opts
, **headers
, *new_headers
;
1278 getObject(http_request_object
, obj
);
1280 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
1284 opts
= GET_PROP(obj
, options
);
1286 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1287 array_merge(new_headers
, *headers
);
1289 zval_add_ref(&new_headers
);
1290 add_assoc_zval(opts
, "headers", new_headers
);
1297 /* {{{ proto array HttpRequest::getHeaders()
1299 * Get previously set request headers.
1301 PHP_METHOD(HttpRequest
, getHeaders
)
1306 zval
*opts
, **headers
;
1307 getObject(http_request_object
, obj
);
1309 opts
= GET_PROP(obj
, options
);
1311 array_init(return_value
);
1313 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1314 array_copy(*headers
, return_value
);
1320 /* {{{ proto void HttpRequest::unsetHeaders()
1322 * Unset previously set request headers.
1324 PHP_METHOD(HttpRequest
, unsetHeaders
)
1327 getObject(http_request_object
, obj
);
1331 opts
= GET_PROP(obj
, options
);
1332 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
1336 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1340 PHP_METHOD(HttpRequest
, addCookies
)
1342 zval
*opts
, **cookies
, *new_cookies
;
1343 getObject(http_request_object
, obj
);
1345 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
1349 opts
= GET_PROP(obj
, options
);
1351 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1352 array_merge(new_cookies
, *cookies
);
1354 zval_add_ref(&new_cookies
);
1355 add_assoc_zval(opts
, "cookies", new_cookies
);
1362 /* {{{ proto array HttpRequest::getCookies()
1364 * Get previously set cookies.
1366 PHP_METHOD(HttpRequest
, getCookies
)
1371 zval
*opts
, **cookies
;
1372 getObject(http_request_object
, obj
);
1374 opts
= GET_PROP(obj
, options
);
1376 array_init(return_value
);
1378 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1379 array_copy(*cookies
, return_value
);
1385 /* {{{ proto void HttpRequest::unsetCookies()
1388 PHP_METHOD(HttpRequest
, unsetCookies
)
1391 getObject(http_request_object
, obj
);
1395 opts
= GET_PROP(obj
, options
);
1396 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
1400 /* {{{ proto bool HttpRequest::setURL(string url)
1402 * Set the request URL.
1404 PHP_METHOD(HttpRequest
, setURL
)
1408 getObject(http_request_object
, obj
);
1410 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1414 UPD_PROP(obj
, string
, url
, URL
);
1419 /* {{{ proto string HttpRequest::getUrl()
1421 * Get the previously set request URL.
1423 PHP_METHOD(HttpRequest
, getURL
)
1429 getObject(http_request_object
, obj
);
1431 URL
= GET_PROP(obj
, url
);
1432 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
1437 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1439 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1440 * <tt>HTTP_POST</tt> constants.
1442 PHP_METHOD(HttpRequest
, setMethod
)
1445 getObject(http_request_object
, obj
);
1447 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1451 UPD_PROP(obj
, long, method
, meth
);
1456 /* {{{ proto long HttpRequest::getMethod()
1458 * Get the previously set request method.
1460 PHP_METHOD(HttpRequest
, getMethod
)
1466 getObject(http_request_object
, obj
);
1468 meth
= GET_PROP(obj
, method
);
1469 RETURN_LONG(Z_LVAL_P(meth
));
1474 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1476 * Set the content type the post request should have.
1477 * Use this only if you know what you're doing.
1479 PHP_METHOD(HttpRequest
, setContentType
)
1483 getObject(http_request_object
, obj
);
1485 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1489 if (!strchr(ctype
, '/')) {
1490 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
1494 UPD_PROP(obj
, string
, contentType
, ctype
);
1499 /* {{{ proto string HttpRequest::getContentType()
1501 * Get the previously content type.
1503 PHP_METHOD(HttpRequest
, getContentType
)
1509 getObject(http_request_object
, obj
);
1511 ctype
= GET_PROP(obj
, contentType
);
1512 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
1517 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1519 * Set the URL query parameters to use.
1520 * Overwrites previously set query parameters.
1521 * Affects any request types.
1523 PHP_METHOD(HttpRequest
, setQueryData
)
1526 char *query_data
= NULL
;
1527 getObject(http_request_object
, obj
);
1529 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
1533 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1534 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1537 UPD_PROP(obj
, string
, queryData
, query_data
);
1542 convert_to_string(qdata
);
1543 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
1548 /* {{{ proto string HttpRequest::getQueryData()
1550 * Get the current query data in form of an urlencoded query string.
1552 PHP_METHOD(HttpRequest
, getQueryData
)
1558 getObject(http_request_object
, obj
);
1560 qdata
= GET_PROP(obj
, queryData
);
1561 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
1566 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1568 * Add parameters to the query parameter list.
1569 * Affects any request type.
1571 PHP_METHOD(HttpRequest
, addQueryData
)
1573 zval
*qdata
, *old_qdata
;
1574 char *query_data
= NULL
;
1575 getObject(http_request_object
, obj
);
1577 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1581 old_qdata
= GET_PROP(obj
, queryData
);
1583 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1587 UPD_PROP(obj
, string
, queryData
, query_data
);
1594 /* {{{ proto void HttpRequest::unsetQueryData()
1596 * Clean the query parameters.
1597 * Affects any request type.
1599 PHP_METHOD(HttpRequest
, unsetQueryData
)
1601 getObject(http_request_object
, obj
);
1605 UPD_PROP(obj
, string
, queryData
, "");
1609 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1611 * Adds POST data entries.
1612 * Affects only POST requests.
1614 PHP_METHOD(HttpRequest
, addPostFields
)
1616 zval
*post
, *post_data
;
1617 getObject(http_request_object
, obj
);
1619 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1623 post
= GET_PROP(obj
, postFields
);
1624 array_merge(post_data
, post
);
1630 /* {{{ proto bool HttpRequest::setPostFields(array post_data)
1632 * Set the POST data entries.
1633 * Overwrites previously set POST data.
1634 * Affects only POST requests.
1636 PHP_METHOD(HttpRequest
, setPostFields
)
1638 zval
*post
, *post_data
;
1639 getObject(http_request_object
, obj
);
1641 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1645 post
= GET_PROP(obj
, postFields
);
1646 zend_hash_clean(Z_ARRVAL_P(post
));
1647 array_copy(post_data
, post
);
1653 /* {{{ proto array HttpRequest::getPostFields()
1655 * Get previously set POST data.
1657 PHP_METHOD(HttpRequest
, getPostFields
)
1663 getObject(http_request_object
, obj
);
1665 post_data
= GET_PROP(obj
, postFields
);
1666 array_init(return_value
);
1667 array_copy(post_data
, return_value
);
1672 /* {{{ proto void HttpRequest::unsetPostFields()
1674 * Clean POST data entires.
1675 * Affects only POST requests.
1677 PHP_METHOD(HttpRequest
, unsetPostFields
)
1680 getObject(http_request_object
, obj
);
1684 post_data
= GET_PROP(obj
, postFields
);
1685 zend_hash_clean(Z_ARRVAL_P(post_data
));
1689 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1691 * Add a file to the POST request.
1692 * Affects only POST requests.
1694 PHP_METHOD(HttpRequest
, addPostFile
)
1696 zval
*files
, *entry
;
1697 char *name
, *file
, *type
= NULL
;
1698 int name_len
, file_len
, type_len
= 0;
1699 getObject(http_request_object
, obj
);
1701 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1706 if (!strchr(type
, '/')) {
1707 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1711 type
= "application/x-octetstream";
1712 type_len
= sizeof("application/x-octetstream") - 1;
1715 MAKE_STD_ZVAL(entry
);
1718 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1719 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1720 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1722 files
= GET_PROP(obj
, postFiles
);
1723 add_next_index_zval(files
, entry
);
1729 /* {{{ proto bool HttpRequest::setPostFiles()
1731 * Set files to post.
1732 * Overwrites previously set post files.
1733 * Affects only POST requests.
1735 PHP_METHOD(HttpRequest
, setPostFiles
)
1737 zval
*files
, *pFiles
;
1738 getObject(http_request_object
, obj
);
1740 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &files
)) {
1744 pFiles
= GET_PROP(obj
, postFiles
);
1745 zend_hash_clean(Z_ARRVAL_P(pFiles
));
1746 array_copy(files
, pFiles
);
1752 /* {{{ proto array HttpRequest::getPostFiles()
1754 * Get all previously added POST files.
1756 PHP_METHOD(HttpRequest
, getPostFiles
)
1762 getObject(http_request_object
, obj
);
1764 files
= GET_PROP(obj
, postFiles
);
1766 array_init(return_value
);
1767 array_copy(files
, return_value
);
1772 /* {{{ proto void HttpRequest::unsetPostFiles()
1774 * Unset the POST files list.
1775 * Affects only POST requests.
1777 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1780 getObject(http_request_object
, obj
);
1784 files
= GET_PROP(obj
, postFiles
);
1785 zend_hash_clean(Z_ARRVAL_P(files
));
1789 /* {{{ proto bool HttpRequest::SetPutFile(string file)
1792 * Affects only PUT requests.
1794 PHP_METHOD(HttpRequest
, setPutFile
)
1798 getObject(http_request_object
, obj
);
1800 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &file_len
)) {
1804 UPD_PROP(obj
, string
, putFile
, file
);
1809 /* {{{ proto string HttpRequest::getPutFile()
1811 * Get previously set put file.
1813 PHP_METHOD(HttpRequest
, getPutFile
)
1819 getObject(http_request_object
, obj
);
1821 putfile
= GET_PROP(obj
, putFile
);
1822 RETVAL_STRINGL(Z_STRVAL_P(putfile
), Z_STRLEN_P(putfile
), 1);
1827 /* {{{ proto void HttpRequest::unsetPutFile()
1829 * Unset file to put.
1830 * Affects only PUT requests.
1832 PHP_METHOD(HttpRequest
, unsetPutFile
)
1834 getObject(http_request_object
, obj
);
1838 UPD_PROP(obj
, string
, putFile
, "");
1842 /* {{{ proto array HttpRequest::getResponseData()
1844 * Get all response data after the request has been sent.
1846 PHP_METHOD(HttpRequest
, getResponseData
)
1852 getObject(http_request_object
, obj
);
1854 data
= GET_PROP(obj
, responseData
);
1855 array_init(return_value
);
1856 array_copy(data
, return_value
);
1861 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1863 * Get response header(s) after the request has been sent.
1865 PHP_METHOD(HttpRequest
, getResponseHeader
)
1868 zval
*data
, **headers
, **header
;
1869 char *header_name
= NULL
;
1871 getObject(http_response_object
, obj
);
1873 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1877 data
= GET_PROP(obj
, responseData
);
1878 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1882 if (!header_len
|| !header_name
) {
1883 array_init(return_value
);
1884 array_copy(*headers
, return_value
);
1885 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1886 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1894 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1896 * Get response cookie(s) after the request has been sent.
1898 PHP_METHOD(HttpRequest
, getResponseCookie
)
1901 zval
*data
, **headers
;
1902 char *cookie_name
= NULL
;
1904 getObject(http_request_object
, obj
);
1906 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1910 array_init(return_value
);
1912 data
= GET_PROP(obj
, responseData
);
1913 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1916 zval
**header
= NULL
;
1918 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1919 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1920 /* several cookies? */
1921 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1924 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1926 MAKE_STD_ZVAL(cookie_hash
);
1927 array_init(cookie_hash
);
1929 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1931 add_next_index_zval(return_value
, cookie_hash
);
1935 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1936 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1937 add_next_index_zval(return_value
, cookie_hash
);
1938 return; /* <<< FOUND >>> */
1940 zval_dtor(cookie_hash
);
1945 zval_dtor(cookie_hash
);
1951 MAKE_STD_ZVAL(cookie_hash
);
1952 array_init(cookie_hash
);
1954 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1956 add_next_index_zval(return_value
, cookie_hash
);
1960 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1961 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1962 add_next_index_zval(return_value
, cookie_hash
);
1964 zval_dtor(cookie_hash
);
1969 zval_dtor(cookie_hash
);
1983 /* {{{ proto string HttpRequest::getResponseBody()
1985 * Get the response body after the request has been sent.
1987 PHP_METHOD(HttpRequest
, getResponseBody
)
1993 getObject(http_request_object
, obj
);
1995 data
= GET_PROP(obj
, responseData
);
1996 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1997 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
2005 /* {{{ proto int HttpRequest::getResponseCode()
2007 * Get the response code after the request has been sent.
2009 PHP_METHOD(HttpRequest
, getResponseCode
)
2015 getObject(http_request_object
, obj
);
2017 code
= GET_PROP(obj
, responseCode
);
2018 RETURN_LONG(Z_LVAL_P(code
));
2023 /* {{{ proto array HttpRequest::getResponseInfo([string name])
2025 * Get response info after the request has been sent.
2026 * See http_get() for a full list of returned info.
2028 PHP_METHOD(HttpRequest
, getResponseInfo
)
2031 zval
*info
, **infop
;
2032 char *info_name
= NULL
;
2034 getObject(http_request_object
, obj
);
2036 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
2040 info
= GET_PROP(obj
, responseInfo
);
2042 if (info_len
&& info_name
) {
2043 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
2044 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
2046 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Could not find response info named %s", info_name
);
2050 array_init(return_value
);
2051 array_copy(info
, return_value
);
2057 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
2059 * Get the full response as HttpMessage object.
2061 PHP_METHOD(HttpRequest
, getResponseMessage
)
2067 getObject(http_request_object
, obj
);
2069 message
= GET_PROP(obj
, responseMessage
);
2070 Z_TYPE_P(return_value
) = IS_OBJECT
;
2071 return_value
->is_ref
= 1;
2072 return_value
->value
.obj
= message
->value
.obj
;
2073 zval_add_ref(&return_value
);
2077 /* {{{ proto bool HttpRequest::send()
2079 * Send the HTTP request.
2084 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
2085 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
2086 * $r->addQueryData(array('category' => 3));
2089 * if ($r->getResponseCode() == 200) {
2090 * file_put_contents('local.rss', $r->getResponseBody());
2092 * } catch (HttpException $ex) {
2101 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
2102 * $r->setOptions(array('cookies' => array('lang' => 'de')));
2103 * $r->addpostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
2104 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
2106 * echo $r->getResponseBody();
2111 PHP_METHOD(HttpRequest
, send
)
2113 STATUS status
= FAILURE
;
2114 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
2116 getObject(http_request_object
, obj
);
2120 SET_EH_THROW_HTTP();
2122 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
2123 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initilaize curl");
2127 meth
= GET_PROP(obj
, method
);
2128 URL
= GET_PROP(obj
, url
);
2129 qdata
= GET_PROP(obj
, queryData
);
2130 opts
= GET_PROP(obj
, options
);
2131 info
= GET_PROP(obj
, responseInfo
);
2132 resp
= GET_PROP(obj
, responseData
);
2134 // HTTP_URI_MAXLEN+1 long char *
2135 if (!(request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0))) {
2139 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
2140 if (!strchr(request_uri
, '?')) {
2141 strcat(request_uri
, "?");
2143 strcat(request_uri
, "&");
2145 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
2148 switch (Z_LVAL_P(meth
))
2152 status
= http_request_ex(obj
->ch
, Z_LVAL_P(meth
), request_uri
, NULL
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2157 http_request_body body
;
2159 php_stream_statbuf ssb
;
2160 zval
*file
= GET_PROP(obj
, putFile
);
2162 if ( (stream
= php_stream_open_wrapper(Z_STRVAL_P(file
), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
)) &&
2163 !php_stream_stat(stream
, &ssb
)) {
2164 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
2166 body
.size
= ssb
.sb
.st_size
;
2168 status
= http_put_ex(obj
->ch
, request_uri
, &body
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2169 http_request_body_dtor(&body
);
2178 http_request_body body
;
2179 zval
*fields
= GET_PROP(obj
, postFields
), *files
= GET_PROP(obj
, postFiles
);
2181 if (SUCCESS
== (status
= http_request_body_fill(&body
, Z_ARRVAL_P(fields
), Z_ARRVAL_P(files
)))) {
2182 status
= http_post_ex(obj
->ch
, request_uri
, &body
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2183 http_request_body_dtor(&body
);
2190 http_request_body body
;
2191 zval
*post
= GET_PROP(obj
, postData
);
2193 body
.type
= HTTP_REQUEST_BODY_CSTRING
;
2194 body
.data
= Z_STRVAL_P(post
);
2195 body
.size
= Z_STRLEN_P(post
);
2197 status
= http_request_ex(obj
->ch
, Z_LVAL_P(meth
), request_uri
, &body
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2204 /* final data handling */
2205 if (status
== SUCCESS
) {
2208 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->response
), PHPSTR_LEN(&obj
->response
))) {
2209 zval
*headers
, *message
;
2213 UPD_PROP(obj
, long, responseCode
, msg
->info
.response
.code
);
2215 MAKE_STD_ZVAL(headers
)
2216 array_init(headers
);
2218 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
2219 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
2221 add_assoc_zval(resp
, "headers", headers
);
2222 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
2224 message
= GET_PROP(obj
, responseMessage
);
2226 Z_TYPE_P(message
) = IS_OBJECT
;
2227 message
->value
.obj
= http_message_object_from_msg(msg
);
2228 SET_PROP(obj
, responseMessage
, message
);
2235 RETURN_SUCCESS(status
);
2239 #endif /* HTTP_HAVE_CURL */
2241 #endif /* ZEND_ENGINE_2 */
2248 * vim600: noet sw=4 ts=4 fdm=marker
2249 * vim<600: noet sw=4 ts=4