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()
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 zval
*do_cache
, *do_gzip
;
528 getObject(http_response_object
, obj
);
532 do_cache
= GET_PROP(obj
, cache
);
533 do_gzip
= GET_PROP(obj
, gzip
);
536 if (Z_LVAL_P(do_gzip
)) {
537 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC
);
541 if (Z_LVAL_P(do_cache
)) {
544 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
546 etag
= GET_PROP(obj
, eTag
);
547 lmod
= GET_PROP(obj
, lastModified
);
548 cctrl
= GET_PROP(obj
, cacheControl
);
549 ccraw
= GET_PROP(obj
, raw_cache_header
);
551 if (Z_LVAL_P(ccraw
)) {
552 cc_hdr
= Z_STRVAL_P(cctrl
);
553 cc_len
= Z_STRLEN_P(cctrl
);
555 char cc_header
[42] = {0};
556 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
558 cc_len
= Z_STRLEN_P(cctrl
) + lenof(", must-revalidate, max-age=0");
561 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_hdr
, cc_len
);
562 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_hdr
, cc_len
);
567 zval
*ctype
= GET_PROP(obj
, contentType
);
568 if (Z_STRLEN_P(ctype
)) {
569 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
571 http_send_content_type("application/x-octetstream", lenof("application/x-octetstream"));
575 /* content disposition */
577 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
578 if (Z_STRLEN_P(dispo_file
)) {
579 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
580 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
586 zval
*send_buffersize
, *throttle_delay
;
587 send_buffersize
= GET_PROP(obj
, sendBuffersize
);
588 throttle_delay
= GET_PROP(obj
, throttleDelay
);
589 HTTP_G(send
).buffer_size
= Z_LVAL_P(send_buffersize
);
590 HTTP_G(send
).throttle_delay
= Z_DVAL_P(throttle_delay
);
595 zval
*send_mode
= GET_PROP(obj
, send_mode
);
596 switch (Z_LVAL_P(send_mode
))
600 zval
*zdata
= GET_PROP(obj
, data
);
601 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
606 php_stream
*the_real_stream
;
607 zval
*the_stream
= GET_PROP(obj
, stream
);
608 php_stream_from_zval(the_real_stream
, &the_stream
);
609 RETURN_SUCCESS(http_send_stream(the_real_stream
));
614 zval
*zfile
= GET_PROP(obj
, file
);
615 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
623 /* {{{ HttpMessage */
625 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
627 * Create an HttpMessage object from a string.
629 PHP_METHOD(HttpMessage
, fromString
)
633 http_message
*msg
= NULL
;
635 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
639 if (!(msg
= http_message_parse(string
, length
))) {
643 Z_TYPE_P(return_value
) = IS_OBJECT
;
644 return_value
->value
.obj
= http_message_object_from_msg(msg
);
648 /* {{{ proto void HttpMessage::__construct([string message])
650 * Instantiate a new HttpMessage object.
652 PHP_METHOD(HttpMessage
, __construct
)
654 char *message
= NULL
;
656 getObject(http_message_object
, obj
);
659 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
660 if (obj
->message
= http_message_parse(message
, length
)) {
661 if (obj
->message
->parent
) {
662 obj
->parent
= http_message_object_from_msg(obj
->message
->parent
);
665 } else if (!obj
->message
) {
666 obj
->message
= http_message_new();
672 /* {{{ proto string HttpMessage::getBody()
674 * Get the body of the parsed Message.
676 PHP_METHOD(HttpMessage
, getBody
)
681 getObject(http_message_object
, obj
);
682 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
687 /* {{{ proto array HttpMessage::getHeaders()
689 * Get Message Headers.
691 PHP_METHOD(HttpMessage
, getHeaders
)
697 getObject(http_message_object
, obj
);
699 Z_ARRVAL(headers
) = &obj
->message
->hdrs
;
700 array_init(return_value
);
701 array_copy(&headers
, return_value
);
706 /* {{{ proto void HttpMessage::setHeaders(array headers)
710 PHP_METHOD(HttpMessage
, setHeaders
)
712 zval
*new_headers
, old_headers
;
713 getObject(http_message_object
, obj
);
715 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
719 zend_hash_clean(&obj
->message
->hdrs
);
720 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
721 array_copy(new_headers
, &old_headers
);
725 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
727 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
729 PHP_METHOD(HttpMessage
, addHeaders
)
731 zval old_headers
, *new_headers
;
732 zend_bool append
= 0;
733 getObject(http_message_object
, obj
);
735 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
739 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
741 array_append(new_headers
, &old_headers
);
743 array_merge(new_headers
, &old_headers
);
748 /* {{{ proto long HttpMessage::getType()
750 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
752 PHP_METHOD(HttpMessage
, getType
)
757 getObject(http_message_object
, obj
);
758 RETURN_LONG(obj
->message
->type
);
763 /* {{{ proto void HttpMessage::setType(long type)
765 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
767 PHP_METHOD(HttpMessage
, setType
)
770 getObject(http_message_object
, obj
);
772 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
775 http_message_set_type(obj
->message
, type
);
779 /* {{{ proto long HttpMessage::getResponseCode()
781 * Get the Response Code of the Message.
783 PHP_METHOD(HttpMessage
, getResponseCode
)
788 getObject(http_message_object
, obj
);
790 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
791 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
795 RETURN_LONG(obj
->message
->info
.response
.code
);
800 /* {{{ proto bool HttpMessage::setResponseCode(long code)
802 * Set the response code of an HTTP Response Message.
803 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
804 * or if the response code is out of range (100-510).
806 PHP_METHOD(HttpMessage
, setResponseCode
)
809 getObject(http_message_object
, obj
);
811 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
812 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
816 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
819 if (code
< 100 || code
> 510) {
820 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid response code (100-510): %ld", code
);
824 obj
->message
->info
.response
.code
= code
;
829 /* {{{ proto string HttpMessage::getRequestMethod()
831 * Get the Request Method of the Message.
832 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
834 PHP_METHOD(HttpMessage
, getRequestMethod
)
839 getObject(http_message_object
, obj
);
841 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
842 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
846 RETURN_STRING(obj
->message
->info
.request
.method
, 1);
851 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
853 * Set the Request Method of the HTTP Message.
854 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
856 PHP_METHOD(HttpMessage
, setRequestMethod
)
860 getObject(http_message_object
, obj
);
862 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
863 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
867 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
870 if (method_len
< 1) {
871 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
874 if (SUCCESS
!= http_check_method(method
)) {
875 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Unkown request method: %s", method
);
879 STR_SET(obj
->message
->info
.request
.method
, estrndup(method
, method_len
));
884 /* {{{ proto string HttpMessage::getRequestUri()
886 * Get the Request URI of the Message.
888 PHP_METHOD(HttpMessage
, getRequestUri
)
893 getObject(http_message_object
, obj
);
895 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
896 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
900 RETURN_STRING(obj
->message
->info
.request
.URI
, 1);
905 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
907 * Set the Request URI of the HTTP Message.
908 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
909 * or if paramtere URI was empty.
911 PHP_METHOD(HttpMessage
, setRequestUri
)
915 getObject(http_message_object
, obj
);
917 if (!HTTP_MSG_TYPE(REQUEST
, obj
->message
)) {
918 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
921 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
925 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
929 STR_SET(obj
->message
->info
.request
.URI
, estrndup(URI
, URIlen
));
934 /* {{{ proto string HttpMessage::getHttpVersion()
936 * Get the HTTP Protocol Version of the Message.
938 PHP_METHOD(HttpMessage
, getHttpVersion
)
945 getObject(http_message_object
, obj
);
947 switch (obj
->message
->type
)
949 case HTTP_MSG_RESPONSE
:
950 version
= obj
->message
->info
.response
.http_version
;
953 case HTTP_MSG_REQUEST
:
954 version
= obj
->message
->info
.request
.http_version
;
961 sprintf(ver
, "%1.1f", version
);
962 RETURN_STRINGL(ver
, 3, 1);
967 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
969 * Set the HTTP Protocol version of the Message.
970 * Returns false if version is invalid (1.0 and 1.1).
972 PHP_METHOD(HttpMessage
, setHttpVersion
)
976 getObject(http_message_object
, obj
);
978 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
982 if (HTTP_MSG_TYPE(NONE
, obj
->message
)) {
983 http_error(E_WARNING
, HTTP_E_MSG
, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
987 convert_to_double(zv
);
988 sprintf(v
, "%1.1f", Z_DVAL_P(zv
));
989 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
990 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
994 if (HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
995 obj
->message
->info
.response
.http_version
= (float) Z_DVAL_P(zv
);
997 obj
->message
->info
.request
.http_version
= (float) Z_DVAL_P(zv
);
1003 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1005 * Get parent Message.
1007 PHP_METHOD(HttpMessage
, getParentMessage
)
1012 getObject(http_message_object
, obj
);
1014 if (obj
->message
->parent
) {
1015 RETVAL_OBJVAL(obj
->parent
);
1023 /* {{{ proto bool HttpMessage::send()
1025 * Send the Message according to its type as Response or Request.
1027 PHP_METHOD(HttpMessage
, send
)
1029 getObject(http_message_object
, obj
);
1033 RETURN_SUCCESS(http_message_send(obj
->message
));
1037 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
1039 * Get the string representation of the Message.
1041 PHP_METHOD(HttpMessage
, toString
)
1046 zend_bool include_parent
= 1;
1047 getObject(http_message_object
, obj
);
1049 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1053 if (include_parent
) {
1054 http_message_serialize(obj
->message
, &string
, &length
);
1056 http_message_tostring(obj
->message
, &string
, &length
);
1058 RETURN_STRINGL(string
, length
, 0);
1065 #ifdef HTTP_HAVE_CURL
1066 /* {{{ HttpRequest */
1068 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
1070 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
1071 * and POST (including posting files) HTTP requests.
1073 PHP_METHOD(HttpRequest
, __construct
)
1078 getObject(http_request_object
, obj
);
1080 SET_EH_THROW_HTTP();
1081 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
1082 INIT_PARR(obj
, options
);
1083 INIT_PARR(obj
, responseInfo
);
1084 INIT_PARR(obj
, responseData
);
1085 INIT_PARR(obj
, postFields
);
1086 INIT_PARR(obj
, postFiles
);
1089 UPD_PROP(obj
, string
, url
, URL
);
1092 UPD_PROP(obj
, long, method
, meth
);
1099 /* {{{ proto void HttpRequest::__destruct()
1101 * Destroys the HttpRequest object.
1103 PHP_METHOD(HttpRequest
, __destruct
)
1105 getObject(http_request_object
, obj
);
1109 FREE_PARR(obj
, options
);
1110 FREE_PARR(obj
, responseInfo
);
1111 FREE_PARR(obj
, responseData
);
1112 FREE_PARR(obj
, postFields
);
1113 FREE_PARR(obj
, postFiles
);
1117 /* {{{ proto bool HttpRequest::setOptions(array options)
1119 * Set the request options to use. See http_get() for a full list of available options.
1121 PHP_METHOD(HttpRequest
, setOptions
)
1125 zval
*opts
, *old_opts
, **opt
;
1126 getObject(http_request_object
, obj
);
1128 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1132 old_opts
= GET_PROP(obj
, options
);
1134 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
1135 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
1137 if (!strcmp(key
, "headers")) {
1139 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1140 array_merge(*opt
, *headers
);
1143 } else if (!strcmp(key
, "cookies")) {
1145 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1146 array_merge(*opt
, *cookies
);
1151 add_assoc_zval(old_opts
, key
, *opt
);
1162 /* {{{ proto array HttpRequest::getOptions()
1164 * Get current set options.
1166 PHP_METHOD(HttpRequest
, getOptions
)
1172 getObject(http_request_object
, obj
);
1174 opts
= GET_PROP(obj
, options
);
1175 array_init(return_value
);
1176 array_copy(opts
, return_value
);
1181 /* {{{ proto void HttpRequest::unsetOptions()
1183 * Unset all options/headers/cookies.
1185 PHP_METHOD(HttpRequest
, unsetOptions
)
1187 getObject(http_request_object
, obj
);
1191 FREE_PARR(obj
, options
);
1192 INIT_PARR(obj
, options
);
1196 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1198 * Set additional SSL options.
1200 PHP_METHOD(HttpRequest
, setSslOptions
)
1202 zval
*opts
, *old_opts
, **ssl_options
;
1203 getObject(http_request_object
, obj
);
1205 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1209 old_opts
= GET_PROP(obj
, options
);
1211 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1212 array_merge(opts
, *ssl_options
);
1214 zval_add_ref(&opts
);
1215 add_assoc_zval(old_opts
, "ssl", opts
);
1222 /* {{{ proto array HttpRequest::getSslOtpions()
1224 * Get previously set SSL options.
1226 PHP_METHOD(HttpRequest
, getSslOptions
)
1231 zval
*opts
, **ssl_options
;
1232 getObject(http_request_object
, obj
);
1234 opts
= GET_PROP(obj
, options
);
1236 array_init(return_value
);
1238 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1239 array_copy(*ssl_options
, return_value
);
1245 /* {{{ proto void HttpRequest::unsetSslOptions()
1247 * Unset previously set SSL options.
1249 PHP_METHOD(HttpRequest
, unsetSslOptions
)
1252 getObject(http_request_object
, obj
);
1256 opts
= GET_PROP(obj
, options
);
1257 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
1261 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1263 * Add request header name/value pairs.
1265 PHP_METHOD(HttpRequest
, addHeaders
)
1267 zval
*opts
, **headers
, *new_headers
;
1268 getObject(http_request_object
, obj
);
1270 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
1274 opts
= GET_PROP(obj
, options
);
1276 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1277 array_merge(new_headers
, *headers
);
1279 zval_add_ref(&new_headers
);
1280 add_assoc_zval(opts
, "headers", new_headers
);
1287 /* {{{ proto array HttpRequest::getHeaders()
1289 * Get previously set request headers.
1291 PHP_METHOD(HttpRequest
, getHeaders
)
1296 zval
*opts
, **headers
;
1297 getObject(http_request_object
, obj
);
1299 opts
= GET_PROP(obj
, options
);
1301 array_init(return_value
);
1303 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1304 array_copy(*headers
, return_value
);
1310 /* {{{ proto void HttpRequest::unsetHeaders()
1312 * Unset previously set request headers.
1314 PHP_METHOD(HttpRequest
, unsetHeaders
)
1317 getObject(http_request_object
, obj
);
1321 opts
= GET_PROP(obj
, options
);
1322 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
1326 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1330 PHP_METHOD(HttpRequest
, addCookies
)
1332 zval
*opts
, **cookies
, *new_cookies
;
1333 getObject(http_request_object
, obj
);
1335 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
1339 opts
= GET_PROP(obj
, options
);
1341 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1342 array_merge(new_cookies
, *cookies
);
1344 zval_add_ref(&new_cookies
);
1345 add_assoc_zval(opts
, "cookies", new_cookies
);
1352 /* {{{ proto array HttpRequest::getCookies()
1354 * Get previously set cookies.
1356 PHP_METHOD(HttpRequest
, getCookies
)
1361 zval
*opts
, **cookies
;
1362 getObject(http_request_object
, obj
);
1364 opts
= GET_PROP(obj
, options
);
1366 array_init(return_value
);
1368 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1369 array_copy(*cookies
, return_value
);
1375 /* {{{ proto void HttpRequest::unsetCookies()
1378 PHP_METHOD(HttpRequest
, unsetCookies
)
1381 getObject(http_request_object
, obj
);
1385 opts
= GET_PROP(obj
, options
);
1386 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
1390 /* {{{ proto bool HttpRequest::setURL(string url)
1392 * Set the request URL.
1394 PHP_METHOD(HttpRequest
, setURL
)
1398 getObject(http_request_object
, obj
);
1400 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1404 UPD_PROP(obj
, string
, url
, URL
);
1409 /* {{{ proto string HttpRequest::getUrl()
1411 * Get the previously set request URL.
1413 PHP_METHOD(HttpRequest
, getURL
)
1419 getObject(http_request_object
, obj
);
1421 URL
= GET_PROP(obj
, url
);
1422 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
1427 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1429 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1430 * <tt>HTTP_POST</tt> constants.
1432 PHP_METHOD(HttpRequest
, setMethod
)
1435 getObject(http_request_object
, obj
);
1437 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1441 UPD_PROP(obj
, long, method
, meth
);
1446 /* {{{ proto long HttpRequest::getMethod()
1448 * Get the previously set request method.
1450 PHP_METHOD(HttpRequest
, getMethod
)
1456 getObject(http_request_object
, obj
);
1458 meth
= GET_PROP(obj
, method
);
1459 RETURN_LONG(Z_LVAL_P(meth
));
1464 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1466 * Set the content type the post request should have.
1467 * Use this only if you know what you're doing.
1469 PHP_METHOD(HttpRequest
, setContentType
)
1473 getObject(http_request_object
, obj
);
1475 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1479 if (!strchr(ctype
, '/')) {
1480 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
1484 UPD_PROP(obj
, string
, contentType
, ctype
);
1489 /* {{{ proto string HttpRequest::getContentType()
1491 * Get the previously content type.
1493 PHP_METHOD(HttpRequest
, getContentType
)
1499 getObject(http_request_object
, obj
);
1501 ctype
= GET_PROP(obj
, contentType
);
1502 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
1507 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1509 * Set the URL query parameters to use.
1510 * Overwrites previously set query parameters.
1511 * Affects any request types.
1513 PHP_METHOD(HttpRequest
, setQueryData
)
1516 char *query_data
= NULL
;
1517 getObject(http_request_object
, obj
);
1519 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
1523 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1524 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1527 UPD_PROP(obj
, string
, queryData
, query_data
);
1532 convert_to_string(qdata
);
1533 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
1538 /* {{{ proto string HttpRequest::getQueryData()
1540 * Get the current query data in form of an urlencoded query string.
1542 PHP_METHOD(HttpRequest
, getQueryData
)
1548 getObject(http_request_object
, obj
);
1550 qdata
= GET_PROP(obj
, queryData
);
1551 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
1556 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1558 * Add parameters to the query parameter list.
1559 * Affects any request type.
1561 PHP_METHOD(HttpRequest
, addQueryData
)
1563 zval
*qdata
, *old_qdata
;
1564 char *query_data
= NULL
;
1565 getObject(http_request_object
, obj
);
1567 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1571 old_qdata
= GET_PROP(obj
, queryData
);
1573 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1577 UPD_PROP(obj
, string
, queryData
, query_data
);
1584 /* {{{ proto void HttpRequest::unsetQueryData()
1586 * Clean the query parameters.
1587 * Affects any request type.
1589 PHP_METHOD(HttpRequest
, unsetQueryData
)
1591 getObject(http_request_object
, obj
);
1595 UPD_PROP(obj
, string
, queryData
, "");
1599 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1601 * Adds POST data entries.
1602 * Affects only POST requests.
1604 PHP_METHOD(HttpRequest
, addPostFields
)
1606 zval
*post
, *post_data
;
1607 getObject(http_request_object
, obj
);
1609 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1613 post
= GET_PROP(obj
, postFields
);
1614 array_merge(post_data
, post
);
1620 /* {{{ proto bool HttpRequest::setPostFields(array post_data)
1622 * Set the POST data entries.
1623 * Overwrites previously set POST data.
1624 * Affects only POST requests.
1626 PHP_METHOD(HttpRequest
, setPostFields
)
1628 zval
*post
, *post_data
;
1629 getObject(http_request_object
, obj
);
1631 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1635 post
= GET_PROP(obj
, postFields
);
1636 zend_hash_clean(Z_ARRVAL_P(post
));
1637 array_copy(post_data
, post
);
1643 /* {{{ proto array HttpRequest::getPostFields()
1645 * Get previously set POST data.
1647 PHP_METHOD(HttpRequest
, getPostFields
)
1653 getObject(http_request_object
, obj
);
1655 post_data
= GET_PROP(obj
, postFields
);
1656 array_init(return_value
);
1657 array_copy(post_data
, return_value
);
1662 /* {{{ proto void HttpRequest::unsetPostFields()
1664 * Clean POST data entires.
1665 * Affects only POST requests.
1667 PHP_METHOD(HttpRequest
, unsetPostFields
)
1670 getObject(http_request_object
, obj
);
1674 post_data
= GET_PROP(obj
, postFields
);
1675 zend_hash_clean(Z_ARRVAL_P(post_data
));
1679 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1681 * Add a file to the POST request.
1682 * Affects only POST requests.
1684 PHP_METHOD(HttpRequest
, addPostFile
)
1686 zval
*files
, *entry
;
1687 char *name
, *file
, *type
= NULL
;
1688 int name_len
, file_len
, type_len
= 0;
1689 getObject(http_request_object
, obj
);
1691 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1696 if (!strchr(type
, '/')) {
1697 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1701 type
= "application/x-octetstream";
1702 type_len
= sizeof("application/x-octetstream") - 1;
1705 MAKE_STD_ZVAL(entry
);
1708 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1709 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1710 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1712 files
= GET_PROP(obj
, postFiles
);
1713 add_next_index_zval(files
, entry
);
1719 /* {{{ proto array HttpRequest::getPostFiles()
1721 * Get all previously added POST files.
1723 PHP_METHOD(HttpRequest
, getPostFiles
)
1729 getObject(http_request_object
, obj
);
1731 files
= GET_PROP(obj
, postFiles
);
1733 array_init(return_value
);
1734 array_copy(files
, return_value
);
1739 /* {{{ proto void HttpRequest::unsetPostFiles()
1741 * Unset the POST files list.
1742 * Affects only POST requests.
1744 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1747 getObject(http_request_object
, obj
);
1751 files
= GET_PROP(obj
, postFiles
);
1752 zend_hash_clean(Z_ARRVAL_P(files
));
1756 /* {{{ proto array HttpRequest::getResponseData()
1758 * Get all response data after the request has been sent.
1760 PHP_METHOD(HttpRequest
, getResponseData
)
1766 getObject(http_request_object
, obj
);
1768 data
= GET_PROP(obj
, responseData
);
1769 array_init(return_value
);
1770 array_copy(data
, return_value
);
1775 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1777 * Get response header(s) after the request has been sent.
1779 PHP_METHOD(HttpRequest
, getResponseHeader
)
1782 zval
*data
, **headers
, **header
;
1783 char *header_name
= NULL
;
1785 getObject(http_response_object
, obj
);
1787 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1791 data
= GET_PROP(obj
, responseData
);
1792 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1796 if (!header_len
|| !header_name
) {
1797 array_init(return_value
);
1798 array_copy(*headers
, return_value
);
1799 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1800 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1808 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1810 * Get response cookie(s) after the request has been sent.
1812 PHP_METHOD(HttpRequest
, getResponseCookie
)
1815 zval
*data
, **headers
;
1816 char *cookie_name
= NULL
;
1818 getObject(http_request_object
, obj
);
1820 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1824 array_init(return_value
);
1826 data
= GET_PROP(obj
, responseData
);
1827 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1830 zval
**header
= NULL
;
1832 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1833 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1834 /* several cookies? */
1835 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1838 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1840 MAKE_STD_ZVAL(cookie_hash
);
1841 array_init(cookie_hash
);
1843 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1845 add_next_index_zval(return_value
, cookie_hash
);
1849 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1850 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1851 add_next_index_zval(return_value
, cookie_hash
);
1852 return; /* <<< FOUND >>> */
1854 zval_dtor(cookie_hash
);
1859 zval_dtor(cookie_hash
);
1865 MAKE_STD_ZVAL(cookie_hash
);
1866 array_init(cookie_hash
);
1868 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1870 add_next_index_zval(return_value
, cookie_hash
);
1874 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1875 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1876 add_next_index_zval(return_value
, cookie_hash
);
1878 zval_dtor(cookie_hash
);
1883 zval_dtor(cookie_hash
);
1897 /* {{{ proto string HttpRequest::getResponseBody()
1899 * Get the response body after the request has been sent.
1901 PHP_METHOD(HttpRequest
, getResponseBody
)
1907 getObject(http_request_object
, obj
);
1909 data
= GET_PROP(obj
, responseData
);
1910 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1911 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
1919 /* {{{ proto int HttpRequest::getResponseCode()
1921 * Get the response code after the request has been sent.
1923 PHP_METHOD(HttpRequest
, getResponseCode
)
1929 getObject(http_request_object
, obj
);
1931 code
= GET_PROP(obj
, responseCode
);
1932 RETURN_LONG(Z_LVAL_P(code
));
1937 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1939 * Get response info after the request has been sent.
1940 * See http_get() for a full list of returned info.
1942 PHP_METHOD(HttpRequest
, getResponseInfo
)
1945 zval
*info
, **infop
;
1946 char *info_name
= NULL
;
1948 getObject(http_request_object
, obj
);
1950 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1954 info
= GET_PROP(obj
, responseInfo
);
1956 if (info_len
&& info_name
) {
1957 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1958 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
1960 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Could not find response info named %s", info_name
);
1964 array_init(return_value
);
1965 array_copy(info
, return_value
);
1971 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1973 * Get the full response as HttpMessage object.
1975 PHP_METHOD(HttpRequest
, getResponseMessage
)
1981 getObject(http_request_object
, obj
);
1983 message
= GET_PROP(obj
, responseMessage
);
1984 Z_TYPE_P(return_value
) = IS_OBJECT
;
1985 return_value
->is_ref
= 1;
1986 return_value
->value
.obj
= message
->value
.obj
;
1987 zval_add_ref(&return_value
);
1991 /* {{{ proto bool HttpRequest::send()
1993 * Send the HTTP request.
1998 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1999 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
2000 * $r->addQueryData(array('category' => 3));
2003 * if ($r->getResponseCode() == 200) {
2004 * file_put_contents('local.rss', $r->getResponseBody());
2006 * } catch (HttpException $ex) {
2015 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
2016 * $r->setOptions(array('cookies' => array('lang' => 'de')));
2017 * $r->addpostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
2018 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
2020 * echo $r->getResponseBody();
2025 PHP_METHOD(HttpRequest
, send
)
2027 STATUS status
= FAILURE
;
2028 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
2030 getObject(http_request_object
, obj
);
2034 SET_EH_THROW_HTTP();
2036 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
2037 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initilaize curl");
2041 meth
= GET_PROP(obj
, method
);
2042 URL
= GET_PROP(obj
, url
);
2043 qdata
= GET_PROP(obj
, queryData
);
2044 opts
= GET_PROP(obj
, options
);
2045 info
= GET_PROP(obj
, responseInfo
);
2046 resp
= GET_PROP(obj
, responseData
);
2048 // HTTP_URI_MAXLEN+1 long char *
2049 if (!(request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0))) {
2053 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
2054 if (!strchr(request_uri
, '?')) {
2055 strcat(request_uri
, "?");
2057 strcat(request_uri
, "&");
2059 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
2062 switch (Z_LVAL_P(meth
))
2066 status
= http_request_ex(obj
->ch
, Z_LVAL_P(meth
), request_uri
, NULL
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2071 http_request_body body
;
2073 php_stream_statbuf ssb
;
2074 zval
*file
= GET_PROP(obj
, putFile
);
2076 if ( (stream
= php_stream_open_wrapper(Z_STRVAL_P(file
), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
)) &&
2077 !php_stream_stat(stream
, &ssb
)) {
2078 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
2080 body
.size
= ssb
.sb
.st_size
;
2082 status
= http_put_ex(obj
->ch
, request_uri
, &body
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2083 http_request_body_dtor(&body
);
2092 http_request_body body
;
2093 zval
*fields
= GET_PROP(obj
, postFields
), *files
= GET_PROP(obj
, postFiles
);
2095 if (SUCCESS
== (status
= http_request_body_fill(&body
, Z_ARRVAL_P(fields
), Z_ARRVAL_P(files
)))) {
2096 status
= http_post_ex(obj
->ch
, request_uri
, &body
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2097 http_request_body_dtor(&body
);
2104 http_request_body body
;
2105 zval
*post
= GET_PROP(obj
, postData
);
2107 body
.type
= HTTP_REQUEST_BODY_CSTRING
;
2108 body
.data
= Z_STRVAL_P(post
);
2109 body
.size
= Z_STRLEN_P(post
);
2111 status
= http_request_ex(obj
->ch
, Z_LVAL_P(meth
), request_uri
, &body
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
2118 /* final data handling */
2119 if (status
== SUCCESS
) {
2122 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->response
), PHPSTR_LEN(&obj
->response
))) {
2123 zval
*headers
, *message
;
2127 UPD_PROP(obj
, long, responseCode
, msg
->info
.response
.code
);
2129 MAKE_STD_ZVAL(headers
)
2130 array_init(headers
);
2132 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
2133 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
2135 add_assoc_zval(resp
, "headers", headers
);
2136 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
2138 message
= GET_PROP(obj
, responseMessage
);
2140 Z_TYPE_P(message
) = IS_OBJECT
;
2141 message
->value
.obj
= http_message_object_from_msg(msg
);
2142 SET_PROP(obj
, responseMessage
, message
);
2149 RETURN_SUCCESS(status
);
2153 #endif /* HTTP_HAVE_CURL */
2155 #endif /* ZEND_ENGINE_2 */
2162 * vim600: noet sw=4 ts=4 fdm=marker
2163 * vim<600: noet sw=4 ts=4