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 +----------------------------------------------------------------------+
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_cache_api.h"
27 #include "php_http_curl_api.h"
28 #include "php_http_date_api.h"
29 #include "php_http_headers_api.h"
30 #include "php_http_message_api.h"
31 #include "php_http_send_api.h"
32 #include "php_http_url_api.h"
34 #include "php_http_message_object.h"
35 #include "php_http_response_object.h"
36 #include "php_http_request_object.h"
37 #include "php_http_exception_object.h"
41 /* {{{ HttpResponse */
43 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
45 * Instantiates a new HttpResponse object, which can be used to send
46 * any data/resource/file to an HTTP client with caching and multiple
47 * ranges/resuming support.
49 * NOTE: GZIPping is not implemented yet.
51 PHP_METHOD(HttpResponse
, __construct
)
53 zend_bool do_cache
= 0, do_gzip
= 0;
54 getObject(http_response_object
, obj
);
57 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
58 UPD_PROP(obj
, long, cache
, do_cache
);
59 UPD_PROP(obj
, long, gzip
, do_gzip
);
65 /* {{{ proto bool HttpResponse::setCache(bool cache)
67 * Whether it sould be attempted to cache the entitity.
68 * This will result in necessary caching headers and checks of clients
69 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
70 * matches a "304 Not Modified" status code will be issued.
72 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
73 * to something more appropriate than "no-cache"!
75 PHP_METHOD(HttpResponse
, setCache
)
77 zend_bool do_cache
= 0;
78 getObject(http_response_object
, obj
);
80 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
84 UPD_PROP(obj
, long, cache
, do_cache
);
89 /* {{{ proto bool HttpResponse::getCache()
91 * Get current caching setting.
93 PHP_METHOD(HttpResponse
, getCache
)
95 zval
*do_cache
= NULL
;
96 getObject(http_response_object
, obj
);
100 do_cache
= GET_PROP(obj
, cache
);
101 RETURN_BOOL(Z_LVAL_P(do_cache
));
105 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
107 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
109 PHP_METHOD(HttpResponse
, setGzip
)
111 zend_bool do_gzip
= 0;
112 getObject(http_response_object
, obj
);
114 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
118 UPD_PROP(obj
, long, gzip
, do_gzip
);
123 /* {{{ proto bool HttpResponse::getGzip()
125 * Get current gzipping setting.
127 PHP_METHOD(HttpResponse
, getGzip
)
129 zval
*do_gzip
= NULL
;
130 getObject(http_response_object
, obj
);
134 do_gzip
= GET_PROP(obj
, gzip
);
135 RETURN_BOOL(Z_LVAL_P(do_gzip
));
139 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
141 * Set a custom cache-control header, usually being "private" or "public"; if
142 * $raw is set to true the header will be sent as-is.
144 PHP_METHOD(HttpResponse
, setCacheControl
)
149 getObject(http_response_object
, obj
);
151 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
155 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
156 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
160 UPD_PROP(obj
, long, raw_cache_header
, raw
);
161 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
166 /* {{{ proto string HttpResponse::getCacheControl()
168 * Get current Cache-Control header setting.
170 PHP_METHOD(HttpResponse
, getCacheControl
)
173 getObject(http_response_object
, obj
);
177 ccontrol
= GET_PROP(obj
, cacheControl
);
178 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
182 /* {{{ proto bool HttpResponse::setContentType(string content_type)
184 * Set the content-type of the sent entity.
186 PHP_METHOD(HttpResponse
, setContentType
)
190 getObject(http_response_object
, obj
);
192 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
196 if (!strchr(ctype
, '/')) {
197 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
201 UPD_PROP(obj
, string
, contentType
, ctype
);
207 /* {{{ proto string HttpResponse::getContentType()
209 * Get current Content-Type header setting.
211 PHP_METHOD(HttpResponse
, getContentType
)
214 getObject(http_response_object
, obj
);
218 ctype
= GET_PROP(obj
, contentType
);
219 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
223 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
225 * Set the Content-Disposition of the sent entity. This setting aims to suggest
226 * the receiveing user agent how to handle the sent entity; usually the client
227 * will show the user a "Save As..." popup.
229 PHP_METHOD(HttpResponse
, setContentDisposition
)
233 zend_bool is_inline
= 0;
234 getObject(http_response_object
, obj
);
236 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
240 UPD_PROP(obj
, string
, dispoFile
, file
);
241 UPD_PROP(obj
, long, dispoInline
, is_inline
);
246 /* {{{ proto array HttpResponse::getContentDisposition()
248 * Get current Content-Disposition setting.
249 * Will return an associative array like:
252 * 'filename' => 'foo.bar',
257 PHP_METHOD(HttpResponse
, getContentDisposition
)
261 getObject(http_response_object
, obj
);
263 if (ZEND_NUM_ARGS()) {
267 file
= GET_PROP(obj
, dispoFile
);
268 is_inline
= GET_PROP(obj
, dispoInline
);
270 array_init(return_value
);
271 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
272 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
276 /* {{{ proto bool HttpResponse::setETag(string etag)
278 * Set a custom ETag. Use this only if you know what you're doing.
280 PHP_METHOD(HttpResponse
, setETag
)
284 getObject(http_response_object
, obj
);
286 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
290 UPD_PROP(obj
, string
, eTag
, etag
);
295 /* {{{ proto string HttpResponse::getETag()
297 * Get the previously set custom ETag.
299 PHP_METHOD(HttpResponse
, getETag
)
302 getObject(http_response_object
, obj
);
306 etag
= GET_PROP(obj
, eTag
);
307 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
311 /* {{{ proto bool HttpResponse::setData(string data)
313 * Set the data to be sent.
315 PHP_METHOD(HttpResponse
, setData
)
318 getObject(http_response_object
, obj
);
320 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &the_data
)) {
324 convert_to_string_ex(&the_data
);
325 SET_PROP(obj
, data
, the_data
);
326 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_data
, SEND_DATA
));
327 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
332 /* {{{ proto string HttpResponse::getData()
334 * Get the previously set data to be sent.
336 PHP_METHOD(HttpResponse
, getData
)
339 getObject(http_response_object
, obj
);
343 the_data
= GET_PROP(obj
, data
);
344 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
348 /* {{{ proto bool HttpResponse::setStream(resource stream)
350 * Set the resource to be sent.
352 PHP_METHOD(HttpResponse
, setStream
)
355 php_stream
*the_real_stream
;
356 getObject(http_response_object
, obj
);
358 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
362 php_stream_from_zval(the_real_stream
, &the_stream
);
364 SET_PROP(obj
, stream
, the_stream
);
365 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_real_stream
, SEND_RSRC
));
366 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
371 /* {{{ proto resource HttpResponse::getStream()
373 * Get the previously set resource to be sent.
375 PHP_METHOD(HttpResponse
, getStream
)
378 getObject(http_response_object
, obj
);
382 the_stream
= GET_PROP(obj
, stream
);
383 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
387 /* {{{ proto bool HttpResponse::setFile(string file)
389 * Set the file to be sent.
391 PHP_METHOD(HttpResponse
, setFile
)
394 getObject(http_response_object
, obj
);
396 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
400 convert_to_string_ex(&the_file
);
402 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
403 UPD_PROP(obj
, long, lastModified
, http_last_modified(the_file
, -1));
404 UPD_PROP(obj
, long, send_mode
, -1);
409 /* {{{ proto string HttpResponse::getFile()
411 * Get the previously set file to be sent.
413 PHP_METHOD(HttpResponse
, getFile
)
416 getObject(http_response_object
, obj
);
420 the_file
= GET_PROP(obj
, file
);
421 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
425 /* {{{ proto bool HttpResponse::send()
427 * Finally send the entity.
432 * $r = new HttpResponse(true);
433 * $r->setFile('../hidden/contract.pdf');
434 * $r->setContentType('application/pdf');
440 PHP_METHOD(HttpResponse
, send
)
442 zval
*do_cache
, *do_gzip
;
443 getObject(http_response_object
, obj
);
447 do_cache
= GET_PROP(obj
, cache
);
448 do_gzip
= GET_PROP(obj
, gzip
);
451 if (Z_LVAL_P(do_gzip
)) {
452 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC
);
456 if (Z_LVAL_P(do_cache
)) {
457 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
459 etag
= GET_PROP(obj
, eTag
);
460 lmod
= GET_PROP(obj
, lastModified
);
461 cctrl
= GET_PROP(obj
, cacheControl
);
462 ccraw
= GET_PROP(obj
, raw_cache_header
);
464 if (Z_LVAL_P(ccraw
)) {
465 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
466 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
));
468 char cc_header
[42] = {0};
469 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
470 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
471 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
477 zval
*ctype
= GET_PROP(obj
, contentType
);
478 if (Z_STRLEN_P(ctype
)) {
479 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
481 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
485 /* content disposition */
487 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
488 if (Z_STRLEN_P(dispo_file
)) {
489 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
490 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
496 zval
*send_mode
= GET_PROP(obj
, send_mode
);
497 switch (Z_LVAL_P(send_mode
))
501 zval
*zdata
= GET_PROP(obj
, data
);
502 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
507 php_stream
*the_real_stream
;
508 zval
*the_stream
= GET_PROP(obj
, stream
);
509 php_stream_from_zval(the_real_stream
, &the_stream
);
510 RETURN_SUCCESS(http_send_stream(the_real_stream
));
515 zval
*zfile
= GET_PROP(obj
, file
);
516 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
524 /* {{{ HttpMessage */
526 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
528 * Create an HttpMessage object from a string.
530 PHP_METHOD(HttpMessage
, fromString
)
534 http_message
*msg
= NULL
;
535 http_message_object obj
;
537 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &string
, &length
)) {
541 if (!(msg
= http_message_parse(string
, length
))) {
545 Z_TYPE_P(return_value
) = IS_OBJECT
;
546 return_value
->value
.obj
= http_message_object_from_msg(msg
);
550 /* {{{ proto string HttpMessage::getBody()
552 * Get the body of the parsed Message.
554 PHP_METHOD(HttpMessage
, getBody
)
557 getObject(http_message_object
, obj
);
561 RETURN_PHPSTR(&obj
->message
->body
, PHPSTR_FREE_NOT
, 1);
565 /* {{{ proto array HttpMessage::getHeaders()
567 * Get Message Headers.
569 PHP_METHOD(HttpMessage
, getHeaders
)
572 getObject(http_message_object
, obj
);
576 Z_ARRVAL(headers
) = &obj
->message
->hdrs
;
577 array_init(return_value
);
578 array_copy(&headers
, return_value
);
582 /* {{{ proto void HttpMessage::setHeaders(array headers)
586 PHP_METHOD(HttpMessage
, setHeaders
)
588 zval
*new_headers
, old_headers
;
589 getObject(http_message_object
, obj
);
591 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
595 zend_hash_clean(&obj
->message
->hdrs
);
596 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
597 array_copy(new_headers
, &old_headers
);
601 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
603 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
605 PHP_METHOD(HttpMessage
, addHeaders
)
607 zval old_headers
, *new_headers
;
608 zend_bool append
= 0;
609 getObject(http_message_object
, obj
);
611 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &new_headers
, &append
)) {
615 Z_ARRVAL(old_headers
) = &obj
->message
->hdrs
;
617 array_append(new_headers
, &old_headers
);
619 array_merge(new_headers
, &old_headers
);
624 /* {{{ proto long HttpMessage::getType()
626 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
628 PHP_METHOD(HttpMessage
, getType
)
630 getObject(http_message_object
, obj
);
634 RETURN_LONG(obj
->message
->type
);
638 /* {{{ proto void HttpMessage::setType(long type)
640 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
642 PHP_METHOD(HttpMessage
, setType
)
645 getObject(http_message_object
, obj
);
647 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &type
)) {
650 if (type
!= obj
->message
->type
) {
651 if (obj
->message
->type
== HTTP_MSG_REQUEST
) {
652 if (obj
->message
->info
.request
.method
) {
653 efree(obj
->message
->info
.request
.method
);
655 if (obj
->message
->info
.request
.URI
) {
656 efree(obj
->message
->info
.request
.URI
);
659 obj
->message
->type
= type
;
660 if (obj
->message
->type
== HTTP_MSG_REQUEST
) {
661 obj
->message
->info
.request
.method
= NULL
;
662 obj
->message
->info
.request
.URI
= NULL
;
668 /* {{{ proto long HttpMessage::getResponseCode()
670 * Get the Response Code of the Message.
672 PHP_METHOD(HttpMessage
, getResponseCode
)
674 getObject(http_message_object
, obj
);
678 if (obj
->message
->type
!= HTTP_MSG_RESPONSE
) {
679 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
683 RETURN_LONG(obj
->message
->info
.response
.code
);
687 /* {{{ proto bool HttpMessage::setResponseCode(long code)
689 * Set the response code of an HTTP Response Message.
690 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
691 * or if the response code is out of range (100-510).
693 PHP_METHOD(HttpMessage
, setResponseCode
)
696 getObject(http_message_object
, obj
);
698 if (obj
->message
->type
!= HTTP_MSG_RESPONSE
) {
699 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_RESPONSE");
703 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &code
)) {
706 if (code
< 100 || code
> 510) {
707 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid response code (100-510): %ld", code
);
711 obj
->message
->info
.response
.code
= code
;
716 /* {{{ proto string HttpMessage::getRequestMethod()
718 * Get the Request Method of the Message.
719 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
721 PHP_METHOD(HttpMessage
, getRequestMethod
)
723 getObject(http_message_object
, obj
);
727 if (obj
->message
->type
!= HTTP_MSG_REQUEST
) {
728 http_error(E_NOTICE
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
732 RETURN_STRING(obj
->message
->info
.request
.method
, 1);
736 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
738 * Set the Request Method of the HTTP Message.
739 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
741 PHP_METHOD(HttpMessage
, setRequestMethod
)
745 getObject(http_message_object
, obj
);
747 if (obj
->message
->type
!= HTTP_MSG_REQUEST
) {
748 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
752 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
755 if (method_len
< 1) {
756 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestMethod to an empty string");
759 if (SUCCESS
!= http_check_method(method
)) {
760 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Unkown request method: %s", method
);
764 if (obj
->message
->info
.request
.method
) {
765 efree(obj
->message
->info
.request
.method
);
767 obj
->message
->info
.request
.method
= estrndup(method
, method_len
);
772 /* {{{ proto string HttpMessage::getRequestUri()
774 * Get the Request URI of the Message.
776 PHP_METHOD(HttpMessage
, getRequestUri
)
779 getObject(http_message_object
, obj
);
783 if (obj
->message
->type
!= HTTP_MSG_REQUEST
) {
784 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
788 RETURN_STRING(obj
->message
->info
.request
.URI
, 1);
792 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
794 * Set the Request URI of the HTTP Message.
795 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
796 * or if paramtere URI was empty.
798 PHP_METHOD(HttpMessage
, setRequestUri
)
802 getObject(http_message_object
, obj
);
804 if (obj
->message
->type
!= HTTP_MSG_REQUEST
) {
805 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is not of type HTTP_MSG_REQUEST");
808 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URI
, &URIlen
)) {
812 http_error(E_WARNING
, HTTP_E_PARAM
, "Cannot set HttpMessage::requestUri to an empty string");
816 if (obj
->message
->info
.request
.URI
) {
817 efree(obj
->message
->info
.request
.URI
);
819 obj
->message
->info
.request
.URI
= estrndup(URI
, URIlen
);
824 /* {{{ proto string HttpMessage::getHttpVersion()
826 * Get the HTTP Protocol Version of the Message.
828 PHP_METHOD(HttpMessage
, getHttpVersion
)
832 getObject(http_message_object
, obj
);
836 switch (obj
->message
->type
)
838 case HTTP_MSG_RESPONSE
:
839 version
= &obj
->message
->info
.response
.http_version
;
841 case HTTP_MSG_REQUEST
:
842 version
= &obj
->message
->info
.request
.http_version
;
848 sprintf(ver
, "1.1f", version
);
849 RETURN_STRINGL(ver
, 3, 1);
853 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
855 * Set the HTTP Protocol version of the Message.
856 * Returns false if version is invalid (1.0 and 1.1).
858 PHP_METHOD(HttpMessage
, setHttpVersion
)
862 getObject(http_message_object
, obj
);
864 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &zv
)) {
868 if (obj
->message
->type
== HTTP_MSG_NONE
) {
869 http_error(E_WARNING
, HTTP_E_MSG
, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
873 convert_to_double_ex(&zv
);
874 sprintf(v
, "%1.1f", Z_DVAL_P(zv
));
875 if (strcmp(v
, "1.0") && strcmp(v
, "1.1")) {
876 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Invalid HTTP protocol version (1.0 or 1.1): %s", v
);
880 if (obj
->message
->type
== HTTP_MSG_RESPONSE
) {
881 obj
->message
->info
.response
.http_version
= (float) Z_DVAL_P(zv
);
883 obj
->message
->info
.request
.http_version
= (float) Z_DVAL_P(zv
);
889 /* {{{ proto HttpMessage HttpMessage::getNestedMessage()
891 * Get nested Message.
893 PHP_METHOD(HttpMessage
, getNestedMessage
)
895 getObject(http_message_object
, obj
);
899 if (obj
->message
->nested
) {
900 Z_TYPE_P(return_value
) = IS_OBJECT
;
901 return_value
->value
.obj
= obj
->nested
;
902 return_value
->is_ref
= 1;
903 zend_objects_store_add_ref(return_value TSRMLS_CC
);
910 /* {{{ proto string HttpMessage::toString()
912 * Get the string representation of the Message.
914 PHP_METHOD(HttpMessage
, toString
)
918 getObject(http_message_object
, obj
);
922 http_message_tostring(obj
->message
, &string
, &length
);
923 RETURN_STRINGL(string
, length
, 0);
929 #ifdef HTTP_HAVE_CURL
930 /* {{{ HttpRequest */
932 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
934 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
935 * and POST (including posting files) HTTP requests.
937 PHP_METHOD(HttpRequest
, __construct
)
942 getObject(http_request_object
, obj
);
945 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
946 INIT_PARR(obj
, options
);
947 INIT_PARR(obj
, responseInfo
);
948 INIT_PARR(obj
, responseData
);
949 INIT_PARR(obj
, postData
);
950 INIT_PARR(obj
, postFiles
);
953 UPD_PROP(obj
, string
, url
, URL
);
956 UPD_PROP(obj
, long, method
, meth
);
963 /* {{{ proto void HttpRequest::__destruct()
965 * Destroys the HttpRequest object.
967 PHP_METHOD(HttpRequest
, __destruct
)
969 getObject(http_request_object
, obj
);
973 FREE_PARR(obj
, options
);
974 FREE_PARR(obj
, responseInfo
);
975 FREE_PARR(obj
, responseData
);
976 FREE_PARR(obj
, postData
);
977 FREE_PARR(obj
, postFiles
);
981 /* {{{ proto bool HttpRequest::setOptions(array options)
983 * Set the request options to use. See http_get() for a full list of available options.
985 PHP_METHOD(HttpRequest
, setOptions
)
989 zval
*opts
, *old_opts
, **opt
;
990 getObject(http_request_object
, obj
);
992 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
996 old_opts
= GET_PROP(obj
, options
);
998 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
999 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
1001 if (!strcmp(key
, "headers")) {
1003 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1004 array_merge(*opt
, *headers
);
1007 } else if (!strcmp(key
, "cookies")) {
1009 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1010 array_merge(*opt
, *cookies
);
1015 add_assoc_zval(old_opts
, key
, *opt
);
1026 /* {{{ proto array HttpRequest::getOptions()
1028 * Get current set options.
1030 PHP_METHOD(HttpRequest
, getOptions
)
1033 getObject(http_request_object
, obj
);
1037 opts
= GET_PROP(obj
, options
);
1038 array_init(return_value
);
1039 array_copy(opts
, return_value
);
1043 /* {{{ proto void HttpRequest::unsetOptions()
1045 * Unset all options/headers/cookies.
1047 PHP_METHOD(HttpRequest
, unsetOptions
)
1049 getObject(http_request_object
, obj
);
1053 FREE_PARR(obj
, options
);
1054 INIT_PARR(obj
, options
);
1058 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1060 * Set additional SSL options.
1062 PHP_METHOD(HttpRequest
, setSslOptions
)
1064 zval
*opts
, *old_opts
, **ssl_options
;
1065 getObject(http_request_object
, obj
);
1067 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
1071 old_opts
= GET_PROP(obj
, options
);
1073 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1074 array_merge(opts
, *ssl_options
);
1076 zval_add_ref(&opts
);
1077 add_assoc_zval(old_opts
, "ssl", opts
);
1084 /* {{{ proto array HttpRequest::getSslOtpions()
1086 * Get previously set SSL options.
1088 PHP_METHOD(HttpRequest
, getSslOptions
)
1090 zval
*opts
, **ssl_options
;
1091 getObject(http_request_object
, obj
);
1095 opts
= GET_PROP(obj
, options
);
1097 array_init(return_value
);
1099 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
1100 array_copy(*ssl_options
, return_value
);
1105 /* {{{ proto void HttpRequest::unsetSslOptions()
1107 * Unset previously set SSL options.
1109 PHP_METHOD(HttpRequest
, unsetSslOptions
)
1112 getObject(http_request_object
, obj
);
1116 opts
= GET_PROP(obj
, options
);
1117 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
1121 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1123 * Add request header name/value pairs.
1125 PHP_METHOD(HttpRequest
, addHeaders
)
1127 zval
*opts
, **headers
, *new_headers
;
1128 getObject(http_request_object
, obj
);
1130 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
1134 opts
= GET_PROP(obj
, options
);
1136 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1137 array_merge(new_headers
, *headers
);
1139 zval_add_ref(&new_headers
);
1140 add_assoc_zval(opts
, "headers", new_headers
);
1147 /* {{{ proto array HttpRequest::getHeaders()
1149 * Get previously set request headers.
1151 PHP_METHOD(HttpRequest
, getHeaders
)
1153 zval
*opts
, **headers
;
1154 getObject(http_request_object
, obj
);
1158 opts
= GET_PROP(obj
, options
);
1160 array_init(return_value
);
1162 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
1163 array_copy(*headers
, return_value
);
1168 /* {{{ proto void HttpRequest::unsetHeaders()
1170 * Unset previously set request headers.
1172 PHP_METHOD(HttpRequest
, unsetHeaders
)
1175 getObject(http_request_object
, obj
);
1179 opts
= GET_PROP(obj
, options
);
1180 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
1184 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1188 PHP_METHOD(HttpRequest
, addCookies
)
1190 zval
*opts
, **cookies
, *new_cookies
;
1191 getObject(http_request_object
, obj
);
1193 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
1197 opts
= GET_PROP(obj
, options
);
1199 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1200 array_merge(new_cookies
, *cookies
);
1202 zval_add_ref(&new_cookies
);
1203 add_assoc_zval(opts
, "cookies", new_cookies
);
1210 /* {{{ proto array HttpRequest::getCookies()
1212 * Get previously set cookies.
1214 PHP_METHOD(HttpRequest
, getCookies
)
1216 zval
*opts
, **cookies
;
1217 getObject(http_request_object
, obj
);
1221 opts
= GET_PROP(obj
, options
);
1223 array_init(return_value
);
1225 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
1226 array_copy(*cookies
, return_value
);
1231 /* {{{ proto void HttpRequest::unsetCookies()
1234 PHP_METHOD(HttpRequest
, unsetCookies
)
1237 getObject(http_request_object
, obj
);
1241 opts
= GET_PROP(obj
, options
);
1242 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
1246 /* {{{ proto bool HttpRequest::setURL(string url)
1248 * Set the request URL.
1250 PHP_METHOD(HttpRequest
, setURL
)
1254 getObject(http_request_object
, obj
);
1256 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1260 UPD_PROP(obj
, string
, url
, URL
);
1265 /* {{{ proto string HttpRequest::getUrl()
1267 * Get the previously set request URL.
1269 PHP_METHOD(HttpRequest
, getURL
)
1272 getObject(http_request_object
, obj
);
1276 URL
= GET_PROP(obj
, url
);
1277 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
1281 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1283 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1284 * <tt>HTTP_POST</tt> constants.
1286 PHP_METHOD(HttpRequest
, setMethod
)
1289 getObject(http_request_object
, obj
);
1291 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1295 UPD_PROP(obj
, long, method
, meth
);
1300 /* {{{ proto long HttpRequest::getMethod()
1302 * Get the previously set request method.
1304 PHP_METHOD(HttpRequest
, getMethod
)
1307 getObject(http_request_object
, obj
);
1311 meth
= GET_PROP(obj
, method
);
1312 RETURN_LONG(Z_LVAL_P(meth
));
1316 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1318 * Set the content type the post request should have.
1319 * Use this only if you know what you're doing.
1321 PHP_METHOD(HttpRequest
, setContentType
)
1325 getObject(http_request_object
, obj
);
1327 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1331 if (!strchr(ctype
, '/')) {
1332 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
1336 UPD_PROP(obj
, string
, contentType
, ctype
);
1341 /* {{{ proto string HttpRequest::getContentType()
1343 * Get the previously content type.
1345 PHP_METHOD(HttpRequest
, getContentType
)
1348 getObject(http_request_object
, obj
);
1352 ctype
= GET_PROP(obj
, contentType
);
1353 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
1357 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1359 * Set the URL query parameters to use.
1360 * Overwrites previously set query parameters.
1361 * Affects any request types.
1363 PHP_METHOD(HttpRequest
, setQueryData
)
1366 char *query_data
= NULL
;
1367 getObject(http_request_object
, obj
);
1369 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
1373 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1374 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1377 UPD_PROP(obj
, string
, queryData
, query_data
);
1382 convert_to_string(qdata
);
1383 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
1388 /* {{{ proto string HttpRequest::getQueryData()
1390 * Get the current query data in form of an urlencoded query string.
1392 PHP_METHOD(HttpRequest
, getQueryData
)
1395 getObject(http_request_object
, obj
);
1399 qdata
= GET_PROP(obj
, queryData
);
1400 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
1404 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1406 * Add parameters to the query parameter list.
1407 * Affects any request type.
1409 PHP_METHOD(HttpRequest
, addQueryData
)
1411 zval
*qdata
, *old_qdata
;
1412 char *query_data
= NULL
;
1413 getObject(http_request_object
, obj
);
1415 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1419 old_qdata
= GET_PROP(obj
, queryData
);
1421 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1425 UPD_PROP(obj
, string
, queryData
, query_data
);
1432 /* {{{ proto void HttpRequest::unsetQueryData()
1434 * Clean the query parameters.
1435 * Affects any request type.
1437 PHP_METHOD(HttpRequest
, unsetQueryData
)
1439 getObject(http_request_object
, obj
);
1443 UPD_PROP(obj
, string
, queryData
, "");
1447 /* {{{ proto bool HttpRequest::addPostData(array post_data)
1449 * Adds POST data entries.
1450 * Affects only POST requests.
1452 PHP_METHOD(HttpRequest
, addPostData
)
1454 zval
*post
, *post_data
;
1455 getObject(http_request_object
, obj
);
1457 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1461 post
= GET_PROP(obj
, postData
);
1462 array_merge(post_data
, post
);
1468 /* {{{ proto bool HttpRequest::setPostData(array post_data)
1470 * Set the POST data entries.
1471 * Overwrites previously set POST data.
1472 * Affects only POST requests.
1474 PHP_METHOD(HttpRequest
, setPostData
)
1476 zval
*post
, *post_data
;
1477 getObject(http_request_object
, obj
);
1479 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1483 post
= GET_PROP(obj
, postData
);
1484 zend_hash_clean(Z_ARRVAL_P(post
));
1485 array_copy(post_data
, post
);
1491 /* {{{ proto array HttpRequest::getPostData()
1493 * Get previously set POST data.
1495 PHP_METHOD(HttpRequest
, getPostData
)
1498 getObject(http_request_object
, obj
);
1502 post_data
= GET_PROP(obj
, postData
);
1503 array_init(return_value
);
1504 array_copy(post_data
, return_value
);
1508 /* {{{ proto void HttpRequest::unsetPostData()
1510 * Clean POST data entires.
1511 * Affects only POST requests.
1513 PHP_METHOD(HttpRequest
, unsetPostData
)
1516 getObject(http_request_object
, obj
);
1520 post_data
= GET_PROP(obj
, postData
);
1521 zend_hash_clean(Z_ARRVAL_P(post_data
));
1525 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1527 * Add a file to the POST request.
1528 * Affects only POST requests.
1530 PHP_METHOD(HttpRequest
, addPostFile
)
1532 zval
*files
, *entry
;
1533 char *name
, *file
, *type
= NULL
;
1534 int name_len
, file_len
, type_len
= 0;
1535 getObject(http_request_object
, obj
);
1537 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1542 if (!strchr(type
, '/')) {
1543 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1547 type
= "application/x-octetstream";
1548 type_len
= sizeof("application/x-octetstream") - 1;
1551 MAKE_STD_ZVAL(entry
);
1554 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1555 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1556 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1558 files
= GET_PROP(obj
, postFiles
);
1559 add_next_index_zval(files
, entry
);
1565 /* {{{ proto array HttpRequest::getPostFiles()
1567 * Get all previously added POST files.
1569 PHP_METHOD(HttpRequest
, getPostFiles
)
1572 getObject(http_request_object
, obj
);
1576 files
= GET_PROP(obj
, postFiles
);
1578 array_init(return_value
);
1579 array_copy(files
, return_value
);
1583 /* {{{ proto void HttpRequest::unsetPostFiles()
1585 * Unset the POST files list.
1586 * Affects only POST requests.
1588 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1591 getObject(http_request_object
, obj
);
1595 files
= GET_PROP(obj
, postFiles
);
1596 zend_hash_clean(Z_ARRVAL_P(files
));
1600 /* {{{ proto array HttpRequest::getResponseData()
1602 * Get all response data after the request has been sent.
1604 PHP_METHOD(HttpRequest
, getResponseData
)
1607 getObject(http_request_object
, obj
);
1611 data
= GET_PROP(obj
, responseData
);
1612 array_init(return_value
);
1613 array_copy(data
, return_value
);
1617 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1619 * Get response header(s) after the request has been sent.
1621 PHP_METHOD(HttpRequest
, getResponseHeader
)
1623 zval
*data
, **headers
, **header
;
1624 char *header_name
= NULL
;
1626 getObject(http_response_object
, obj
);
1628 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1632 data
= GET_PROP(obj
, responseData
);
1633 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1637 if (!header_len
|| !header_name
) {
1638 array_init(return_value
);
1639 array_copy(*headers
, return_value
);
1640 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1641 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1648 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1650 * Get response cookie(s) after the request has been sent.
1652 PHP_METHOD(HttpRequest
, getResponseCookie
)
1654 zval
*data
, **headers
;
1655 char *cookie_name
= NULL
;
1657 getObject(http_request_object
, obj
);
1659 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1663 array_init(return_value
);
1665 data
= GET_PROP(obj
, responseData
);
1666 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1669 zval
**header
= NULL
;
1671 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1672 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1673 /* several cookies? */
1674 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1677 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1679 MAKE_STD_ZVAL(cookie_hash
);
1680 array_init(cookie_hash
);
1682 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1684 add_next_index_zval(return_value
, cookie_hash
);
1688 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1689 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1690 add_next_index_zval(return_value
, cookie_hash
);
1691 return; /* <<< FOUND >>> */
1693 zval_dtor(cookie_hash
);
1698 zval_dtor(cookie_hash
);
1704 MAKE_STD_ZVAL(cookie_hash
);
1705 array_init(cookie_hash
);
1707 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1709 add_next_index_zval(return_value
, cookie_hash
);
1713 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1714 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1715 add_next_index_zval(return_value
, cookie_hash
);
1717 zval_dtor(cookie_hash
);
1722 zval_dtor(cookie_hash
);
1735 /* {{{ proto string HttpRequest::getResponseBody()
1737 * Get the response body after the request has been sent.
1739 PHP_METHOD(HttpRequest
, getResponseBody
)
1742 getObject(http_request_object
, obj
);
1746 data
= GET_PROP(obj
, responseData
);
1747 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1748 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
1755 /* {{{ proto int HttpRequest::getResponseCode()
1757 * Get the response code after the request has been sent.
1759 PHP_METHOD(HttpRequest
, getResponseCode
)
1762 getObject(http_request_object
, obj
);
1766 code
= GET_PROP(obj
, responseCode
);
1767 RETURN_LONG(Z_LVAL_P(code
));
1771 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1773 * Get response info after the request has been sent.
1774 * See http_get() for a full list of returned info.
1776 PHP_METHOD(HttpRequest
, getResponseInfo
)
1778 zval
*info
, **infop
;
1779 char *info_name
= NULL
;
1781 getObject(http_request_object
, obj
);
1783 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1787 info
= GET_PROP(obj
, responseInfo
);
1789 if (info_len
&& info_name
) {
1790 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1791 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
1793 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Could not find response info named %s", info_name
);
1797 array_init(return_value
);
1798 array_copy(info
, return_value
);
1803 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1805 * Get the full response as HttpMessage object.
1807 PHP_METHOD(HttpRequest
, getResponseMessage
)
1810 getObject(http_request_object
, obj
);
1814 message
= GET_PROP(obj
, responseMessage
);
1815 Z_TYPE_P(return_value
) = IS_OBJECT
;
1816 return_value
->is_ref
= 1;
1817 return_value
->value
.obj
= message
->value
.obj
;
1818 zval_add_ref(&return_value
);
1821 /* {{{ proto bool HttpRequest::send()
1823 * Send the HTTP request.
1828 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1829 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1830 * $r->addQueryData(array('category' => 3));
1833 * if ($r->getResponseCode() == 200) {
1834 * file_put_contents('local.rss', $r->getResponseBody());
1836 * } catch (HttpException $ex) {
1845 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1846 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1847 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1848 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1850 * echo $r->getResponseBody();
1855 PHP_METHOD(HttpRequest
, send
)
1857 STATUS status
= FAILURE
;
1858 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
1860 getObject(http_request_object
, obj
);
1864 SET_EH_THROW_HTTP();
1866 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
1867 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initilaize curl");
1871 meth
= GET_PROP(obj
, method
);
1872 URL
= GET_PROP(obj
, url
);
1873 qdata
= GET_PROP(obj
, queryData
);
1874 opts
= GET_PROP(obj
, options
);
1875 info
= GET_PROP(obj
, responseInfo
);
1876 resp
= GET_PROP(obj
, responseData
);
1878 // HTTP_URI_MAXLEN+1 long char *
1879 request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0);
1881 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
1882 if (!strchr(request_uri
, '?')) {
1883 strcat(request_uri
, "?");
1885 strcat(request_uri
, "&");
1887 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1890 switch (Z_LVAL_P(meth
))
1893 status
= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
1897 status
= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
1902 zval
*post_files
, *post_data
;
1904 post_files
= GET_PROP(obj
, postFiles
);
1905 post_data
= GET_PROP(obj
, postData
);
1907 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files
))) {
1909 /* urlencoded post */
1910 status
= http_post_array_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(post_data
), Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
1920 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
1923 FOREACH_KEYVAL(post_data
, key
, idx
, data
) {
1925 convert_to_string_ex(data
);
1926 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1927 CURLFORM_COPYNAME
, key
,
1928 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
1929 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
1939 FOREACH_VAL(post_files
, data
) {
1940 zval
**file
, **type
, **name
;
1942 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
1943 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
1944 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
1946 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1947 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
1948 CURLFORM_FILE
, Z_STRVAL_PP(file
),
1949 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
1955 status
= http_post_curldata_ex(obj
->ch
, request_uri
, http_post_data
[0], Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &obj
->response
);
1956 curl_formfree(http_post_data
[0]);
1967 /* final data handling */
1968 if (status
== SUCCESS
) {
1971 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->response
), PHPSTR_LEN(&obj
->response
))) {
1972 zval
*headers
, *message
;
1976 UPD_PROP(obj
, long, responseCode
, msg
->info
.response
.code
);
1978 MAKE_STD_ZVAL(headers
)
1979 array_init(headers
);
1981 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1982 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
1984 add_assoc_zval(resp
, "headers", headers
);
1985 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
1987 message
= GET_PROP(obj
, responseMessage
);
1989 Z_TYPE_P(message
) = IS_OBJECT
;
1990 message
->value
.obj
= http_message_object_from_msg(msg
);
1991 SET_PROP(obj
, responseMessage
, message
);
1998 RETURN_SUCCESS(status
);
2002 #endif /* HTTP_HAVE_CURL */
2004 #endif /* ZEND_ENGINE_2 */
2011 * vim600: noet sw=4 ts=4 fdm=marker
2012 * vim<600: noet sw=4 ts=4