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_BEGIN_ARGS(__construct
, 0, 0)
44 HTTP_ARG_VAL(method
, 0)
45 HTTP_ARG_VAL(options
, 0)
48 HTTP_EMPTY_ARGS(getOptions
, 0);
49 HTTP_BEGIN_ARGS(setOptions
, 0, 0)
50 HTTP_ARG_VAL(options
, 0)
53 HTTP_EMPTY_ARGS(getSslOptions
, 0);
54 HTTP_BEGIN_ARGS(setSslOptions
, 0, 0)
55 HTTP_ARG_VAL(ssl_options
, 0)
58 HTTP_EMPTY_ARGS(getHeaders
, 0);
59 HTTP_BEGIN_ARGS(setHeaders
, 0, 0)
60 HTTP_ARG_VAL(headers
, 0)
63 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
64 HTTP_ARG_VAL(headers
, 0)
67 HTTP_EMPTY_ARGS(getCookies
, 0);
68 HTTP_BEGIN_ARGS(setCookies
, 0, 0)
69 HTTP_ARG_VAL(cookies
, 0)
72 HTTP_BEGIN_ARGS(addCookies
, 0, 1)
73 HTTP_ARG_VAL(cookies
, 0)
76 HTTP_EMPTY_ARGS(getUrl
, 0);
77 HTTP_BEGIN_ARGS(setUrl
, 0, 1)
81 HTTP_EMPTY_ARGS(getMethod
, 0);
82 HTTP_BEGIN_ARGS(setMethod
, 0, 1)
83 HTTP_ARG_VAL(request_method
, 0)
86 HTTP_EMPTY_ARGS(getContentType
, 0);
87 HTTP_BEGIN_ARGS(setContentType
, 0, 1)
88 HTTP_ARG_VAL(content_type
, 0)
91 HTTP_EMPTY_ARGS(getQueryData
, 0);
92 HTTP_BEGIN_ARGS(setQueryData
, 0, 0)
93 HTTP_ARG_VAL(query_data
, 0)
96 HTTP_BEGIN_ARGS(addQueryData
, 0, 1)
97 HTTP_ARG_VAL(query_data
, 0)
100 HTTP_EMPTY_ARGS(getPostFields
, 0);
101 HTTP_BEGIN_ARGS(setPostFields
, 0, 0)
102 HTTP_ARG_VAL(post_fields
, 0)
105 HTTP_BEGIN_ARGS(addPostFields
, 0, 1)
106 HTTP_ARG_VAL(post_fields
, 0)
109 HTTP_EMPTY_ARGS(getPostFiles
, 0);
110 HTTP_BEGIN_ARGS(setPostFiles
, 0, 0)
111 HTTP_ARG_VAL(post_files
, 0)
114 HTTP_BEGIN_ARGS(addPostFile
, 0, 2)
115 HTTP_ARG_VAL(formname
, 0)
116 HTTP_ARG_VAL(filename
, 0)
117 HTTP_ARG_VAL(content_type
, 0)
120 HTTP_EMPTY_ARGS(getRawPostData
, 0);
121 HTTP_BEGIN_ARGS(setRawPostData
, 0, 0)
122 HTTP_ARG_VAL(raw_post_data
, 0)
125 HTTP_BEGIN_ARGS(addRawPostData
, 0, 1)
126 HTTP_ARG_VAL(raw_post_data
, 0)
129 HTTP_EMPTY_ARGS(getPutFile
, 0);
130 HTTP_BEGIN_ARGS(setPutFile
, 0, 0)
131 HTTP_ARG_VAL(filename
, 0)
134 HTTP_EMPTY_ARGS(getResponseData
, 0);
135 HTTP_BEGIN_ARGS(getResponseHeader
, 0, 0)
136 HTTP_ARG_VAL(name
, 0)
139 HTTP_BEGIN_ARGS(getResponseCookie
, 0, 0)
140 HTTP_ARG_VAL(name
, 0)
143 HTTP_EMPTY_ARGS(getResponseBody
, 0);
144 HTTP_EMPTY_ARGS(getResponseCode
, 0);
145 HTTP_BEGIN_ARGS(getResponseInfo
, 0, 0)
146 HTTP_ARG_VAL(name
, 0)
149 HTTP_EMPTY_ARGS(getResponseMessage
, 0);
150 HTTP_EMPTY_ARGS(getRawResponseMessage
, 0);
151 HTTP_EMPTY_ARGS(getRequestMessage
, 0);
152 HTTP_EMPTY_ARGS(getRawRequestMessage
, 0);
153 HTTP_EMPTY_ARGS(getHistory
, 0);
154 HTTP_EMPTY_ARGS(clearHistory
, 0);
155 HTTP_EMPTY_ARGS(send
, 0);
157 HTTP_BEGIN_ARGS(get
, 0, 1)
159 HTTP_ARG_VAL(options
, 0)
160 HTTP_ARG_VAL(info
, 1)
163 HTTP_BEGIN_ARGS(head
, 0, 1)
165 HTTP_ARG_VAL(options
, 0)
166 HTTP_ARG_VAL(info
, 1)
169 HTTP_BEGIN_ARGS(postData
, 0, 2)
171 HTTP_ARG_VAL(data
, 0)
172 HTTP_ARG_VAL(options
, 0)
173 HTTP_ARG_VAL(info
, 1)
176 HTTP_BEGIN_ARGS(postFields
, 0, 2)
178 HTTP_ARG_VAL(data
, 0)
179 HTTP_ARG_VAL(options
, 0)
180 HTTP_ARG_VAL(info
, 1)
183 HTTP_BEGIN_ARGS(putFile
, 0, 2)
185 HTTP_ARG_VAL(file
, 0)
186 HTTP_ARG_VAL(options
, 0)
187 HTTP_ARG_VAL(info
, 1)
190 HTTP_BEGIN_ARGS(putStream
, 0, 2)
192 HTTP_ARG_VAL(stream
, 0)
193 HTTP_ARG_VAL(options
, 0)
194 HTTP_ARG_VAL(info
, 1)
197 HTTP_BEGIN_ARGS(methodRegister
, 0, 1)
198 HTTP_ARG_VAL(method_name
, 0)
201 HTTP_BEGIN_ARGS(methodUnregister
, 0, 1)
202 HTTP_ARG_VAL(method
, 0)
205 HTTP_BEGIN_ARGS(methodName
, 0, 1)
206 HTTP_ARG_VAL(method_id
, 0)
209 HTTP_BEGIN_ARGS(methodExists
, 0, 1)
210 HTTP_ARG_VAL(method
, 0)
213 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
214 static inline void _http_request_object_declare_default_properties(TSRMLS_D
);
216 zend_class_entry
*http_request_object_ce
;
217 zend_function_entry http_request_object_fe
[] = {
218 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
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(getRawResponseMessage
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
272 HTTP_REQUEST_ME(getRawRequestMessage
, ZEND_ACC_PUBLIC
)
273 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
274 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ALIAS(get
, http_get
)
277 HTTP_REQUEST_ALIAS(head
, http_head
)
278 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
279 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
280 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
281 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
283 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
284 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
285 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
286 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
290 static zend_object_handlers http_request_object_handlers
;
292 PHP_MINIT_FUNCTION(http_request_object
)
294 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
295 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
299 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
301 return http_request_object_new_ex(ce
, curl_easy_init(), NULL
);
304 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
306 zend_object_value ov
;
307 http_request_object
*o
;
309 o
= ecalloc(1, sizeof(http_request_object
));
311 o
->request
= http_request_init_ex(NULL
, ch
, 0, NULL
);
312 phpstr_init(&o
->history
);
318 ALLOC_HASHTABLE(OBJ_PROP(o
));
319 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
320 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
322 ov
.handle
= putObject(http_request_object
, o
);
323 ov
.handlers
= &http_request_object_handlers
;
328 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
331 zend_object_value new_ov
;
332 http_request_object
*new_obj
;
333 getObject(http_request_object
, old_obj
);
335 old_zo
= zend_objects_get_address(this_ptr TSRMLS_CC
);
336 new_ov
= http_request_object_new_ex(old_zo
->ce
, curl_easy_duphandle(old_obj
->request
->ch
), &new_obj
);
338 zend_objects_clone_members(&new_obj
->zo
, new_ov
, old_zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
339 phpstr_append(&new_obj
->history
, old_obj
->history
.data
, old_obj
->history
.used
);
340 phpstr_append(&new_obj
->request
->conv
.request
, old_obj
->request
->conv
.request
.data
, old_obj
->request
->conv
.request
.used
);
341 phpstr_append(&new_obj
->request
->conv
.response
, old_obj
->request
->conv
.response
.data
, old_obj
->request
->conv
.response
.used
);
346 static inline void _http_request_object_declare_default_properties(TSRMLS_D
)
348 zend_class_entry
*ce
= http_request_object_ce
;
350 DCL_PROP_N(PRIVATE
, options
);
351 DCL_PROP_N(PRIVATE
, postFields
);
352 DCL_PROP_N(PRIVATE
, postFiles
);
353 DCL_PROP_N(PRIVATE
, responseInfo
);
354 DCL_PROP_N(PRIVATE
, responseData
);
355 DCL_PROP_N(PRIVATE
, responseMessage
);
356 DCL_PROP(PRIVATE
, long, responseCode
, 0);
357 DCL_PROP(PRIVATE
, long, method
, HTTP_GET
);
358 DCL_PROP(PRIVATE
, string
, url
, "");
359 DCL_PROP(PRIVATE
, string
, contentType
, "");
360 DCL_PROP(PRIVATE
, string
, rawPostData
, "");
361 DCL_PROP(PRIVATE
, string
, queryData
, "");
362 DCL_PROP(PRIVATE
, 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 STATUS status
= SUCCESS
;
429 http_request_reset(obj
->request
);
430 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, curl_easy_init(), return FAILURE
);
432 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(GET_PROP(obj
, url
)));
434 switch (obj
->request
->meth
= Z_LVAL_P(GET_PROP(obj
, method
)))
442 php_stream_statbuf ssb
;
443 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj
, putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
445 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
446 http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
456 /* check for raw post data */
457 zval
*raw_data
= GET_PROP(obj
, rawPostData
);
459 if (Z_STRLEN_P(raw_data
)) {
460 zval
*ctype
= GET_PROP(obj
, contentType
);
462 if (Z_STRLEN_P(ctype
)) {
463 zval
**headers
, *opts
= GET_PROP(obj
, options
);
465 if ((SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) && (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
468 /* only override if not already set */
469 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) && (Z_TYPE_PP(ct_header
) == IS_STRING
)) {
470 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
475 MAKE_STD_ZVAL(headers
);
477 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
478 add_assoc_zval(opts
, "headers", headers
);
482 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
483 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
486 zval
*zfields
= GET_PROP(obj
, postFields
), *zfiles
= GET_PROP(obj
, postFiles
);
490 fields
= (Z_TYPE_P(zfields
) == IS_ARRAY
) ? Z_ARRVAL_P(zfields
) : NULL
;
491 files
= (Z_TYPE_P(zfiles
) == IS_ARRAY
) ? Z_ARRVAL_P(zfiles
) : NULL
;
493 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
501 if (status
== SUCCESS
) {
502 zval
*qdata
= GET_PROP(obj
, queryData
);
503 zval
*options
= GET_PROP(obj
, options
);
505 if (Z_STRLEN_P(qdata
)) {
506 if (!strchr(obj
->request
->url
, '?')) {
507 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
509 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
511 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
514 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
520 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
524 phpstr_fix(&obj
->request
->conv
.request
);
525 phpstr_fix(&obj
->request
->conv
.response
);
527 msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
));
534 zval
*headers
, *message
, *resp
, *info
;
536 if (zval_is_true(GET_PROP(obj
, recordHistory
))) {
537 /* we need to act like a zipper, as we'll receive
538 * the requests and the responses in separate chains
541 http_message
*response
= msg
, *request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
542 http_message
*free_msg
= request
;
548 http_message_tostring(response
, &message
, &msglen
);
549 phpstr_append(&obj
->history
, message
, msglen
);
552 http_message_tostring(request
, &message
, &msglen
);
553 phpstr_append(&obj
->history
, message
, msglen
);
556 } while ((response
= response
->parent
) && (request
= request
->parent
));
558 http_message_free(&free_msg
);
559 phpstr_fix(&obj
->history
);
562 UPD_PROP(obj
, long, responseCode
, msg
->http
.info
.response
.code
);
566 MAKE_STD_ZVAL(headers
);
568 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
569 add_assoc_zval(resp
, "headers", headers
);
570 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
571 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
572 SET_PROP(obj
, responseData
, resp
);
573 zval_ptr_dtor(&resp
);
575 MAKE_STD_ZVAL(message
);
576 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
577 SET_PROP(obj
, responseMessage
, message
);
578 zval_ptr_dtor(&message
);
582 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
583 SET_PROP(obj
, responseInfo
, info
);
584 zval_ptr_dtor(&info
);
590 #define http_request_object_set_options_subr(key, ow) \
591 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
592 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
594 zval
*old_opts
, *new_opts
, *opts
, **entry
;
595 getObject(http_request_object
, obj
);
597 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
601 MAKE_STD_ZVAL(new_opts
);
602 array_init(new_opts
);
603 old_opts
= GET_PROP(obj
, options
);
604 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
605 array_copy(old_opts
, new_opts
);
608 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), key
, len
, (void **) &entry
)) {
610 zend_hash_clean(Z_ARRVAL_PP(entry
));
612 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
614 array_copy(opts
, *entry
);
616 array_merge(opts
, *entry
);
621 add_assoc_zval(new_opts
, key
, opts
);
623 SET_PROP(obj
, options
, new_opts
);
624 zval_ptr_dtor(&new_opts
);
629 #define http_request_object_get_options_subr(key) \
630 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
631 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
636 zval
*opts
, **options
;
637 getObject(http_request_object
, obj
);
639 opts
= GET_PROP(obj
, options
);
641 array_init(return_value
);
643 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
644 convert_to_array(*options
);
645 array_copy(*options
, return_value
);
651 /* ### USERLAND ### */
653 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
655 * Instantiate a new HttpRequest object.
657 * Accepts a string as optional parameter containing the target request url.
658 * Additianally accepts an optional int parameter specifying the request method
659 * to use and an associative array as optional third parameter which will be
660 * passed to HttpRequest::setOptions().
662 * Throws HttpException.
664 PHP_METHOD(HttpRequest
, __construct
)
669 zval
*options
= NULL
;
670 getObject(http_request_object
, obj
);
673 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
675 UPD_STRL(obj
, url
, URL
, URL_len
);
678 UPD_PROP(obj
, long, method
, meth
);
681 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
688 /* {{{ proto bool HttpRequest::setOptions([array options])
690 * Set the request options to use. See http_get() for a full list of available options.
692 * Accepts an array as optional parameters, wich values will overwrite the
693 * currently set request options. If the parameter is empty or mitted,
694 * the optoions of the HttpRequest object will be reset.
696 * Returns TRUE on success, or FALSE on failure.
698 PHP_METHOD(HttpRequest
, setOptions
)
703 zval
*opts
= NULL
, *old_opts
, *new_opts
, **opt
;
704 getObject(http_request_object
, obj
);
706 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
710 MAKE_STD_ZVAL(new_opts
);
711 array_init(new_opts
);
713 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
714 SET_PROP(obj
, options
, new_opts
);
718 old_opts
= GET_PROP(obj
, options
);
719 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
720 array_copy(old_opts
, new_opts
);
723 /* some options need extra attention -- thus cannot use array_merge() directly */
724 FOREACH_KEYVAL(pos
, opts
, key
, idx
, opt
) {
726 if (!strcmp(key
, "headers")) {
728 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
729 convert_to_array_ex(opt
);
730 convert_to_array(*headers
);
731 array_merge(*opt
, *headers
);
734 } else if (!strcmp(key
, "cookies")) {
736 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
737 convert_to_array_ex(opt
);
738 convert_to_array(*cookies
);
739 array_merge(*opt
, *cookies
);
742 } else if (!strcmp(key
, "ssl")) {
744 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), "ssl", sizeof("ssl"), (void **) &ssl
)) {
745 convert_to_array_ex(opt
);
746 convert_to_array(*ssl
);
747 array_merge(*opt
, *ssl
);
750 } else if ((Z_TYPE_PP(opt
) == IS_STRING
) && ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri")))) {
751 UPD_STRL(obj
, url
, Z_STRVAL_PP(opt
), Z_STRLEN_PP(opt
));
753 } else if (!strcmp(key
, "method")) {
754 if (Z_TYPE_PP(opt
) != IS_LONG
) {
755 convert_to_long_ex(opt
);
757 UPD_PROP(obj
, long, method
, Z_LVAL_PP(opt
));
762 add_assoc_zval(new_opts
, key
, *opt
);
768 SET_PROP(obj
, options
, new_opts
);
769 zval_ptr_dtor(&new_opts
);
775 /* {{{ proto array HttpRequest::getOptions()
777 * Get currently set options.
779 * Returns an associative array containing currently set options.
781 PHP_METHOD(HttpRequest
, getOptions
)
786 getObject(http_request_object
, obj
);
787 zval
*options
= GET_PROP(obj
, options
);
789 RETURN_ZVAL(options
, 1, 0);
794 /* {{{ proto bool HttpRequest::setSslOptions([array options])
798 * Accepts an associative array as parameter containing any SSL specific options.
799 * If the parameter is empty or omitted, the SSL options will be reset.
801 * Returns TRUE on success, or FALSE on failure.
803 PHP_METHOD(HttpRequest
, setSslOptions
)
805 http_request_object_set_options_subr("ssl", 1);
809 /* {{{ proto bool HttpRequest::addSslOptions(array options)
811 * Set additional SSL options.
813 * Expects an associative array as parameter containing additional SSL specific options.
815 * Returns TRUE on success, or FALSE on failure.
817 PHP_METHOD(HttpRequest
, addSslOptions
)
819 http_request_object_set_options_subr("ssl", 0);
823 /* {{{ proto array HttpRequest::getSslOtpions()
825 * Get previously set SSL options.
827 * Returns an associative array containing any previously set SSL options.
829 PHP_METHOD(HttpRequest
, getSslOptions
)
831 http_request_object_get_options_subr("ssl");
835 /* {{{ proto bool HttpRequest::addHeaders(array headers)
837 * Add request header name/value pairs.
839 * Expects an ssociative array as parameter containing additional header
842 * Returns TRUE on success, or FALSE on failure.
844 PHP_METHOD(HttpRequest
, addHeaders
)
846 http_request_object_set_options_subr("headers", 0);
849 /* {{{ proto bool HttpRequest::setHeaders([array headers])
851 * Set request header name/value pairs.
853 * Accepts an associative array as parameter containing header name/value pairs.
854 * If the parameter is empty or omitted, all previously set headers will be unset.
856 * Returns TRUE on success, or FALSE on failure.
858 PHP_METHOD(HttpRequest
, setHeaders
)
860 http_request_object_set_options_subr("headers", 1);
864 /* {{{ proto array HttpRequest::getHeaders()
866 * Get previously set request headers.
868 * Returns an associative array containing all currently set headers.
870 PHP_METHOD(HttpRequest
, getHeaders
)
872 http_request_object_get_options_subr("headers");
876 /* {{{ proto bool HttpRequest::setCookies([array cookies])
880 * Accepts an associative array as parameter containing cookie name/value pairs.
881 * If the parameter is empty or omitted, all previously set cookies will be unset.
883 * Returns TRUE on success, or FALSE on failure.
885 PHP_METHOD(HttpRequest
, setCookies
)
887 http_request_object_set_options_subr("cookies", 1);
891 /* {{{ proto bool HttpRequest::addCookies(array cookies)
895 * Expects an associative array as parameter containing any cookie name/value
898 * Returns TRUE on success, or FALSE on failure.
900 PHP_METHOD(HttpRequest
, addCookies
)
902 http_request_object_set_options_subr("cookies", 0);
906 /* {{{ proto array HttpRequest::getCookies()
908 * Get previously set cookies.
910 * Returns an associative array containing any previously set cookies.
912 PHP_METHOD(HttpRequest
, getCookies
)
914 http_request_object_get_options_subr("cookies");
918 /* {{{ proto bool HttpRequest::setUrl(string url)
920 * Set the request URL.
922 * Expects a string as parameter specifying the request url.
924 * Returns TRUE on success, or FALSE on failure.
926 PHP_METHOD(HttpRequest
, setUrl
)
930 getObject(http_request_object
, obj
);
932 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
936 UPD_STRL(obj
, url
, URL
, URL_len
);
941 /* {{{ proto string HttpRequest::getUrl()
943 * Get the previously set request URL.
945 * Returns the currently set request url as string.
947 PHP_METHOD(HttpRequest
, getUrl
)
952 getObject(http_request_object
, obj
);
953 zval
*url
= GET_PROP(obj
, url
);
955 RETURN_ZVAL(url
, 1, 0);
960 /* {{{ proto bool HttpRequest::setMethod(int request_method)
962 * Set the request method.
964 * Expects an int as parameter specifying the request method to use.
965 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
967 * Returns TRUE on success, or FALSE on failure.
969 PHP_METHOD(HttpRequest
, setMethod
)
972 getObject(http_request_object
, obj
);
974 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
978 UPD_PROP(obj
, long, method
, meth
);
983 /* {{{ proto int HttpRequest::getMethod()
985 * Get the previously set request method.
987 * Returns the currently set request method.
989 PHP_METHOD(HttpRequest
, getMethod
)
994 getObject(http_request_object
, obj
);
995 zval
*method
= GET_PROP(obj
, method
);
997 RETURN_ZVAL(method
, 1, 0);
1002 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1004 * Set the content type the post request should have.
1006 * Expects a string as parameters containing the content type of the request
1007 * (primary/secondary).
1009 * Returns TRUE on success, or FALSE if the content type does not seem to
1010 * contain a primary and a secondary part.
1012 PHP_METHOD(HttpRequest
, setContentType
)
1016 getObject(http_request_object
, obj
);
1018 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1022 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1023 UPD_STRL(obj
, contentType
, ctype
, ct_len
);
1028 /* {{{ proto string HttpRequest::getContentType()
1030 * Get the previously content type.
1032 * Returns the previously set content type as string.
1034 PHP_METHOD(HttpRequest
, getContentType
)
1039 getObject(http_request_object
, obj
);
1040 zval
*ctype
= GET_PROP(obj
, contentType
);
1042 RETURN_ZVAL(ctype
, 1, 0);
1047 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1049 * Set the URL query parameters to use, overwriting previously set query parameters.
1050 * Affects any request types.
1052 * Accepts a string or associative array parameter containing the pre-encoded
1053 * query string or to be encoded query fields. If the parameter is empty or
1054 * omitted, the query data will be unset.
1056 * Returns TRUE on success, or FALSE on failure.
1058 PHP_METHOD(HttpRequest
, setQueryData
)
1061 getObject(http_request_object
, obj
);
1063 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1067 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1068 UPD_STRL(obj
, queryData
, "", 0);
1069 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1070 char *query_data
= NULL
;
1072 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1076 UPD_PROP(obj
, string
, queryData
, query_data
);
1081 convert_to_string_ex(&qdata
);
1082 UPD_STRL(obj
, queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1083 if (orig
!= qdata
) {
1084 zval_ptr_dtor(&qdata
);
1091 /* {{{ proto string HttpRequest::getQueryData()
1093 * Get the current query data in form of an urlencoded query string.
1095 * Returns a string containing the urlencoded query.
1097 PHP_METHOD(HttpRequest
, getQueryData
)
1102 getObject(http_request_object
, obj
);
1103 zval
*qdata
= GET_PROP(obj
, queryData
);
1105 RETURN_ZVAL(qdata
, 1, 0);
1110 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1112 * Add parameters to the query parameter list, leaving previously set unchanged.
1113 * Affects any request type.
1115 * Expects an associative array as parameter containing the query fields to add.
1117 * Returns TRUE on success, or FALSE on failure.
1119 PHP_METHOD(HttpRequest
, addQueryData
)
1121 zval
*qdata
, *old_qdata
;
1122 char *query_data
= NULL
;
1123 size_t query_data_len
= 0;
1124 getObject(http_request_object
, obj
);
1126 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &qdata
)) {
1130 old_qdata
= GET_PROP(obj
, queryData
);
1132 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
)) {
1136 UPD_STRL(obj
, queryData
, query_data
, query_data_len
);
1143 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1145 * Adds POST data entries, leaving previously set unchanged, unless a
1146 * post entry with the same name already exists.
1147 * Affects only POST and custom requests.
1149 * Expects an associative array as parameter containing the post fields.
1151 * Returns TRUE on success, or FALSE on failure.
1153 PHP_METHOD(HttpRequest
, addPostFields
)
1155 zval
*post_data
, *old_post
, *new_post
;
1156 getObject(http_request_object
, obj
);
1158 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &post_data
)) {
1162 if (zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1163 MAKE_STD_ZVAL(new_post
);
1164 array_init(new_post
);
1165 old_post
= GET_PROP(obj
, postFields
);
1166 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1167 array_copy(old_post
, new_post
);
1169 array_merge(post_data
, new_post
);
1170 SET_PROP(obj
, postFields
, new_post
);
1171 zval_ptr_dtor(&new_post
);
1178 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1180 * Set the POST data entries, overwriting previously set POST data.
1181 * Affects only POST and custom requests.
1183 * Accepts an associative array as parameter containing the post fields.
1184 * If the parameter is empty or omitted, the post data will be unset.
1186 * Returns TRUE on success, or FALSE on failure.
1188 PHP_METHOD(HttpRequest
, setPostFields
)
1190 zval
*post
, *post_data
= NULL
;
1191 getObject(http_request_object
, obj
);
1193 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &post_data
)) {
1197 MAKE_STD_ZVAL(post
);
1199 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1200 array_copy(post_data
, post
);
1202 SET_PROP(obj
, postFields
, post
);
1203 zval_ptr_dtor(&post
);
1209 /* {{{ proto array HttpRequest::getPostFields()
1211 * Get previously set POST data.
1213 * Returns the currently set post fields as associative array.
1215 PHP_METHOD(HttpRequest
, getPostFields
)
1220 getObject(http_request_object
, obj
);
1221 zval
*post
= GET_PROP(obj
, postFields
);
1223 RETURN_ZVAL(post
, 1, 0);
1228 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1230 * Set raw post data to send, overwriting previously set raw post data. Don't
1231 * forget to specify a content type. Affects only POST and custom requests.
1232 * Only either post fields or raw post data can be used for each request.
1233 * Raw post data has higher precedence and will be used even if post fields
1236 * Accepts a string as parameter containing the *raw* post data.
1238 * Returns TRUE on success, or FALSE on failure.
1240 PHP_METHOD(HttpRequest
, setRawPostData
)
1242 char *raw_data
= NULL
;
1244 getObject(http_request_object
, obj
);
1246 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1254 UPD_STRL(obj
, rawPostData
, raw_data
, data_len
);
1259 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1261 * Add raw post data, leaving previously set raw post data unchanged.
1262 * Affects only POST and custom requests.
1264 * Expects a string as parameter containing the raw post data to concatenate.
1266 * Returns TRUE on success, or FALSE on failure.
1268 PHP_METHOD(HttpRequest
, addRawPostData
)
1272 getObject(http_request_object
, obj
);
1274 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1279 zval
*data
, *zdata
= GET_PROP(obj
, rawPostData
);
1283 zval_copy_ctor(data
);
1285 convert_to_string(data
);
1286 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1287 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1288 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, raw_data
, data_len
);
1289 SET_PROP(obj
, rawPostData
, data
);
1290 zval_ptr_dtor(&data
);
1297 /* {{{ proto string HttpRequest::getRawPostData()
1299 * Get previously set raw post data.
1301 * Returns a string containing the currently set raw post data.
1303 PHP_METHOD(HttpRequest
, getRawPostData
)
1308 getObject(http_request_object
, obj
);
1309 zval
*post
= GET_PROP(obj
, rawPostData
);
1311 RETURN_ZVAL(post
, 1, 0);
1316 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1318 * Add a file to the POST request, leaving prefiously set files unchanged.
1319 * Affects only POST and custom requests. Cannot be used with raw post data.
1321 * Expects a string parameter containing the form element name, and a string
1322 * paremeter containing the path to the file which should be uploaded.
1323 * Additionally accepts an optional string parameter which chould contain
1324 * the content type of the file.
1326 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1327 * primary and a secondary content type part.
1329 PHP_METHOD(HttpRequest
, addPostFile
)
1331 zval
*entry
, *old_post
, *new_post
;
1332 char *name
, *file
, *type
= NULL
;
1333 int name_len
, file_len
, type_len
= 0;
1334 getObject(http_request_object
, obj
);
1336 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1341 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1343 type
= "application/x-octetstream";
1344 type_len
= sizeof("application/x-octetstream") - 1;
1347 MAKE_STD_ZVAL(entry
);
1350 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1351 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1352 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1354 MAKE_STD_ZVAL(new_post
);
1355 array_init(new_post
);
1356 old_post
= GET_PROP(obj
, postFiles
);
1357 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1358 array_copy(old_post
, new_post
);
1360 add_next_index_zval(new_post
, entry
);
1361 SET_PROP(obj
, postFiles
, new_post
);
1362 zval_ptr_dtor(&new_post
);
1368 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1370 * Set files to post, overwriting previously set post files.
1371 * Affects only POST and requests. Cannot be used with raw post data.
1373 * Accepts an array containing the files to post. Each entry should be an
1374 * associative array with "name", "file" and "type" keys. If the parameter
1375 * is empty or omitted the post files will be unset.
1377 * Returns TRUE on success, or FALSE on failure.
1379 PHP_METHOD(HttpRequest
, setPostFiles
)
1381 zval
*files
= NULL
, *post
;
1382 getObject(http_request_object
, obj
);
1384 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!/", &files
)) {
1388 MAKE_STD_ZVAL(post
);
1390 if (files
&& (Z_TYPE_P(files
) == IS_ARRAY
)) {
1391 array_copy(files
, post
);
1393 SET_PROP(obj
, postFiles
, post
);
1399 /* {{{ proto array HttpRequest::getPostFiles()
1401 * Get all previously added POST files.
1403 * Returns an array containing currently set post files.
1405 PHP_METHOD(HttpRequest
, getPostFiles
)
1410 getObject(http_request_object
, obj
);
1412 array_init(return_value
);
1413 array_copy(GET_PROP(obj
, postFiles
), return_value
);
1418 /* {{{ proto bool HttpRequest::setPutFile([string file])
1420 * Set file to put. Affects only PUT requests.
1422 * Accepts a string as parameter referencing the path to file.
1423 * If the parameter is empty or omitted the put file will be unset.
1425 * Returns TRUE on success, or FALSE on failure.
1427 PHP_METHOD(HttpRequest
, setPutFile
)
1431 getObject(http_request_object
, obj
);
1433 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1437 UPD_STRL(obj
, putFile
, file
, file_len
);
1442 /* {{{ proto string HttpRequest::getPutFile()
1444 * Get previously set put file.
1446 * Returns a string containing the path to the currently set put file.
1448 PHP_METHOD(HttpRequest
, getPutFile
)
1453 getObject(http_request_object
, obj
);
1454 zval
*file
= GET_PROP(obj
, putFile
);
1456 RETVAL_ZVAL(file
, 1, 0);
1461 /* {{{ proto array HttpRequest::getResponseData()
1463 * Get all response data after the request has been sent.
1465 * Returns an associative array with the key "headers" containing an associative
1466 * array holding all response headers, as well as the ley "body" containing a
1467 * string with the response body.
1469 * If redirects were allowed and several responses were received, the data
1470 * references the last received response.
1472 PHP_METHOD(HttpRequest
, getResponseData
)
1477 getObject(http_request_object
, obj
);
1479 array_init(return_value
);
1480 array_copy(GET_PROP(obj
, responseData
), return_value
);
1485 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1487 * Get response header(s) after the request has been sent.
1489 * Accepts an string as optional parameter specifying a certain header to read.
1490 * If the parameter is empty or omitted all response headers will be returned.
1492 * Returns either a string with the value of the header matching name if requested,
1493 * FALSE on failure, or an associative array containing all reponse headers.
1495 * If redirects were allowed and several responses were received, the data
1496 * references the last received response.
1498 PHP_METHOD(HttpRequest
, getResponseHeader
)
1501 zval
*data
, **headers
, **header
;
1502 char *header_name
= NULL
;
1504 getObject(http_request_object
, obj
);
1506 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1510 data
= GET_PROP(obj
, responseData
);
1511 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1512 convert_to_array(*headers
);
1513 if (!header_len
|| !header_name
) {
1514 RETVAL_ZVAL(*headers
, 1, 0);
1515 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1516 RETVAL_ZVAL(*header
, 1, 0);
1527 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1529 * Get response cookie(s) after the request has been sent.
1531 * Accepts a string as optional parameter specifying the name of the cookie to read.
1532 * If the parameter is empty or omitted, an associative array with all received
1533 * cookies will be returned.
1535 * Returns either an associative array with the cookie's name, value and any
1536 * additional params of the cookie matching name if requested, FALSE on failure,
1537 * or an array containing all received cookies as arrays.
1539 * If redirects were allowed and several responses were received, the data
1540 * references the last received response.
1542 PHP_METHOD(HttpRequest
, getResponseCookie
)
1545 zval
*data
, **headers
;
1546 char *cookie_name
= NULL
;
1548 getObject(http_request_object
, obj
);
1550 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1554 array_init(return_value
);
1556 data
= GET_PROP(obj
, responseData
);
1557 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1560 zval
**header
= NULL
;
1563 convert_to_array(*headers
);
1564 FOREACH_HASH_KEYVAL(pos1
, Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1565 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1566 /* several cookies? */
1567 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1571 FOREACH_HASH_VAL(pos2
, Z_ARRVAL_PP(header
), cookie
) {
1573 MAKE_STD_ZVAL(cookie_hash
);
1574 array_init(cookie_hash
);
1576 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1578 add_next_index_zval(return_value
, cookie_hash
);
1582 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1583 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1584 add_next_index_zval(return_value
, cookie_hash
);
1585 return; /* <<< FOUND >>> */
1587 zval_dtor(cookie_hash
);
1592 zval_dtor(cookie_hash
);
1599 MAKE_STD_ZVAL(cookie_hash
);
1600 array_init(cookie_hash
);
1601 convert_to_string_ex(header
);
1603 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1605 add_next_index_zval(return_value
, cookie_hash
);
1609 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1610 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1611 add_next_index_zval(return_value
, cookie_hash
);
1613 zval_dtor(cookie_hash
);
1618 zval_dtor(cookie_hash
);
1632 /* {{{ proto string HttpRequest::getResponseBody()
1634 * Get the response body after the request has been sent.
1636 * Returns a string containing the response body.
1638 * If redirects were allowed and several responses were received, the data
1639 * references the last received response.
1641 PHP_METHOD(HttpRequest
, getResponseBody
)
1647 getObject(http_request_object
, obj
);
1648 zval
*data
= GET_PROP(obj
, responseData
);
1650 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1651 RETURN_ZVAL(*body
, 1, 0);
1659 /* {{{ proto int HttpRequest::getResponseCode()
1661 * Get the response code after the request has been sent.
1663 * Returns an int representing the response code.
1665 * If redirects were allowed and several responses were received, the data
1666 * references the last received response.
1668 PHP_METHOD(HttpRequest
, getResponseCode
)
1673 getObject(http_request_object
, obj
);
1674 zval
*code
= GET_PROP(obj
, responseCode
);
1676 RETVAL_ZVAL(code
, 1, 0);
1681 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1683 * Get response info after the request has been sent.
1684 * See http_get() for a full list of returned info.
1686 * Accepts a string as optional parameter specifying the info to read.
1687 * If the parameter is empty or omitted, an associative array containing
1688 * all available info will be returned.
1690 * Returns either a scalar containing the value of the info matching name if
1691 * requested, FALSE on failure, or an associative array containing all
1694 * If redirects were allowed and several responses were received, the data
1695 * references the last received response.
1697 PHP_METHOD(HttpRequest
, getResponseInfo
)
1700 zval
*info
, **infop
;
1701 char *info_name
= NULL
;
1703 getObject(http_request_object
, obj
);
1705 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1709 info
= GET_PROP(obj
, responseInfo
);
1711 if (info_len
&& info_name
) {
1712 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1713 RETURN_ZVAL(*infop
, 1, 0);
1715 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1719 RETURN_ZVAL(info
, 1, 0);
1725 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1727 * Get the full response as HttpMessage object after the request has been sent.
1729 * Returns an HttpMessage object of the response.
1731 * If redirects were allowed and several responses were received, the data
1732 * references the last received response. Use HttpMessage::getParentMessage()
1733 * to access the data of previously received responses whithin this request
1736 * Throws HttpException.
1738 PHP_METHOD(HttpRequest
, getResponseMessage
)
1744 getObject(http_request_object
, obj
);
1746 SET_EH_THROW_HTTP();
1747 message
= GET_PROP(obj
, responseMessage
);
1748 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1749 RETVAL_OBJECT(message
);
1758 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1760 * Get sent HTTP message.
1762 * Returns an HttpMessage object representing the sent request.
1764 * If redirects were allowed and several responses were received, the data
1765 * references the last received response. Use HttpMessage::getParentMessage()
1766 * to access the data of previously sent requests whithin this request
1769 * Note that the internal request message is immutable, that means that the
1770 * request message received through HttpRequest::getRequestMessage() will
1771 * always look the same for the same request, regardless of any changes you
1772 * may have made to the returned object.
1774 * Throws HttpMalformedHeadersException, HttpEncodingException.
1776 PHP_METHOD(HttpRequest
, getRequestMessage
)
1782 getObject(http_request_object
, obj
);
1784 SET_EH_THROW_HTTP();
1785 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1786 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1793 /* {{{ proto string HttpRequest::getRawRequestMessage()
1795 * Get sent HTTP message.
1797 * Returns an HttpMessage in a form of a string
1800 PHP_METHOD(HttpRequest
, getRawRequestMessage
)
1805 getObject(http_request_object
, obj
);
1807 RETURN_PHPSTR_DUP(&obj
->request
->conv
.request
);
1812 /* {{{ proto string HttpRequest::getRawResponseMessage()
1814 * Get the entire HTTP response.
1816 * Returns the complete web server response, including the headers in a form of a string.
1819 PHP_METHOD(HttpRequest
, getRawResponseMessage
)
1824 getObject(http_request_object
, obj
);
1826 RETURN_PHPSTR_DUP(&obj
->request
->conv
.response
);
1831 /* {{{ proto HttpMessage HttpRequest::getHistory()
1833 * Get all sent requests and received responses as an HttpMessage object.
1835 * If you don't want to record history at all, set the instance variable
1836 * HttpRequest::$recoedHistory to FALSE.
1838 * Returns an HttpMessage object representing the complete request/response
1841 * The object references the last received response, use HttpMessage::getParentMessage()
1842 * to access the data of previously sent requests and received responses.
1844 * Note that the internal history is immutable, that means that any changes
1845 * you make the the message list won't affect a history message list newly
1846 * created by another call to HttpRequest::getHistory().
1848 * Throws HttpMalformedHeaderException, HttpEncodingException.
1850 PHP_METHOD(HttpRequest
, getHistory
)
1856 getObject(http_request_object
, obj
);
1858 SET_EH_THROW_HTTP();
1859 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->history
), PHPSTR_LEN(&obj
->history
)))) {
1860 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1867 /* {{{ proto void HttpRequest::clearHistory()
1869 * Clear the history.
1871 PHP_METHOD(HttpRequest
, clearHistory
)
1874 getObject(http_request_object
, obj
);
1875 phpstr_dtor(&obj
->history
);
1880 /* {{{ proto HttpMessage HttpRequest::send()
1882 * Send the HTTP request.
1884 * Returns the received response as HttpMessage object.
1886 * NOTE: While an exception may be thrown, the transfer could have succeeded
1887 * at least partially, so you might want to check the return values of various
1888 * HttpRequest::getResponse*() methods.
1890 * Throws HttpRuntimeException, HttpRequestException,
1891 * HttpMalformedHeaderException, HttpEncodingException.
1896 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
1897 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1898 * $r->addQueryData(array('category' => 3));
1901 * if ($r->getResponseCode() == 200) {
1902 * file_put_contents('local.rss', $r->getResponseBody());
1904 * } catch (HttpException $ex) {
1913 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
1914 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1915 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1916 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1918 * echo $r->send()->getBody();
1919 * } catch (HttpException $ex) {
1925 PHP_METHOD(HttpRequest
, send
)
1927 getObject(http_request_object
, obj
);
1931 SET_EH_THROW_HTTP();
1936 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1937 } else if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
1938 http_request_exec(obj
->request
);
1939 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
1940 RETVAL_OBJECT(GET_PROP(obj
, responseMessage
));
1948 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1955 * vim600: noet sw=4 ts=4 fdm=marker
1956 * vim<600: noet sw=4 ts=4