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 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
26 #include "php_http_std_defs.h"
27 #include "php_http_request_object.h"
28 #include "php_http_request_api.h"
29 #include "php_http_request_pool_api.h"
31 #include "php_http_api.h"
32 #include "php_http_url_api.h"
33 #include "php_http_message_api.h"
34 #include "php_http_message_object.h"
35 #include "php_http_exception_object.h"
40 # include <winsock2.h>
42 #include <curl/curl.h>
44 ZEND_EXTERN_MODULE_GLOBALS(http
);
46 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
47 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
48 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
49 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
51 HTTP_EMPTY_ARGS(__destruct
, 0);
52 HTTP_BEGIN_ARGS(__construct
, 0, 0)
54 HTTP_ARG_VAL(method
, 0)
57 HTTP_EMPTY_ARGS(getOptions
, 0);
58 HTTP_BEGIN_ARGS(setOptions
, 0, 0)
59 HTTP_ARG_VAL(options
, 0)
62 HTTP_EMPTY_ARGS(getSslOptions
, 0);
63 HTTP_BEGIN_ARGS(setSslOptions
, 0, 0)
64 HTTP_ARG_VAL(ssl_options
, 0)
67 HTTP_EMPTY_ARGS(getHeaders
, 0);
68 HTTP_BEGIN_ARGS(setHeaders
, 0, 0)
69 HTTP_ARG_VAL(headers
, 0)
72 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
73 HTTP_ARG_VAL(headers
, 0)
76 HTTP_EMPTY_ARGS(getCookies
, 0);
77 HTTP_BEGIN_ARGS(setCookies
, 0, 0)
78 HTTP_ARG_VAL(cookies
, 0)
81 HTTP_BEGIN_ARGS(addCookies
, 0, 1)
82 HTTP_ARG_VAL(cookies
, 0)
85 HTTP_EMPTY_ARGS(getUrl
, 0);
86 HTTP_BEGIN_ARGS(setUrl
, 0, 1)
90 HTTP_EMPTY_ARGS(getMethod
, 0);
91 HTTP_BEGIN_ARGS(setMethod
, 0, 1)
92 HTTP_ARG_VAL(request_method
, 0)
95 HTTP_EMPTY_ARGS(getContentType
, 0);
96 HTTP_BEGIN_ARGS(setContentType
, 0, 1)
97 HTTP_ARG_VAL(content_type
, 0)
100 HTTP_EMPTY_ARGS(getQueryData
, 0);
101 HTTP_BEGIN_ARGS(setQueryData
, 0, 0)
102 HTTP_ARG_VAL(query_data
, 0)
105 HTTP_BEGIN_ARGS(addQueryData
, 0, 1)
106 HTTP_ARG_VAL(query_data
, 0)
109 HTTP_EMPTY_ARGS(getPostFields
, 0);
110 HTTP_BEGIN_ARGS(setPostFields
, 0, 0)
111 HTTP_ARG_VAL(post_fields
, 0)
114 HTTP_BEGIN_ARGS(addPostFields
, 0, 1)
115 HTTP_ARG_VAL(post_fields
, 0)
118 HTTP_EMPTY_ARGS(getPostFiles
, 0);
119 HTTP_BEGIN_ARGS(setPostFiles
, 0, 0)
120 HTTP_ARG_VAL(post_files
, 0)
123 HTTP_BEGIN_ARGS(addPostFile
, 0, 2)
124 HTTP_ARG_VAL(formname
, 0)
125 HTTP_ARG_VAL(filename
, 0)
126 HTTP_ARG_VAL(content_type
, 0)
129 HTTP_EMPTY_ARGS(getRawPostData
, 0);
130 HTTP_BEGIN_ARGS(setRawPostData
, 0, 0)
131 HTTP_ARG_VAL(raw_post_data
, 0)
134 HTTP_BEGIN_ARGS(addRawPostData
, 0, 1)
135 HTTP_ARG_VAL(raw_post_data
, 0)
138 HTTP_EMPTY_ARGS(getPutFile
, 0);
139 HTTP_BEGIN_ARGS(setPutFile
, 0, 0)
140 HTTP_ARG_VAL(filename
, 0)
143 HTTP_EMPTY_ARGS(getResponseData
, 0);
144 HTTP_BEGIN_ARGS(getResponseHeader
, 0, 0)
145 HTTP_ARG_VAL(name
, 0)
148 HTTP_BEGIN_ARGS(getResponseCookie
, 0, 0)
149 HTTP_ARG_VAL(name
, 0)
152 HTTP_EMPTY_ARGS(getResponseBody
, 0);
153 HTTP_EMPTY_ARGS(getResponseCode
, 0);
154 HTTP_BEGIN_ARGS(getResponseInfo
, 0, 0)
155 HTTP_ARG_VAL(name
, 0)
158 HTTP_EMPTY_ARGS(getResponseMessage
, 1);
159 HTTP_EMPTY_ARGS(getRequestMessage
, 1);
160 HTTP_EMPTY_ARGS(getHistory
, 1);
161 HTTP_EMPTY_ARGS(clearHistory
, 0);
162 HTTP_EMPTY_ARGS(send
, 1);
164 HTTP_BEGIN_ARGS(get
, 0, 1)
166 HTTP_ARG_VAL(options
, 0)
167 HTTP_ARG_VAL(info
, 1)
170 HTTP_BEGIN_ARGS(head
, 0, 1)
172 HTTP_ARG_VAL(options
, 0)
173 HTTP_ARG_VAL(info
, 1)
176 HTTP_BEGIN_ARGS(postData
, 0, 2)
178 HTTP_ARG_VAL(data
, 0)
179 HTTP_ARG_VAL(options
, 0)
180 HTTP_ARG_VAL(info
, 1)
183 HTTP_BEGIN_ARGS(postFields
, 0, 2)
185 HTTP_ARG_VAL(data
, 0)
186 HTTP_ARG_VAL(options
, 0)
187 HTTP_ARG_VAL(info
, 1)
190 HTTP_BEGIN_ARGS(putFile
, 0, 2)
192 HTTP_ARG_VAL(file
, 0)
193 HTTP_ARG_VAL(options
, 0)
194 HTTP_ARG_VAL(info
, 1)
197 HTTP_BEGIN_ARGS(putStream
, 0, 2)
199 HTTP_ARG_VAL(stream
, 0)
200 HTTP_ARG_VAL(options
, 0)
201 HTTP_ARG_VAL(info
, 1)
204 HTTP_BEGIN_ARGS(methodRegister
, 0, 1)
205 HTTP_ARG_VAL(method_name
, 0)
208 HTTP_BEGIN_ARGS(methodUnregister
, 0, 1)
209 HTTP_ARG_VAL(method
, 0)
212 HTTP_BEGIN_ARGS(methodName
, 0, 1)
213 HTTP_ARG_VAL(method_id
, 0)
216 HTTP_BEGIN_ARGS(methodExists
, 0, 1)
217 HTTP_ARG_VAL(method
, 0)
220 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
221 static inline void _http_request_object_declare_default_properties(TSRMLS_D
);
223 zend_class_entry
*http_request_object_ce
;
224 zend_function_entry http_request_object_fe
[] = {
225 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
226 HTTP_REQUEST_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
228 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
229 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
230 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
231 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
233 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
234 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
235 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
237 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
238 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
239 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
241 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
242 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
244 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
245 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
247 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
248 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
250 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
251 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
252 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
254 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
255 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
256 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
258 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
260 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
262 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
263 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
266 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
269 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
272 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
273 HTTP_REQUEST_ME(getResponseCookie
, ZEND_ACC_PUBLIC
)
274 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
275 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
277 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
278 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
279 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
280 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
282 HTTP_REQUEST_ALIAS(get
, http_get
)
283 HTTP_REQUEST_ALIAS(head
, http_head
)
284 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
285 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
286 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
287 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
289 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
290 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
291 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
292 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
296 static zend_object_handlers http_request_object_handlers
;
298 void _http_request_object_init(INIT_FUNC_ARGS
)
300 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
303 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
305 zend_object_value ov
;
306 http_request_object
*o
;
308 o
= ecalloc(1, sizeof(http_request_object
));
310 o
->ch
= curl_easy_init();
312 phpstr_init(&o
->history
);
313 phpstr_init(&o
->request
);
314 phpstr_init_ex(&o
->response
, HTTP_CURLBUF_SIZE
, 0);
316 ALLOC_HASHTABLE(OBJ_PROP(o
));
317 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
318 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
320 ov
.handle
= putObject(http_request_object
, o
);
321 ov
.handlers
= &http_request_object_handlers
;
326 static inline void _http_request_object_declare_default_properties(TSRMLS_D
)
328 zend_class_entry
*ce
= http_request_object_ce
;
330 DCL_PROP_N(PROTECTED
, options
);
331 DCL_PROP_N(PROTECTED
, responseInfo
);
332 DCL_PROP_N(PROTECTED
, responseData
);
333 DCL_PROP_N(PROTECTED
, responseCode
);
334 DCL_PROP_N(PROTECTED
, responseMessage
);
335 DCL_PROP_N(PROTECTED
, postFields
);
336 DCL_PROP_N(PROTECTED
, postFiles
);
338 DCL_PROP(PROTECTED
, long, method
, HTTP_GET
);
340 DCL_PROP(PROTECTED
, string
, url
, "");
341 DCL_PROP(PROTECTED
, string
, contentType
, "");
342 DCL_PROP(PROTECTED
, string
, rawPostData
, "");
343 DCL_PROP(PROTECTED
, string
, queryData
, "");
344 DCL_PROP(PROTECTED
, string
, putFile
, "");
346 DCL_PROP(PUBLIC
, bool, recordHistory
, 1);
350 * Request Method Constants
353 DCL_CONST(long, "METH_GET", HTTP_GET
);
354 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
355 DCL_CONST(long, "METH_POST", HTTP_POST
);
356 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
357 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
358 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
359 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
360 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
361 /* WebDAV - RFC 2518 */
362 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
363 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
364 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
365 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
366 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
367 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
368 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
369 /* WebDAV Versioning - RFC 3253 */
370 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
371 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
372 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
373 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
374 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
375 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
376 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
377 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
378 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
379 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
380 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
381 /* WebDAV Access Control - RFC 3744 */
382 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
387 # if LIBCURL_VERSION_NUM >= 0x070a05
388 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
389 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
390 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
391 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
392 # endif /* LIBCURL_VERSION_NUM */
396 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
398 http_request_object
*o
= (http_request_object
*) object
;
401 zend_hash_destroy(OBJ_PROP(o
));
402 FREE_HASHTABLE(OBJ_PROP(o
));
405 /* avoid nasty segfaults with already cleaned up callbacks */
406 curl_easy_setopt(o
->ch
, CURLOPT_NOPROGRESS
, 1);
407 curl_easy_setopt(o
->ch
, CURLOPT_PROGRESSFUNCTION
, NULL
);
408 curl_easy_setopt(o
->ch
, CURLOPT_VERBOSE
, 0);
409 curl_easy_setopt(o
->ch
, CURLOPT_DEBUGFUNCTION
, NULL
);
410 curl_easy_cleanup(o
->ch
);
412 phpstr_dtor(&o
->response
);
413 phpstr_dtor(&o
->request
);
414 phpstr_dtor(&o
->history
);
418 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr
, http_request_body
*body TSRMLS_DC
)
422 STATUS status
= SUCCESS
;
427 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
428 http_error(HE_WARNING
, HTTP_E_REQUEST
, "Could not initilaize curl");
432 URL
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, url
));
433 // HTTP_URI_MAXLEN+1 long char *
434 if (!(request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0))) {
438 meth
= convert_to_type_ex(IS_LONG
, GET_PROP(obj
, method
));
439 switch (Z_LVAL_P(meth
))
449 php_stream_statbuf ssb
;
450 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj
, putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
452 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
453 body
->type
= HTTP_REQUEST_BODY_UPLOADFILE
;
455 body
->size
= ssb
.sb
.st_size
;
465 /* check for raw post data */
466 zval
*raw_data
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
));
468 if (Z_STRLEN_P(raw_data
)) {
469 zval
*ctype
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, contentType
));
471 if (Z_STRLEN_P(ctype
)) {
472 zval
**headers
, *opts
= GET_PROP(obj
, options
);
474 convert_to_array(opts
);
476 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
479 convert_to_array(*headers
);
480 /* only override if not already set */
481 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) {
482 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
487 MAKE_STD_ZVAL(headers
);
489 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
490 add_assoc_zval(opts
, "headers", headers
);
494 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
495 body
->data
= estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
));
496 body
->size
= Z_STRLEN_P(raw_data
);
498 status
= http_request_body_fill(body
, Z_ARRVAL_P(GET_PROP(obj
, postFields
)), Z_ARRVAL_P(GET_PROP(obj
, postFiles
)));
504 if (status
== SUCCESS
) {
505 zval
*qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
));
507 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
508 if (!strchr(request_uri
, '?')) {
509 strcat(request_uri
, "?");
511 strcat(request_uri
, "&");
513 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
516 status
= http_request_init(obj
->ch
, Z_LVAL_P(meth
), request_uri
, body
, Z_ARRVAL_P(GET_PROP(obj
, options
)));
520 /* clean previous response */
521 phpstr_dtor(&obj
->response
);
522 /* clean previous request */
523 phpstr_dtor(&obj
->request
);
528 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
532 phpstr_fix(&obj
->request
);
533 phpstr_fix(&obj
->response
);
535 msg
= http_message_parse(PHPSTR_VAL(&obj
->response
), PHPSTR_LEN(&obj
->response
));
542 zval
*headers
, *message
,
543 *resp
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
)),
544 *info
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseInfo
));
546 if (zval_is_true(GET_PROP(obj
, recordHistory
))) {
547 /* we need to act like a zipper, as we'll receive
548 * the requests and the responses in separate chains
551 http_message
*response
= msg
, *request
= http_message_parse(PHPSTR_VAL(&obj
->request
), PHPSTR_LEN(&obj
->request
));
552 http_message
*free_msg
= request
;
558 http_message_tostring(response
, &message
, &msglen
);
559 phpstr_append(&obj
->history
, message
, msglen
);
562 http_message_tostring(request
, &message
, &msglen
);
563 phpstr_append(&obj
->history
, message
, msglen
);
566 } while ((response
= response
->parent
) && (request
= request
->parent
));
568 http_message_free(&free_msg
);
569 phpstr_fix(&obj
->history
);
572 UPD_PROP(obj
, long, responseCode
, msg
->http
.info
.response
.code
);
574 MAKE_STD_ZVAL(headers
)
577 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
578 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
580 add_assoc_zval(resp
, "headers", headers
);
581 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
583 MAKE_STD_ZVAL(message
);
584 ZVAL_OBJVAL(message
, http_message_object_from_msg(msg
));
585 SET_PROP(obj
, responseMessage
, message
);
586 zval_ptr_dtor(&message
);
588 http_request_info(obj
->ch
, Z_ARRVAL_P(info
));
589 SET_PROP(obj
, responseInfo
, info
);
595 #define http_request_object_set_options_subr(key, ow) \
596 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
597 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
599 zval
*opts
, **options
, *new_options
= NULL
;
600 getObject(http_request_object
, obj
);
602 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &new_options
)) {
606 opts
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, options
));
608 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
609 convert_to_array(*options
);
611 zend_hash_clean(Z_ARRVAL_PP(options
));
613 if (new_options
&& zend_hash_num_elements(Z_ARRVAL_P(new_options
))) {
615 array_copy(new_options
, *options
);
617 array_merge(new_options
, *options
);
620 } else if (new_options
&& zend_hash_num_elements(Z_ARRVAL_P(new_options
))) {
621 zval_add_ref(&new_options
);
622 add_assoc_zval(opts
, key
, new_options
);
628 #define http_request_object_get_options_subr(key) \
629 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
630 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
635 zval
*opts
, **options
;
636 getObject(http_request_object
, obj
);
638 opts
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
));
640 array_init(return_value
);
642 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
643 convert_to_array(*options
);
644 array_copy(*options
, return_value
);
650 /* ### USERLAND ### */
652 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET]])
654 * Instantiate a new HttpRequest object.
656 * Accepts a string as optional parameter containing the target request url.
657 * Additianally accepts an optional int parameter specifying the request method
660 * Throws HttpException.
662 PHP_METHOD(HttpRequest
, __construct
)
667 getObject(http_request_object
, obj
);
670 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
671 INIT_PARR(obj
, options
);
672 INIT_PARR(obj
, responseInfo
);
673 INIT_PARR(obj
, responseData
);
674 INIT_PARR(obj
, postFields
);
675 INIT_PARR(obj
, postFiles
);
678 UPD_STRL(obj
, url
, URL
, URL_len
);
681 UPD_PROP(obj
, long, method
, meth
);
688 /* {{{ proto void HttpRequest::__destruct()
690 * Destroys the HttpRequest object.
692 PHP_METHOD(HttpRequest
, __destruct
)
694 getObject(http_request_object
, obj
);
698 FREE_PARR(obj
, options
);
699 FREE_PARR(obj
, responseInfo
);
700 FREE_PARR(obj
, responseData
);
701 FREE_PARR(obj
, postFields
);
702 FREE_PARR(obj
, postFiles
);
706 /* {{{ proto bool HttpRequest::setOptions([array options])
708 * Set the request options to use. See http_get() for a full list of available options.
710 * Accepts an array as optional parameters, wich values will overwrite the
711 * currently set request options. If the parameter is empty or mitted,
712 * the optoions of the HttpRequest object will be reset.
714 * Returns TRUE on success, or FALSE on failure.
716 PHP_METHOD(HttpRequest
, setOptions
)
720 zval
*opts
= NULL
, *old_opts
, **opt
;
721 getObject(http_request_object
, obj
);
723 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
727 old_opts
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, options
));
729 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
730 zend_hash_clean(Z_ARRVAL_P(old_opts
));
734 /* some options need extra attention -- thus cannot use array_merge() directly */
735 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
737 if (!strcmp(key
, "headers")) {
739 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
740 convert_to_array(*opt
);
741 convert_to_array(*headers
);
742 array_merge(*opt
, *headers
);
745 } else if (!strcmp(key
, "cookies")) {
747 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
748 convert_to_array(*opt
);
749 convert_to_array(*cookies
);
750 array_merge(*opt
, *cookies
);
753 } else if (!strcmp(key
, "ssl")) {
755 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl
)) {
756 convert_to_array(*opt
);
757 convert_to_array(*ssl
);
758 array_merge(*opt
, *ssl
);
761 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
762 if (Z_TYPE_PP(opt
) != IS_STRING
) {
763 convert_to_string_ex(opt
);
765 UPD_STRL(obj
, url
, Z_STRVAL_PP(opt
), Z_STRLEN_PP(opt
));
767 } else if (!strcmp(key
, "method")) {
768 if (Z_TYPE_PP(opt
) != IS_LONG
) {
769 convert_to_long_ex(opt
);
771 UPD_PROP(obj
, long, method
, Z_LVAL_PP(opt
));
776 add_assoc_zval(old_opts
, key
, *opt
);
787 /* {{{ proto array HttpRequest::getOptions()
789 * Get currently set options.
791 * Returns an associative array containing currently set options.
793 PHP_METHOD(HttpRequest
, getOptions
)
799 getObject(http_request_object
, obj
);
801 opts
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
));
802 array_init(return_value
);
803 array_copy(opts
, return_value
);
808 /* {{{ proto bool HttpRequest::setSslOptions([array options])
812 * Accepts an associative array as parameter containing any SSL specific options.
813 * If the parameter is empty or omitted, the SSL options will be reset.
815 * Returns TRUE on success, or FALSE on failure.
817 PHP_METHOD(HttpRequest
, setSslOptions
)
819 http_request_object_set_options_subr("ssl", 1);
823 /* {{{ proto bool HttpRequest::addSslOptions(array options)
825 * Set additional SSL options.
827 * Expects an associative array as parameter containing additional SSL specific options.
829 * Returns TRUE on success, or FALSE on failure.
831 PHP_METHOD(HttpRequest
, addSslOptions
)
833 http_request_object_set_options_subr("ssl", 0);
837 /* {{{ proto array HttpRequest::getSslOtpions()
839 * Get previously set SSL options.
841 * Returns an associative array containing any previously set SSL options.
843 PHP_METHOD(HttpRequest
, getSslOptions
)
845 http_request_object_get_options_subr("ssl");
849 /* {{{ proto bool HttpRequest::addHeaders(array headers)
851 * Add request header name/value pairs.
853 * Expects an ssociative array as parameter containing additional header
856 * Returns TRUE on success, or FALSE on failure.
858 PHP_METHOD(HttpRequest
, addHeaders
)
860 http_request_object_set_options_subr("headers", 0);
863 /* {{{ proto bool HttpRequest::setHeaders([array headers])
865 * Set request header name/value pairs.
867 * Accepts an associative array as parameter containing header name/value pairs.
868 * If the parameter is empty or omitted, all previously set headers will be unset.
870 * Returns TRUE on success, or FALSE on failure.
872 PHP_METHOD(HttpRequest
, setHeaders
)
874 http_request_object_set_options_subr("headers", 1);
878 /* {{{ proto array HttpRequest::getHeaders()
880 * Get previously set request headers.
882 * Returns an associative array containing all currently set headers.
884 PHP_METHOD(HttpRequest
, getHeaders
)
886 http_request_object_get_options_subr("headers");
890 /* {{{ proto bool HttpRequest::setCookies([array cookies])
894 * Accepts an associative array as parameter containing cookie name/value pairs.
895 * If the parameter is empty or omitted, all previously set cookies will be unset.
897 * Returns TRUE on success, or FALSE on failure.
899 PHP_METHOD(HttpRequest
, setCookies
)
901 http_request_object_set_options_subr("cookies", 1);
905 /* {{{ proto bool HttpRequest::addCookies(array cookies)
909 * Expects an associative array as parameter containing any cookie name/value
912 * Returns TRUE on success, or FALSE on failure.
914 PHP_METHOD(HttpRequest
, addCookies
)
916 http_request_object_set_options_subr("cookies", 0);
920 /* {{{ proto array HttpRequest::getCookies()
922 * Get previously set cookies.
924 * Returns an associative array containing any previously set cookies.
926 PHP_METHOD(HttpRequest
, getCookies
)
928 http_request_object_get_options_subr("cookies");
932 /* {{{ proto bool HttpRequest::setUrl(string url)
934 * Set the request URL.
936 * Expects a string as parameter specifying the request url.
938 * Returns TRUE on success, or FALSE on failure.
940 PHP_METHOD(HttpRequest
, setUrl
)
944 getObject(http_request_object
, obj
);
946 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
950 UPD_STRL(obj
, url
, URL
, URL_len
);
955 /* {{{ proto string HttpRequest::getUrl()
957 * Get the previously set request URL.
959 * Returns the currently set request url as string.
961 PHP_METHOD(HttpRequest
, getUrl
)
966 getObject(http_request_object
, obj
);
967 zval
*URL
= GET_PROP(obj
, url
);
969 RETURN_ZVAL(URL
, 1, 0);
974 /* {{{ proto bool HttpRequest::setMethod(int request_method)
976 * Set the request method.
978 * Expects an int as parameter specifying the request method to use.
979 * In PHP 5.1+ HttpRequest::METH, otherwise the HTTP_METH constants can be used.
981 * Returns TRUE on success, or FALSE on failure.
983 PHP_METHOD(HttpRequest
, setMethod
)
986 getObject(http_request_object
, obj
);
988 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
992 UPD_PROP(obj
, long, method
, meth
);
997 /* {{{ proto int HttpRequest::getMethod()
999 * Get the previously set request method.
1001 * Returns the currently set request method.
1003 PHP_METHOD(HttpRequest
, getMethod
)
1008 getObject(http_request_object
, obj
);
1009 zval
*meth
= GET_PROP(obj
, method
);
1011 RETURN_ZVAL(meth
, 1, 0);
1016 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1018 * Set the content type the post request should have.
1020 * Expects a string as parameters containing the content type of the request
1021 * (primary/secondary).
1023 * Returns TRUE on success, or FALSE if the content type does not seem to
1024 * contain a primary and a secondary part.
1026 PHP_METHOD(HttpRequest
, setContentType
)
1030 getObject(http_request_object
, obj
);
1032 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1036 if (!strchr(ctype
, '/')) {
1037 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
1041 UPD_STRL(obj
, contentType
, ctype
, ct_len
);
1046 /* {{{ proto string HttpRequest::getContentType()
1048 * Get the previously content type.
1050 * Returns the previously set content type as string.
1052 PHP_METHOD(HttpRequest
, getContentType
)
1057 getObject(http_request_object
, obj
);
1058 zval
*ctype
= GET_PROP(obj
, contentType
);
1060 RETURN_ZVAL(ctype
, 1, 0);
1065 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1067 * Set the URL query parameters to use, overwriting previously set query parameters.
1068 * Affects any request types.
1070 * Accepts a string or associative array parameter containing the pre-encoded
1071 * query string or to be encoded query fields. If the parameter is empty or
1072 * omitted, the query data will be unset.
1074 * Returns TRUE on success, or FALSE on failure.
1076 PHP_METHOD(HttpRequest
, setQueryData
)
1079 getObject(http_request_object
, obj
);
1081 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1085 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1086 UPD_STRL(obj
, queryData
, "", 0);
1087 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1088 char *query_data
= NULL
;
1090 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1094 UPD_PROP(obj
, string
, queryData
, query_data
);
1097 convert_to_string_ex(&qdata
);
1098 UPD_STRL(obj
, queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1104 /* {{{ proto string HttpRequest::getQueryData()
1106 * Get the current query data in form of an urlencoded query string.
1108 * Returns a string containing the urlencoded query.
1110 PHP_METHOD(HttpRequest
, getQueryData
)
1115 getObject(http_request_object
, obj
);
1116 zval
*qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
));
1118 RETURN_ZVAL(qdata
, 1, 0);
1123 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1125 * Add parameters to the query parameter list, leaving previously set unchanged.
1126 * Affects any request type.
1128 * Expects an associative array as parameter containing the query fields to add.
1130 * Returns TRUE on success, or FALSE on failure.
1132 PHP_METHOD(HttpRequest
, addQueryData
)
1134 zval
*qdata
, *old_qdata
;
1135 char *query_data
= NULL
;
1136 size_t query_data_len
= 0;
1137 getObject(http_request_object
, obj
);
1139 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1143 old_qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
));
1145 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, &query_data_len
)) {
1149 UPD_STRL(obj
, queryData
, query_data
, query_data_len
);
1156 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1158 * Adds POST data entries, leaving previously set unchanged, unless a
1159 * post entry with the same name already exists.
1160 * Affects only POST and custom requests.
1162 * Expects an associative array as parameter containing the post fields.
1164 * Returns TRUE on success, or FALSE on failure.
1166 PHP_METHOD(HttpRequest
, addPostFields
)
1168 zval
*post
, *post_data
;
1169 getObject(http_request_object
, obj
);
1171 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1175 post
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFields
));
1176 array_merge(post_data
, post
);
1182 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1184 * Set the POST data entries, overwriting previously set POST data.
1185 * Affects only POST and custom requests.
1187 * Accepts an associative array as parameter containing the post fields.
1188 * If the parameter is empty or omitted, the post data will be unset.
1190 * Returns TRUE on success, or FALSE on failure.
1192 PHP_METHOD(HttpRequest
, setPostFields
)
1194 zval
*post
, *post_data
= NULL
;
1195 getObject(http_request_object
, obj
);
1197 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!", &post_data
)) {
1201 post
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFields
));
1202 zend_hash_clean(Z_ARRVAL_P(post
));
1204 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1205 array_copy(post_data
, post
);
1212 /* {{{ proto array HttpRequest::getPostFields()
1214 * Get previously set POST data.
1216 * Returns the currently set post fields as associative array.
1218 PHP_METHOD(HttpRequest
, getPostFields
)
1223 getObject(http_request_object
, obj
);
1224 zval
*post_data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFields
));
1226 array_init(return_value
);
1227 array_copy(post_data
, return_value
);
1232 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1234 * Set raw post data to send, overwriting previously set raw post data. Don't
1235 * forget to specify a content type. Affects only POST and custom requests.
1236 * Only either post fields or raw post data can be used for each request.
1237 * Raw post data has higher precedence and will be used even if post fields
1240 * Accepts a string as parameter containing the *raw* post data.
1242 * Returns TRUE on success, or FALSE on failure.
1244 PHP_METHOD(HttpRequest
, setRawPostData
)
1246 char *raw_data
= NULL
;
1248 getObject(http_request_object
, obj
);
1250 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1258 UPD_STRL(obj
, rawPostData
, raw_data
, data_len
);
1263 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1265 * Add raw post data, leaving previously set raw post data unchanged.
1266 * Affects only POST and custom requests.
1268 * Expects a string as parameter containing the raw post data to concatenate.
1270 * Returns TRUE on success, or FALSE on failure.
1272 PHP_METHOD(HttpRequest
, addRawPostData
)
1274 char *raw_data
, *new_data
;
1276 getObject(http_request_object
, obj
);
1278 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1283 zval
*zdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
));
1285 new_data
= emalloc(Z_STRLEN_P(zdata
) + data_len
+ 1);
1286 new_data
[Z_STRLEN_P(zdata
) + data_len
] = '\0';
1288 if (Z_STRLEN_P(zdata
)) {
1289 memcpy(new_data
, Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
));
1292 memcpy(new_data
+ Z_STRLEN_P(zdata
), raw_data
, data_len
);
1293 UPD_STRL(obj
, rawPostData
, new_data
, Z_STRLEN_P(zdata
) + data_len
);
1300 /* {{{ proto string HttpRequest::getRawPostData()
1302 * Get previously set raw post data.
1304 * Returns a string containing the currently set raw post data.
1306 PHP_METHOD(HttpRequest
, getRawPostData
)
1311 getObject(http_request_object
, obj
);
1312 zval
*raw_data
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
));
1314 RETURN_ZVAL(raw_data
, 1, 0);
1319 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1321 * Add a file to the POST request, leaving prefiously set files unchanged.
1322 * Affects only POST and custom requests. Cannot be used with raw post data.
1324 * Expects a string parameter containing the form element name, and a string
1325 * paremeter containing the path to the file which should be uploaded.
1326 * Additionally accepts an optional string parameter which chould contain
1327 * the content type of the file.
1329 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1330 * primary and a secondary content type part.
1332 PHP_METHOD(HttpRequest
, addPostFile
)
1334 zval
*files
, *entry
;
1335 char *name
, *file
, *type
= NULL
;
1336 int name_len
, file_len
, type_len
= 0;
1337 getObject(http_request_object
, obj
);
1339 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1344 if (!strchr(type
, '/')) {
1345 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1349 type
= "application/x-octetstream";
1350 type_len
= sizeof("application/x-octetstream") - 1;
1353 MAKE_STD_ZVAL(entry
);
1356 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1357 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1358 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1360 files
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1361 add_next_index_zval(files
, entry
);
1367 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1369 * Set files to post, overwriting previously set post files.
1370 * Affects only POST and requests. Cannot be used with raw post data.
1372 * Accepts an array containing the files to post. Each entry should be an
1373 * associative array with "name", "file" and "type" keys. If the parameter
1374 * is empty or omitted the post files will be unset.
1376 * Returns TRUE on success, or FALSE on failure.
1378 PHP_METHOD(HttpRequest
, setPostFiles
)
1380 zval
*files
, *pFiles
;
1381 getObject(http_request_object
, obj
);
1383 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &files
)) {
1387 pFiles
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1388 zend_hash_clean(Z_ARRVAL_P(pFiles
));
1390 if (files
&& zend_hash_num_elements(Z_ARRVAL_P(files
))) {
1391 array_copy(files
, pFiles
);
1398 /* {{{ proto array HttpRequest::getPostFiles()
1400 * Get all previously added POST files.
1402 * Returns an array containing currently set post files.
1404 PHP_METHOD(HttpRequest
, getPostFiles
)
1409 getObject(http_request_object
, obj
);
1410 zval
*files
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1412 array_init(return_value
);
1413 array_copy(files
, return_value
);
1418 /* {{{ proto bool HttpRequest::setPutFile([string file])
1420 * Set file to put. Affects only PUT requests.
1422 * Accepts a string as parameter referencing the path to file.
1423 * If the parameter is empty or omitted the put file will be unset.
1425 * Returns TRUE on success, or FALSE on failure.
1427 PHP_METHOD(HttpRequest
, setPutFile
)
1431 getObject(http_request_object
, obj
);
1433 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1437 UPD_STRL(obj
, putFile
, file
, file_len
);
1442 /* {{{ proto string HttpRequest::getPutFile()
1444 * Get previously set put file.
1446 * Returns a string containing the path to the currently set put file.
1448 PHP_METHOD(HttpRequest
, getPutFile
)
1453 getObject(http_request_object
, obj
);
1454 zval
*putfile
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, putFile
));
1456 RETURN_ZVAL(putfile
, 1, 0);
1461 /* {{{ proto array HttpRequest::getResponseData()
1463 * Get all response data after the request has been sent.
1465 * Returns an associative array with the key "headers" containing an associative
1466 * array holding all response headers, as well as the ley "body" containing a
1467 * string with the response body.
1469 * If redirects were allowed and several responses were received, the data
1470 * references the last received response.
1472 PHP_METHOD(HttpRequest
, getResponseData
)
1477 getObject(http_request_object
, obj
);
1478 zval
*data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
));
1480 array_init(return_value
);
1481 array_copy(data
, return_value
);
1486 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1488 * Get response header(s) after the request has been sent.
1490 * Accepts an string as optional parameter specifying a certain header to read.
1491 * If the parameter is empty or omitted all response headers will be returned.
1493 * Returns either a string with the value of the header matching name if requested,
1494 * FALSE on failure, or an associative array containing all reponse headers.
1496 * If redirects were allowed and several responses were received, the data
1497 * references the last received response.
1499 PHP_METHOD(HttpRequest
, getResponseHeader
)
1502 zval
*data
, **headers
, **header
;
1503 char *header_name
= NULL
;
1505 getObject(http_request_object
, obj
);
1507 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1511 data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
));
1512 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1515 convert_to_array_ex(headers
);
1516 if (!header_len
|| !header_name
) {
1517 array_init(return_value
);
1518 array_copy(*headers
, return_value
);
1519 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1520 RETURN_ZVAL(*header
, 1, 0);
1528 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1530 * Get response cookie(s) after the request has been sent.
1532 * Accepts a string as optional parameter specifying the name of the cookie to read.
1533 * If the parameter is empty or omitted, an associative array with all received
1534 * cookies will be returned.
1536 * Returns either an associative array with the cookie's name, value and any
1537 * additional params of the cookie matching name if requested, FALSE on failure,
1538 * or an array containing all received cookies as arrays.
1540 * If redirects were allowed and several responses were received, the data
1541 * references the last received response.
1543 PHP_METHOD(HttpRequest
, getResponseCookie
)
1546 zval
*data
, **headers
;
1547 char *cookie_name
= NULL
;
1549 getObject(http_request_object
, obj
);
1551 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1555 array_init(return_value
);
1557 data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
));
1558 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1561 zval
**header
= NULL
;
1563 convert_to_array_ex(headers
);
1564 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1565 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1566 /* several cookies? */
1567 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1570 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1572 MAKE_STD_ZVAL(cookie_hash
);
1573 array_init(cookie_hash
);
1575 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1577 add_next_index_zval(return_value
, cookie_hash
);
1581 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1582 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1583 add_next_index_zval(return_value
, cookie_hash
);
1584 return; /* <<< FOUND >>> */
1586 zval_dtor(cookie_hash
);
1591 zval_dtor(cookie_hash
);
1598 MAKE_STD_ZVAL(cookie_hash
);
1599 array_init(cookie_hash
);
1600 convert_to_string_ex(header
);
1602 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1604 add_next_index_zval(return_value
, cookie_hash
);
1608 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1609 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1610 add_next_index_zval(return_value
, cookie_hash
);
1612 zval_dtor(cookie_hash
);
1617 zval_dtor(cookie_hash
);
1631 /* {{{ proto string HttpRequest::getResponseBody()
1633 * Get the response body after the request has been sent.
1635 * Returns a string containing the response body.
1637 * If redirects were allowed and several responses were received, the data
1638 * references the last received response.
1640 PHP_METHOD(HttpRequest
, getResponseBody
)
1646 getObject(http_request_object
, obj
);
1647 zval
*data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
));
1649 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1650 convert_to_string_ex(body
);
1651 RETURN_ZVAL(*body
, 1, 0);
1659 /* {{{ proto int HttpRequest::getResponseCode()
1661 * Get the response code after the request has been sent.
1663 * Returns an int representing the response code.
1665 * If redirects were allowed and several responses were received, the data
1666 * references the last received response.
1668 PHP_METHOD(HttpRequest
, getResponseCode
)
1673 getObject(http_request_object
, obj
);
1674 zval
*code
= convert_to_type_ex(IS_LONG
, GET_PROP(obj
, responseCode
));
1676 RETURN_ZVAL(code
, 1, 0);
1681 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1683 * Get response info after the request has been sent.
1684 * See http_get() for a full list of returned info.
1686 * Accepts a string as optional parameter specifying the info to read.
1687 * If the parameter is empty or omitted, an associative array containing
1688 * all available info will be returned.
1690 * Returns either a scalar containing the value of the info matching name if
1691 * requested, FALSE on failure, or an associative array containing all
1694 * If redirects were allowed and several responses were received, the data
1695 * references the last received response.
1697 PHP_METHOD(HttpRequest
, getResponseInfo
)
1700 zval
*info
, **infop
;
1701 char *info_name
= NULL
;
1703 getObject(http_request_object
, obj
);
1705 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1709 info
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseInfo
));
1711 if (info_len
&& info_name
) {
1712 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1713 RETURN_ZVAL(*infop
, 1, 0);
1715 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1719 array_init(return_value
);
1720 array_copy(info
, return_value
);
1726 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1728 * Get the full response as HttpMessage object after the request has been sent.
1730 * Returns an HttpMessage object of the response.
1732 * If redirects were allowed and several responses were received, the data
1733 * references the last received response. Use HttpMessage::getParentMessage()
1734 * to access the data of previously received responses whithin this request
1737 * Throws HttpException.
1739 PHP_METHOD(HttpRequest
, getResponseMessage
)
1745 getObject(http_request_object
, obj
);
1747 SET_EH_THROW_HTTP();
1748 message
= GET_PROP(obj
, responseMessage
);
1749 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1750 RETVAL_OBJECT(message
);
1759 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1761 * Get sent HTTP message.
1763 * Returns an HttpMessage object representing the sent request.
1765 * If redirects were allowed and several responses were received, the data
1766 * references the last received response. Use HttpMessage::getParentMessage()
1767 * to access the data of previously sent requests whithin this request
1770 PHP_METHOD(HttpRequest
, getRequestMessage
)
1776 getObject(http_request_object
, obj
);
1778 SET_EH_THROW_HTTP();
1779 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->request
), PHPSTR_LEN(&obj
->request
))) {
1780 RETVAL_OBJVAL(http_message_object_from_msg(msg
));
1787 /* {{{ proto HttpMessage HttpRequest::getHistory()
1789 * Get all sent requests and received responses as an HttpMessage object.
1791 * If you don't want to record history at all, set the instance variable
1792 * HttpRequest::$recoedHistory to FALSE.
1794 * Returns an HttpMessage object representing the complete request/response
1797 * The object references the last received response, use HttpMessage::getParentMessage()
1798 * to access the data of previously sent requests and received responses.
1800 * Throws HttpMalformedHeaderException.
1802 PHP_METHOD(HttpRequest
, getHistory
)
1808 getObject(http_request_object
, obj
);
1810 SET_EH_THROW_HTTP();
1811 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->history
), PHPSTR_LEN(&obj
->history
))) {
1812 RETVAL_OBJVAL(http_message_object_from_msg(msg
));
1819 /* {{{ proto void HttpRequest::clearHistory()
1821 * Clear the history.
1823 PHP_METHOD(HttpRequest
, clearHistory
)
1826 getObject(http_request_object
, obj
);
1827 phpstr_dtor(&obj
->history
);
1832 /* {{{ proto HttpMessage HttpRequest::send()
1834 * Send the HTTP request.
1836 * Returns the received response as HttpMessage object.
1838 * Throws HttpRuntimeException, HttpRequestException,
1839 * HttpMalformedHeaderException, HttpEncodingException.
1844 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1845 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1846 * $r->addQueryData(array('category' => 3));
1849 * if ($r->getResponseCode() == 200) {
1850 * file_put_contents('local.rss', $r->getResponseBody());
1852 * } catch (HttpException $ex) {
1861 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1862 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1863 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1864 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1866 * echo $r->send()->getBody();
1867 * } catch (HttpException $ex) {
1873 PHP_METHOD(HttpRequest
, send
)
1875 http_request_body body
= {0, NULL
, 0};
1876 getObject(http_request_object
, obj
);
1880 SET_EH_THROW_HTTP();
1883 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1890 if ( (SUCCESS
== http_request_object_requesthandler(obj
, getThis(), &body
)) &&
1891 (SUCCESS
== http_request_exec(obj
->ch
, NULL
, &obj
->response
, &obj
->request
)) &&
1892 (SUCCESS
== http_request_object_responsehandler(obj
, getThis()))) {
1893 RETVAL_OBJECT(GET_PROP(obj
, responseMessage
));
1895 http_request_body_dtor(&body
);
1901 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1908 * vim600: noet sw=4 ts=4 fdm=marker
1909 * vim<600: noet sw=4 ts=4