2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
21 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
23 #include "zend_interfaces.h"
25 #include "php_http_std_defs.h"
26 #include "php_http_request_object.h"
27 #include "php_http_request_api.h"
28 #include "php_http_request_pool_api.h"
30 #include "php_http_api.h"
31 #include "php_http_url_api.h"
32 #include "php_http_message_api.h"
33 #include "php_http_message_object.h"
34 #include "php_http_exception_object.h"
39 # include <winsock2.h>
41 #include <curl/curl.h>
43 ZEND_EXTERN_MODULE_GLOBALS(http
);
45 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
46 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
47 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
48 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
50 HTTP_EMPTY_ARGS(__destruct
, 0);
51 HTTP_BEGIN_ARGS(__construct
, 0, 0)
53 HTTP_ARG_VAL(method
, 0)
54 HTTP_ARG_VAL(options
, 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 PHP_MINIT_FUNCTION(http_request_object
)
300 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
301 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
305 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
307 return http_request_object_new_ex(ce
, curl_easy_init(), NULL
);
310 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
312 zend_object_value ov
;
313 http_request_object
*o
;
315 o
= ecalloc(1, sizeof(http_request_object
));
323 phpstr_init(&o
->history
);
324 phpstr_init(&o
->request
);
325 phpstr_init_ex(&o
->response
, HTTP_CURLBUF_SIZE
, 0);
327 ALLOC_HASHTABLE(OBJ_PROP(o
));
328 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
329 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
331 ov
.handle
= putObject(http_request_object
, o
);
332 ov
.handlers
= &http_request_object_handlers
;
337 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
340 zend_object_value new_ov
;
341 http_request_object
*new_obj
;
342 getObject(http_request_object
, old_obj
);
344 old_zo
= zend_objects_get_address(this_ptr TSRMLS_CC
);
345 new_ov
= http_request_object_new_ex(old_zo
->ce
, curl_easy_duphandle(old_obj
->ch
), &new_obj
);
347 zend_objects_clone_members(&new_obj
->zo
, new_ov
, old_zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
348 phpstr_append(&new_obj
->history
, old_obj
->history
.data
, old_obj
->history
.used
);
349 phpstr_append(&new_obj
->request
, old_obj
->request
.data
, old_obj
->request
.used
);
350 phpstr_append(&new_obj
->response
, old_obj
->response
.data
, old_obj
->response
.used
);
355 static inline void _http_request_object_declare_default_properties(TSRMLS_D
)
357 zend_class_entry
*ce
= http_request_object_ce
;
359 DCL_PROP_N(PROTECTED
, options
);
360 DCL_PROP_N(PROTECTED
, responseInfo
);
361 DCL_PROP_N(PROTECTED
, responseData
);
362 DCL_PROP_N(PROTECTED
, responseCode
);
363 DCL_PROP_N(PROTECTED
, responseMessage
);
364 DCL_PROP_N(PROTECTED
, postFields
);
365 DCL_PROP_N(PROTECTED
, postFiles
);
367 DCL_PROP(PROTECTED
, long, method
, HTTP_GET
);
369 DCL_PROP(PROTECTED
, string
, url
, "");
370 DCL_PROP(PROTECTED
, string
, contentType
, "");
371 DCL_PROP(PROTECTED
, string
, rawPostData
, "");
372 DCL_PROP(PROTECTED
, string
, queryData
, "");
373 DCL_PROP(PROTECTED
, string
, putFile
, "");
375 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
379 * Request Method Constants
382 DCL_CONST(long, "METH_GET", HTTP_GET
);
383 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
384 DCL_CONST(long, "METH_POST", HTTP_POST
);
385 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
386 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
387 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
388 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
389 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
390 /* WebDAV - RFC 2518 */
391 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
392 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
393 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
394 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
395 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
396 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
397 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
398 /* WebDAV Versioning - RFC 3253 */
399 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
400 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
401 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
402 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
403 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
404 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
405 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
406 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
407 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
408 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
409 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
410 /* WebDAV Access Control - RFC 3744 */
411 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
416 # if LIBCURL_VERSION_NUM >= 0x070a05
417 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
418 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
419 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
420 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
421 # endif /* LIBCURL_VERSION_NUM */
425 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
427 http_request_object
*o
= (http_request_object
*) object
;
430 zend_hash_destroy(OBJ_PROP(o
));
431 FREE_HASHTABLE(OBJ_PROP(o
));
434 /* avoid nasty segfaults with already cleaned up callbacks */
435 curl_easy_setopt(o
->ch
, CURLOPT_NOPROGRESS
, 1);
436 curl_easy_setopt(o
->ch
, CURLOPT_PROGRESSFUNCTION
, NULL
);
437 curl_easy_setopt(o
->ch
, CURLOPT_VERBOSE
, 0);
438 curl_easy_setopt(o
->ch
, CURLOPT_DEBUGFUNCTION
, NULL
);
439 curl_easy_cleanup(o
->ch
);
441 phpstr_dtor(&o
->response
);
442 phpstr_dtor(&o
->request
);
443 phpstr_dtor(&o
->history
);
447 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr
, http_request_body
*body TSRMLS_DC
)
449 zval
*meth
, *URL
, *meth_p
, *URL_p
;
451 STATUS status
= SUCCESS
;
456 HTTP_CHECK_CURL_INIT(obj
->ch
, curl_easy_init(), return FAILURE
);
458 URL
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, url
), &URL_p
);
459 // HTTP_URI_MAXLEN+1 long char *
460 if (!(request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0))) {
462 zval_ptr_dtor(&URL_p
);
467 zval_ptr_dtor(&URL_p
);
470 meth
= convert_to_type_ex(IS_LONG
, GET_PROP(obj
, method
), &meth_p
);
471 switch (Z_LVAL_P(meth
))
481 php_stream_statbuf ssb
;
482 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj
, putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
484 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
485 body
->type
= HTTP_REQUEST_BODY_UPLOADFILE
;
487 body
->size
= ssb
.sb
.st_size
;
497 /* check for raw post data */
498 zval
*raw_data_p
, *raw_data
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
), &raw_data_p
);
500 if (Z_STRLEN_P(raw_data
)) {
501 zval
*ctype_p
, *ctype
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, contentType
), &ctype_p
);
503 if (Z_STRLEN_P(ctype
)) {
504 zval
**headers
, *opts
= GET_PROP(obj
, options
);
506 convert_to_array(opts
);
508 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
511 convert_to_array(*headers
);
512 /* only override if not already set */
513 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) {
514 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
519 MAKE_STD_ZVAL(headers
);
521 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
522 add_assoc_zval(opts
, "headers", headers
);
527 zval_ptr_dtor(&ctype_p
);
530 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
531 body
->data
= estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
));
532 body
->size
= Z_STRLEN_P(raw_data
);
534 status
= http_request_body_fill(body
, Z_ARRVAL_P(GET_PROP(obj
, postFields
)), Z_ARRVAL_P(GET_PROP(obj
, postFiles
)));
538 zval_ptr_dtor(&raw_data_p
);
545 zval_ptr_dtor(&meth_p
);
548 if (status
== SUCCESS
) {
549 zval
*qdata_p
, *qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
), &qdata_p
);
551 if (Z_STRLEN_P(qdata
)) {
552 if (!strchr(request_uri
, '?')) {
553 strlcat(request_uri
, "?", HTTP_URI_MAXLEN
);
555 strlcat(request_uri
, "&", HTTP_URI_MAXLEN
);
557 strlcat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
);
561 zval_ptr_dtor(&qdata_p
);
564 status
= http_request_init(obj
->ch
, Z_LVAL_P(meth
), request_uri
, body
, Z_ARRVAL_P(GET_PROP(obj
, options
)));
568 /* clean previous response */
569 phpstr_dtor(&obj
->response
);
570 /* clean previous request */
571 phpstr_dtor(&obj
->request
);
576 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
580 phpstr_fix(&obj
->request
);
581 phpstr_fix(&obj
->response
);
583 msg
= http_message_parse(PHPSTR_VAL(&obj
->response
), PHPSTR_LEN(&obj
->response
));
590 zval
*headers
, *message
,
591 *resp
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
)),
592 *info
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseInfo
));
597 if (zval_is_true(GET_PROP(obj
, recordHistory
))) {
598 /* we need to act like a zipper, as we'll receive
599 * the requests and the responses in separate chains
602 http_message
*response
= msg
, *request
= http_message_parse(PHPSTR_VAL(&obj
->request
), PHPSTR_LEN(&obj
->request
));
603 http_message
*free_msg
= request
;
609 http_message_tostring(response
, &message
, &msglen
);
610 phpstr_append(&obj
->history
, message
, msglen
);
613 http_message_tostring(request
, &message
, &msglen
);
614 phpstr_append(&obj
->history
, message
, msglen
);
617 } while ((response
= response
->parent
) && (request
= request
->parent
));
619 http_message_free(&free_msg
);
620 phpstr_fix(&obj
->history
);
623 UPD_PROP(obj
, long, responseCode
, msg
->http
.info
.response
.code
);
625 MAKE_STD_ZVAL(headers
)
628 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
629 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
631 add_assoc_zval(resp
, "headers", headers
);
632 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
633 SET_PROP(obj
, responseData
, resp
);
635 MAKE_STD_ZVAL(message
);
636 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
637 SET_PROP(obj
, responseMessage
, message
);
638 zval_ptr_dtor(&message
);
640 http_request_info(obj
->ch
, Z_ARRVAL_P(info
));
641 SET_PROP(obj
, responseInfo
, info
);
647 #define http_request_object_set_options_subr(key, ow) \
648 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
649 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
651 zval
*opts
, **options
, *new_options
= NULL
;
652 getObject(http_request_object
, obj
);
654 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &new_options
)) {
658 opts
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, options
));
660 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
661 convert_to_array(*options
);
663 zend_hash_clean(Z_ARRVAL_PP(options
));
665 if (new_options
&& zend_hash_num_elements(Z_ARRVAL_P(new_options
))) {
667 array_copy(new_options
, *options
);
669 array_merge(new_options
, *options
);
672 } else if (new_options
&& zend_hash_num_elements(Z_ARRVAL_P(new_options
))) {
673 ZVAL_ADDREF(new_options
);
674 add_assoc_zval(opts
, key
, new_options
);
680 #define http_request_object_get_options_subr(key) \
681 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
682 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
687 zval
*opts_p
, *opts
, **options
;
688 getObject(http_request_object
, obj
);
690 opts
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
), &opts_p
);
692 array_init(return_value
);
694 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
695 convert_to_array(*options
);
696 array_copy(*options
, return_value
);
700 zval_ptr_dtor(&opts_p
);
706 /* ### USERLAND ### */
708 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
710 * Instantiate a new HttpRequest object.
712 * Accepts a string as optional parameter containing the target request url.
713 * Additianally accepts an optional int parameter specifying the request method
714 * to use and an associative array as optional third parameter which will be
715 * passed to HttpRequest::setOptions().
717 * Throws HttpException.
719 PHP_METHOD(HttpRequest
, __construct
)
724 zval
*options
= NULL
;
725 getObject(http_request_object
, obj
);
728 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
729 INIT_PARR(obj
, options
);
730 INIT_PARR(obj
, responseInfo
);
731 INIT_PARR(obj
, responseData
);
732 INIT_PARR(obj
, postFields
);
733 INIT_PARR(obj
, postFiles
);
736 UPD_STRL(obj
, url
, URL
, URL_len
);
739 UPD_PROP(obj
, long, method
, meth
);
742 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
749 /* {{{ proto void HttpRequest::__destruct()
751 * Destroys the HttpRequest object.
753 PHP_METHOD(HttpRequest
, __destruct
)
755 getObject(http_request_object
, obj
);
759 FREE_PARR(obj
, options
);
760 FREE_PARR(obj
, responseInfo
);
761 FREE_PARR(obj
, responseData
);
762 FREE_PARR(obj
, postFields
);
763 FREE_PARR(obj
, postFiles
);
767 /* {{{ proto bool HttpRequest::setOptions([array options])
769 * Set the request options to use. See http_get() for a full list of available options.
771 * Accepts an array as optional parameters, wich values will overwrite the
772 * currently set request options. If the parameter is empty or mitted,
773 * the optoions of the HttpRequest object will be reset.
775 * Returns TRUE on success, or FALSE on failure.
777 PHP_METHOD(HttpRequest
, setOptions
)
781 zval
*opts
= NULL
, *old_opts
, **opt
;
782 getObject(http_request_object
, obj
);
784 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!", &opts
)) {
788 old_opts
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, options
));
791 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
792 zend_hash_clean(Z_ARRVAL_P(old_opts
));
793 SET_PROP(obj
, options
, old_opts
);
797 /* some options need extra attention -- thus cannot use array_merge() directly */
798 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
800 if (!strcmp(key
, "headers")) {
802 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
803 convert_to_array_ex(opt
);
804 convert_to_array(*headers
);
805 array_merge(*opt
, *headers
);
808 } else if (!strcmp(key
, "cookies")) {
810 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
811 convert_to_array_ex(opt
);
812 convert_to_array(*cookies
);
813 array_merge(*opt
, *cookies
);
816 } else if (!strcmp(key
, "ssl")) {
818 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl
)) {
819 convert_to_array_ex(opt
);
820 convert_to_array(*ssl
);
821 array_merge(*opt
, *ssl
);
824 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
825 if (Z_TYPE_PP(opt
) != IS_STRING
) {
826 convert_to_string_ex(opt
);
828 UPD_STRL(obj
, url
, Z_STRVAL_PP(opt
), Z_STRLEN_PP(opt
));
830 } else if (!strcmp(key
, "method")) {
831 if (Z_TYPE_PP(opt
) != IS_LONG
) {
832 convert_to_long_ex(opt
);
834 UPD_PROP(obj
, long, method
, Z_LVAL_PP(opt
));
839 add_assoc_zval(old_opts
, key
, *opt
);
845 SET_PROP(obj
, options
, old_opts
);
851 /* {{{ proto array HttpRequest::getOptions()
853 * Get currently set options.
855 * Returns an associative array containing currently set options.
857 PHP_METHOD(HttpRequest
, getOptions
)
863 getObject(http_request_object
, obj
);
865 opts
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
), &opts_p
);
866 array_init(return_value
);
867 array_copy(opts
, return_value
);
870 zval_ptr_dtor(&opts_p
);
876 /* {{{ proto bool HttpRequest::setSslOptions([array options])
880 * Accepts an associative array as parameter containing any SSL specific options.
881 * If the parameter is empty or omitted, the SSL options will be reset.
883 * Returns TRUE on success, or FALSE on failure.
885 PHP_METHOD(HttpRequest
, setSslOptions
)
887 http_request_object_set_options_subr("ssl", 1);
891 /* {{{ proto bool HttpRequest::addSslOptions(array options)
893 * Set additional SSL options.
895 * Expects an associative array as parameter containing additional SSL specific options.
897 * Returns TRUE on success, or FALSE on failure.
899 PHP_METHOD(HttpRequest
, addSslOptions
)
901 http_request_object_set_options_subr("ssl", 0);
905 /* {{{ proto array HttpRequest::getSslOtpions()
907 * Get previously set SSL options.
909 * Returns an associative array containing any previously set SSL options.
911 PHP_METHOD(HttpRequest
, getSslOptions
)
913 http_request_object_get_options_subr("ssl");
917 /* {{{ proto bool HttpRequest::addHeaders(array headers)
919 * Add request header name/value pairs.
921 * Expects an ssociative array as parameter containing additional header
924 * Returns TRUE on success, or FALSE on failure.
926 PHP_METHOD(HttpRequest
, addHeaders
)
928 http_request_object_set_options_subr("headers", 0);
931 /* {{{ proto bool HttpRequest::setHeaders([array headers])
933 * Set request header name/value pairs.
935 * Accepts an associative array as parameter containing header name/value pairs.
936 * If the parameter is empty or omitted, all previously set headers will be unset.
938 * Returns TRUE on success, or FALSE on failure.
940 PHP_METHOD(HttpRequest
, setHeaders
)
942 http_request_object_set_options_subr("headers", 1);
946 /* {{{ proto array HttpRequest::getHeaders()
948 * Get previously set request headers.
950 * Returns an associative array containing all currently set headers.
952 PHP_METHOD(HttpRequest
, getHeaders
)
954 http_request_object_get_options_subr("headers");
958 /* {{{ proto bool HttpRequest::setCookies([array cookies])
962 * Accepts an associative array as parameter containing cookie name/value pairs.
963 * If the parameter is empty or omitted, all previously set cookies will be unset.
965 * Returns TRUE on success, or FALSE on failure.
967 PHP_METHOD(HttpRequest
, setCookies
)
969 http_request_object_set_options_subr("cookies", 1);
973 /* {{{ proto bool HttpRequest::addCookies(array cookies)
977 * Expects an associative array as parameter containing any cookie name/value
980 * Returns TRUE on success, or FALSE on failure.
982 PHP_METHOD(HttpRequest
, addCookies
)
984 http_request_object_set_options_subr("cookies", 0);
988 /* {{{ proto array HttpRequest::getCookies()
990 * Get previously set cookies.
992 * Returns an associative array containing any previously set cookies.
994 PHP_METHOD(HttpRequest
, getCookies
)
996 http_request_object_get_options_subr("cookies");
1000 /* {{{ proto bool HttpRequest::setUrl(string url)
1002 * Set the request URL.
1004 * Expects a string as parameter specifying the request url.
1006 * Returns TRUE on success, or FALSE on failure.
1008 PHP_METHOD(HttpRequest
, setUrl
)
1012 getObject(http_request_object
, obj
);
1014 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1018 UPD_STRL(obj
, url
, URL
, URL_len
);
1023 /* {{{ proto string HttpRequest::getUrl()
1025 * Get the previously set request URL.
1027 * Returns the currently set request url as string.
1029 PHP_METHOD(HttpRequest
, getUrl
)
1034 getObject(http_request_object
, obj
);
1035 zval
*URL
= GET_PROP(obj
, url
);
1037 RETURN_ZVAL(URL
, 1, 0);
1042 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1044 * Set the request method.
1046 * Expects an int as parameter specifying the request method to use.
1047 * In PHP 5.1+ HttpRequest::METH, otherwise the HTTP_METH constants can be used.
1049 * Returns TRUE on success, or FALSE on failure.
1051 PHP_METHOD(HttpRequest
, setMethod
)
1054 getObject(http_request_object
, obj
);
1056 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1060 UPD_PROP(obj
, long, method
, meth
);
1065 /* {{{ proto int HttpRequest::getMethod()
1067 * Get the previously set request method.
1069 * Returns the currently set request method.
1071 PHP_METHOD(HttpRequest
, getMethod
)
1076 getObject(http_request_object
, obj
);
1077 zval
*meth
= GET_PROP(obj
, method
);
1079 RETURN_ZVAL(meth
, 1, 0);
1084 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1086 * Set the content type the post request should have.
1088 * Expects a string as parameters containing the content type of the request
1089 * (primary/secondary).
1091 * Returns TRUE on success, or FALSE if the content type does not seem to
1092 * contain a primary and a secondary part.
1094 PHP_METHOD(HttpRequest
, setContentType
)
1098 getObject(http_request_object
, obj
);
1100 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1104 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1105 UPD_STRL(obj
, contentType
, ctype
, ct_len
);
1110 /* {{{ proto string HttpRequest::getContentType()
1112 * Get the previously content type.
1114 * Returns the previously set content type as string.
1116 PHP_METHOD(HttpRequest
, getContentType
)
1121 getObject(http_request_object
, obj
);
1122 zval
*ctype
= GET_PROP(obj
, contentType
);
1124 RETURN_ZVAL(ctype
, 1, 0);
1129 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1131 * Set the URL query parameters to use, overwriting previously set query parameters.
1132 * Affects any request types.
1134 * Accepts a string or associative array parameter containing the pre-encoded
1135 * query string or to be encoded query fields. If the parameter is empty or
1136 * omitted, the query data will be unset.
1138 * Returns TRUE on success, or FALSE on failure.
1140 PHP_METHOD(HttpRequest
, setQueryData
)
1143 getObject(http_request_object
, obj
);
1145 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1149 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1150 UPD_STRL(obj
, queryData
, "", 0);
1151 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1152 char *query_data
= NULL
;
1154 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1158 UPD_PROP(obj
, string
, queryData
, query_data
);
1161 convert_to_string_ex(&qdata
);
1162 UPD_STRL(obj
, queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1168 /* {{{ proto string HttpRequest::getQueryData()
1170 * Get the current query data in form of an urlencoded query string.
1172 * Returns a string containing the urlencoded query.
1174 PHP_METHOD(HttpRequest
, getQueryData
)
1179 getObject(http_request_object
, obj
);
1180 zval
*qdata_p
, *qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
), &qdata_p
);
1182 RETURN_ZVAL(qdata
, 1, 0);
1185 zval_ptr_dtor(&qdata_p
);
1191 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1193 * Add parameters to the query parameter list, leaving previously set unchanged.
1194 * Affects any request type.
1196 * Expects an associative array as parameter containing the query fields to add.
1198 * Returns TRUE on success, or FALSE on failure.
1200 PHP_METHOD(HttpRequest
, addQueryData
)
1202 zval
*qdata
, *old_qdata
, *old_qdata_p
;
1203 char *query_data
= NULL
;
1204 size_t query_data_len
= 0;
1205 getObject(http_request_object
, obj
);
1207 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1211 old_qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
), &old_qdata_p
);
1213 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
)) {
1215 zval_ptr_dtor(&old_qdata_p
);
1220 UPD_STRL(obj
, queryData
, query_data
, query_data_len
);
1224 zval_ptr_dtor(&old_qdata_p
);
1231 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1233 * Adds POST data entries, leaving previously set unchanged, unless a
1234 * post entry with the same name already exists.
1235 * Affects only POST and custom requests.
1237 * Expects an associative array as parameter containing the post fields.
1239 * Returns TRUE on success, or FALSE on failure.
1241 PHP_METHOD(HttpRequest
, addPostFields
)
1243 zval
*post
, *post_data
;
1244 getObject(http_request_object
, obj
);
1246 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1250 post
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFields
));
1251 array_merge(post_data
, post
);
1257 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1259 * Set the POST data entries, overwriting previously set POST data.
1260 * Affects only POST and custom requests.
1262 * Accepts an associative array as parameter containing the post fields.
1263 * If the parameter is empty or omitted, the post data will be unset.
1265 * Returns TRUE on success, or FALSE on failure.
1267 PHP_METHOD(HttpRequest
, setPostFields
)
1269 zval
*post
, *post_data
= NULL
;
1270 getObject(http_request_object
, obj
);
1272 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!", &post_data
)) {
1276 post
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFields
));
1279 zend_hash_clean(Z_ARRVAL_P(post
));
1280 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1281 array_copy(post_data
, post
);
1283 SET_PROP(obj
, postFields
, post
);
1289 /* {{{ proto array HttpRequest::getPostFields()
1291 * Get previously set POST data.
1293 * Returns the currently set post fields as associative array.
1295 PHP_METHOD(HttpRequest
, getPostFields
)
1300 getObject(http_request_object
, obj
);
1301 zval
*post_data_p
, *post_data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFields
), &post_data_p
);
1303 array_init(return_value
);
1304 array_copy(post_data
, return_value
);
1307 zval_ptr_dtor(&post_data_p
);
1313 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1315 * Set raw post data to send, overwriting previously set raw post data. Don't
1316 * forget to specify a content type. Affects only POST and custom requests.
1317 * Only either post fields or raw post data can be used for each request.
1318 * Raw post data has higher precedence and will be used even if post fields
1321 * Accepts a string as parameter containing the *raw* post data.
1323 * Returns TRUE on success, or FALSE on failure.
1325 PHP_METHOD(HttpRequest
, setRawPostData
)
1327 char *raw_data
= NULL
;
1329 getObject(http_request_object
, obj
);
1331 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1339 UPD_STRL(obj
, rawPostData
, raw_data
, data_len
);
1344 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1346 * Add raw post data, leaving previously set raw post data unchanged.
1347 * Affects only POST and custom requests.
1349 * Expects a string as parameter containing the raw post data to concatenate.
1351 * Returns TRUE on success, or FALSE on failure.
1353 PHP_METHOD(HttpRequest
, addRawPostData
)
1355 char *raw_data
, *new_data
;
1357 getObject(http_request_object
, obj
);
1359 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1364 zval
*zdata_p
, *zdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
), &zdata_p
);
1366 new_data
= emalloc(Z_STRLEN_P(zdata
) + data_len
+ 1);
1367 new_data
[Z_STRLEN_P(zdata
) + data_len
] = '\0';
1369 if (Z_STRLEN_P(zdata
)) {
1370 memcpy(new_data
, Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
));
1373 memcpy(new_data
+ Z_STRLEN_P(zdata
), raw_data
, data_len
);
1374 UPD_STRL(obj
, rawPostData
, new_data
, Z_STRLEN_P(zdata
) + data_len
);
1377 zval_ptr_dtor(&zdata_p
);
1385 /* {{{ proto string HttpRequest::getRawPostData()
1387 * Get previously set raw post data.
1389 * Returns a string containing the currently set raw post data.
1391 PHP_METHOD(HttpRequest
, getRawPostData
)
1396 getObject(http_request_object
, obj
);
1397 zval
*raw_data_p
, *raw_data
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
), &raw_data_p
);
1399 RETVAL_ZVAL(raw_data
, 1, 0);
1402 zval_ptr_dtor(&raw_data_p
);
1408 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1410 * Add a file to the POST request, leaving prefiously set files unchanged.
1411 * Affects only POST and custom requests. Cannot be used with raw post data.
1413 * Expects a string parameter containing the form element name, and a string
1414 * paremeter containing the path to the file which should be uploaded.
1415 * Additionally accepts an optional string parameter which chould contain
1416 * the content type of the file.
1418 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1419 * primary and a secondary content type part.
1421 PHP_METHOD(HttpRequest
, addPostFile
)
1423 zval
*files
, *entry
;
1424 char *name
, *file
, *type
= NULL
;
1425 int name_len
, file_len
, type_len
= 0;
1426 getObject(http_request_object
, obj
);
1428 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1433 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1435 type
= "application/x-octetstream";
1436 type_len
= sizeof("application/x-octetstream") - 1;
1439 MAKE_STD_ZVAL(entry
);
1442 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1443 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1444 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1446 files
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1447 add_next_index_zval(files
, entry
);
1453 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1455 * Set files to post, overwriting previously set post files.
1456 * Affects only POST and requests. Cannot be used with raw post data.
1458 * Accepts an array containing the files to post. Each entry should be an
1459 * associative array with "name", "file" and "type" keys. If the parameter
1460 * is empty or omitted the post files will be unset.
1462 * Returns TRUE on success, or FALSE on failure.
1464 PHP_METHOD(HttpRequest
, setPostFiles
)
1466 zval
*files
, *pFiles
;
1467 getObject(http_request_object
, obj
);
1469 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &files
)) {
1473 pFiles
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1476 zend_hash_clean(Z_ARRVAL_P(pFiles
));
1477 if (files
&& zend_hash_num_elements(Z_ARRVAL_P(files
))) {
1478 array_copy(files
, pFiles
);
1480 SET_PROP(obj
, postFiles
, pFiles
);
1486 /* {{{ proto array HttpRequest::getPostFiles()
1488 * Get all previously added POST files.
1490 * Returns an array containing currently set post files.
1492 PHP_METHOD(HttpRequest
, getPostFiles
)
1497 getObject(http_request_object
, obj
);
1498 zval
*files_p
, *files
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFiles
), &files_p
);
1500 array_init(return_value
);
1501 array_copy(files
, return_value
);
1504 zval_ptr_dtor(&files_p
);
1510 /* {{{ proto bool HttpRequest::setPutFile([string file])
1512 * Set file to put. Affects only PUT requests.
1514 * Accepts a string as parameter referencing the path to file.
1515 * If the parameter is empty or omitted the put file will be unset.
1517 * Returns TRUE on success, or FALSE on failure.
1519 PHP_METHOD(HttpRequest
, setPutFile
)
1523 getObject(http_request_object
, obj
);
1525 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1529 UPD_STRL(obj
, putFile
, file
, file_len
);
1534 /* {{{ proto string HttpRequest::getPutFile()
1536 * Get previously set put file.
1538 * Returns a string containing the path to the currently set put file.
1540 PHP_METHOD(HttpRequest
, getPutFile
)
1545 getObject(http_request_object
, obj
);
1546 zval
*putfile_p
, *putfile
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, putFile
), &putfile_p
);
1548 RETVAL_ZVAL(putfile
, 1, 0);
1551 zval_ptr_dtor(&putfile_p
);
1557 /* {{{ proto array HttpRequest::getResponseData()
1559 * Get all response data after the request has been sent.
1561 * Returns an associative array with the key "headers" containing an associative
1562 * array holding all response headers, as well as the ley "body" containing a
1563 * string with the response body.
1565 * If redirects were allowed and several responses were received, the data
1566 * references the last received response.
1568 PHP_METHOD(HttpRequest
, getResponseData
)
1573 getObject(http_request_object
, obj
);
1574 zval
*data_p
, *data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
), &data_p
);
1576 array_init(return_value
);
1577 array_copy(data
, return_value
);
1580 zval_ptr_dtor(&data_p
);
1586 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1588 * Get response header(s) after the request has been sent.
1590 * Accepts an string as optional parameter specifying a certain header to read.
1591 * If the parameter is empty or omitted all response headers will be returned.
1593 * Returns either a string with the value of the header matching name if requested,
1594 * FALSE on failure, or an associative array containing all reponse headers.
1596 * If redirects were allowed and several responses were received, the data
1597 * references the last received response.
1599 PHP_METHOD(HttpRequest
, getResponseHeader
)
1602 zval
*data_p
, *data
, **headers
, **header
;
1603 char *header_name
= NULL
;
1605 getObject(http_request_object
, obj
);
1607 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1611 data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
), &data_p
);
1612 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1613 convert_to_array(*headers
);
1614 if (!header_len
|| !header_name
) {
1615 array_init(return_value
);
1616 array_copy(*headers
, return_value
);
1617 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1618 RETVAL_ZVAL(*header
, 1, 0);
1627 zval_ptr_dtor(&data_p
);
1633 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1635 * Get response cookie(s) after the request has been sent.
1637 * Accepts a string as optional parameter specifying the name of the cookie to read.
1638 * If the parameter is empty or omitted, an associative array with all received
1639 * cookies will be returned.
1641 * Returns either an associative array with the cookie's name, value and any
1642 * additional params of the cookie matching name if requested, FALSE on failure,
1643 * or an array containing all received cookies as arrays.
1645 * If redirects were allowed and several responses were received, the data
1646 * references the last received response.
1648 PHP_METHOD(HttpRequest
, getResponseCookie
)
1651 zval
*data
, **headers
;
1652 char *cookie_name
= NULL
;
1654 getObject(http_request_object
, obj
);
1656 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1660 array_init(return_value
);
1662 data
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
));
1663 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1666 zval
**header
= NULL
;
1668 convert_to_array_ex(headers
);
1669 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1670 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1671 /* several cookies? */
1672 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1675 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1677 MAKE_STD_ZVAL(cookie_hash
);
1678 array_init(cookie_hash
);
1680 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1682 add_next_index_zval(return_value
, cookie_hash
);
1686 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1687 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1688 add_next_index_zval(return_value
, cookie_hash
);
1689 return; /* <<< FOUND >>> */
1691 zval_dtor(cookie_hash
);
1696 zval_dtor(cookie_hash
);
1703 MAKE_STD_ZVAL(cookie_hash
);
1704 array_init(cookie_hash
);
1705 convert_to_string_ex(header
);
1707 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1709 add_next_index_zval(return_value
, cookie_hash
);
1713 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1714 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1715 add_next_index_zval(return_value
, cookie_hash
);
1717 zval_dtor(cookie_hash
);
1722 zval_dtor(cookie_hash
);
1736 /* {{{ proto string HttpRequest::getResponseBody()
1738 * Get the response body after the request has been sent.
1740 * Returns a string containing the response body.
1742 * If redirects were allowed and several responses were received, the data
1743 * references the last received response.
1745 PHP_METHOD(HttpRequest
, getResponseBody
)
1751 getObject(http_request_object
, obj
);
1752 zval
*data
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
));
1754 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1755 RETURN_ZVAL(*body
, 1, 0);
1763 /* {{{ proto int HttpRequest::getResponseCode()
1765 * Get the response code after the request has been sent.
1767 * Returns an int representing the response code.
1769 * If redirects were allowed and several responses were received, the data
1770 * references the last received response.
1772 PHP_METHOD(HttpRequest
, getResponseCode
)
1777 getObject(http_request_object
, obj
);
1778 zval
*code_p
, *code
= convert_to_type_ex(IS_LONG
, GET_PROP(obj
, responseCode
), &code_p
);
1780 RETVAL_ZVAL(code
, 1, 0);
1783 zval_ptr_dtor(&code_p
);
1789 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1791 * Get response info after the request has been sent.
1792 * See http_get() for a full list of returned info.
1794 * Accepts a string as optional parameter specifying the info to read.
1795 * If the parameter is empty or omitted, an associative array containing
1796 * all available info will be returned.
1798 * Returns either a scalar containing the value of the info matching name if
1799 * requested, FALSE on failure, or an associative array containing all
1802 * If redirects were allowed and several responses were received, the data
1803 * references the last received response.
1805 PHP_METHOD(HttpRequest
, getResponseInfo
)
1808 zval
*info
, **infop
;
1809 char *info_name
= NULL
;
1811 getObject(http_request_object
, obj
);
1813 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1817 info
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseInfo
));
1819 if (info_len
&& info_name
) {
1820 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1821 RETURN_ZVAL(*infop
, 1, 0);
1823 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1827 array_init(return_value
);
1828 array_copy(info
, return_value
);
1834 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1836 * Get the full response as HttpMessage object after the request has been sent.
1838 * Returns an HttpMessage object of the response.
1840 * If redirects were allowed and several responses were received, the data
1841 * references the last received response. Use HttpMessage::getParentMessage()
1842 * to access the data of previously received responses whithin this request
1845 * Throws HttpException.
1847 PHP_METHOD(HttpRequest
, getResponseMessage
)
1853 getObject(http_request_object
, obj
);
1855 SET_EH_THROW_HTTP();
1856 message
= GET_PROP(obj
, responseMessage
);
1857 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1858 RETVAL_OBJECT(message
);
1867 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1869 * Get sent HTTP message.
1871 * Returns an HttpMessage object representing the sent request.
1873 * If redirects were allowed and several responses were received, the data
1874 * references the last received response. Use HttpMessage::getParentMessage()
1875 * to access the data of previously sent requests whithin this request
1878 * Note that the internal request message is immutable, that means that the
1879 * request message received through HttpRequest::getRequestMessage() will
1880 * always look the same for the same request, regardless of any changes you
1881 * may have made to the returned object.
1883 * Throws HttpMalformedHeadersException, HttpEncodingException.
1885 PHP_METHOD(HttpRequest
, getRequestMessage
)
1891 getObject(http_request_object
, obj
);
1893 SET_EH_THROW_HTTP();
1894 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->request
), PHPSTR_LEN(&obj
->request
))) {
1895 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1902 /* {{{ proto HttpMessage HttpRequest::getHistory()
1904 * Get all sent requests and received responses as an HttpMessage object.
1906 * If you don't want to record history at all, set the instance variable
1907 * HttpRequest::$recoedHistory to FALSE.
1909 * Returns an HttpMessage object representing the complete request/response
1912 * The object references the last received response, use HttpMessage::getParentMessage()
1913 * to access the data of previously sent requests and received responses.
1915 * Note that the internal history is immutable, that means that any changes
1916 * you make the the message list won't affect a history message list newly
1917 * created by another call to HttpRequest::getHistory().
1919 * Throws HttpMalformedHeaderException, HttpEncodingException.
1921 PHP_METHOD(HttpRequest
, getHistory
)
1927 getObject(http_request_object
, obj
);
1929 SET_EH_THROW_HTTP();
1930 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->history
), PHPSTR_LEN(&obj
->history
))) {
1931 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1938 /* {{{ proto void HttpRequest::clearHistory()
1940 * Clear the history.
1942 PHP_METHOD(HttpRequest
, clearHistory
)
1945 getObject(http_request_object
, obj
);
1946 phpstr_dtor(&obj
->history
);
1951 /* {{{ proto HttpMessage HttpRequest::send()
1953 * Send the HTTP request.
1955 * Returns the received response as HttpMessage object.
1957 * Throws HttpRuntimeException, HttpRequestException,
1958 * HttpMalformedHeaderException, HttpEncodingException.
1963 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1964 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1965 * $r->addQueryData(array('category' => 3));
1968 * if ($r->getResponseCode() == 200) {
1969 * file_put_contents('local.rss', $r->getResponseBody());
1971 * } catch (HttpException $ex) {
1980 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1981 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1982 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1983 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1985 * echo $r->send()->getBody();
1986 * } catch (HttpException $ex) {
1992 PHP_METHOD(HttpRequest
, send
)
1994 http_request_body body
= {0, NULL
, 0};
1995 getObject(http_request_object
, obj
);
1999 SET_EH_THROW_HTTP();
2002 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
2009 if ( (SUCCESS
== http_request_object_requesthandler(obj
, getThis(), &body
)) &&
2010 (SUCCESS
== http_request_exec(obj
->ch
, NULL
, &obj
->response
, &obj
->request
)) &&
2011 (SUCCESS
== http_request_object_responsehandler(obj
, getThis()))) {
2012 RETVAL_OBJECT(GET_PROP(obj
, responseMessage
));
2014 http_request_body_dtor(&body
);
2020 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
2027 * vim600: noet sw=4 ts=4 fdm=marker
2028 * vim<600: noet sw=4 ts=4