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 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
36 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
37 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
38 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
40 HTTP_BEGIN_ARGS(__construct
, 0, 0)
42 HTTP_ARG_VAL(method
, 0)
43 HTTP_ARG_VAL(options
, 0)
46 HTTP_EMPTY_ARGS(getOptions
, 0);
47 HTTP_BEGIN_ARGS(setOptions
, 0, 0)
48 HTTP_ARG_VAL(options
, 0)
51 HTTP_EMPTY_ARGS(getSslOptions
, 0);
52 HTTP_BEGIN_ARGS(setSslOptions
, 0, 0)
53 HTTP_ARG_VAL(ssl_options
, 0)
56 HTTP_EMPTY_ARGS(getHeaders
, 0);
57 HTTP_BEGIN_ARGS(setHeaders
, 0, 0)
58 HTTP_ARG_VAL(headers
, 0)
61 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
62 HTTP_ARG_VAL(headers
, 0)
65 HTTP_EMPTY_ARGS(getCookies
, 0);
66 HTTP_BEGIN_ARGS(setCookies
, 0, 0)
67 HTTP_ARG_VAL(cookies
, 0)
70 HTTP_BEGIN_ARGS(addCookies
, 0, 1)
71 HTTP_ARG_VAL(cookies
, 0)
74 HTTP_EMPTY_ARGS(getUrl
, 0);
75 HTTP_BEGIN_ARGS(setUrl
, 0, 1)
79 HTTP_EMPTY_ARGS(getMethod
, 0);
80 HTTP_BEGIN_ARGS(setMethod
, 0, 1)
81 HTTP_ARG_VAL(request_method
, 0)
84 HTTP_EMPTY_ARGS(getContentType
, 0);
85 HTTP_BEGIN_ARGS(setContentType
, 0, 1)
86 HTTP_ARG_VAL(content_type
, 0)
89 HTTP_EMPTY_ARGS(getQueryData
, 0);
90 HTTP_BEGIN_ARGS(setQueryData
, 0, 0)
91 HTTP_ARG_VAL(query_data
, 0)
94 HTTP_BEGIN_ARGS(addQueryData
, 0, 1)
95 HTTP_ARG_VAL(query_data
, 0)
98 HTTP_EMPTY_ARGS(getPostFields
, 0);
99 HTTP_BEGIN_ARGS(setPostFields
, 0, 0)
100 HTTP_ARG_VAL(post_fields
, 0)
103 HTTP_BEGIN_ARGS(addPostFields
, 0, 1)
104 HTTP_ARG_VAL(post_fields
, 0)
107 HTTP_EMPTY_ARGS(getPostFiles
, 0);
108 HTTP_BEGIN_ARGS(setPostFiles
, 0, 0)
109 HTTP_ARG_VAL(post_files
, 0)
112 HTTP_BEGIN_ARGS(addPostFile
, 0, 2)
113 HTTP_ARG_VAL(formname
, 0)
114 HTTP_ARG_VAL(filename
, 0)
115 HTTP_ARG_VAL(content_type
, 0)
118 HTTP_EMPTY_ARGS(getRawPostData
, 0);
119 HTTP_BEGIN_ARGS(setRawPostData
, 0, 0)
120 HTTP_ARG_VAL(raw_post_data
, 0)
123 HTTP_BEGIN_ARGS(addRawPostData
, 0, 1)
124 HTTP_ARG_VAL(raw_post_data
, 0)
127 HTTP_EMPTY_ARGS(getPutFile
, 0);
128 HTTP_BEGIN_ARGS(setPutFile
, 0, 0)
129 HTTP_ARG_VAL(filename
, 0)
132 HTTP_EMPTY_ARGS(getResponseData
, 0);
133 HTTP_BEGIN_ARGS(getResponseHeader
, 0, 0)
134 HTTP_ARG_VAL(name
, 0)
137 HTTP_BEGIN_ARGS(getResponseCookie
, 0, 0)
138 HTTP_ARG_VAL(name
, 0)
141 HTTP_EMPTY_ARGS(getResponseBody
, 0);
142 HTTP_EMPTY_ARGS(getResponseCode
, 0);
143 HTTP_BEGIN_ARGS(getResponseInfo
, 0, 0)
144 HTTP_ARG_VAL(name
, 0)
147 HTTP_EMPTY_ARGS(getResponseMessage
, 0);
148 HTTP_EMPTY_ARGS(getRawResponseMessage
, 0);
149 HTTP_EMPTY_ARGS(getRequestMessage
, 0);
150 HTTP_EMPTY_ARGS(getRawRequestMessage
, 0);
151 HTTP_EMPTY_ARGS(getHistory
, 0);
152 HTTP_EMPTY_ARGS(clearHistory
, 0);
153 HTTP_EMPTY_ARGS(send
, 0);
155 HTTP_BEGIN_ARGS(get
, 0, 1)
157 HTTP_ARG_VAL(options
, 0)
158 HTTP_ARG_VAL(info
, 1)
161 HTTP_BEGIN_ARGS(head
, 0, 1)
163 HTTP_ARG_VAL(options
, 0)
164 HTTP_ARG_VAL(info
, 1)
167 HTTP_BEGIN_ARGS(postData
, 0, 2)
169 HTTP_ARG_VAL(data
, 0)
170 HTTP_ARG_VAL(options
, 0)
171 HTTP_ARG_VAL(info
, 1)
174 HTTP_BEGIN_ARGS(postFields
, 0, 2)
176 HTTP_ARG_VAL(data
, 0)
177 HTTP_ARG_VAL(options
, 0)
178 HTTP_ARG_VAL(info
, 1)
181 HTTP_BEGIN_ARGS(putFile
, 0, 2)
183 HTTP_ARG_VAL(file
, 0)
184 HTTP_ARG_VAL(options
, 0)
185 HTTP_ARG_VAL(info
, 1)
188 HTTP_BEGIN_ARGS(putStream
, 0, 2)
190 HTTP_ARG_VAL(stream
, 0)
191 HTTP_ARG_VAL(options
, 0)
192 HTTP_ARG_VAL(info
, 1)
195 HTTP_BEGIN_ARGS(methodRegister
, 0, 1)
196 HTTP_ARG_VAL(method_name
, 0)
199 HTTP_BEGIN_ARGS(methodUnregister
, 0, 1)
200 HTTP_ARG_VAL(method
, 0)
203 HTTP_BEGIN_ARGS(methodName
, 0, 1)
204 HTTP_ARG_VAL(method_id
, 0)
207 HTTP_BEGIN_ARGS(methodExists
, 0, 1)
208 HTTP_ARG_VAL(method
, 0)
211 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
212 static inline void _http_request_object_declare_default_properties(TSRMLS_D
);
214 zend_class_entry
*http_request_object_ce
;
215 zend_function_entry http_request_object_fe
[] = {
216 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
218 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
219 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
220 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
221 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
223 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
224 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
225 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
227 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
228 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
229 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
231 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
232 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
234 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
235 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
237 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
238 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
240 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
241 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
242 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
244 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
245 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
246 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
248 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
249 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
250 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
252 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
253 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
254 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
256 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
257 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
261 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
262 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
263 HTTP_REQUEST_ME(getResponseCookie
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
265 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
266 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(getRawResponseMessage
, ZEND_ACC_PUBLIC
)
269 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
270 HTTP_REQUEST_ME(getRawRequestMessage
, 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(PRIVATE
, options
);
349 DCL_PROP_N(PRIVATE
, postFields
);
350 DCL_PROP_N(PRIVATE
, postFiles
);
351 DCL_PROP_N(PRIVATE
, responseInfo
);
352 DCL_PROP_N(PRIVATE
, responseData
);
353 DCL_PROP_N(PRIVATE
, responseMessage
);
354 DCL_PROP(PRIVATE
, long, responseCode
, 0);
355 DCL_PROP(PRIVATE
, long, method
, HTTP_GET
);
356 DCL_PROP(PRIVATE
, string
, url
, "");
357 DCL_PROP(PRIVATE
, string
, contentType
, "");
358 DCL_PROP(PRIVATE
, string
, rawPostData
, "");
359 DCL_PROP(PRIVATE
, string
, queryData
, "");
360 DCL_PROP(PRIVATE
, string
, putFile
, "");
362 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
366 * Request Method Constants
369 DCL_CONST(long, "METH_GET", HTTP_GET
);
370 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
371 DCL_CONST(long, "METH_POST", HTTP_POST
);
372 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
373 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
374 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
375 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
376 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
377 /* WebDAV - RFC 2518 */
378 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
379 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
380 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
381 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
382 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
383 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
384 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
385 /* WebDAV Versioning - RFC 3253 */
386 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
387 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
388 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
389 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
390 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
391 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
392 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
393 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
394 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
395 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
396 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
397 /* WebDAV Access Control - RFC 3744 */
398 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
403 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
404 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
405 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
406 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
410 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
412 http_request_object
*o
= (http_request_object
*) object
;
415 zend_hash_destroy(OBJ_PROP(o
));
416 FREE_HASHTABLE(OBJ_PROP(o
));
418 http_request_free(&o
->request
);
419 phpstr_dtor(&o
->history
);
423 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
425 STATUS status
= SUCCESS
;
427 http_request_reset(obj
->request
);
428 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, curl_easy_init(), return FAILURE
);
430 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(GET_PROP(obj
, url
)));
432 switch (obj
->request
->meth
= Z_LVAL_P(GET_PROP(obj
, method
)))
440 php_stream_statbuf ssb
;
441 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj
, putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
443 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
444 http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
454 /* check for raw post data */
455 zval
*raw_data
= GET_PROP(obj
, rawPostData
);
457 if (Z_STRLEN_P(raw_data
)) {
458 zval
*ctype
= GET_PROP(obj
, contentType
);
460 if (Z_STRLEN_P(ctype
)) {
461 zval
**headers
, *opts
= GET_PROP(obj
, options
);
463 if ((SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) && (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
466 /* only override if not already set */
467 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) && (Z_TYPE_PP(ct_header
) == IS_STRING
)) {
468 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
473 MAKE_STD_ZVAL(headers
);
475 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
476 add_assoc_zval(opts
, "headers", headers
);
480 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
481 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
484 zval
*zfields
= GET_PROP(obj
, postFields
), *zfiles
= GET_PROP(obj
, postFiles
);
488 fields
= (Z_TYPE_P(zfields
) == IS_ARRAY
) ? Z_ARRVAL_P(zfields
) : NULL
;
489 files
= (Z_TYPE_P(zfiles
) == IS_ARRAY
) ? Z_ARRVAL_P(zfiles
) : NULL
;
491 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
499 if (status
== SUCCESS
) {
500 zval
*qdata
= GET_PROP(obj
, queryData
);
501 zval
*options
= GET_PROP(obj
, options
);
503 if (Z_STRLEN_P(qdata
)) {
504 if (!strchr(obj
->request
->url
, '?')) {
505 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
507 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
509 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
512 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
518 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
522 phpstr_fix(&obj
->request
->conv
.request
);
523 phpstr_fix(&obj
->request
->conv
.response
);
525 msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
));
532 zval
*headers
, *message
, *resp
, *info
;
534 if (zval_is_true(GET_PROP(obj
, recordHistory
))) {
535 /* we need to act like a zipper, as we'll receive
536 * the requests and the responses in separate chains
539 http_message
*response
= msg
, *request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
540 http_message
*free_msg
= request
;
546 http_message_tostring(response
, &message
, &msglen
);
547 phpstr_append(&obj
->history
, message
, msglen
);
550 http_message_tostring(request
, &message
, &msglen
);
551 phpstr_append(&obj
->history
, message
, msglen
);
554 } while ((response
= response
->parent
) && (request
= request
->parent
));
556 http_message_free(&free_msg
);
557 phpstr_fix(&obj
->history
);
560 UPD_PROP(obj
, long, responseCode
, msg
->http
.info
.response
.code
);
564 MAKE_STD_ZVAL(headers
);
566 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
567 add_assoc_zval(resp
, "headers", headers
);
568 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
569 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
570 SET_PROP(obj
, responseData
, resp
);
571 zval_ptr_dtor(&resp
);
573 MAKE_STD_ZVAL(message
);
574 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
575 SET_PROP(obj
, responseMessage
, message
);
576 zval_ptr_dtor(&message
);
580 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
581 SET_PROP(obj
, responseInfo
, info
);
582 zval_ptr_dtor(&info
);
588 #define http_request_object_set_options_subr(key, ow) \
589 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
590 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
592 zval
*old_opts
, *new_opts
, *opts
, **entry
;
593 getObject(http_request_object
, obj
);
595 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
599 MAKE_STD_ZVAL(new_opts
);
600 array_init(new_opts
);
601 old_opts
= GET_PROP(obj
, options
);
602 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
603 array_copy(old_opts
, new_opts
);
606 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), key
, len
, (void **) &entry
)) {
608 zend_hash_clean(Z_ARRVAL_PP(entry
));
610 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
612 array_copy(opts
, *entry
);
614 array_merge(opts
, *entry
);
619 add_assoc_zval(new_opts
, key
, opts
);
621 SET_PROP(obj
, options
, new_opts
);
622 zval_ptr_dtor(&new_opts
);
627 #define http_request_object_get_options_subr(key) \
628 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
629 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
634 zval
*opts
, **options
;
635 getObject(http_request_object
, obj
);
637 opts
= GET_PROP(obj
, options
);
639 array_init(return_value
);
641 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
642 convert_to_array(*options
);
643 array_copy(*options
, return_value
);
649 /* ### USERLAND ### */
651 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
653 * Instantiate a new HttpRequest object.
655 * Accepts a string as optional parameter containing the target request url.
656 * Additianally accepts an optional int parameter specifying the request method
657 * to use and an associative array as optional third parameter which will be
658 * passed to HttpRequest::setOptions().
660 * Throws HttpException.
662 PHP_METHOD(HttpRequest
, __construct
)
667 zval
*options
= NULL
;
668 getObject(http_request_object
, obj
);
671 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
673 UPD_STRL(obj
, url
, URL
, URL_len
);
676 UPD_PROP(obj
, long, method
, meth
);
679 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
686 /* {{{ proto bool HttpRequest::setOptions([array options])
688 * Set the request options to use. See http_get() for a full list of available options.
690 * Accepts an array as optional parameters, wich values will overwrite the
691 * currently set request options. If the parameter is empty or mitted,
692 * the optoions of the HttpRequest object will be reset.
694 * Returns TRUE on success, or FALSE on failure.
696 PHP_METHOD(HttpRequest
, setOptions
)
701 zval
*opts
= NULL
, *old_opts
, *new_opts
, **opt
;
702 getObject(http_request_object
, obj
);
704 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
708 MAKE_STD_ZVAL(new_opts
);
709 array_init(new_opts
);
711 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
712 SET_PROP(obj
, options
, new_opts
);
716 old_opts
= GET_PROP(obj
, options
);
717 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
718 array_copy(old_opts
, new_opts
);
721 /* some options need extra attention -- thus cannot use array_merge() directly */
722 FOREACH_KEYVAL(pos
, opts
, key
, idx
, opt
) {
724 if (!strcmp(key
, "headers")) {
726 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
727 convert_to_array_ex(opt
);
728 convert_to_array(*headers
);
729 array_merge(*opt
, *headers
);
732 } else if (!strcmp(key
, "cookies")) {
734 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
735 convert_to_array_ex(opt
);
736 convert_to_array(*cookies
);
737 array_merge(*opt
, *cookies
);
740 } else if (!strcmp(key
, "ssl")) {
742 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), "ssl", sizeof("ssl"), (void **) &ssl
)) {
743 convert_to_array_ex(opt
);
744 convert_to_array(*ssl
);
745 array_merge(*opt
, *ssl
);
748 } else if ((Z_TYPE_PP(opt
) == IS_STRING
) && ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri")))) {
749 UPD_STRL(obj
, url
, Z_STRVAL_PP(opt
), Z_STRLEN_PP(opt
));
751 } else if (!strcmp(key
, "method")) {
752 if (Z_TYPE_PP(opt
) != IS_LONG
) {
753 convert_to_long_ex(opt
);
755 UPD_PROP(obj
, long, method
, Z_LVAL_PP(opt
));
760 add_assoc_zval(new_opts
, key
, *opt
);
766 SET_PROP(obj
, options
, new_opts
);
767 zval_ptr_dtor(&new_opts
);
773 /* {{{ proto array HttpRequest::getOptions()
775 * Get currently set options.
777 * Returns an associative array containing currently set options.
779 PHP_METHOD(HttpRequest
, getOptions
)
784 getObject(http_request_object
, obj
);
785 zval
*options
= GET_PROP(obj
, options
);
787 RETURN_ZVAL(options
, 1, 0);
792 /* {{{ proto bool HttpRequest::setSslOptions([array options])
796 * Accepts an associative array as parameter containing any SSL specific options.
797 * If the parameter is empty or omitted, the SSL options will be reset.
799 * Returns TRUE on success, or FALSE on failure.
801 PHP_METHOD(HttpRequest
, setSslOptions
)
803 http_request_object_set_options_subr("ssl", 1);
807 /* {{{ proto bool HttpRequest::addSslOptions(array options)
809 * Set additional SSL options.
811 * Expects an associative array as parameter containing additional SSL specific options.
813 * Returns TRUE on success, or FALSE on failure.
815 PHP_METHOD(HttpRequest
, addSslOptions
)
817 http_request_object_set_options_subr("ssl", 0);
821 /* {{{ proto array HttpRequest::getSslOtpions()
823 * Get previously set SSL options.
825 * Returns an associative array containing any previously set SSL options.
827 PHP_METHOD(HttpRequest
, getSslOptions
)
829 http_request_object_get_options_subr("ssl");
833 /* {{{ proto bool HttpRequest::addHeaders(array headers)
835 * Add request header name/value pairs.
837 * Expects an ssociative array as parameter containing additional header
840 * Returns TRUE on success, or FALSE on failure.
842 PHP_METHOD(HttpRequest
, addHeaders
)
844 http_request_object_set_options_subr("headers", 0);
847 /* {{{ proto bool HttpRequest::setHeaders([array headers])
849 * Set request header name/value pairs.
851 * Accepts an associative array as parameter containing header name/value pairs.
852 * If the parameter is empty or omitted, all previously set headers will be unset.
854 * Returns TRUE on success, or FALSE on failure.
856 PHP_METHOD(HttpRequest
, setHeaders
)
858 http_request_object_set_options_subr("headers", 1);
862 /* {{{ proto array HttpRequest::getHeaders()
864 * Get previously set request headers.
866 * Returns an associative array containing all currently set headers.
868 PHP_METHOD(HttpRequest
, getHeaders
)
870 http_request_object_get_options_subr("headers");
874 /* {{{ proto bool HttpRequest::setCookies([array cookies])
878 * Accepts an associative array as parameter containing cookie name/value pairs.
879 * If the parameter is empty or omitted, all previously set cookies will be unset.
881 * Returns TRUE on success, or FALSE on failure.
883 PHP_METHOD(HttpRequest
, setCookies
)
885 http_request_object_set_options_subr("cookies", 1);
889 /* {{{ proto bool HttpRequest::addCookies(array cookies)
893 * Expects an associative array as parameter containing any cookie name/value
896 * Returns TRUE on success, or FALSE on failure.
898 PHP_METHOD(HttpRequest
, addCookies
)
900 http_request_object_set_options_subr("cookies", 0);
904 /* {{{ proto array HttpRequest::getCookies()
906 * Get previously set cookies.
908 * Returns an associative array containing any previously set cookies.
910 PHP_METHOD(HttpRequest
, getCookies
)
912 http_request_object_get_options_subr("cookies");
916 /* {{{ proto bool HttpRequest::setUrl(string url)
918 * Set the request URL.
920 * Expects a string as parameter specifying the request url.
922 * Returns TRUE on success, or FALSE on failure.
924 PHP_METHOD(HttpRequest
, setUrl
)
928 getObject(http_request_object
, obj
);
930 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
934 UPD_STRL(obj
, url
, URL
, URL_len
);
939 /* {{{ proto string HttpRequest::getUrl()
941 * Get the previously set request URL.
943 * Returns the currently set request url as string.
945 PHP_METHOD(HttpRequest
, getUrl
)
950 getObject(http_request_object
, obj
);
951 zval
*url
= GET_PROP(obj
, url
);
953 RETURN_ZVAL(url
, 1, 0);
958 /* {{{ proto bool HttpRequest::setMethod(int request_method)
960 * Set the request method.
962 * Expects an int as parameter specifying the request method to use.
963 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
965 * Returns TRUE on success, or FALSE on failure.
967 PHP_METHOD(HttpRequest
, setMethod
)
970 getObject(http_request_object
, obj
);
972 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
976 UPD_PROP(obj
, long, method
, meth
);
981 /* {{{ proto int HttpRequest::getMethod()
983 * Get the previously set request method.
985 * Returns the currently set request method.
987 PHP_METHOD(HttpRequest
, getMethod
)
992 getObject(http_request_object
, obj
);
993 zval
*method
= GET_PROP(obj
, method
);
995 RETURN_ZVAL(method
, 1, 0);
1000 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1002 * Set the content type the post request should have.
1004 * Expects a string as parameters containing the content type of the request
1005 * (primary/secondary).
1007 * Returns TRUE on success, or FALSE if the content type does not seem to
1008 * contain a primary and a secondary part.
1010 PHP_METHOD(HttpRequest
, setContentType
)
1014 getObject(http_request_object
, obj
);
1016 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1020 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1021 UPD_STRL(obj
, contentType
, ctype
, ct_len
);
1026 /* {{{ proto string HttpRequest::getContentType()
1028 * Get the previously content type.
1030 * Returns the previously set content type as string.
1032 PHP_METHOD(HttpRequest
, getContentType
)
1037 getObject(http_request_object
, obj
);
1038 zval
*ctype
= GET_PROP(obj
, contentType
);
1040 RETURN_ZVAL(ctype
, 1, 0);
1045 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1047 * Set the URL query parameters to use, overwriting previously set query parameters.
1048 * Affects any request types.
1050 * Accepts a string or associative array parameter containing the pre-encoded
1051 * query string or to be encoded query fields. If the parameter is empty or
1052 * omitted, the query data will be unset.
1054 * Returns TRUE on success, or FALSE on failure.
1056 PHP_METHOD(HttpRequest
, setQueryData
)
1059 getObject(http_request_object
, obj
);
1061 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1065 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1066 UPD_STRL(obj
, queryData
, "", 0);
1067 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1068 char *query_data
= NULL
;
1070 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1074 UPD_PROP(obj
, string
, queryData
, query_data
);
1079 convert_to_string_ex(&qdata
);
1080 UPD_STRL(obj
, queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1081 if (orig
!= qdata
) {
1082 zval_ptr_dtor(&qdata
);
1089 /* {{{ proto string HttpRequest::getQueryData()
1091 * Get the current query data in form of an urlencoded query string.
1093 * Returns a string containing the urlencoded query.
1095 PHP_METHOD(HttpRequest
, getQueryData
)
1100 getObject(http_request_object
, obj
);
1101 zval
*qdata
= GET_PROP(obj
, queryData
);
1103 RETURN_ZVAL(qdata
, 1, 0);
1108 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1110 * Add parameters to the query parameter list, leaving previously set unchanged.
1111 * Affects any request type.
1113 * Expects an associative array as parameter containing the query fields to add.
1115 * Returns TRUE on success, or FALSE on failure.
1117 PHP_METHOD(HttpRequest
, addQueryData
)
1119 zval
*qdata
, *old_qdata
;
1120 char *query_data
= NULL
;
1121 size_t query_data_len
= 0;
1122 getObject(http_request_object
, obj
);
1124 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &qdata
)) {
1128 old_qdata
= GET_PROP(obj
, queryData
);
1130 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
)) {
1134 UPD_STRL(obj
, queryData
, query_data
, query_data_len
);
1141 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1143 * Adds POST data entries, leaving previously set unchanged, unless a
1144 * post entry with the same name already exists.
1145 * Affects only POST and custom requests.
1147 * Expects an associative array as parameter containing the post fields.
1149 * Returns TRUE on success, or FALSE on failure.
1151 PHP_METHOD(HttpRequest
, addPostFields
)
1153 zval
*post_data
, *old_post
, *new_post
;
1154 getObject(http_request_object
, obj
);
1156 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &post_data
)) {
1160 if (zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1161 MAKE_STD_ZVAL(new_post
);
1162 array_init(new_post
);
1163 old_post
= GET_PROP(obj
, postFields
);
1164 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1165 array_copy(old_post
, new_post
);
1167 array_merge(post_data
, new_post
);
1168 SET_PROP(obj
, postFields
, new_post
);
1169 zval_ptr_dtor(&new_post
);
1176 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1178 * Set the POST data entries, overwriting previously set POST data.
1179 * Affects only POST and custom requests.
1181 * Accepts an associative array as parameter containing the post fields.
1182 * If the parameter is empty or omitted, the post data will be unset.
1184 * Returns TRUE on success, or FALSE on failure.
1186 PHP_METHOD(HttpRequest
, setPostFields
)
1188 zval
*post
, *post_data
= NULL
;
1189 getObject(http_request_object
, obj
);
1191 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &post_data
)) {
1195 MAKE_STD_ZVAL(post
);
1197 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1198 array_copy(post_data
, post
);
1200 SET_PROP(obj
, postFields
, post
);
1201 zval_ptr_dtor(&post
);
1207 /* {{{ proto array HttpRequest::getPostFields()
1209 * Get previously set POST data.
1211 * Returns the currently set post fields as associative array.
1213 PHP_METHOD(HttpRequest
, getPostFields
)
1218 getObject(http_request_object
, obj
);
1219 zval
*post
= GET_PROP(obj
, postFields
);
1221 RETURN_ZVAL(post
, 1, 0);
1226 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1228 * Set raw post data to send, overwriting previously set raw post data. Don't
1229 * forget to specify a content type. Affects only POST and custom requests.
1230 * Only either post fields or raw post data can be used for each request.
1231 * Raw post data has higher precedence and will be used even if post fields
1234 * Accepts a string as parameter containing the *raw* post data.
1236 * Returns TRUE on success, or FALSE on failure.
1238 PHP_METHOD(HttpRequest
, setRawPostData
)
1240 char *raw_data
= NULL
;
1242 getObject(http_request_object
, obj
);
1244 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1252 UPD_STRL(obj
, rawPostData
, raw_data
, data_len
);
1257 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1259 * Add raw post data, leaving previously set raw post data unchanged.
1260 * Affects only POST and custom requests.
1262 * Expects a string as parameter containing the raw post data to concatenate.
1264 * Returns TRUE on success, or FALSE on failure.
1266 PHP_METHOD(HttpRequest
, addRawPostData
)
1270 getObject(http_request_object
, obj
);
1272 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1277 zval
*data
, *zdata
= GET_PROP(obj
, rawPostData
);
1281 zval_copy_ctor(data
);
1283 convert_to_string(data
);
1284 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1285 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1286 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, raw_data
, data_len
);
1287 SET_PROP(obj
, rawPostData
, data
);
1288 zval_ptr_dtor(&data
);
1295 /* {{{ proto string HttpRequest::getRawPostData()
1297 * Get previously set raw post data.
1299 * Returns a string containing the currently set raw post data.
1301 PHP_METHOD(HttpRequest
, getRawPostData
)
1306 getObject(http_request_object
, obj
);
1307 zval
*post
= GET_PROP(obj
, rawPostData
);
1309 RETURN_ZVAL(post
, 1, 0);
1314 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1316 * Add a file to the POST request, leaving prefiously set files unchanged.
1317 * Affects only POST and custom requests. Cannot be used with raw post data.
1319 * Expects a string parameter containing the form element name, and a string
1320 * paremeter containing the path to the file which should be uploaded.
1321 * Additionally accepts an optional string parameter which chould contain
1322 * the content type of the file.
1324 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1325 * primary and a secondary content type part.
1327 PHP_METHOD(HttpRequest
, addPostFile
)
1329 zval
*entry
, *old_post
, *new_post
;
1330 char *name
, *file
, *type
= NULL
;
1331 int name_len
, file_len
, type_len
= 0;
1332 getObject(http_request_object
, obj
);
1334 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1339 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1341 type
= "application/x-octetstream";
1342 type_len
= sizeof("application/x-octetstream") - 1;
1345 MAKE_STD_ZVAL(entry
);
1348 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1349 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1350 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1352 MAKE_STD_ZVAL(new_post
);
1353 array_init(new_post
);
1354 old_post
= GET_PROP(obj
, postFiles
);
1355 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1356 array_copy(old_post
, new_post
);
1358 add_next_index_zval(new_post
, entry
);
1359 SET_PROP(obj
, postFiles
, new_post
);
1360 zval_ptr_dtor(&new_post
);
1366 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1368 * Set files to post, overwriting previously set post files.
1369 * Affects only POST and requests. Cannot be used with raw post data.
1371 * Accepts an array containing the files to post. Each entry should be an
1372 * associative array with "name", "file" and "type" keys. If the parameter
1373 * is empty or omitted the post files will be unset.
1375 * Returns TRUE on success, or FALSE on failure.
1377 PHP_METHOD(HttpRequest
, setPostFiles
)
1379 zval
*files
= NULL
, *post
;
1380 getObject(http_request_object
, obj
);
1382 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!/", &files
)) {
1386 MAKE_STD_ZVAL(post
);
1388 if (files
&& (Z_TYPE_P(files
) == IS_ARRAY
)) {
1389 array_copy(files
, post
);
1391 SET_PROP(obj
, postFiles
, post
);
1397 /* {{{ proto array HttpRequest::getPostFiles()
1399 * Get all previously added POST files.
1401 * Returns an array containing currently set post files.
1403 PHP_METHOD(HttpRequest
, getPostFiles
)
1408 getObject(http_request_object
, obj
);
1410 array_init(return_value
);
1411 array_copy(GET_PROP(obj
, postFiles
), return_value
);
1416 /* {{{ proto bool HttpRequest::setPutFile([string file])
1418 * Set file to put. Affects only PUT requests.
1420 * Accepts a string as parameter referencing the path to file.
1421 * If the parameter is empty or omitted the put file will be unset.
1423 * Returns TRUE on success, or FALSE on failure.
1425 PHP_METHOD(HttpRequest
, setPutFile
)
1429 getObject(http_request_object
, obj
);
1431 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1435 UPD_STRL(obj
, putFile
, file
, file_len
);
1440 /* {{{ proto string HttpRequest::getPutFile()
1442 * Get previously set put file.
1444 * Returns a string containing the path to the currently set put file.
1446 PHP_METHOD(HttpRequest
, getPutFile
)
1451 getObject(http_request_object
, obj
);
1452 zval
*file
= GET_PROP(obj
, putFile
);
1454 RETVAL_ZVAL(file
, 1, 0);
1459 /* {{{ proto array HttpRequest::getResponseData()
1461 * Get all response data after the request has been sent.
1463 * Returns an associative array with the key "headers" containing an associative
1464 * array holding all response headers, as well as the ley "body" containing a
1465 * string with the response body.
1467 * If redirects were allowed and several responses were received, the data
1468 * references the last received response.
1470 PHP_METHOD(HttpRequest
, getResponseData
)
1475 getObject(http_request_object
, obj
);
1477 array_init(return_value
);
1478 array_copy(GET_PROP(obj
, responseData
), return_value
);
1483 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1485 * Get response header(s) after the request has been sent.
1487 * Accepts an string as optional parameter specifying a certain header to read.
1488 * If the parameter is empty or omitted all response headers will be returned.
1490 * Returns either a string with the value of the header matching name if requested,
1491 * FALSE on failure, or an associative array containing all reponse headers.
1493 * If redirects were allowed and several responses were received, the data
1494 * references the last received response.
1496 PHP_METHOD(HttpRequest
, getResponseHeader
)
1499 zval
*data
, **headers
, **header
;
1500 char *header_name
= NULL
;
1502 getObject(http_request_object
, obj
);
1504 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1508 data
= GET_PROP(obj
, responseData
);
1509 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1510 convert_to_array(*headers
);
1511 if (!header_len
|| !header_name
) {
1512 RETVAL_ZVAL(*headers
, 1, 0);
1513 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1514 RETVAL_ZVAL(*header
, 1, 0);
1525 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1527 * Get response cookie(s) after the request has been sent.
1529 * Accepts a string as optional parameter specifying the name of the cookie to read.
1530 * If the parameter is empty or omitted, an associative array with all received
1531 * cookies will be returned.
1533 * Returns either an associative array with the cookie's name, value and any
1534 * additional params of the cookie matching name if requested, FALSE on failure,
1535 * or an array containing all received cookies as arrays.
1537 * If redirects were allowed and several responses were received, the data
1538 * references the last received response.
1540 PHP_METHOD(HttpRequest
, getResponseCookie
)
1543 zval
*data
, **headers
;
1544 char *cookie_name
= NULL
;
1546 getObject(http_request_object
, obj
);
1548 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1552 array_init(return_value
);
1554 data
= GET_PROP(obj
, responseData
);
1555 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1558 zval
**header
= NULL
;
1561 convert_to_array(*headers
);
1562 FOREACH_HASH_KEYVAL(pos1
, Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1563 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1564 /* several cookies? */
1565 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1569 FOREACH_HASH_VAL(pos2
, Z_ARRVAL_PP(header
), cookie
) {
1571 MAKE_STD_ZVAL(cookie_hash
);
1572 array_init(cookie_hash
);
1574 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1576 add_next_index_zval(return_value
, cookie_hash
);
1580 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1581 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1582 add_next_index_zval(return_value
, cookie_hash
);
1583 return; /* <<< FOUND >>> */
1585 zval_dtor(cookie_hash
);
1590 zval_dtor(cookie_hash
);
1597 MAKE_STD_ZVAL(cookie_hash
);
1598 array_init(cookie_hash
);
1599 convert_to_string_ex(header
);
1601 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1603 add_next_index_zval(return_value
, cookie_hash
);
1607 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1608 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1609 add_next_index_zval(return_value
, cookie_hash
);
1611 zval_dtor(cookie_hash
);
1616 zval_dtor(cookie_hash
);
1630 /* {{{ proto string HttpRequest::getResponseBody()
1632 * Get the response body after the request has been sent.
1634 * Returns a string containing the response body.
1636 * If redirects were allowed and several responses were received, the data
1637 * references the last received response.
1639 PHP_METHOD(HttpRequest
, getResponseBody
)
1645 getObject(http_request_object
, obj
);
1646 zval
*data
= GET_PROP(obj
, responseData
);
1648 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1649 RETURN_ZVAL(*body
, 1, 0);
1657 /* {{{ proto int HttpRequest::getResponseCode()
1659 * Get the response code after the request has been sent.
1661 * Returns an int representing the response code.
1663 * If redirects were allowed and several responses were received, the data
1664 * references the last received response.
1666 PHP_METHOD(HttpRequest
, getResponseCode
)
1671 getObject(http_request_object
, obj
);
1672 zval
*code
= GET_PROP(obj
, responseCode
);
1674 RETVAL_ZVAL(code
, 1, 0);
1679 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1681 * Get response info after the request has been sent.
1682 * See http_get() for a full list of returned info.
1684 * Accepts a string as optional parameter specifying the info to read.
1685 * If the parameter is empty or omitted, an associative array containing
1686 * all available info will be returned.
1688 * Returns either a scalar containing the value of the info matching name if
1689 * requested, FALSE on failure, or an associative array containing all
1692 * If redirects were allowed and several responses were received, the data
1693 * references the last received response.
1695 PHP_METHOD(HttpRequest
, getResponseInfo
)
1698 zval
*info
, **infop
;
1699 char *info_name
= NULL
;
1701 getObject(http_request_object
, obj
);
1703 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1707 info
= GET_PROP(obj
, responseInfo
);
1709 if (info_len
&& info_name
) {
1710 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1711 RETURN_ZVAL(*infop
, 1, 0);
1713 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1717 RETURN_ZVAL(info
, 1, 0);
1723 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1725 * Get the full response as HttpMessage object after the request has been sent.
1727 * Returns an HttpMessage object of the response.
1729 * If redirects were allowed and several responses were received, the data
1730 * references the last received response. Use HttpMessage::getParentMessage()
1731 * to access the data of previously received responses whithin this request
1734 * Throws HttpException.
1736 PHP_METHOD(HttpRequest
, getResponseMessage
)
1742 getObject(http_request_object
, obj
);
1744 SET_EH_THROW_HTTP();
1745 message
= GET_PROP(obj
, responseMessage
);
1746 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1747 RETVAL_OBJECT(message
);
1756 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1758 * Get sent HTTP message.
1760 * Returns an HttpMessage object representing the sent request.
1762 * If redirects were allowed and several responses were received, the data
1763 * references the last received response. Use HttpMessage::getParentMessage()
1764 * to access the data of previously sent requests whithin this request
1767 * Note that the internal request message is immutable, that means that the
1768 * request message received through HttpRequest::getRequestMessage() will
1769 * always look the same for the same request, regardless of any changes you
1770 * may have made to the returned object.
1772 * Throws HttpMalformedHeadersException, HttpEncodingException.
1774 PHP_METHOD(HttpRequest
, getRequestMessage
)
1780 getObject(http_request_object
, obj
);
1782 SET_EH_THROW_HTTP();
1783 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1784 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1791 /* {{{ proto string HttpRequest::getRawRequestMessage()
1793 * Get sent HTTP message.
1795 * Returns an HttpMessage in a form of a string
1798 PHP_METHOD(HttpRequest
, getRawRequestMessage
)
1803 getObject(http_request_object
, obj
);
1805 RETURN_PHPSTR_DUP(&obj
->request
->conv
.request
);
1810 /* {{{ proto string HttpRequest::getRawResponseMessage()
1812 * Get the entire HTTP response.
1814 * Returns the complete web server response, including the headers in a form of a string.
1817 PHP_METHOD(HttpRequest
, getRawResponseMessage
)
1822 getObject(http_request_object
, obj
);
1824 RETURN_PHPSTR_DUP(&obj
->request
->conv
.response
);
1829 /* {{{ proto HttpMessage HttpRequest::getHistory()
1831 * Get all sent requests and received responses as an HttpMessage object.
1833 * If you don't want to record history at all, set the instance variable
1834 * HttpRequest::$recoedHistory to FALSE.
1836 * Returns an HttpMessage object representing the complete request/response
1839 * The object references the last received response, use HttpMessage::getParentMessage()
1840 * to access the data of previously sent requests and received responses.
1842 * Note that the internal history is immutable, that means that any changes
1843 * you make the the message list won't affect a history message list newly
1844 * created by another call to HttpRequest::getHistory().
1846 * Throws HttpMalformedHeaderException, HttpEncodingException.
1848 PHP_METHOD(HttpRequest
, getHistory
)
1854 getObject(http_request_object
, obj
);
1856 SET_EH_THROW_HTTP();
1857 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->history
), PHPSTR_LEN(&obj
->history
)))) {
1858 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1865 /* {{{ proto void HttpRequest::clearHistory()
1867 * Clear the history.
1869 PHP_METHOD(HttpRequest
, clearHistory
)
1872 getObject(http_request_object
, obj
);
1873 phpstr_dtor(&obj
->history
);
1878 /* {{{ proto HttpMessage HttpRequest::send()
1880 * Send the HTTP request.
1882 * Returns the received response as HttpMessage object.
1884 * NOTE: While an exception may be thrown, the transfer could have succeeded
1885 * at least partially, so you might want to check the return values of various
1886 * HttpRequest::getResponse*() methods.
1888 * Throws HttpRuntimeException, HttpRequestException,
1889 * HttpMalformedHeaderException, HttpEncodingException.
1894 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
1895 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1896 * $r->addQueryData(array('category' => 3));
1899 * if ($r->getResponseCode() == 200) {
1900 * file_put_contents('local.rss', $r->getResponseBody());
1902 * } catch (HttpException $ex) {
1911 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
1912 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1913 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1914 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1916 * echo $r->send()->getBody();
1917 * } catch (HttpException $ex) {
1923 PHP_METHOD(HttpRequest
, send
)
1925 getObject(http_request_object
, obj
);
1929 SET_EH_THROW_HTTP();
1934 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1935 } else if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
1936 http_request_exec(obj
->request
);
1937 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
1938 RETVAL_OBJECT(GET_PROP(obj
, responseMessage
));
1946 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1953 * vim600: noet sw=4 ts=4 fdm=marker
1954 * vim<600: noet sw=4 ts=4