3 +----------------------------------------------------------------------+
7 +----------------------------------------------------------------------+
9 | This source file is subject to version 3.0 of the PHP license, that |
11 | is bundled with this package in the file LICENSE, and is available |
13 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
15 | If you did not receive a copy of the PHP license and are unable to |
17 | obtain it through the world-wide-web, please send a note to |
19 | license@php.net so we can mail you a copy immediately. |
21 +----------------------------------------------------------------------+
23 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
25 +----------------------------------------------------------------------+
47 #include "php_http_std_defs.h"
49 #include "php_http_api.h"
51 #include "php_http_cache_api.h"
53 #include "php_http_request_api.h"
55 #include "php_http_date_api.h"
57 #include "php_http_headers_api.h"
59 #include "php_http_message_api.h"
61 #include "php_http_send_api.h"
63 #include "php_http_url_api.h"
67 #include "php_http_message_object.h"
69 #include "php_http_response_object.h"
71 #include "php_http_request_object.h"
73 #include "php_http_exception_object.h"
81 /* {{{ HttpResponse */
85 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
89 * Instantiates a new HttpResponse object, which can be used to send
91 * any data/resource/file to an HTTP client with caching and multiple
93 * ranges/resuming support.
97 * NOTE: GZIPping is not implemented yet.
101 PHP_METHOD(HttpResponse
, __construct
)
105 zend_bool do_cache
= 0, do_gzip
= 0;
107 getObject(http_response_object
, obj
);
113 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
115 UPD_PROP(obj
, long, cache
, do_cache
);
117 UPD_PROP(obj
, long, gzip
, do_gzip
);
129 /* {{{ proto bool HttpResponse::setCache(bool cache)
133 * Whether it sould be attempted to cache the entitity.
135 * This will result in necessary caching headers and checks of clients
137 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
139 * matches a "304 Not Modified" status code will be issued.
143 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
145 * to something more appropriate than "no-cache"!
149 PHP_METHOD(HttpResponse
, setCache
)
153 zend_bool do_cache
= 0;
155 getObject(http_response_object
, obj
);
159 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
167 UPD_PROP(obj
, long, cache
, do_cache
);
177 /* {{{ proto bool HttpResponse::getCache()
181 * Get current caching setting.
185 PHP_METHOD(HttpResponse
, getCache
)
189 zval
*do_cache
= NULL
;
191 getObject(http_response_object
, obj
);
199 do_cache
= GET_PROP(obj
, cache
);
201 RETURN_BOOL(Z_LVAL_P(do_cache
));
209 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
213 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
217 PHP_METHOD(HttpResponse
, setGzip
)
221 zend_bool do_gzip
= 0;
223 getObject(http_response_object
, obj
);
227 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
235 UPD_PROP(obj
, long, gzip
, do_gzip
);
245 /* {{{ proto bool HttpResponse::getGzip()
249 * Get current gzipping setting.
253 PHP_METHOD(HttpResponse
, getGzip
)
257 zval
*do_gzip
= NULL
;
259 getObject(http_response_object
, obj
);
267 do_gzip
= GET_PROP(obj
, gzip
);
269 RETURN_BOOL(Z_LVAL_P(do_gzip
));
277 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
281 * Set a custom cache-control header, usually being "private" or "public"; if
283 * $raw is set to true the header will be sent as-is.
287 PHP_METHOD(HttpResponse
, setCacheControl
)
297 getObject(http_response_object
, obj
);
301 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
309 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
311 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
319 UPD_PROP(obj
, long, raw_cache_header
, raw
);
321 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
331 /* {{{ proto string HttpResponse::getCacheControl()
335 * Get current Cache-Control header setting.
339 PHP_METHOD(HttpResponse
, getCacheControl
)
345 getObject(http_response_object
, obj
);
353 ccontrol
= GET_PROP(obj
, cacheControl
);
355 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
363 /* {{{ proto bool HttpResponse::setContentType(string content_type)
367 * Set the content-type of the sent entity.
371 PHP_METHOD(HttpResponse
, setContentType
)
379 getObject(http_response_object
, obj
);
383 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
391 if (!strchr(ctype
, '/')) {
393 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
401 UPD_PROP(obj
, string
, contentType
, ctype
);
413 /* {{{ proto string HttpResponse::getContentType()
417 * Get current Content-Type header setting.
421 PHP_METHOD(HttpResponse
, getContentType
)
427 getObject(http_response_object
, obj
);
435 ctype
= GET_PROP(obj
, contentType
);
437 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
445 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
449 * Set the Content-Disposition of the sent entity. This setting aims to suggest
451 * the receiveing user agent how to handle the sent entity; usually the client
453 * will show the user a "Save As..." popup.
457 PHP_METHOD(HttpResponse
, setContentDisposition
)
465 zend_bool is_inline
= 0;
467 getObject(http_response_object
, obj
);
471 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
479 UPD_PROP(obj
, string
, dispoFile
, file
);
481 UPD_PROP(obj
, long, dispoInline
, is_inline
);
491 /* {{{ proto array HttpResponse::getContentDisposition()
495 * Get current Content-Disposition setting.
497 * Will return an associative array like:
503 * 'filename' => 'foo.bar',
513 PHP_METHOD(HttpResponse
, getContentDisposition
)
521 getObject(http_response_object
, obj
);
525 if (ZEND_NUM_ARGS()) {
533 file
= GET_PROP(obj
, dispoFile
);
535 is_inline
= GET_PROP(obj
, dispoInline
);
539 array_init(return_value
);
541 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
543 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
551 /* {{{ proto bool HttpResponse::setETag(string etag)
555 * Set a custom ETag. Use this only if you know what you're doing.
559 PHP_METHOD(HttpResponse
, setETag
)
567 getObject(http_response_object
, obj
);
571 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
579 UPD_PROP(obj
, string
, eTag
, etag
);
589 /* {{{ proto string HttpResponse::getETag()
593 * Get the previously set custom ETag.
597 PHP_METHOD(HttpResponse
, getETag
)
603 getObject(http_response_object
, obj
);
611 etag
= GET_PROP(obj
, eTag
);
613 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
621 /* {{{ proto bool HttpResponse::setData(string data)
625 * Set the data to be sent.
629 PHP_METHOD(HttpResponse
, setData
)
635 getObject(http_response_object
, obj
);
639 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &the_data
)) {
647 convert_to_string_ex(&the_data
);
649 SET_PROP(obj
, data
, the_data
);
651 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_data
, SEND_DATA
));
653 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
663 /* {{{ proto string HttpResponse::getData()
667 * Get the previously set data to be sent.
671 PHP_METHOD(HttpResponse
, getData
)
677 getObject(http_response_object
, obj
);
685 the_data
= GET_PROP(obj
, data
);
687 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
695 /* {{{ proto bool HttpResponse::setStream(resource stream)
699 * Set the resource to be sent.
703 PHP_METHOD(HttpResponse
, setStream
)
709 php_stream
*the_real_stream
;
711 getObject(http_response_object
, obj
);
715 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
723 php_stream_from_zval(the_real_stream
, &the_stream
);
727 SET_PROP(obj
, stream
, the_stream
);
729 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_real_stream
, SEND_RSRC
));
731 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
741 /* {{{ proto resource HttpResponse::getStream()
745 * Get the previously set resource to be sent.
749 PHP_METHOD(HttpResponse
, getStream
)
755 getObject(http_response_object
, obj
);
763 the_stream
= GET_PROP(obj
, stream
);
765 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
773 /* {{{ proto bool HttpResponse::setFile(string file)
777 * Set the file to be sent.
781 PHP_METHOD(HttpResponse
, setFile
)
787 getObject(http_response_object
, obj
);
791 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
799 convert_to_string_ex(&the_file
);
803 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
805 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_file
, -1));
807 UPD_PROP(obj
, long, send_mode
, -1);
817 /* {{{ proto string HttpResponse::getFile()
821 * Get the previously set file to be sent.
825 PHP_METHOD(HttpResponse
, getFile
)
831 getObject(http_response_object
, obj
);
839 the_file
= GET_PROP(obj
, file
);
841 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
849 /* {{{ proto bool HttpResponse::send()
853 * Finally send the entity.
863 * $r = new HttpResponse(true);
865 * $r->setFile('../hidden/contract.pdf');
867 * $r->setContentType('application/pdf');
879 PHP_METHOD(HttpResponse
, send
)
883 zval
*do_cache
, *do_gzip
;
885 getObject(http_response_object
, obj
);
893 do_cache
= GET_PROP(obj
, cache
);
895 do_gzip
= GET_PROP(obj
, gzip
);
901 if (Z_LVAL_P(do_gzip
)) {
903 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC
);
911 if (Z_LVAL_P(do_cache
)) {
913 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
917 etag
= GET_PROP(obj
, eTag
);
919 lmod
= GET_PROP(obj
, lastModified
);
921 cctrl
= GET_PROP(obj
, cacheControl
);
923 ccraw
= GET_PROP(obj
, raw_cache_header
);
927 if (Z_LVAL_P(ccraw
)) {
929 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
931 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
935 char cc_header
[42] = {0};
937 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
939 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
941 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
953 zval
*ctype
= GET_PROP(obj
, contentType
);
955 if (Z_STRLEN_P(ctype
)) {
957 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
961 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
969 /* content disposition */
973 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
975 if (Z_STRLEN_P(dispo_file
)) {
977 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
979 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
991 zval
*send_mode
= GET_PROP(obj
, send_mode
);
993 switch (Z_LVAL_P(send_mode
))
1001 zval
*zdata
= GET_PROP(obj
, data
);
1003 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
1013 php_stream
*the_real_stream
;
1015 zval
*the_stream
= GET_PROP(obj
, stream
);
1017 php_stream_from_zval(the_real_stream
, &the_stream
);
1019 RETURN_SUCCESS(http_send_stream(the_real_stream
));
1029 zval
*zfile
= GET_PROP(obj
, file
);
1031 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
1047 /* {{{ HttpMessage */
1051 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
1055 * Create an HttpMessage object from a string.
1059 PHP_METHOD(HttpMessage
, fromString
)
1063 char *string
= NULL
;
1067 http_message
*msg
= NULL
;
1069 http_message_object obj
;
1073 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
1081 if (!(msg
= http_message_parse(string
, length
))) {
1089 Z_TYPE_P(return_value
) = IS_OBJECT
;
1091 return_value
->value
.obj
= http_message_object_from_msg(msg
);
1099 /* {{{ proto void HttpMessage::__construct([string message])
1103 * Instantiate a new HttpMessage object.
1107 PHP_METHOD(HttpMessage
, __construct
)
1111 char *message
= NULL
;
1115 getObject(http_message_object
, obj
);
1119 SET_EH_THROW_HTTP();
1121 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &message
, &length
) && message
&& length
) {
1123 if (obj
->message
= http_message_parse(message
, length
)) {
1125 if (obj
->message
->parent
) {
1127 obj
->parent
= http_message_object_from_msg(obj
->message
->parent
);
1133 } else if (!obj
->message
) {
1135 obj
->message
= http_message_new();
1147 /* {{{ proto string HttpMessage::getBody()
1151 * Get the body of the parsed Message.
1155 PHP_METHOD(HttpMessage
, getBody
)
1161 getObject(http_message_object
, obj
);
1169 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
1177 /* {{{ proto array HttpMessage::getHeaders()
1181 * Get Message Headers.
1185 PHP_METHOD(HttpMessage
, getHeaders
)
1191 getObject(http_message_object
, obj
);
1199 Z_ARRVAL(headers
) = &obj
->message
->hdrs
;
1201 array_init(return_value
);
1203 array_copy(&headers
, return_value
);
1211 /* {{{ proto void HttpMessage::setHeaders(array headers)
1219 PHP_METHOD(HttpMessage
, setHeaders
)
1223 zval
*new_headers
, old_headers
;
1225 getObject(http_message_object
, obj
);
1229 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
1237 zend_hash_clean(&obj
->message
->hdrs
);
1239 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
1241 array_copy(new_headers
, &old_headers
);
1249 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
1253 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
1257 PHP_METHOD(HttpMessage
, addHeaders
)
1261 zval old_headers
, *new_headers
;
1263 zend_bool append
= 0;
1265 getObject(http_message_object
, obj
);
1269 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
1277 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
1281 array_append(new_headers
, &old_headers
);
1285 array_merge(new_headers
, &old_headers
);
1295 /* {{{ proto long HttpMessage::getType()
1299 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
1303 PHP_METHOD(HttpMessage
, getType
)
1307 getObject(http_message_object
, obj
);
1315 RETURN_LONG(obj
->message
->type
);
1323 /* {{{ proto void HttpMessage::setType(long type)
1327 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
1331 PHP_METHOD(HttpMessage
, setType
)
1337 getObject(http_message_object
, obj
);
1341 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
1347 http_message_set_type(obj
->message
, type
);
1355 /* {{{ proto long HttpMessage::getResponseCode()
1359 * Get the Response Code of the Message.
1363 PHP_METHOD(HttpMessage
, getResponseCode
)
1367 getObject(http_message_object
, obj
);
1375 if (!HTTP_MSG_TYPE(RESPONSE
, obj
->message
)) {
1377 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
1385 RETURN_LONG(obj
->message
->info
.response
.code
);
1393 /* {{{ proto bool HttpMessage::setResponseCode(long code)
1397 * Set the response code of an HTTP Response Message.
1399 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
1401 * or if the response code is out of range (100-510).
1405 PHP_METHOD(HttpMessage
, setResponseCode
)
1411 getObject(http_message_object
, obj
);
1415 if (obj
->message
->type
!= HTTP_MSG_RESPONSE
) {
1417 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
1425 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
1431 if (code
< 100 || code
> 510) {
1433 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid response code (100-510): %ld", code
);
1441 obj
->message
->info
.response
.code
= code
;
1451 /* {{{ proto string HttpMessage::getRequestMethod()
1455 * Get the Request Method of the Message.
1457 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
1461 PHP_METHOD(HttpMessage
, getRequestMethod
)
1465 getObject(http_message_object
, obj
);
1473 if (obj
->message
->type
!= HTTP_MSG_REQUEST
) {
1475 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
1483 RETURN_STRING(obj
->message
->info
.request
.method
, 1);
1491 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
1495 * Set the Request Method of the HTTP Message.
1497 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
1501 PHP_METHOD(HttpMessage
, setRequestMethod
)
1509 getObject(http_message_object
, obj
);
1513 if (obj
->message
->type
!= HTTP_MSG_REQUEST
) {
1515 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
1523 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1529 if (method_len
< 1) {
1531 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
1537 if (SUCCESS
!= http_check_method(method
)) {
1539 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Unkown request method: %s", method
);
1547 if (obj
->message
->info
.request
.method
) {
1549 efree(obj
->message
->info
.request
.method
);
1553 obj
->message
->info
.request
.method
= estrndup(method
, method_len
);
1563 /* {{{ proto string HttpMessage::getRequestUri()
1567 * Get the Request URI of the Message.
1571 PHP_METHOD(HttpMessage
, getRequestUri
)
1577 getObject(http_message_object
, obj
);
1585 if (obj
->message
->type
!= HTTP_MSG_REQUEST
) {
1587 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
1595 RETURN_STRING(obj
->message
->info
.request
.URI
, 1);
1603 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
1607 * Set the Request URI of the HTTP Message.
1609 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
1611 * or if paramtere URI was empty.
1615 PHP_METHOD(HttpMessage
, setRequestUri
)
1623 getObject(http_message_object
, obj
);
1627 if (obj
->message
->type
!= HTTP_MSG_REQUEST
) {
1629 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
1635 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
1643 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
1651 if (obj
->message
->info
.request
.URI
) {
1653 efree(obj
->message
->info
.request
.URI
);
1657 obj
->message
->info
.request
.URI
= estrndup(URI
, URIlen
);
1667 /* {{{ proto string HttpMessage::getHttpVersion()
1671 * Get the HTTP Protocol Version of the Message.
1675 PHP_METHOD(HttpMessage
, getHttpVersion
)
1683 getObject(http_message_object
, obj
);
1691 switch (obj
->message
->type
)
1695 case HTTP_MSG_RESPONSE
:
1697 version
= obj
->message
->info
.response
.http_version
;
1703 case HTTP_MSG_REQUEST
:
1705 version
= obj
->message
->info
.request
.http_version
;
1719 sprintf(ver
, "%1.1f", version
);
1721 RETURN_STRINGL(ver
, 3, 1);
1729 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
1733 * Set the HTTP Protocol version of the Message.
1735 * Returns false if version is invalid (1.0 and 1.1).
1739 PHP_METHOD(HttpMessage
, setHttpVersion
)
1747 getObject(http_message_object
, obj
);
1751 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
1759 if (obj
->message
->type
== HTTP_MSG_NONE
) {
1761 http_error(E_WARNING
, HTTP_E_MSG
, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
1769 convert_to_double_ex(&zv
);
1771 sprintf(v
, "%1.1f", Z_DVAL_P(zv
));
1773 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
1775 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
1783 if (obj
->message
->type
== HTTP_MSG_RESPONSE
) {
1785 obj
->message
->info
.response
.http_version
= (float) Z_DVAL_P(zv
);
1789 obj
->message
->info
.request
.http_version
= (float) Z_DVAL_P(zv
);
1801 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1805 * Get parent Message.
1809 PHP_METHOD(HttpMessage
, getParentMessage
)
1813 getObject(http_message_object
, obj
);
1821 if (obj
->message
->parent
) {
1823 RETVAL_OBJVAL(obj
->parent
);
1837 /* {{{ proto bool HttpMessage::send()
1841 * Send the Message according to its type as Response or Request.
1845 PHP_METHOD(HttpMessage
, send
)
1849 getObject(http_message_object
, obj
);
1857 RETURN_SUCCESS(http_message_send(obj
->message
));
1865 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
1869 * Get the string representation of the Message.
1873 PHP_METHOD(HttpMessage
, toString
)
1881 zend_bool include_parent
= 1;
1883 getObject(http_message_object
, obj
);
1887 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &include_parent
)) {
1895 if (include_parent
) {
1897 http_message_serialize(obj
->message
, &string
, &length
);
1901 http_message_tostring(obj
->message
, &string
, &length
);
1905 RETURN_STRINGL(string
, length
, 0);
1917 #ifdef HTTP_HAVE_CURL
1919 /* {{{ HttpRequest */
1923 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
1927 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
1929 * and POST (including posting files) HTTP requests.
1933 PHP_METHOD(HttpRequest
, __construct
)
1943 getObject(http_request_object
, obj
);
1947 SET_EH_THROW_HTTP();
1949 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
1951 INIT_PARR(obj
, options
);
1953 INIT_PARR(obj
, responseInfo
);
1955 INIT_PARR(obj
, responseData
);
1957 INIT_PARR(obj
, postData
);
1959 INIT_PARR(obj
, postFiles
);
1965 UPD_PROP(obj
, string
, url
, URL
);
1971 UPD_PROP(obj
, long, method
, meth
);
1985 /* {{{ proto void HttpRequest::__destruct()
1989 * Destroys the HttpRequest object.
1993 PHP_METHOD(HttpRequest
, __destruct
)
1997 getObject(http_request_object
, obj
);
2005 FREE_PARR(obj
, options
);
2007 FREE_PARR(obj
, responseInfo
);
2009 FREE_PARR(obj
, responseData
);
2011 FREE_PARR(obj
, postData
);
2013 FREE_PARR(obj
, postFiles
);
2021 /* {{{ proto bool HttpRequest::setOptions(array options)
2025 * Set the request options to use. See http_get() for a full list of available options.
2029 PHP_METHOD(HttpRequest
, setOptions
)
2037 zval
*opts
, *old_opts
, **opt
;
2039 getObject(http_request_object
, obj
);
2043 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
2051 old_opts
= GET_PROP(obj
, options
);
2055 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
2057 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
2061 if (!strcmp(key
, "headers")) {
2065 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
2067 array_merge(*opt
, *headers
);
2073 } else if (!strcmp(key
, "cookies")) {
2077 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
2079 array_merge(*opt
, *cookies
);
2089 add_assoc_zval(old_opts
, key
, *opt
);
2111 /* {{{ proto array HttpRequest::getOptions()
2115 * Get current set options.
2119 PHP_METHOD(HttpRequest
, getOptions
)
2125 getObject(http_request_object
, obj
);
2133 opts
= GET_PROP(obj
, options
);
2135 array_init(return_value
);
2137 array_copy(opts
, return_value
);
2145 /* {{{ proto void HttpRequest::unsetOptions()
2149 * Unset all options/headers/cookies.
2153 PHP_METHOD(HttpRequest
, unsetOptions
)
2157 getObject(http_request_object
, obj
);
2165 FREE_PARR(obj
, options
);
2167 INIT_PARR(obj
, options
);
2175 /* {{{ proto bool HttpRequest::setSslOptions(array options)
2179 * Set additional SSL options.
2183 PHP_METHOD(HttpRequest
, setSslOptions
)
2187 zval
*opts
, *old_opts
, **ssl_options
;
2189 getObject(http_request_object
, obj
);
2193 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
2201 old_opts
= GET_PROP(obj
, options
);
2205 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
2207 array_merge(opts
, *ssl_options
);
2211 zval_add_ref(&opts
);
2213 add_assoc_zval(old_opts
, "ssl", opts
);
2227 /* {{{ proto array HttpRequest::getSslOtpions()
2231 * Get previously set SSL options.
2235 PHP_METHOD(HttpRequest
, getSslOptions
)
2239 zval
*opts
, **ssl_options
;
2241 getObject(http_request_object
, obj
);
2249 opts
= GET_PROP(obj
, options
);
2253 array_init(return_value
);
2257 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
2259 array_copy(*ssl_options
, return_value
);
2269 /* {{{ proto void HttpRequest::unsetSslOptions()
2273 * Unset previously set SSL options.
2277 PHP_METHOD(HttpRequest
, unsetSslOptions
)
2283 getObject(http_request_object
, obj
);
2291 opts
= GET_PROP(obj
, options
);
2293 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
2301 /* {{{ proto bool HttpRequest::addHeaders(array headers)
2305 * Add request header name/value pairs.
2309 PHP_METHOD(HttpRequest
, addHeaders
)
2313 zval
*opts
, **headers
, *new_headers
;
2315 getObject(http_request_object
, obj
);
2319 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
2327 opts
= GET_PROP(obj
, options
);
2331 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
2333 array_merge(new_headers
, *headers
);
2337 zval_add_ref(&new_headers
);
2339 add_assoc_zval(opts
, "headers", new_headers
);
2353 /* {{{ proto array HttpRequest::getHeaders()
2357 * Get previously set request headers.
2361 PHP_METHOD(HttpRequest
, getHeaders
)
2365 zval
*opts
, **headers
;
2367 getObject(http_request_object
, obj
);
2375 opts
= GET_PROP(obj
, options
);
2379 array_init(return_value
);
2383 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
2385 array_copy(*headers
, return_value
);
2395 /* {{{ proto void HttpRequest::unsetHeaders()
2399 * Unset previously set request headers.
2403 PHP_METHOD(HttpRequest
, unsetHeaders
)
2409 getObject(http_request_object
, obj
);
2417 opts
= GET_PROP(obj
, options
);
2419 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
2427 /* {{{ proto bool HttpRequest::addCookies(array cookies)
2435 PHP_METHOD(HttpRequest
, addCookies
)
2439 zval
*opts
, **cookies
, *new_cookies
;
2441 getObject(http_request_object
, obj
);
2445 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
2453 opts
= GET_PROP(obj
, options
);
2457 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
2459 array_merge(new_cookies
, *cookies
);
2463 zval_add_ref(&new_cookies
);
2465 add_assoc_zval(opts
, "cookies", new_cookies
);
2479 /* {{{ proto array HttpRequest::getCookies()
2483 * Get previously set cookies.
2487 PHP_METHOD(HttpRequest
, getCookies
)
2491 zval
*opts
, **cookies
;
2493 getObject(http_request_object
, obj
);
2501 opts
= GET_PROP(obj
, options
);
2505 array_init(return_value
);
2509 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
2511 array_copy(*cookies
, return_value
);
2521 /* {{{ proto void HttpRequest::unsetCookies()
2527 PHP_METHOD(HttpRequest
, unsetCookies
)
2533 getObject(http_request_object
, obj
);
2541 opts
= GET_PROP(obj
, options
);
2543 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
2551 /* {{{ proto bool HttpRequest::setURL(string url)
2555 * Set the request URL.
2559 PHP_METHOD(HttpRequest
, setURL
)
2567 getObject(http_request_object
, obj
);
2571 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
2579 UPD_PROP(obj
, string
, url
, URL
);
2589 /* {{{ proto string HttpRequest::getUrl()
2593 * Get the previously set request URL.
2597 PHP_METHOD(HttpRequest
, getURL
)
2603 getObject(http_request_object
, obj
);
2611 URL
= GET_PROP(obj
, url
);
2613 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
2621 /* {{{ proto bool HttpRequest::setMethod(long request_method)
2625 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
2627 * <tt>HTTP_POST</tt> constants.
2631 PHP_METHOD(HttpRequest
, setMethod
)
2637 getObject(http_request_object
, obj
);
2641 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
2649 UPD_PROP(obj
, long, method
, meth
);
2659 /* {{{ proto long HttpRequest::getMethod()
2663 * Get the previously set request method.
2667 PHP_METHOD(HttpRequest
, getMethod
)
2673 getObject(http_request_object
, obj
);
2681 meth
= GET_PROP(obj
, method
);
2683 RETURN_LONG(Z_LVAL_P(meth
));
2691 /* {{{ proto bool HttpRequest::setContentType(string content_type)
2695 * Set the content type the post request should have.
2697 * Use this only if you know what you're doing.
2701 PHP_METHOD(HttpRequest
, setContentType
)
2709 getObject(http_request_object
, obj
);
2713 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
2721 if (!strchr(ctype
, '/')) {
2723 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
2731 UPD_PROP(obj
, string
, contentType
, ctype
);
2741 /* {{{ proto string HttpRequest::getContentType()
2745 * Get the previously content type.
2749 PHP_METHOD(HttpRequest
, getContentType
)
2755 getObject(http_request_object
, obj
);
2763 ctype
= GET_PROP(obj
, contentType
);
2765 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
2773 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
2777 * Set the URL query parameters to use.
2779 * Overwrites previously set query parameters.
2781 * Affects any request types.
2785 PHP_METHOD(HttpRequest
, setQueryData
)
2791 char *query_data
= NULL
;
2793 getObject(http_request_object
, obj
);
2797 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
2805 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
2807 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
2813 UPD_PROP(obj
, string
, queryData
, query_data
);
2823 convert_to_string(qdata
);
2825 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
2835 /* {{{ proto string HttpRequest::getQueryData()
2839 * Get the current query data in form of an urlencoded query string.
2843 PHP_METHOD(HttpRequest
, getQueryData
)
2849 getObject(http_request_object
, obj
);
2857 qdata
= GET_PROP(obj
, queryData
);
2859 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
2867 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
2871 * Add parameters to the query parameter list.
2873 * Affects any request type.
2877 PHP_METHOD(HttpRequest
, addQueryData
)
2881 zval
*qdata
, *old_qdata
;
2883 char *query_data
= NULL
;
2885 getObject(http_request_object
, obj
);
2889 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
2897 old_qdata
= GET_PROP(obj
, queryData
);
2901 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
2909 UPD_PROP(obj
, string
, queryData
, query_data
);
2923 /* {{{ proto void HttpRequest::unsetQueryData()
2927 * Clean the query parameters.
2929 * Affects any request type.
2933 PHP_METHOD(HttpRequest
, unsetQueryData
)
2937 getObject(http_request_object
, obj
);
2945 UPD_PROP(obj
, string
, queryData
, "");
2953 /* {{{ proto bool HttpRequest::addPostData(array post_data)
2957 * Adds POST data entries.
2959 * Affects only POST requests.
2963 PHP_METHOD(HttpRequest
, addPostData
)
2967 zval
*post
, *post_data
;
2969 getObject(http_request_object
, obj
);
2973 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
2981 post
= GET_PROP(obj
, postData
);
2983 array_merge(post_data
, post
);
2995 /* {{{ proto bool HttpRequest::setPostData(array post_data)
2999 * Set the POST data entries.
3001 * Overwrites previously set POST data.
3003 * Affects only POST requests.
3007 PHP_METHOD(HttpRequest
, setPostData
)
3011 zval
*post
, *post_data
;
3013 getObject(http_request_object
, obj
);
3017 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
3025 post
= GET_PROP(obj
, postData
);
3027 zend_hash_clean(Z_ARRVAL_P(post
));
3029 array_copy(post_data
, post
);
3041 /* {{{ proto array HttpRequest::getPostData()
3045 * Get previously set POST data.
3049 PHP_METHOD(HttpRequest
, getPostData
)
3055 getObject(http_request_object
, obj
);
3063 post_data
= GET_PROP(obj
, postData
);
3065 array_init(return_value
);
3067 array_copy(post_data
, return_value
);
3075 /* {{{ proto void HttpRequest::unsetPostData()
3079 * Clean POST data entires.
3081 * Affects only POST requests.
3085 PHP_METHOD(HttpRequest
, unsetPostData
)
3091 getObject(http_request_object
, obj
);
3099 post_data
= GET_PROP(obj
, postData
);
3101 zend_hash_clean(Z_ARRVAL_P(post_data
));
3109 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
3113 * Add a file to the POST request.
3115 * Affects only POST requests.
3119 PHP_METHOD(HttpRequest
, addPostFile
)
3123 zval
*files
, *entry
;
3125 char *name
, *file
, *type
= NULL
;
3127 int name_len
, file_len
, type_len
= 0;
3129 getObject(http_request_object
, obj
);
3133 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
3143 if (!strchr(type
, '/')) {
3145 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
3153 type
= "application/x-octetstream";
3155 type_len
= sizeof("application/x-octetstream") - 1;
3161 MAKE_STD_ZVAL(entry
);
3167 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
3169 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
3171 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
3175 files
= GET_PROP(obj
, postFiles
);
3177 add_next_index_zval(files
, entry
);
3189 /* {{{ proto array HttpRequest::getPostFiles()
3193 * Get all previously added POST files.
3197 PHP_METHOD(HttpRequest
, getPostFiles
)
3203 getObject(http_request_object
, obj
);
3211 files
= GET_PROP(obj
, postFiles
);
3215 array_init(return_value
);
3217 array_copy(files
, return_value
);
3225 /* {{{ proto void HttpRequest::unsetPostFiles()
3229 * Unset the POST files list.
3231 * Affects only POST requests.
3235 PHP_METHOD(HttpRequest
, unsetPostFiles
)
3241 getObject(http_request_object
, obj
);
3249 files
= GET_PROP(obj
, postFiles
);
3251 zend_hash_clean(Z_ARRVAL_P(files
));
3259 /* {{{ proto array HttpRequest::getResponseData()
3263 * Get all response data after the request has been sent.
3267 PHP_METHOD(HttpRequest
, getResponseData
)
3273 getObject(http_request_object
, obj
);
3281 data
= GET_PROP(obj
, responseData
);
3283 array_init(return_value
);
3285 array_copy(data
, return_value
);
3293 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
3297 * Get response header(s) after the request has been sent.
3301 PHP_METHOD(HttpRequest
, getResponseHeader
)
3305 zval
*data
, **headers
, **header
;
3307 char *header_name
= NULL
;
3311 getObject(http_response_object
, obj
);
3315 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
3323 data
= GET_PROP(obj
, responseData
);
3325 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
3333 if (!header_len
|| !header_name
) {
3335 array_init(return_value
);
3337 array_copy(*headers
, return_value
);
3339 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
3341 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
3355 /* {{{ proto array HttpRequest::getResponseCookie([string name])
3359 * Get response cookie(s) after the request has been sent.
3363 PHP_METHOD(HttpRequest
, getResponseCookie
)
3367 zval
*data
, **headers
;
3369 char *cookie_name
= NULL
;
3373 getObject(http_request_object
, obj
);
3377 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
3385 array_init(return_value
);
3389 data
= GET_PROP(obj
, responseData
);
3391 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
3397 zval
**header
= NULL
;
3401 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
3403 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
3405 /* several cookies? */
3407 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
3413 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
3417 MAKE_STD_ZVAL(cookie_hash
);
3419 array_init(cookie_hash
);
3423 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
3427 add_next_index_zval(return_value
, cookie_hash
);
3435 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
3437 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
3439 add_next_index_zval(return_value
, cookie_hash
);
3441 return; /* <<< FOUND >>> */
3445 zval_dtor(cookie_hash
);
3455 zval_dtor(cookie_hash
);
3467 MAKE_STD_ZVAL(cookie_hash
);
3469 array_init(cookie_hash
);
3473 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
3477 add_next_index_zval(return_value
, cookie_hash
);
3485 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
3487 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
3489 add_next_index_zval(return_value
, cookie_hash
);
3493 zval_dtor(cookie_hash
);
3503 zval_dtor(cookie_hash
);
3529 /* {{{ proto string HttpRequest::getResponseBody()
3533 * Get the response body after the request has been sent.
3537 PHP_METHOD(HttpRequest
, getResponseBody
)
3543 getObject(http_request_object
, obj
);
3551 data
= GET_PROP(obj
, responseData
);
3553 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
3555 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
3569 /* {{{ proto int HttpRequest::getResponseCode()
3573 * Get the response code after the request has been sent.
3577 PHP_METHOD(HttpRequest
, getResponseCode
)
3583 getObject(http_request_object
, obj
);
3591 code
= GET_PROP(obj
, responseCode
);
3593 RETURN_LONG(Z_LVAL_P(code
));
3601 /* {{{ proto array HttpRequest::getResponseInfo([string name])
3605 * Get response info after the request has been sent.
3607 * See http_get() for a full list of returned info.
3611 PHP_METHOD(HttpRequest
, getResponseInfo
)
3615 zval
*info
, **infop
;
3617 char *info_name
= NULL
;
3621 getObject(http_request_object
, obj
);
3625 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
3633 info
= GET_PROP(obj
, responseInfo
);
3637 if (info_len
&& info_name
) {
3639 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
3641 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
3645 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Could not find response info named %s", info_name
);
3653 array_init(return_value
);
3655 array_copy(info
, return_value
);
3665 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
3669 * Get the full response as HttpMessage object.
3673 PHP_METHOD(HttpRequest
, getResponseMessage
)
3679 getObject(http_request_object
, obj
);
3687 message
= GET_PROP(obj
, responseMessage
);
3689 Z_TYPE_P(return_value
) = IS_OBJECT
;
3691 return_value
->is_ref
= 1;
3693 return_value
->value
.obj
= message
->value
.obj
;
3695 zval_add_ref(&return_value
);
3701 /* {{{ proto bool HttpRequest::send()
3705 * Send the HTTP request.
3715 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
3717 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
3719 * $r->addQueryData(array('category' => 3));
3725 * if ($r->getResponseCode() == 200) {
3727 * file_put_contents('local.rss', $r->getResponseBody());
3731 * } catch (HttpException $ex) {
3749 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
3751 * $r->setOptions(array('cookies' => array('lang' => 'de')));
3753 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
3755 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
3759 * echo $r->getResponseBody();
3769 PHP_METHOD(HttpRequest
, send
)
3773 STATUS status
= FAILURE
;
3775 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
3779 getObject(http_request_object
, obj
);
3787 SET_EH_THROW_HTTP();
3791 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
3793 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initilaize curl");
3801 meth
= GET_PROP(obj
, method
);
3803 URL
= GET_PROP(obj
, url
);
3805 qdata
= GET_PROP(obj
, queryData
);
3807 opts
= GET_PROP(obj
, options
);
3809 info
= GET_PROP(obj
, responseInfo
);
3811 resp
= GET_PROP(obj
, responseData
);
3815 // HTTP_URI_MAXLEN+1 long char *
3817 request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0);
3821 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
3823 if (!strchr(request_uri
, '?')) {
3825 strcat(request_uri
, "?");
3829 strcat(request_uri
, "&");
3833 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
3839 switch (Z_LVAL_P(meth
))
3847 status
= http_request_ex(obj
->ch
, Z_LVAL_P(meth
), request_uri
, NULL
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
3865 http_request_body body
;
3867 zval
*fields
= GET_PROP(obj
, postData
), *files
= GET_PROP(obj
, postFiles
);
3871 if (SUCCESS
== (status
= http_request_body_fill(&body
, Z_ARRVAL_P(fields
), Z_ARRVAL_P(files
)))) {
3873 status
= http_request_ex(obj
->ch
, Z_LVAL_P(meth
), request_uri
, &body
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
3875 http_request_body_dtor(&body
);
3891 /* final data handling */
3893 if (status
== SUCCESS
) {
3899 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->response
), PHPSTR_LEN(&obj
->response
))) {
3901 zval
*headers
, *message
;
3909 UPD_PROP(obj
, long, responseCode
, msg
->info
.response
.code
);
3913 MAKE_STD_ZVAL(headers
)
3915 array_init(headers
);
3919 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
3921 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
3925 add_assoc_zval(resp
, "headers", headers
);
3927 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
3931 message
= GET_PROP(obj
, responseMessage
);
3935 Z_TYPE_P(message
) = IS_OBJECT
;
3937 message
->value
.obj
= http_message_object_from_msg(msg
);
3939 SET_PROP(obj
, responseMessage
, message
);
3953 RETURN_SUCCESS(status
);
3961 #endif /* HTTP_HAVE_CURL */
3965 #endif /* ZEND_ENGINE_2 */
3979 * vim600: noet sw=4 ts=4 fdm=marker
3981 * vim<600: noet sw=4 ts=4