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 "zend_interfaces.h"
28 #include "php_http_std_defs.h"
29 #include "php_http_request_object.h"
30 #include "php_http_request_api.h"
31 #include "php_http_request_pool_api.h"
33 #include "php_http_api.h"
34 #include "php_http_url_api.h"
35 #include "php_http_message_api.h"
36 #include "php_http_message_object.h"
37 #include "php_http_exception_object.h"
42 # include <winsock2.h>
44 #include <curl/curl.h>
46 ZEND_EXTERN_MODULE_GLOBALS(http
);
48 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
49 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
50 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
51 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
53 HTTP_EMPTY_ARGS(__destruct
, 0);
54 HTTP_BEGIN_ARGS(__construct
, 0, 0)
56 HTTP_ARG_VAL(method
, 0)
57 HTTP_ARG_VAL(options
, 0)
60 HTTP_EMPTY_ARGS(getOptions
, 0);
61 HTTP_BEGIN_ARGS(setOptions
, 0, 0)
62 HTTP_ARG_VAL(options
, 0)
65 HTTP_EMPTY_ARGS(getSslOptions
, 0);
66 HTTP_BEGIN_ARGS(setSslOptions
, 0, 0)
67 HTTP_ARG_VAL(ssl_options
, 0)
70 HTTP_EMPTY_ARGS(getHeaders
, 0);
71 HTTP_BEGIN_ARGS(setHeaders
, 0, 0)
72 HTTP_ARG_VAL(headers
, 0)
75 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
76 HTTP_ARG_VAL(headers
, 0)
79 HTTP_EMPTY_ARGS(getCookies
, 0);
80 HTTP_BEGIN_ARGS(setCookies
, 0, 0)
81 HTTP_ARG_VAL(cookies
, 0)
84 HTTP_BEGIN_ARGS(addCookies
, 0, 1)
85 HTTP_ARG_VAL(cookies
, 0)
88 HTTP_EMPTY_ARGS(getUrl
, 0);
89 HTTP_BEGIN_ARGS(setUrl
, 0, 1)
93 HTTP_EMPTY_ARGS(getMethod
, 0);
94 HTTP_BEGIN_ARGS(setMethod
, 0, 1)
95 HTTP_ARG_VAL(request_method
, 0)
98 HTTP_EMPTY_ARGS(getContentType
, 0);
99 HTTP_BEGIN_ARGS(setContentType
, 0, 1)
100 HTTP_ARG_VAL(content_type
, 0)
103 HTTP_EMPTY_ARGS(getQueryData
, 0);
104 HTTP_BEGIN_ARGS(setQueryData
, 0, 0)
105 HTTP_ARG_VAL(query_data
, 0)
108 HTTP_BEGIN_ARGS(addQueryData
, 0, 1)
109 HTTP_ARG_VAL(query_data
, 0)
112 HTTP_EMPTY_ARGS(getPostFields
, 0);
113 HTTP_BEGIN_ARGS(setPostFields
, 0, 0)
114 HTTP_ARG_VAL(post_fields
, 0)
117 HTTP_BEGIN_ARGS(addPostFields
, 0, 1)
118 HTTP_ARG_VAL(post_fields
, 0)
121 HTTP_EMPTY_ARGS(getPostFiles
, 0);
122 HTTP_BEGIN_ARGS(setPostFiles
, 0, 0)
123 HTTP_ARG_VAL(post_files
, 0)
126 HTTP_BEGIN_ARGS(addPostFile
, 0, 2)
127 HTTP_ARG_VAL(formname
, 0)
128 HTTP_ARG_VAL(filename
, 0)
129 HTTP_ARG_VAL(content_type
, 0)
132 HTTP_EMPTY_ARGS(getRawPostData
, 0);
133 HTTP_BEGIN_ARGS(setRawPostData
, 0, 0)
134 HTTP_ARG_VAL(raw_post_data
, 0)
137 HTTP_BEGIN_ARGS(addRawPostData
, 0, 1)
138 HTTP_ARG_VAL(raw_post_data
, 0)
141 HTTP_EMPTY_ARGS(getPutFile
, 0);
142 HTTP_BEGIN_ARGS(setPutFile
, 0, 0)
143 HTTP_ARG_VAL(filename
, 0)
146 HTTP_EMPTY_ARGS(getResponseData
, 0);
147 HTTP_BEGIN_ARGS(getResponseHeader
, 0, 0)
148 HTTP_ARG_VAL(name
, 0)
151 HTTP_BEGIN_ARGS(getResponseCookie
, 0, 0)
152 HTTP_ARG_VAL(name
, 0)
155 HTTP_EMPTY_ARGS(getResponseBody
, 0);
156 HTTP_EMPTY_ARGS(getResponseCode
, 0);
157 HTTP_BEGIN_ARGS(getResponseInfo
, 0, 0)
158 HTTP_ARG_VAL(name
, 0)
161 HTTP_EMPTY_ARGS(getResponseMessage
, 1);
162 HTTP_EMPTY_ARGS(getRequestMessage
, 1);
163 HTTP_EMPTY_ARGS(getHistory
, 1);
164 HTTP_EMPTY_ARGS(clearHistory
, 0);
165 HTTP_EMPTY_ARGS(send
, 1);
167 HTTP_BEGIN_ARGS(get
, 0, 1)
169 HTTP_ARG_VAL(options
, 0)
170 HTTP_ARG_VAL(info
, 1)
173 HTTP_BEGIN_ARGS(head
, 0, 1)
175 HTTP_ARG_VAL(options
, 0)
176 HTTP_ARG_VAL(info
, 1)
179 HTTP_BEGIN_ARGS(postData
, 0, 2)
181 HTTP_ARG_VAL(data
, 0)
182 HTTP_ARG_VAL(options
, 0)
183 HTTP_ARG_VAL(info
, 1)
186 HTTP_BEGIN_ARGS(postFields
, 0, 2)
188 HTTP_ARG_VAL(data
, 0)
189 HTTP_ARG_VAL(options
, 0)
190 HTTP_ARG_VAL(info
, 1)
193 HTTP_BEGIN_ARGS(putFile
, 0, 2)
195 HTTP_ARG_VAL(file
, 0)
196 HTTP_ARG_VAL(options
, 0)
197 HTTP_ARG_VAL(info
, 1)
200 HTTP_BEGIN_ARGS(putStream
, 0, 2)
202 HTTP_ARG_VAL(stream
, 0)
203 HTTP_ARG_VAL(options
, 0)
204 HTTP_ARG_VAL(info
, 1)
207 HTTP_BEGIN_ARGS(methodRegister
, 0, 1)
208 HTTP_ARG_VAL(method_name
, 0)
211 HTTP_BEGIN_ARGS(methodUnregister
, 0, 1)
212 HTTP_ARG_VAL(method
, 0)
215 HTTP_BEGIN_ARGS(methodName
, 0, 1)
216 HTTP_ARG_VAL(method_id
, 0)
219 HTTP_BEGIN_ARGS(methodExists
, 0, 1)
220 HTTP_ARG_VAL(method
, 0)
223 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
224 static inline void _http_request_object_declare_default_properties(TSRMLS_D
);
225 #define http_request_object_clone_obj _http_request_object_clone_obj
226 static inline zend_object_value
_http_request_object_clone_obj(zval
*object TSRMLS_DC
);
228 zend_class_entry
*http_request_object_ce
;
229 zend_function_entry http_request_object_fe
[] = {
230 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
231 HTTP_REQUEST_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
233 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
234 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
235 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
236 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
238 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
239 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
240 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
242 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
243 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
244 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
246 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
247 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
249 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
250 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
252 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
253 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
255 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
256 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
257 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
260 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
261 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
263 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
265 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
269 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
272 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
274 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
277 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
278 HTTP_REQUEST_ME(getResponseCookie
, ZEND_ACC_PUBLIC
)
279 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
280 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
281 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
282 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
283 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
284 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
285 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
287 HTTP_REQUEST_ALIAS(get
, http_get
)
288 HTTP_REQUEST_ALIAS(head
, http_head
)
289 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
290 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
291 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
292 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
294 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
295 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
296 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
297 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
301 static zend_object_handlers http_request_object_handlers
;
303 PHP_MINIT_FUNCTION(http_request_object
)
305 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
306 http_request_object_handlers
.clone_obj
= NULL
;
310 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
312 zend_object_value ov
;
313 http_request_object
*o
;
315 o
= ecalloc(1, sizeof(http_request_object
));
317 o
->ch
= curl_easy_init();
319 phpstr_init(&o
->history
);
320 phpstr_init(&o
->request
);
321 phpstr_init_ex(&o
->response
, HTTP_CURLBUF_SIZE
, 0);
323 ALLOC_HASHTABLE(OBJ_PROP(o
));
324 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
325 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
327 ov
.handle
= putObject(http_request_object
, o
);
328 ov
.handlers
= &http_request_object_handlers
;
333 static inline void _http_request_object_declare_default_properties(TSRMLS_D
)
335 zend_class_entry
*ce
= http_request_object_ce
;
337 DCL_PROP_N(PROTECTED
, options
);
338 DCL_PROP_N(PROTECTED
, responseInfo
);
339 DCL_PROP_N(PROTECTED
, responseData
);
340 DCL_PROP_N(PROTECTED
, responseCode
);
341 DCL_PROP_N(PROTECTED
, responseMessage
);
342 DCL_PROP_N(PROTECTED
, postFields
);
343 DCL_PROP_N(PROTECTED
, postFiles
);
345 DCL_PROP(PROTECTED
, long, method
, HTTP_GET
);
347 DCL_PROP(PROTECTED
, string
, url
, "");
348 DCL_PROP(PROTECTED
, string
, contentType
, "");
349 DCL_PROP(PROTECTED
, string
, rawPostData
, "");
350 DCL_PROP(PROTECTED
, string
, queryData
, "");
351 DCL_PROP(PROTECTED
, string
, putFile
, "");
353 DCL_PROP(PUBLIC
, bool, recordHistory
, 1);
357 * Request Method Constants
360 DCL_CONST(long, "METH_GET", HTTP_GET
);
361 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
362 DCL_CONST(long, "METH_POST", HTTP_POST
);
363 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
364 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
365 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
366 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
367 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
368 /* WebDAV - RFC 2518 */
369 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
370 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
371 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
372 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
373 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
374 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
375 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
376 /* WebDAV Versioning - RFC 3253 */
377 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
378 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
379 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
380 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
381 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
382 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
383 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
384 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
385 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
386 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
387 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
388 /* WebDAV Access Control - RFC 3744 */
389 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
394 # if LIBCURL_VERSION_NUM >= 0x070a05
395 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
396 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
397 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
398 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
399 # endif /* LIBCURL_VERSION_NUM */
403 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
405 http_request_object
*o
= (http_request_object
*) object
;
408 zend_hash_destroy(OBJ_PROP(o
));
409 FREE_HASHTABLE(OBJ_PROP(o
));
412 /* avoid nasty segfaults with already cleaned up callbacks */
413 curl_easy_setopt(o
->ch
, CURLOPT_NOPROGRESS
, 1);
414 curl_easy_setopt(o
->ch
, CURLOPT_PROGRESSFUNCTION
, NULL
);
415 curl_easy_setopt(o
->ch
, CURLOPT_VERBOSE
, 0);
416 curl_easy_setopt(o
->ch
, CURLOPT_DEBUGFUNCTION
, NULL
);
417 curl_easy_cleanup(o
->ch
);
419 phpstr_dtor(&o
->response
);
420 phpstr_dtor(&o
->request
);
421 phpstr_dtor(&o
->history
);
425 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr
, http_request_body
*body TSRMLS_DC
)
429 STATUS status
= SUCCESS
;
434 HTTP_CHECK_CURL_INIT(obj
->ch
, curl_easy_init(), return FAILURE
);
436 URL
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, url
));
437 // HTTP_URI_MAXLEN+1 long char *
438 if (!(request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0))) {
442 meth
= convert_to_type_ex(IS_LONG
, GET_PROP(obj
, method
));
443 switch (Z_LVAL_P(meth
))
453 php_stream_statbuf ssb
;
454 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj
, putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
456 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
457 body
->type
= HTTP_REQUEST_BODY_UPLOADFILE
;
459 body
->size
= ssb
.sb
.st_size
;
469 /* check for raw post data */
470 zval
*raw_data
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
));
472 if (Z_STRLEN_P(raw_data
)) {
473 zval
*ctype
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, contentType
));
475 if (Z_STRLEN_P(ctype
)) {
476 zval
**headers
, *opts
= GET_PROP(obj
, options
);
478 convert_to_array(opts
);
480 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
483 convert_to_array(*headers
);
484 /* only override if not already set */
485 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) {
486 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
491 MAKE_STD_ZVAL(headers
);
493 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
494 add_assoc_zval(opts
, "headers", headers
);
498 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
499 body
->data
= estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
));
500 body
->size
= Z_STRLEN_P(raw_data
);
502 status
= http_request_body_fill(body
, Z_ARRVAL_P(GET_PROP(obj
, postFields
)), Z_ARRVAL_P(GET_PROP(obj
, postFiles
)));
508 if (status
== SUCCESS
) {
509 zval
*qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
));
511 if (Z_STRLEN_P(qdata
)) {
512 if (!strchr(request_uri
, '?')) {
513 strlcat(request_uri
, "?", HTTP_URI_MAXLEN
);
515 strlcat(request_uri
, "&", HTTP_URI_MAXLEN
);
517 strlcat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
);
520 status
= http_request_init(obj
->ch
, Z_LVAL_P(meth
), request_uri
, body
, Z_ARRVAL_P(GET_PROP(obj
, options
)));
524 /* clean previous response */
525 phpstr_dtor(&obj
->response
);
526 /* clean previous request */
527 phpstr_dtor(&obj
->request
);
532 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
536 phpstr_fix(&obj
->request
);
537 phpstr_fix(&obj
->response
);
539 msg
= http_message_parse(PHPSTR_VAL(&obj
->response
), PHPSTR_LEN(&obj
->response
));
546 zval
*headers
, *message
,
547 *resp
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
)),
548 *info
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseInfo
));
550 if (zval_is_true(GET_PROP(obj
, recordHistory
))) {
551 /* we need to act like a zipper, as we'll receive
552 * the requests and the responses in separate chains
555 http_message
*response
= msg
, *request
= http_message_parse(PHPSTR_VAL(&obj
->request
), PHPSTR_LEN(&obj
->request
));
556 http_message
*free_msg
= request
;
562 http_message_tostring(response
, &message
, &msglen
);
563 phpstr_append(&obj
->history
, message
, msglen
);
566 http_message_tostring(request
, &message
, &msglen
);
567 phpstr_append(&obj
->history
, message
, msglen
);
570 } while ((response
= response
->parent
) && (request
= request
->parent
));
572 http_message_free(&free_msg
);
573 phpstr_fix(&obj
->history
);
576 UPD_PROP(obj
, long, responseCode
, msg
->http
.info
.response
.code
);
578 MAKE_STD_ZVAL(headers
)
581 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
582 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
584 add_assoc_zval(resp
, "headers", headers
);
585 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
587 MAKE_STD_ZVAL(message
);
588 ZVAL_OBJVAL(message
, http_message_object_from_msg(msg
));
589 SET_PROP(obj
, responseMessage
, message
);
590 zval_ptr_dtor(&message
);
592 http_request_info(obj
->ch
, Z_ARRVAL_P(info
));
593 SET_PROP(obj
, responseInfo
, info
);
599 #define http_request_object_set_options_subr(key, ow) \
600 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
601 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
603 zval
*opts
, **options
, *new_options
= NULL
;
604 getObject(http_request_object
, obj
);
606 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &new_options
)) {
610 opts
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, options
));
612 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
613 convert_to_array(*options
);
615 zend_hash_clean(Z_ARRVAL_PP(options
));
617 if (new_options
&& zend_hash_num_elements(Z_ARRVAL_P(new_options
))) {
619 array_copy(new_options
, *options
);
621 array_merge(new_options
, *options
);
624 } else if (new_options
&& zend_hash_num_elements(Z_ARRVAL_P(new_options
))) {
625 zval_add_ref(&new_options
);
626 add_assoc_zval(opts
, key
, new_options
);
632 #define http_request_object_get_options_subr(key) \
633 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
634 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
639 zval
*opts
, **options
;
640 getObject(http_request_object
, obj
);
642 opts
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
));
644 array_init(return_value
);
646 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
647 convert_to_array(*options
);
648 array_copy(*options
, return_value
);
654 /* ### USERLAND ### */
656 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
658 * Instantiate a new HttpRequest object.
660 * Accepts a string as optional parameter containing the target request url.
661 * Additianally accepts an optional int parameter specifying the request method
662 * to use and an associative array as optional third parameter which will be
663 * passed to HttpRequest::setOptions().
665 * Throws HttpException.
667 PHP_METHOD(HttpRequest
, __construct
)
672 zval
*options
= NULL
;
673 getObject(http_request_object
, obj
);
676 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
677 INIT_PARR(obj
, options
);
678 INIT_PARR(obj
, responseInfo
);
679 INIT_PARR(obj
, responseData
);
680 INIT_PARR(obj
, postFields
);
681 INIT_PARR(obj
, postFiles
);
684 UPD_STRL(obj
, url
, URL
, URL_len
);
687 UPD_PROP(obj
, long, method
, meth
);
690 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
697 /* {{{ proto void HttpRequest::__destruct()
699 * Destroys the HttpRequest object.
701 PHP_METHOD(HttpRequest
, __destruct
)
703 getObject(http_request_object
, obj
);
707 FREE_PARR(obj
, options
);
708 FREE_PARR(obj
, responseInfo
);
709 FREE_PARR(obj
, responseData
);
710 FREE_PARR(obj
, postFields
);
711 FREE_PARR(obj
, postFiles
);
715 /* {{{ proto bool HttpRequest::setOptions([array options])
717 * Set the request options to use. See http_get() for a full list of available options.
719 * Accepts an array as optional parameters, wich values will overwrite the
720 * currently set request options. If the parameter is empty or mitted,
721 * the optoions of the HttpRequest object will be reset.
723 * Returns TRUE on success, or FALSE on failure.
725 PHP_METHOD(HttpRequest
, setOptions
)
729 zval
*opts
= NULL
, *old_opts
, **opt
;
730 getObject(http_request_object
, obj
);
732 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!", &opts
)) {
736 old_opts
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, options
));
738 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
739 zend_hash_clean(Z_ARRVAL_P(old_opts
));
743 /* some options need extra attention -- thus cannot use array_merge() directly */
744 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
746 if (!strcmp(key
, "headers")) {
748 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
749 convert_to_array_ex(opt
);
750 convert_to_array(*headers
);
751 array_merge(*opt
, *headers
);
754 } else if (!strcmp(key
, "cookies")) {
756 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
757 convert_to_array_ex(opt
);
758 convert_to_array(*cookies
);
759 array_merge(*opt
, *cookies
);
762 } else if (!strcmp(key
, "ssl")) {
764 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl
)) {
765 convert_to_array_ex(opt
);
766 convert_to_array(*ssl
);
767 array_merge(*opt
, *ssl
);
770 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
771 if (Z_TYPE_PP(opt
) != IS_STRING
) {
772 convert_to_string_ex(opt
);
774 UPD_STRL(obj
, url
, Z_STRVAL_PP(opt
), Z_STRLEN_PP(opt
));
776 } else if (!strcmp(key
, "method")) {
777 if (Z_TYPE_PP(opt
) != IS_LONG
) {
778 convert_to_long_ex(opt
);
780 UPD_PROP(obj
, long, method
, Z_LVAL_PP(opt
));
785 add_assoc_zval(old_opts
, key
, *opt
);
796 /* {{{ proto array HttpRequest::getOptions()
798 * Get currently set options.
800 * Returns an associative array containing currently set options.
802 PHP_METHOD(HttpRequest
, getOptions
)
808 getObject(http_request_object
, obj
);
810 opts
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
));
811 array_init(return_value
);
812 array_copy(opts
, return_value
);
817 /* {{{ proto bool HttpRequest::setSslOptions([array options])
821 * Accepts an associative array as parameter containing any SSL specific options.
822 * If the parameter is empty or omitted, the SSL options will be reset.
824 * Returns TRUE on success, or FALSE on failure.
826 PHP_METHOD(HttpRequest
, setSslOptions
)
828 http_request_object_set_options_subr("ssl", 1);
832 /* {{{ proto bool HttpRequest::addSslOptions(array options)
834 * Set additional SSL options.
836 * Expects an associative array as parameter containing additional SSL specific options.
838 * Returns TRUE on success, or FALSE on failure.
840 PHP_METHOD(HttpRequest
, addSslOptions
)
842 http_request_object_set_options_subr("ssl", 0);
846 /* {{{ proto array HttpRequest::getSslOtpions()
848 * Get previously set SSL options.
850 * Returns an associative array containing any previously set SSL options.
852 PHP_METHOD(HttpRequest
, getSslOptions
)
854 http_request_object_get_options_subr("ssl");
858 /* {{{ proto bool HttpRequest::addHeaders(array headers)
860 * Add request header name/value pairs.
862 * Expects an ssociative array as parameter containing additional header
865 * Returns TRUE on success, or FALSE on failure.
867 PHP_METHOD(HttpRequest
, addHeaders
)
869 http_request_object_set_options_subr("headers", 0);
872 /* {{{ proto bool HttpRequest::setHeaders([array headers])
874 * Set request header name/value pairs.
876 * Accepts an associative array as parameter containing header name/value pairs.
877 * If the parameter is empty or omitted, all previously set headers will be unset.
879 * Returns TRUE on success, or FALSE on failure.
881 PHP_METHOD(HttpRequest
, setHeaders
)
883 http_request_object_set_options_subr("headers", 1);
887 /* {{{ proto array HttpRequest::getHeaders()
889 * Get previously set request headers.
891 * Returns an associative array containing all currently set headers.
893 PHP_METHOD(HttpRequest
, getHeaders
)
895 http_request_object_get_options_subr("headers");
899 /* {{{ proto bool HttpRequest::setCookies([array cookies])
903 * Accepts an associative array as parameter containing cookie name/value pairs.
904 * If the parameter is empty or omitted, all previously set cookies will be unset.
906 * Returns TRUE on success, or FALSE on failure.
908 PHP_METHOD(HttpRequest
, setCookies
)
910 http_request_object_set_options_subr("cookies", 1);
914 /* {{{ proto bool HttpRequest::addCookies(array cookies)
918 * Expects an associative array as parameter containing any cookie name/value
921 * Returns TRUE on success, or FALSE on failure.
923 PHP_METHOD(HttpRequest
, addCookies
)
925 http_request_object_set_options_subr("cookies", 0);
929 /* {{{ proto array HttpRequest::getCookies()
931 * Get previously set cookies.
933 * Returns an associative array containing any previously set cookies.
935 PHP_METHOD(HttpRequest
, getCookies
)
937 http_request_object_get_options_subr("cookies");
941 /* {{{ proto bool HttpRequest::setUrl(string url)
943 * Set the request URL.
945 * Expects a string as parameter specifying the request url.
947 * Returns TRUE on success, or FALSE on failure.
949 PHP_METHOD(HttpRequest
, setUrl
)
953 getObject(http_request_object
, obj
);
955 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
959 UPD_STRL(obj
, url
, URL
, URL_len
);
964 /* {{{ proto string HttpRequest::getUrl()
966 * Get the previously set request URL.
968 * Returns the currently set request url as string.
970 PHP_METHOD(HttpRequest
, getUrl
)
975 getObject(http_request_object
, obj
);
976 zval
*URL
= GET_PROP(obj
, url
);
978 RETURN_ZVAL(URL
, 1, 0);
983 /* {{{ proto bool HttpRequest::setMethod(int request_method)
985 * Set the request method.
987 * Expects an int as parameter specifying the request method to use.
988 * In PHP 5.1+ HttpRequest::METH, otherwise the HTTP_METH constants can be used.
990 * Returns TRUE on success, or FALSE on failure.
992 PHP_METHOD(HttpRequest
, setMethod
)
995 getObject(http_request_object
, obj
);
997 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1001 UPD_PROP(obj
, long, method
, meth
);
1006 /* {{{ proto int HttpRequest::getMethod()
1008 * Get the previously set request method.
1010 * Returns the currently set request method.
1012 PHP_METHOD(HttpRequest
, getMethod
)
1017 getObject(http_request_object
, obj
);
1018 zval
*meth
= GET_PROP(obj
, method
);
1020 RETURN_ZVAL(meth
, 1, 0);
1025 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1027 * Set the content type the post request should have.
1029 * Expects a string as parameters containing the content type of the request
1030 * (primary/secondary).
1032 * Returns TRUE on success, or FALSE if the content type does not seem to
1033 * contain a primary and a secondary part.
1035 PHP_METHOD(HttpRequest
, setContentType
)
1039 getObject(http_request_object
, obj
);
1041 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1045 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1046 UPD_STRL(obj
, contentType
, ctype
, ct_len
);
1051 /* {{{ proto string HttpRequest::getContentType()
1053 * Get the previously content type.
1055 * Returns the previously set content type as string.
1057 PHP_METHOD(HttpRequest
, getContentType
)
1062 getObject(http_request_object
, obj
);
1063 zval
*ctype
= GET_PROP(obj
, contentType
);
1065 RETURN_ZVAL(ctype
, 1, 0);
1070 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1072 * Set the URL query parameters to use, overwriting previously set query parameters.
1073 * Affects any request types.
1075 * Accepts a string or associative array parameter containing the pre-encoded
1076 * query string or to be encoded query fields. If the parameter is empty or
1077 * omitted, the query data will be unset.
1079 * Returns TRUE on success, or FALSE on failure.
1081 PHP_METHOD(HttpRequest
, setQueryData
)
1084 getObject(http_request_object
, obj
);
1086 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1090 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1091 UPD_STRL(obj
, queryData
, "", 0);
1092 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1093 char *query_data
= NULL
;
1095 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1099 UPD_PROP(obj
, string
, queryData
, query_data
);
1102 convert_to_string_ex(&qdata
);
1103 UPD_STRL(obj
, queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1109 /* {{{ proto string HttpRequest::getQueryData()
1111 * Get the current query data in form of an urlencoded query string.
1113 * Returns a string containing the urlencoded query.
1115 PHP_METHOD(HttpRequest
, getQueryData
)
1120 getObject(http_request_object
, obj
);
1121 zval
*qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
));
1123 RETURN_ZVAL(qdata
, 1, 0);
1128 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1130 * Add parameters to the query parameter list, leaving previously set unchanged.
1131 * Affects any request type.
1133 * Expects an associative array as parameter containing the query fields to add.
1135 * Returns TRUE on success, or FALSE on failure.
1137 PHP_METHOD(HttpRequest
, addQueryData
)
1139 zval
*qdata
, *old_qdata
;
1140 char *query_data
= NULL
;
1141 size_t query_data_len
= 0;
1142 getObject(http_request_object
, obj
);
1144 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1148 old_qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
));
1150 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
)) {
1154 UPD_STRL(obj
, queryData
, query_data
, query_data_len
);
1161 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1163 * Adds POST data entries, leaving previously set unchanged, unless a
1164 * post entry with the same name already exists.
1165 * Affects only POST and custom requests.
1167 * Expects an associative array as parameter containing the post fields.
1169 * Returns TRUE on success, or FALSE on failure.
1171 PHP_METHOD(HttpRequest
, addPostFields
)
1173 zval
*post
, *post_data
;
1174 getObject(http_request_object
, obj
);
1176 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1180 post
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFields
));
1181 array_merge(post_data
, post
);
1187 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1189 * Set the POST data entries, overwriting previously set POST data.
1190 * Affects only POST and custom requests.
1192 * Accepts an associative array as parameter containing the post fields.
1193 * If the parameter is empty or omitted, the post data will be unset.
1195 * Returns TRUE on success, or FALSE on failure.
1197 PHP_METHOD(HttpRequest
, setPostFields
)
1199 zval
*post
, *post_data
= NULL
;
1200 getObject(http_request_object
, obj
);
1202 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!", &post_data
)) {
1206 post
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFields
));
1207 zend_hash_clean(Z_ARRVAL_P(post
));
1209 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1210 array_copy(post_data
, post
);
1217 /* {{{ proto array HttpRequest::getPostFields()
1219 * Get previously set POST data.
1221 * Returns the currently set post fields as associative array.
1223 PHP_METHOD(HttpRequest
, getPostFields
)
1228 getObject(http_request_object
, obj
);
1229 zval
*post_data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFields
));
1231 array_init(return_value
);
1232 array_copy(post_data
, return_value
);
1237 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1239 * Set raw post data to send, overwriting previously set raw post data. Don't
1240 * forget to specify a content type. Affects only POST and custom requests.
1241 * Only either post fields or raw post data can be used for each request.
1242 * Raw post data has higher precedence and will be used even if post fields
1245 * Accepts a string as parameter containing the *raw* post data.
1247 * Returns TRUE on success, or FALSE on failure.
1249 PHP_METHOD(HttpRequest
, setRawPostData
)
1251 char *raw_data
= NULL
;
1253 getObject(http_request_object
, obj
);
1255 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1263 UPD_STRL(obj
, rawPostData
, raw_data
, data_len
);
1268 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1270 * Add raw post data, leaving previously set raw post data unchanged.
1271 * Affects only POST and custom requests.
1273 * Expects a string as parameter containing the raw post data to concatenate.
1275 * Returns TRUE on success, or FALSE on failure.
1277 PHP_METHOD(HttpRequest
, addRawPostData
)
1279 char *raw_data
, *new_data
;
1281 getObject(http_request_object
, obj
);
1283 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1288 zval
*zdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
));
1290 new_data
= emalloc(Z_STRLEN_P(zdata
) + data_len
+ 1);
1291 new_data
[Z_STRLEN_P(zdata
) + data_len
] = '\0';
1293 if (Z_STRLEN_P(zdata
)) {
1294 memcpy(new_data
, Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
));
1297 memcpy(new_data
+ Z_STRLEN_P(zdata
), raw_data
, data_len
);
1298 UPD_STRL(obj
, rawPostData
, new_data
, Z_STRLEN_P(zdata
) + data_len
);
1305 /* {{{ proto string HttpRequest::getRawPostData()
1307 * Get previously set raw post data.
1309 * Returns a string containing the currently set raw post data.
1311 PHP_METHOD(HttpRequest
, getRawPostData
)
1316 getObject(http_request_object
, obj
);
1317 zval
*raw_data
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
));
1319 RETURN_ZVAL(raw_data
, 1, 0);
1324 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1326 * Add a file to the POST request, leaving prefiously set files unchanged.
1327 * Affects only POST and custom requests. Cannot be used with raw post data.
1329 * Expects a string parameter containing the form element name, and a string
1330 * paremeter containing the path to the file which should be uploaded.
1331 * Additionally accepts an optional string parameter which chould contain
1332 * the content type of the file.
1334 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1335 * primary and a secondary content type part.
1337 PHP_METHOD(HttpRequest
, addPostFile
)
1339 zval
*files
, *entry
;
1340 char *name
, *file
, *type
= NULL
;
1341 int name_len
, file_len
, type_len
= 0;
1342 getObject(http_request_object
, obj
);
1344 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1349 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1351 type
= "application/x-octetstream";
1352 type_len
= sizeof("application/x-octetstream") - 1;
1355 MAKE_STD_ZVAL(entry
);
1358 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1359 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1360 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1362 files
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1363 add_next_index_zval(files
, entry
);
1369 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1371 * Set files to post, overwriting previously set post files.
1372 * Affects only POST and requests. Cannot be used with raw post data.
1374 * Accepts an array containing the files to post. Each entry should be an
1375 * associative array with "name", "file" and "type" keys. If the parameter
1376 * is empty or omitted the post files will be unset.
1378 * Returns TRUE on success, or FALSE on failure.
1380 PHP_METHOD(HttpRequest
, setPostFiles
)
1382 zval
*files
, *pFiles
;
1383 getObject(http_request_object
, obj
);
1385 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &files
)) {
1389 pFiles
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1390 zend_hash_clean(Z_ARRVAL_P(pFiles
));
1392 if (files
&& zend_hash_num_elements(Z_ARRVAL_P(files
))) {
1393 array_copy(files
, pFiles
);
1400 /* {{{ proto array HttpRequest::getPostFiles()
1402 * Get all previously added POST files.
1404 * Returns an array containing currently set post files.
1406 PHP_METHOD(HttpRequest
, getPostFiles
)
1411 getObject(http_request_object
, obj
);
1412 zval
*files
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1414 array_init(return_value
);
1415 array_copy(files
, return_value
);
1420 /* {{{ proto bool HttpRequest::setPutFile([string file])
1422 * Set file to put. Affects only PUT requests.
1424 * Accepts a string as parameter referencing the path to file.
1425 * If the parameter is empty or omitted the put file will be unset.
1427 * Returns TRUE on success, or FALSE on failure.
1429 PHP_METHOD(HttpRequest
, setPutFile
)
1433 getObject(http_request_object
, obj
);
1435 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1439 UPD_STRL(obj
, putFile
, file
, file_len
);
1444 /* {{{ proto string HttpRequest::getPutFile()
1446 * Get previously set put file.
1448 * Returns a string containing the path to the currently set put file.
1450 PHP_METHOD(HttpRequest
, getPutFile
)
1455 getObject(http_request_object
, obj
);
1456 zval
*putfile
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, putFile
));
1458 RETURN_ZVAL(putfile
, 1, 0);
1463 /* {{{ proto array HttpRequest::getResponseData()
1465 * Get all response data after the request has been sent.
1467 * Returns an associative array with the key "headers" containing an associative
1468 * array holding all response headers, as well as the ley "body" containing a
1469 * string with the response body.
1471 * If redirects were allowed and several responses were received, the data
1472 * references the last received response.
1474 PHP_METHOD(HttpRequest
, getResponseData
)
1479 getObject(http_request_object
, obj
);
1480 zval
*data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
));
1482 array_init(return_value
);
1483 array_copy(data
, return_value
);
1488 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1490 * Get response header(s) after the request has been sent.
1492 * Accepts an string as optional parameter specifying a certain header to read.
1493 * If the parameter is empty or omitted all response headers will be returned.
1495 * Returns either a string with the value of the header matching name if requested,
1496 * FALSE on failure, or an associative array containing all reponse headers.
1498 * If redirects were allowed and several responses were received, the data
1499 * references the last received response.
1501 PHP_METHOD(HttpRequest
, getResponseHeader
)
1504 zval
*data
, **headers
, **header
;
1505 char *header_name
= NULL
;
1507 getObject(http_request_object
, obj
);
1509 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1513 data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
));
1514 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1517 convert_to_array_ex(headers
);
1518 if (!header_len
|| !header_name
) {
1519 array_init(return_value
);
1520 array_copy(*headers
, return_value
);
1521 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1522 RETURN_ZVAL(*header
, 1, 0);
1530 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1532 * Get response cookie(s) after the request has been sent.
1534 * Accepts a string as optional parameter specifying the name of the cookie to read.
1535 * If the parameter is empty or omitted, an associative array with all received
1536 * cookies will be returned.
1538 * Returns either an associative array with the cookie's name, value and any
1539 * additional params of the cookie matching name if requested, FALSE on failure,
1540 * or an array containing all received cookies as arrays.
1542 * If redirects were allowed and several responses were received, the data
1543 * references the last received response.
1545 PHP_METHOD(HttpRequest
, getResponseCookie
)
1548 zval
*data
, **headers
;
1549 char *cookie_name
= NULL
;
1551 getObject(http_request_object
, obj
);
1553 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1557 array_init(return_value
);
1559 data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
));
1560 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1563 zval
**header
= NULL
;
1565 convert_to_array_ex(headers
);
1566 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1567 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1568 /* several cookies? */
1569 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1572 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1574 MAKE_STD_ZVAL(cookie_hash
);
1575 array_init(cookie_hash
);
1577 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1579 add_next_index_zval(return_value
, cookie_hash
);
1583 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1584 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1585 add_next_index_zval(return_value
, cookie_hash
);
1586 return; /* <<< FOUND >>> */
1588 zval_dtor(cookie_hash
);
1593 zval_dtor(cookie_hash
);
1600 MAKE_STD_ZVAL(cookie_hash
);
1601 array_init(cookie_hash
);
1602 convert_to_string_ex(header
);
1604 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1606 add_next_index_zval(return_value
, cookie_hash
);
1610 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1611 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1612 add_next_index_zval(return_value
, cookie_hash
);
1614 zval_dtor(cookie_hash
);
1619 zval_dtor(cookie_hash
);
1633 /* {{{ proto string HttpRequest::getResponseBody()
1635 * Get the response body after the request has been sent.
1637 * Returns a string containing the response body.
1639 * If redirects were allowed and several responses were received, the data
1640 * references the last received response.
1642 PHP_METHOD(HttpRequest
, getResponseBody
)
1648 getObject(http_request_object
, obj
);
1649 zval
*data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
));
1651 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1652 convert_to_string_ex(body
);
1653 RETURN_ZVAL(*body
, 1, 0);
1661 /* {{{ proto int HttpRequest::getResponseCode()
1663 * Get the response code after the request has been sent.
1665 * Returns an int representing the response code.
1667 * If redirects were allowed and several responses were received, the data
1668 * references the last received response.
1670 PHP_METHOD(HttpRequest
, getResponseCode
)
1675 getObject(http_request_object
, obj
);
1676 zval
*code
= convert_to_type_ex(IS_LONG
, GET_PROP(obj
, responseCode
));
1678 RETURN_ZVAL(code
, 1, 0);
1683 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1685 * Get response info after the request has been sent.
1686 * See http_get() for a full list of returned info.
1688 * Accepts a string as optional parameter specifying the info to read.
1689 * If the parameter is empty or omitted, an associative array containing
1690 * all available info will be returned.
1692 * Returns either a scalar containing the value of the info matching name if
1693 * requested, FALSE on failure, or an associative array containing all
1696 * If redirects were allowed and several responses were received, the data
1697 * references the last received response.
1699 PHP_METHOD(HttpRequest
, getResponseInfo
)
1702 zval
*info
, **infop
;
1703 char *info_name
= NULL
;
1705 getObject(http_request_object
, obj
);
1707 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1711 info
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseInfo
));
1713 if (info_len
&& info_name
) {
1714 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1715 RETURN_ZVAL(*infop
, 1, 0);
1717 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1721 array_init(return_value
);
1722 array_copy(info
, return_value
);
1728 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1730 * Get the full response as HttpMessage object after the request has been sent.
1732 * Returns an HttpMessage object of the response.
1734 * If redirects were allowed and several responses were received, the data
1735 * references the last received response. Use HttpMessage::getParentMessage()
1736 * to access the data of previously received responses whithin this request
1739 * Throws HttpException.
1741 PHP_METHOD(HttpRequest
, getResponseMessage
)
1747 getObject(http_request_object
, obj
);
1749 SET_EH_THROW_HTTP();
1750 message
= GET_PROP(obj
, responseMessage
);
1751 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1752 RETVAL_OBJECT(message
);
1761 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1763 * Get sent HTTP message.
1765 * Returns an HttpMessage object representing the sent request.
1767 * If redirects were allowed and several responses were received, the data
1768 * references the last received response. Use HttpMessage::getParentMessage()
1769 * to access the data of previously sent requests whithin this request
1772 PHP_METHOD(HttpRequest
, getRequestMessage
)
1778 getObject(http_request_object
, obj
);
1780 SET_EH_THROW_HTTP();
1781 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->request
), PHPSTR_LEN(&obj
->request
))) {
1782 RETVAL_OBJVAL(http_message_object_from_msg(msg
));
1789 /* {{{ proto HttpMessage HttpRequest::getHistory()
1791 * Get all sent requests and received responses as an HttpMessage object.
1793 * If you don't want to record history at all, set the instance variable
1794 * HttpRequest::$recoedHistory to FALSE.
1796 * Returns an HttpMessage object representing the complete request/response
1799 * The object references the last received response, use HttpMessage::getParentMessage()
1800 * to access the data of previously sent requests and received responses.
1802 * Throws HttpMalformedHeaderException.
1804 PHP_METHOD(HttpRequest
, getHistory
)
1810 getObject(http_request_object
, obj
);
1812 SET_EH_THROW_HTTP();
1813 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->history
), PHPSTR_LEN(&obj
->history
))) {
1814 RETVAL_OBJVAL(http_message_object_from_msg(msg
));
1821 /* {{{ proto void HttpRequest::clearHistory()
1823 * Clear the history.
1825 PHP_METHOD(HttpRequest
, clearHistory
)
1828 getObject(http_request_object
, obj
);
1829 phpstr_dtor(&obj
->history
);
1834 /* {{{ proto HttpMessage HttpRequest::send()
1836 * Send the HTTP request.
1838 * Returns the received response as HttpMessage object.
1840 * Throws HttpRuntimeException, HttpRequestException,
1841 * HttpMalformedHeaderException, HttpEncodingException.
1846 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1847 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1848 * $r->addQueryData(array('category' => 3));
1851 * if ($r->getResponseCode() == 200) {
1852 * file_put_contents('local.rss', $r->getResponseBody());
1854 * } catch (HttpException $ex) {
1863 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1864 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1865 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1866 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1868 * echo $r->send()->getBody();
1869 * } catch (HttpException $ex) {
1875 PHP_METHOD(HttpRequest
, send
)
1877 http_request_body body
= {0, NULL
, 0};
1878 getObject(http_request_object
, obj
);
1882 SET_EH_THROW_HTTP();
1885 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1892 if ( (SUCCESS
== http_request_object_requesthandler(obj
, getThis(), &body
)) &&
1893 (SUCCESS
== http_request_exec(obj
->ch
, NULL
, &obj
->response
, &obj
->request
)) &&
1894 (SUCCESS
== http_request_object_responsehandler(obj
, getThis()))) {
1895 RETVAL_OBJECT(GET_PROP(obj
, responseMessage
));
1897 http_request_body_dtor(&body
);
1903 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1910 * vim600: noet sw=4 ts=4 fdm=marker
1911 * vim<600: noet sw=4 ts=4