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 +--------------------------------------------------------------------+
19 #define HTTP_WANT_CURL
22 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
24 #include "zend_interfaces.h"
26 #include "php_http_api.h"
27 #include "php_http_exception_object.h"
28 #include "php_http_message_api.h"
29 #include "php_http_message_object.h"
30 #include "php_http_request_api.h"
31 #include "php_http_request_object.h"
32 #include "php_http_request_pool_api.h"
33 #include "php_http_url_api.h"
35 ZEND_EXTERN_MODULE_GLOBALS(http
);
37 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
38 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
39 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
40 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
42 HTTP_EMPTY_ARGS(__destruct
, 0);
43 HTTP_BEGIN_ARGS(__construct
, 0, 0)
45 HTTP_ARG_VAL(method
, 0)
46 HTTP_ARG_VAL(options
, 0)
49 HTTP_EMPTY_ARGS(getOptions
, 0);
50 HTTP_BEGIN_ARGS(setOptions
, 0, 0)
51 HTTP_ARG_VAL(options
, 0)
54 HTTP_EMPTY_ARGS(getSslOptions
, 0);
55 HTTP_BEGIN_ARGS(setSslOptions
, 0, 0)
56 HTTP_ARG_VAL(ssl_options
, 0)
59 HTTP_EMPTY_ARGS(getHeaders
, 0);
60 HTTP_BEGIN_ARGS(setHeaders
, 0, 0)
61 HTTP_ARG_VAL(headers
, 0)
64 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
65 HTTP_ARG_VAL(headers
, 0)
68 HTTP_EMPTY_ARGS(getCookies
, 0);
69 HTTP_BEGIN_ARGS(setCookies
, 0, 0)
70 HTTP_ARG_VAL(cookies
, 0)
73 HTTP_BEGIN_ARGS(addCookies
, 0, 1)
74 HTTP_ARG_VAL(cookies
, 0)
77 HTTP_EMPTY_ARGS(getUrl
, 0);
78 HTTP_BEGIN_ARGS(setUrl
, 0, 1)
82 HTTP_EMPTY_ARGS(getMethod
, 0);
83 HTTP_BEGIN_ARGS(setMethod
, 0, 1)
84 HTTP_ARG_VAL(request_method
, 0)
87 HTTP_EMPTY_ARGS(getContentType
, 0);
88 HTTP_BEGIN_ARGS(setContentType
, 0, 1)
89 HTTP_ARG_VAL(content_type
, 0)
92 HTTP_EMPTY_ARGS(getQueryData
, 0);
93 HTTP_BEGIN_ARGS(setQueryData
, 0, 0)
94 HTTP_ARG_VAL(query_data
, 0)
97 HTTP_BEGIN_ARGS(addQueryData
, 0, 1)
98 HTTP_ARG_VAL(query_data
, 0)
101 HTTP_EMPTY_ARGS(getPostFields
, 0);
102 HTTP_BEGIN_ARGS(setPostFields
, 0, 0)
103 HTTP_ARG_VAL(post_fields
, 0)
106 HTTP_BEGIN_ARGS(addPostFields
, 0, 1)
107 HTTP_ARG_VAL(post_fields
, 0)
110 HTTP_EMPTY_ARGS(getPostFiles
, 0);
111 HTTP_BEGIN_ARGS(setPostFiles
, 0, 0)
112 HTTP_ARG_VAL(post_files
, 0)
115 HTTP_BEGIN_ARGS(addPostFile
, 0, 2)
116 HTTP_ARG_VAL(formname
, 0)
117 HTTP_ARG_VAL(filename
, 0)
118 HTTP_ARG_VAL(content_type
, 0)
121 HTTP_EMPTY_ARGS(getRawPostData
, 0);
122 HTTP_BEGIN_ARGS(setRawPostData
, 0, 0)
123 HTTP_ARG_VAL(raw_post_data
, 0)
126 HTTP_BEGIN_ARGS(addRawPostData
, 0, 1)
127 HTTP_ARG_VAL(raw_post_data
, 0)
130 HTTP_EMPTY_ARGS(getPutFile
, 0);
131 HTTP_BEGIN_ARGS(setPutFile
, 0, 0)
132 HTTP_ARG_VAL(filename
, 0)
135 HTTP_EMPTY_ARGS(getResponseData
, 0);
136 HTTP_BEGIN_ARGS(getResponseHeader
, 0, 0)
137 HTTP_ARG_VAL(name
, 0)
140 HTTP_BEGIN_ARGS(getResponseCookie
, 0, 0)
141 HTTP_ARG_VAL(name
, 0)
144 HTTP_EMPTY_ARGS(getResponseBody
, 0);
145 HTTP_EMPTY_ARGS(getResponseCode
, 0);
146 HTTP_BEGIN_ARGS(getResponseInfo
, 0, 0)
147 HTTP_ARG_VAL(name
, 0)
150 HTTP_EMPTY_ARGS(getResponseMessage
, 1);
151 HTTP_EMPTY_ARGS(getRequestMessage
, 1);
152 HTTP_EMPTY_ARGS(getHistory
, 1);
153 HTTP_EMPTY_ARGS(clearHistory
, 0);
154 HTTP_EMPTY_ARGS(send
, 1);
156 HTTP_BEGIN_ARGS(get
, 0, 1)
158 HTTP_ARG_VAL(options
, 0)
159 HTTP_ARG_VAL(info
, 1)
162 HTTP_BEGIN_ARGS(head
, 0, 1)
164 HTTP_ARG_VAL(options
, 0)
165 HTTP_ARG_VAL(info
, 1)
168 HTTP_BEGIN_ARGS(postData
, 0, 2)
170 HTTP_ARG_VAL(data
, 0)
171 HTTP_ARG_VAL(options
, 0)
172 HTTP_ARG_VAL(info
, 1)
175 HTTP_BEGIN_ARGS(postFields
, 0, 2)
177 HTTP_ARG_VAL(data
, 0)
178 HTTP_ARG_VAL(options
, 0)
179 HTTP_ARG_VAL(info
, 1)
182 HTTP_BEGIN_ARGS(putFile
, 0, 2)
184 HTTP_ARG_VAL(file
, 0)
185 HTTP_ARG_VAL(options
, 0)
186 HTTP_ARG_VAL(info
, 1)
189 HTTP_BEGIN_ARGS(putStream
, 0, 2)
191 HTTP_ARG_VAL(stream
, 0)
192 HTTP_ARG_VAL(options
, 0)
193 HTTP_ARG_VAL(info
, 1)
196 HTTP_BEGIN_ARGS(methodRegister
, 0, 1)
197 HTTP_ARG_VAL(method_name
, 0)
200 HTTP_BEGIN_ARGS(methodUnregister
, 0, 1)
201 HTTP_ARG_VAL(method
, 0)
204 HTTP_BEGIN_ARGS(methodName
, 0, 1)
205 HTTP_ARG_VAL(method_id
, 0)
208 HTTP_BEGIN_ARGS(methodExists
, 0, 1)
209 HTTP_ARG_VAL(method
, 0)
212 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
213 static inline void _http_request_object_declare_default_properties(TSRMLS_D
);
215 zend_class_entry
*http_request_object_ce
;
216 zend_function_entry http_request_object_fe
[] = {
217 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
218 HTTP_REQUEST_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
220 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
221 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
222 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
223 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
225 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
226 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
227 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
229 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
230 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
231 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
233 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
234 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
236 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
237 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
239 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
240 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
242 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
243 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
244 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
246 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
247 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
248 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
250 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
251 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
252 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
254 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
255 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
256 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
258 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
261 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
263 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
265 HTTP_REQUEST_ME(getResponseCookie
, ZEND_ACC_PUBLIC
)
266 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
269 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
270 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
272 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
274 HTTP_REQUEST_ALIAS(get
, http_get
)
275 HTTP_REQUEST_ALIAS(head
, http_head
)
276 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
277 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
278 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
279 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
281 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
282 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
283 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
284 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
288 static zend_object_handlers http_request_object_handlers
;
290 PHP_MINIT_FUNCTION(http_request_object
)
292 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
293 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
297 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
299 return http_request_object_new_ex(ce
, curl_easy_init(), NULL
);
302 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
304 zend_object_value ov
;
305 http_request_object
*o
;
307 o
= ecalloc(1, sizeof(http_request_object
));
309 o
->request
= http_request_init_ex(NULL
, ch
, 0, NULL
);
310 phpstr_init(&o
->history
);
316 ALLOC_HASHTABLE(OBJ_PROP(o
));
317 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
318 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
320 ov
.handle
= putObject(http_request_object
, o
);
321 ov
.handlers
= &http_request_object_handlers
;
326 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
329 zend_object_value new_ov
;
330 http_request_object
*new_obj
;
331 getObject(http_request_object
, old_obj
);
333 old_zo
= zend_objects_get_address(this_ptr TSRMLS_CC
);
334 new_ov
= http_request_object_new_ex(old_zo
->ce
, curl_easy_duphandle(old_obj
->request
->ch
), &new_obj
);
336 zend_objects_clone_members(&new_obj
->zo
, new_ov
, old_zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
337 phpstr_append(&new_obj
->history
, old_obj
->history
.data
, old_obj
->history
.used
);
338 phpstr_append(&new_obj
->request
->conv
.request
, old_obj
->request
->conv
.request
.data
, old_obj
->request
->conv
.request
.used
);
339 phpstr_append(&new_obj
->request
->conv
.response
, old_obj
->request
->conv
.response
.data
, old_obj
->request
->conv
.response
.used
);
344 static inline void _http_request_object_declare_default_properties(TSRMLS_D
)
346 zend_class_entry
*ce
= http_request_object_ce
;
348 DCL_PROP_N(PROTECTED
, options
);
349 DCL_PROP_N(PROTECTED
, responseInfo
);
350 DCL_PROP_N(PROTECTED
, responseData
);
351 DCL_PROP(PROTECTED
, long, responseCode
, 0);
352 DCL_PROP_N(PROTECTED
, responseMessage
);
353 DCL_PROP_N(PROTECTED
, postFields
);
354 DCL_PROP_N(PROTECTED
, postFiles
);
356 DCL_PROP(PROTECTED
, long, method
, HTTP_GET
);
358 DCL_PROP(PROTECTED
, string
, url
, "");
359 DCL_PROP(PROTECTED
, string
, contentType
, "");
360 DCL_PROP(PROTECTED
, string
, rawPostData
, "");
361 DCL_PROP(PROTECTED
, string
, queryData
, "");
362 DCL_PROP(PROTECTED
, string
, putFile
, "");
364 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
368 * Request Method Constants
371 DCL_CONST(long, "METH_GET", HTTP_GET
);
372 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
373 DCL_CONST(long, "METH_POST", HTTP_POST
);
374 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
375 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
376 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
377 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
378 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
379 /* WebDAV - RFC 2518 */
380 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
381 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
382 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
383 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
384 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
385 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
386 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
387 /* WebDAV Versioning - RFC 3253 */
388 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
389 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
390 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
391 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
392 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
393 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
394 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
395 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
396 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
397 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
398 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
399 /* WebDAV Access Control - RFC 3744 */
400 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
405 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
406 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
407 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
408 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
412 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
414 http_request_object
*o
= (http_request_object
*) object
;
417 zend_hash_destroy(OBJ_PROP(o
));
418 FREE_HASHTABLE(OBJ_PROP(o
));
420 http_request_free(&o
->request
);
421 phpstr_dtor(&o
->history
);
425 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
427 zval
*URL
, *URL_p
, *meth_p
;
428 STATUS status
= SUCCESS
;
430 http_request_reset(obj
->request
);
431 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, curl_easy_init(), return FAILURE
);
433 URL
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, url
), &URL_p
);
434 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(URL
));
435 if (URL_p
) zval_ptr_dtor(&URL_p
);
437 switch (obj
->request
->meth
= Z_LVAL_P(convert_to_type_ex(IS_LONG
, GET_PROP(obj
, method
), &meth_p
)))
445 php_stream_statbuf ssb
;
446 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj
, putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
448 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
449 http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
459 /* check for raw post data */
460 zval
*raw_data_p
, *raw_data
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
), &raw_data_p
);
462 if (Z_STRLEN_P(raw_data
)) {
463 zval
*ctype_p
, *ctype
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, contentType
), &ctype_p
);
465 if (Z_STRLEN_P(ctype
)) {
466 zval
**headers
, *opts
= GET_PROP(obj
, options
);
468 convert_to_array(opts
);
470 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
473 convert_to_array(*headers
);
474 /* only override if not already set */
475 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) {
476 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
481 MAKE_STD_ZVAL(headers
);
483 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
484 add_assoc_zval(opts
, "headers", headers
);
489 zval_ptr_dtor(&ctype_p
);
492 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
493 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
496 zval
*fields_cpy
, *files_cpy
;
497 HashTable
*fields
= Z_ARRVAL_P(convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFields
), &fields_cpy
));
498 HashTable
*files
= Z_ARRVAL_P(convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFiles
), &files_cpy
));
500 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
505 zval_ptr_dtor(&fields_cpy
);
508 zval_ptr_dtor(&files_cpy
);
513 zval_ptr_dtor(&raw_data_p
);
520 zval_ptr_dtor(&meth_p
);
523 if (status
== SUCCESS
) {
524 zval
*qdata_p
, *qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
), &qdata_p
);
525 zval
*opt_p
, *options
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
), &opt_p
);
527 if (Z_STRLEN_P(qdata
)) {
528 if (!strchr(obj
->request
->url
, '?')) {
529 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
531 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
533 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
536 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
539 zval_ptr_dtor(&opt_p
);
542 zval_ptr_dtor(&qdata_p
);
549 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
553 phpstr_fix(&obj
->request
->conv
.request
);
554 phpstr_fix(&obj
->request
->conv
.response
);
556 msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
));
563 zval
*headers
, *message
,
564 *resp
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
)),
565 *info
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseInfo
));
570 if (zval_is_true(GET_PROP(obj
, recordHistory
))) {
571 /* we need to act like a zipper, as we'll receive
572 * the requests and the responses in separate chains
575 http_message
*response
= msg
, *request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
576 http_message
*free_msg
= request
;
582 http_message_tostring(response
, &message
, &msglen
);
583 phpstr_append(&obj
->history
, message
, msglen
);
586 http_message_tostring(request
, &message
, &msglen
);
587 phpstr_append(&obj
->history
, message
, msglen
);
590 } while ((response
= response
->parent
) && (request
= request
->parent
));
592 http_message_free(&free_msg
);
593 phpstr_fix(&obj
->history
);
596 UPD_PROP(obj
, long, responseCode
, msg
->http
.info
.response
.code
);
598 MAKE_STD_ZVAL(headers
);
601 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
602 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
604 add_assoc_zval(resp
, "headers", headers
);
605 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
606 SET_PROP(obj
, responseData
, resp
);
608 MAKE_STD_ZVAL(message
);
609 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
610 SET_PROP(obj
, responseMessage
, message
);
611 zval_ptr_dtor(&message
);
613 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
614 SET_PROP(obj
, responseInfo
, info
);
620 #define http_request_object_set_options_subr(key, ow) \
621 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
622 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
624 zval
*opts
, **options
, *new_options
= NULL
;
625 getObject(http_request_object
, obj
);
627 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &new_options
)) {
631 opts
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, options
));
633 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
634 convert_to_array(*options
);
636 zend_hash_clean(Z_ARRVAL_PP(options
));
638 if (new_options
&& zend_hash_num_elements(Z_ARRVAL_P(new_options
))) {
640 array_copy(new_options
, *options
);
642 array_merge(new_options
, *options
);
645 } else if (new_options
&& zend_hash_num_elements(Z_ARRVAL_P(new_options
))) {
646 ZVAL_ADDREF(new_options
);
647 add_assoc_zval(opts
, key
, new_options
);
653 #define http_request_object_get_options_subr(key) \
654 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
655 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
660 zval
*opts_p
, *opts
, **options
;
661 getObject(http_request_object
, obj
);
663 opts
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
), &opts_p
);
665 array_init(return_value
);
667 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
668 convert_to_array(*options
);
669 array_copy(*options
, return_value
);
673 zval_ptr_dtor(&opts_p
);
679 /* ### USERLAND ### */
681 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
683 * Instantiate a new HttpRequest object.
685 * Accepts a string as optional parameter containing the target request url.
686 * Additianally accepts an optional int parameter specifying the request method
687 * to use and an associative array as optional third parameter which will be
688 * passed to HttpRequest::setOptions().
690 * Throws HttpException.
692 PHP_METHOD(HttpRequest
, __construct
)
697 zval
*options
= NULL
;
698 getObject(http_request_object
, obj
);
701 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
702 INIT_PARR(obj
, options
);
703 INIT_PARR(obj
, responseInfo
);
704 INIT_PARR(obj
, responseData
);
705 INIT_PARR(obj
, postFields
);
706 INIT_PARR(obj
, postFiles
);
709 UPD_STRL(obj
, url
, URL
, URL_len
);
712 UPD_PROP(obj
, long, method
, meth
);
715 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
722 /* {{{ proto void HttpRequest::__destruct()
724 * Destroys the HttpRequest object.
726 PHP_METHOD(HttpRequest
, __destruct
)
728 getObject(http_request_object
, obj
);
732 FREE_PARR(obj
, options
);
733 FREE_PARR(obj
, responseInfo
);
734 FREE_PARR(obj
, responseData
);
735 FREE_PARR(obj
, postFields
);
736 FREE_PARR(obj
, postFiles
);
740 /* {{{ proto bool HttpRequest::setOptions([array options])
742 * Set the request options to use. See http_get() for a full list of available options.
744 * Accepts an array as optional parameters, wich values will overwrite the
745 * currently set request options. If the parameter is empty or mitted,
746 * the optoions of the HttpRequest object will be reset.
748 * Returns TRUE on success, or FALSE on failure.
750 PHP_METHOD(HttpRequest
, setOptions
)
755 zval
*opts
= NULL
, *old_opts
, **opt
;
756 getObject(http_request_object
, obj
);
758 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!", &opts
)) {
762 old_opts
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, options
));
765 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
766 zend_hash_clean(Z_ARRVAL_P(old_opts
));
767 SET_PROP(obj
, options
, old_opts
);
771 /* some options need extra attention -- thus cannot use array_merge() directly */
772 FOREACH_KEYVAL(pos
, opts
, key
, idx
, opt
) {
774 if (!strcmp(key
, "headers")) {
776 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
777 convert_to_array_ex(opt
);
778 convert_to_array(*headers
);
779 array_merge(*opt
, *headers
);
782 } else if (!strcmp(key
, "cookies")) {
784 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
785 convert_to_array_ex(opt
);
786 convert_to_array(*cookies
);
787 array_merge(*opt
, *cookies
);
790 } else if (!strcmp(key
, "ssl")) {
792 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl
)) {
793 convert_to_array_ex(opt
);
794 convert_to_array(*ssl
);
795 array_merge(*opt
, *ssl
);
798 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
799 if (Z_TYPE_PP(opt
) != IS_STRING
) {
800 convert_to_string_ex(opt
);
802 UPD_STRL(obj
, url
, Z_STRVAL_PP(opt
), Z_STRLEN_PP(opt
));
804 } else if (!strcmp(key
, "method")) {
805 if (Z_TYPE_PP(opt
) != IS_LONG
) {
806 convert_to_long_ex(opt
);
808 UPD_PROP(obj
, long, method
, Z_LVAL_PP(opt
));
813 add_assoc_zval(old_opts
, key
, *opt
);
819 SET_PROP(obj
, options
, old_opts
);
825 /* {{{ proto array HttpRequest::getOptions()
827 * Get currently set options.
829 * Returns an associative array containing currently set options.
831 PHP_METHOD(HttpRequest
, getOptions
)
837 getObject(http_request_object
, obj
);
839 opts
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
), &opts_p
);
840 array_init(return_value
);
841 array_copy(opts
, return_value
);
844 zval_ptr_dtor(&opts_p
);
850 /* {{{ proto bool HttpRequest::setSslOptions([array options])
854 * Accepts an associative array as parameter containing any SSL specific options.
855 * If the parameter is empty or omitted, the SSL options will be reset.
857 * Returns TRUE on success, or FALSE on failure.
859 PHP_METHOD(HttpRequest
, setSslOptions
)
861 http_request_object_set_options_subr("ssl", 1);
865 /* {{{ proto bool HttpRequest::addSslOptions(array options)
867 * Set additional SSL options.
869 * Expects an associative array as parameter containing additional SSL specific options.
871 * Returns TRUE on success, or FALSE on failure.
873 PHP_METHOD(HttpRequest
, addSslOptions
)
875 http_request_object_set_options_subr("ssl", 0);
879 /* {{{ proto array HttpRequest::getSslOtpions()
881 * Get previously set SSL options.
883 * Returns an associative array containing any previously set SSL options.
885 PHP_METHOD(HttpRequest
, getSslOptions
)
887 http_request_object_get_options_subr("ssl");
891 /* {{{ proto bool HttpRequest::addHeaders(array headers)
893 * Add request header name/value pairs.
895 * Expects an ssociative array as parameter containing additional header
898 * Returns TRUE on success, or FALSE on failure.
900 PHP_METHOD(HttpRequest
, addHeaders
)
902 http_request_object_set_options_subr("headers", 0);
905 /* {{{ proto bool HttpRequest::setHeaders([array headers])
907 * Set request header name/value pairs.
909 * Accepts an associative array as parameter containing header name/value pairs.
910 * If the parameter is empty or omitted, all previously set headers will be unset.
912 * Returns TRUE on success, or FALSE on failure.
914 PHP_METHOD(HttpRequest
, setHeaders
)
916 http_request_object_set_options_subr("headers", 1);
920 /* {{{ proto array HttpRequest::getHeaders()
922 * Get previously set request headers.
924 * Returns an associative array containing all currently set headers.
926 PHP_METHOD(HttpRequest
, getHeaders
)
928 http_request_object_get_options_subr("headers");
932 /* {{{ proto bool HttpRequest::setCookies([array cookies])
936 * Accepts an associative array as parameter containing cookie name/value pairs.
937 * If the parameter is empty or omitted, all previously set cookies will be unset.
939 * Returns TRUE on success, or FALSE on failure.
941 PHP_METHOD(HttpRequest
, setCookies
)
943 http_request_object_set_options_subr("cookies", 1);
947 /* {{{ proto bool HttpRequest::addCookies(array cookies)
951 * Expects an associative array as parameter containing any cookie name/value
954 * Returns TRUE on success, or FALSE on failure.
956 PHP_METHOD(HttpRequest
, addCookies
)
958 http_request_object_set_options_subr("cookies", 0);
962 /* {{{ proto array HttpRequest::getCookies()
964 * Get previously set cookies.
966 * Returns an associative array containing any previously set cookies.
968 PHP_METHOD(HttpRequest
, getCookies
)
970 http_request_object_get_options_subr("cookies");
974 /* {{{ proto bool HttpRequest::setUrl(string url)
976 * Set the request URL.
978 * Expects a string as parameter specifying the request url.
980 * Returns TRUE on success, or FALSE on failure.
982 PHP_METHOD(HttpRequest
, setUrl
)
986 getObject(http_request_object
, obj
);
988 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
992 UPD_STRL(obj
, url
, URL
, URL_len
);
997 /* {{{ proto string HttpRequest::getUrl()
999 * Get the previously set request URL.
1001 * Returns the currently set request url as string.
1003 PHP_METHOD(HttpRequest
, getUrl
)
1008 getObject(http_request_object
, obj
);
1009 zval
*URL
= GET_PROP(obj
, url
);
1011 RETURN_ZVAL(URL
, 1, 0);
1016 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1018 * Set the request method.
1020 * Expects an int as parameter specifying the request method to use.
1021 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
1023 * Returns TRUE on success, or FALSE on failure.
1025 PHP_METHOD(HttpRequest
, setMethod
)
1028 getObject(http_request_object
, obj
);
1030 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1034 UPD_PROP(obj
, long, method
, meth
);
1039 /* {{{ proto int HttpRequest::getMethod()
1041 * Get the previously set request method.
1043 * Returns the currently set request method.
1045 PHP_METHOD(HttpRequest
, getMethod
)
1050 getObject(http_request_object
, obj
);
1051 zval
*meth
= GET_PROP(obj
, method
);
1053 RETURN_ZVAL(meth
, 1, 0);
1058 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1060 * Set the content type the post request should have.
1062 * Expects a string as parameters containing the content type of the request
1063 * (primary/secondary).
1065 * Returns TRUE on success, or FALSE if the content type does not seem to
1066 * contain a primary and a secondary part.
1068 PHP_METHOD(HttpRequest
, setContentType
)
1072 getObject(http_request_object
, obj
);
1074 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1078 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1079 UPD_STRL(obj
, contentType
, ctype
, ct_len
);
1084 /* {{{ proto string HttpRequest::getContentType()
1086 * Get the previously content type.
1088 * Returns the previously set content type as string.
1090 PHP_METHOD(HttpRequest
, getContentType
)
1095 getObject(http_request_object
, obj
);
1096 zval
*ctype
= GET_PROP(obj
, contentType
);
1098 RETURN_ZVAL(ctype
, 1, 0);
1103 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1105 * Set the URL query parameters to use, overwriting previously set query parameters.
1106 * Affects any request types.
1108 * Accepts a string or associative array parameter containing the pre-encoded
1109 * query string or to be encoded query fields. If the parameter is empty or
1110 * omitted, the query data will be unset.
1112 * Returns TRUE on success, or FALSE on failure.
1114 PHP_METHOD(HttpRequest
, setQueryData
)
1117 getObject(http_request_object
, obj
);
1119 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1123 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1124 UPD_STRL(obj
, queryData
, "", 0);
1125 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1126 char *query_data
= NULL
;
1128 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1132 UPD_PROP(obj
, string
, queryData
, query_data
);
1135 convert_to_string_ex(&qdata
);
1136 UPD_STRL(obj
, queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1142 /* {{{ proto string HttpRequest::getQueryData()
1144 * Get the current query data in form of an urlencoded query string.
1146 * Returns a string containing the urlencoded query.
1148 PHP_METHOD(HttpRequest
, getQueryData
)
1153 getObject(http_request_object
, obj
);
1154 zval
*qdata_p
, *qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
), &qdata_p
);
1156 RETURN_ZVAL(qdata
, 1, 0);
1159 zval_ptr_dtor(&qdata_p
);
1165 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1167 * Add parameters to the query parameter list, leaving previously set unchanged.
1168 * Affects any request type.
1170 * Expects an associative array as parameter containing the query fields to add.
1172 * Returns TRUE on success, or FALSE on failure.
1174 PHP_METHOD(HttpRequest
, addQueryData
)
1176 zval
*qdata
, *old_qdata
, *old_qdata_p
;
1177 char *query_data
= NULL
;
1178 size_t query_data_len
= 0;
1179 getObject(http_request_object
, obj
);
1181 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1185 old_qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
), &old_qdata_p
);
1187 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
)) {
1189 zval_ptr_dtor(&old_qdata_p
);
1194 UPD_STRL(obj
, queryData
, query_data
, query_data_len
);
1198 zval_ptr_dtor(&old_qdata_p
);
1205 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1207 * Adds POST data entries, leaving previously set unchanged, unless a
1208 * post entry with the same name already exists.
1209 * Affects only POST and custom requests.
1211 * Expects an associative array as parameter containing the post fields.
1213 * Returns TRUE on success, or FALSE on failure.
1215 PHP_METHOD(HttpRequest
, addPostFields
)
1217 zval
*post
, *post_data
;
1218 getObject(http_request_object
, obj
);
1220 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1224 post
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFields
));
1225 array_merge(post_data
, post
);
1231 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1233 * Set the POST data entries, overwriting previously set POST data.
1234 * Affects only POST and custom requests.
1236 * Accepts an associative array as parameter containing the post fields.
1237 * If the parameter is empty or omitted, the post data will be unset.
1239 * Returns TRUE on success, or FALSE on failure.
1241 PHP_METHOD(HttpRequest
, setPostFields
)
1243 zval
*post
, *post_data
= NULL
;
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
));
1253 zend_hash_clean(Z_ARRVAL_P(post
));
1254 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1255 array_copy(post_data
, post
);
1257 SET_PROP(obj
, postFields
, post
);
1263 /* {{{ proto array HttpRequest::getPostFields()
1265 * Get previously set POST data.
1267 * Returns the currently set post fields as associative array.
1269 PHP_METHOD(HttpRequest
, getPostFields
)
1274 getObject(http_request_object
, obj
);
1275 zval
*post_data_p
, *post_data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFields
), &post_data_p
);
1277 array_init(return_value
);
1278 array_copy(post_data
, return_value
);
1281 zval_ptr_dtor(&post_data_p
);
1287 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1289 * Set raw post data to send, overwriting previously set raw post data. Don't
1290 * forget to specify a content type. Affects only POST and custom requests.
1291 * Only either post fields or raw post data can be used for each request.
1292 * Raw post data has higher precedence and will be used even if post fields
1295 * Accepts a string as parameter containing the *raw* post data.
1297 * Returns TRUE on success, or FALSE on failure.
1299 PHP_METHOD(HttpRequest
, setRawPostData
)
1301 char *raw_data
= NULL
;
1303 getObject(http_request_object
, obj
);
1305 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1313 UPD_STRL(obj
, rawPostData
, raw_data
, data_len
);
1318 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1320 * Add raw post data, leaving previously set raw post data unchanged.
1321 * Affects only POST and custom requests.
1323 * Expects a string as parameter containing the raw post data to concatenate.
1325 * Returns TRUE on success, or FALSE on failure.
1327 PHP_METHOD(HttpRequest
, addRawPostData
)
1329 char *raw_data
, *new_data
;
1331 getObject(http_request_object
, obj
);
1333 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1338 zval
*zdata_p
, *zdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
), &zdata_p
);
1340 new_data
= emalloc(Z_STRLEN_P(zdata
) + data_len
+ 1);
1341 new_data
[Z_STRLEN_P(zdata
) + data_len
] = '\0';
1343 if (Z_STRLEN_P(zdata
)) {
1344 memcpy(new_data
, Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
));
1347 memcpy(new_data
+ Z_STRLEN_P(zdata
), raw_data
, data_len
);
1348 UPD_STRL(obj
, rawPostData
, new_data
, Z_STRLEN_P(zdata
) + data_len
);
1351 zval_ptr_dtor(&zdata_p
);
1359 /* {{{ proto string HttpRequest::getRawPostData()
1361 * Get previously set raw post data.
1363 * Returns a string containing the currently set raw post data.
1365 PHP_METHOD(HttpRequest
, getRawPostData
)
1370 getObject(http_request_object
, obj
);
1371 zval
*raw_data_p
, *raw_data
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
), &raw_data_p
);
1373 RETVAL_ZVAL(raw_data
, 1, 0);
1376 zval_ptr_dtor(&raw_data_p
);
1382 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1384 * Add a file to the POST request, leaving prefiously set files unchanged.
1385 * Affects only POST and custom requests. Cannot be used with raw post data.
1387 * Expects a string parameter containing the form element name, and a string
1388 * paremeter containing the path to the file which should be uploaded.
1389 * Additionally accepts an optional string parameter which chould contain
1390 * the content type of the file.
1392 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1393 * primary and a secondary content type part.
1395 PHP_METHOD(HttpRequest
, addPostFile
)
1397 zval
*files
, *entry
;
1398 char *name
, *file
, *type
= NULL
;
1399 int name_len
, file_len
, type_len
= 0;
1400 getObject(http_request_object
, obj
);
1402 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1407 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1409 type
= "application/x-octetstream";
1410 type_len
= sizeof("application/x-octetstream") - 1;
1413 MAKE_STD_ZVAL(entry
);
1416 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1417 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1418 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1420 files
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1421 add_next_index_zval(files
, entry
);
1427 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1429 * Set files to post, overwriting previously set post files.
1430 * Affects only POST and requests. Cannot be used with raw post data.
1432 * Accepts an array containing the files to post. Each entry should be an
1433 * associative array with "name", "file" and "type" keys. If the parameter
1434 * is empty or omitted the post files will be unset.
1436 * Returns TRUE on success, or FALSE on failure.
1438 PHP_METHOD(HttpRequest
, setPostFiles
)
1440 zval
*files
, *pFiles
;
1441 getObject(http_request_object
, obj
);
1443 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &files
)) {
1447 pFiles
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1450 zend_hash_clean(Z_ARRVAL_P(pFiles
));
1451 if (files
&& zend_hash_num_elements(Z_ARRVAL_P(files
))) {
1452 array_copy(files
, pFiles
);
1454 SET_PROP(obj
, postFiles
, pFiles
);
1460 /* {{{ proto array HttpRequest::getPostFiles()
1462 * Get all previously added POST files.
1464 * Returns an array containing currently set post files.
1466 PHP_METHOD(HttpRequest
, getPostFiles
)
1471 getObject(http_request_object
, obj
);
1472 zval
*files_p
, *files
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFiles
), &files_p
);
1474 array_init(return_value
);
1475 array_copy(files
, return_value
);
1478 zval_ptr_dtor(&files_p
);
1484 /* {{{ proto bool HttpRequest::setPutFile([string file])
1486 * Set file to put. Affects only PUT requests.
1488 * Accepts a string as parameter referencing the path to file.
1489 * If the parameter is empty or omitted the put file will be unset.
1491 * Returns TRUE on success, or FALSE on failure.
1493 PHP_METHOD(HttpRequest
, setPutFile
)
1497 getObject(http_request_object
, obj
);
1499 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1503 UPD_STRL(obj
, putFile
, file
, file_len
);
1508 /* {{{ proto string HttpRequest::getPutFile()
1510 * Get previously set put file.
1512 * Returns a string containing the path to the currently set put file.
1514 PHP_METHOD(HttpRequest
, getPutFile
)
1519 getObject(http_request_object
, obj
);
1520 zval
*putfile_p
, *putfile
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, putFile
), &putfile_p
);
1522 RETVAL_ZVAL(putfile
, 1, 0);
1525 zval_ptr_dtor(&putfile_p
);
1531 /* {{{ proto array HttpRequest::getResponseData()
1533 * Get all response data after the request has been sent.
1535 * Returns an associative array with the key "headers" containing an associative
1536 * array holding all response headers, as well as the ley "body" containing a
1537 * string with the response body.
1539 * If redirects were allowed and several responses were received, the data
1540 * references the last received response.
1542 PHP_METHOD(HttpRequest
, getResponseData
)
1547 getObject(http_request_object
, obj
);
1548 zval
*data_p
, *data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
), &data_p
);
1550 array_init(return_value
);
1551 array_copy(data
, return_value
);
1554 zval_ptr_dtor(&data_p
);
1560 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1562 * Get response header(s) after the request has been sent.
1564 * Accepts an string as optional parameter specifying a certain header to read.
1565 * If the parameter is empty or omitted all response headers will be returned.
1567 * Returns either a string with the value of the header matching name if requested,
1568 * FALSE on failure, or an associative array containing all reponse headers.
1570 * If redirects were allowed and several responses were received, the data
1571 * references the last received response.
1573 PHP_METHOD(HttpRequest
, getResponseHeader
)
1576 zval
*data_p
, *data
, **headers
, **header
;
1577 char *header_name
= NULL
;
1579 getObject(http_request_object
, obj
);
1581 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1585 data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
), &data_p
);
1586 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1587 convert_to_array(*headers
);
1588 if (!header_len
|| !header_name
) {
1589 array_init(return_value
);
1590 array_copy(*headers
, return_value
);
1591 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1592 RETVAL_ZVAL(*header
, 1, 0);
1601 zval_ptr_dtor(&data_p
);
1607 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1609 * Get response cookie(s) after the request has been sent.
1611 * Accepts a string as optional parameter specifying the name of the cookie to read.
1612 * If the parameter is empty or omitted, an associative array with all received
1613 * cookies will be returned.
1615 * Returns either an associative array with the cookie's name, value and any
1616 * additional params of the cookie matching name if requested, FALSE on failure,
1617 * or an array containing all received cookies as arrays.
1619 * If redirects were allowed and several responses were received, the data
1620 * references the last received response.
1622 PHP_METHOD(HttpRequest
, getResponseCookie
)
1625 zval
*data
, **headers
;
1626 char *cookie_name
= NULL
;
1628 getObject(http_request_object
, obj
);
1630 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1634 array_init(return_value
);
1636 data
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
));
1637 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1640 zval
**header
= NULL
;
1643 convert_to_array(*headers
);
1644 FOREACH_HASH_KEYVAL(pos1
, Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1645 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1646 /* several cookies? */
1647 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1651 FOREACH_HASH_VAL(pos2
, Z_ARRVAL_PP(header
), cookie
) {
1653 MAKE_STD_ZVAL(cookie_hash
);
1654 array_init(cookie_hash
);
1656 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1658 add_next_index_zval(return_value
, cookie_hash
);
1662 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1663 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1664 add_next_index_zval(return_value
, cookie_hash
);
1665 return; /* <<< FOUND >>> */
1667 zval_dtor(cookie_hash
);
1672 zval_dtor(cookie_hash
);
1679 MAKE_STD_ZVAL(cookie_hash
);
1680 array_init(cookie_hash
);
1681 convert_to_string_ex(header
);
1683 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1685 add_next_index_zval(return_value
, cookie_hash
);
1689 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1690 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1691 add_next_index_zval(return_value
, cookie_hash
);
1693 zval_dtor(cookie_hash
);
1698 zval_dtor(cookie_hash
);
1712 /* {{{ proto string HttpRequest::getResponseBody()
1714 * Get the response body after the request has been sent.
1716 * Returns a string containing the response body.
1718 * If redirects were allowed and several responses were received, the data
1719 * references the last received response.
1721 PHP_METHOD(HttpRequest
, getResponseBody
)
1727 getObject(http_request_object
, obj
);
1728 zval
*data
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
));
1730 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1731 RETURN_ZVAL(*body
, 1, 0);
1739 /* {{{ proto int HttpRequest::getResponseCode()
1741 * Get the response code after the request has been sent.
1743 * Returns an int representing the response code.
1745 * If redirects were allowed and several responses were received, the data
1746 * references the last received response.
1748 PHP_METHOD(HttpRequest
, getResponseCode
)
1753 getObject(http_request_object
, obj
);
1754 zval
*code_p
, *code
= convert_to_type_ex(IS_LONG
, GET_PROP(obj
, responseCode
), &code_p
);
1756 RETVAL_ZVAL(code
, 1, 0);
1759 zval_ptr_dtor(&code_p
);
1765 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1767 * Get response info after the request has been sent.
1768 * See http_get() for a full list of returned info.
1770 * Accepts a string as optional parameter specifying the info to read.
1771 * If the parameter is empty or omitted, an associative array containing
1772 * all available info will be returned.
1774 * Returns either a scalar containing the value of the info matching name if
1775 * requested, FALSE on failure, or an associative array containing all
1778 * If redirects were allowed and several responses were received, the data
1779 * references the last received response.
1781 PHP_METHOD(HttpRequest
, getResponseInfo
)
1784 zval
*info
, **infop
;
1785 char *info_name
= NULL
;
1787 getObject(http_request_object
, obj
);
1789 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1793 info
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseInfo
));
1795 if (info_len
&& info_name
) {
1796 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1797 RETURN_ZVAL(*infop
, 1, 0);
1799 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1803 array_init(return_value
);
1804 array_copy(info
, return_value
);
1810 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1812 * Get the full response as HttpMessage object after the request has been sent.
1814 * Returns an HttpMessage object of the response.
1816 * If redirects were allowed and several responses were received, the data
1817 * references the last received response. Use HttpMessage::getParentMessage()
1818 * to access the data of previously received responses whithin this request
1821 * Throws HttpException.
1823 PHP_METHOD(HttpRequest
, getResponseMessage
)
1829 getObject(http_request_object
, obj
);
1831 SET_EH_THROW_HTTP();
1832 message
= GET_PROP(obj
, responseMessage
);
1833 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1834 RETVAL_OBJECT(message
);
1843 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1845 * Get sent HTTP message.
1847 * Returns an HttpMessage object representing the sent request.
1849 * If redirects were allowed and several responses were received, the data
1850 * references the last received response. Use HttpMessage::getParentMessage()
1851 * to access the data of previously sent requests whithin this request
1854 * Note that the internal request message is immutable, that means that the
1855 * request message received through HttpRequest::getRequestMessage() will
1856 * always look the same for the same request, regardless of any changes you
1857 * may have made to the returned object.
1859 * Throws HttpMalformedHeadersException, HttpEncodingException.
1861 PHP_METHOD(HttpRequest
, getRequestMessage
)
1867 getObject(http_request_object
, obj
);
1869 SET_EH_THROW_HTTP();
1870 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1871 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1878 /* {{{ proto HttpMessage HttpRequest::getHistory()
1880 * Get all sent requests and received responses as an HttpMessage object.
1882 * If you don't want to record history at all, set the instance variable
1883 * HttpRequest::$recoedHistory to FALSE.
1885 * Returns an HttpMessage object representing the complete request/response
1888 * The object references the last received response, use HttpMessage::getParentMessage()
1889 * to access the data of previously sent requests and received responses.
1891 * Note that the internal history is immutable, that means that any changes
1892 * you make the the message list won't affect a history message list newly
1893 * created by another call to HttpRequest::getHistory().
1895 * Throws HttpMalformedHeaderException, HttpEncodingException.
1897 PHP_METHOD(HttpRequest
, getHistory
)
1903 getObject(http_request_object
, obj
);
1905 SET_EH_THROW_HTTP();
1906 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->history
), PHPSTR_LEN(&obj
->history
)))) {
1907 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1914 /* {{{ proto void HttpRequest::clearHistory()
1916 * Clear the history.
1918 PHP_METHOD(HttpRequest
, clearHistory
)
1921 getObject(http_request_object
, obj
);
1922 phpstr_dtor(&obj
->history
);
1927 /* {{{ proto HttpMessage HttpRequest::send()
1929 * Send the HTTP request.
1931 * Returns the received response as HttpMessage object.
1933 * NOTE: While an exception may be thrown, the transfer could have succeeded
1934 * at least partially, so you might want to check the return values of various
1935 * HttpRequest::getResponse*() methods.
1937 * Throws HttpRuntimeException, HttpRequestException,
1938 * HttpMalformedHeaderException, HttpEncodingException.
1943 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
1944 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1945 * $r->addQueryData(array('category' => 3));
1948 * if ($r->getResponseCode() == 200) {
1949 * file_put_contents('local.rss', $r->getResponseBody());
1951 * } catch (HttpException $ex) {
1960 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
1961 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1962 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1963 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1965 * echo $r->send()->getBody();
1966 * } catch (HttpException $ex) {
1972 PHP_METHOD(HttpRequest
, send
)
1974 getObject(http_request_object
, obj
);
1978 SET_EH_THROW_HTTP();
1981 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1988 if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
1989 http_request_exec(obj
->request
);
1990 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
1991 RETVAL_OBJECT(GET_PROP(obj
, responseMessage
));
1999 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
2006 * vim600: noet sw=4 ts=4 fdm=marker
2007 * vim<600: noet sw=4 ts=4