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_BEGIN_ARGS(addSslOptions
, 0, 0)
57 HTTP_ARG_VAL(ssl_optins
, 0)
60 HTTP_EMPTY_ARGS(getHeaders
, 0);
61 HTTP_BEGIN_ARGS(setHeaders
, 0, 0)
62 HTTP_ARG_VAL(headers
, 0)
65 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
66 HTTP_ARG_VAL(headers
, 0)
69 HTTP_EMPTY_ARGS(getCookies
, 0);
70 HTTP_BEGIN_ARGS(setCookies
, 0, 0)
71 HTTP_ARG_VAL(cookies
, 0)
74 HTTP_BEGIN_ARGS(addCookies
, 0, 1)
75 HTTP_ARG_VAL(cookies
, 0)
78 HTTP_EMPTY_ARGS(getUrl
, 0);
79 HTTP_BEGIN_ARGS(setUrl
, 0, 1)
83 HTTP_EMPTY_ARGS(getMethod
, 0);
84 HTTP_BEGIN_ARGS(setMethod
, 0, 1)
85 HTTP_ARG_VAL(request_method
, 0)
88 HTTP_EMPTY_ARGS(getContentType
, 0);
89 HTTP_BEGIN_ARGS(setContentType
, 0, 1)
90 HTTP_ARG_VAL(content_type
, 0)
93 HTTP_EMPTY_ARGS(getQueryData
, 0);
94 HTTP_BEGIN_ARGS(setQueryData
, 0, 0)
95 HTTP_ARG_VAL(query_data
, 0)
98 HTTP_BEGIN_ARGS(addQueryData
, 0, 1)
99 HTTP_ARG_VAL(query_data
, 0)
102 HTTP_EMPTY_ARGS(getPostFields
, 0);
103 HTTP_BEGIN_ARGS(setPostFields
, 0, 0)
104 HTTP_ARG_VAL(post_fields
, 0)
107 HTTP_BEGIN_ARGS(addPostFields
, 0, 1)
108 HTTP_ARG_VAL(post_fields
, 0)
111 HTTP_EMPTY_ARGS(getPostFiles
, 0);
112 HTTP_BEGIN_ARGS(setPostFiles
, 0, 0)
113 HTTP_ARG_VAL(post_files
, 0)
116 HTTP_BEGIN_ARGS(addPostFile
, 0, 2)
117 HTTP_ARG_VAL(formname
, 0)
118 HTTP_ARG_VAL(filename
, 0)
119 HTTP_ARG_VAL(content_type
, 0)
122 HTTP_EMPTY_ARGS(getRawPostData
, 0);
123 HTTP_BEGIN_ARGS(setRawPostData
, 0, 0)
124 HTTP_ARG_VAL(raw_post_data
, 0)
127 HTTP_BEGIN_ARGS(addRawPostData
, 0, 1)
128 HTTP_ARG_VAL(raw_post_data
, 0)
131 HTTP_EMPTY_ARGS(getPutFile
, 0);
132 HTTP_BEGIN_ARGS(setPutFile
, 0, 0)
133 HTTP_ARG_VAL(filename
, 0)
136 HTTP_EMPTY_ARGS(getResponseData
, 0);
137 HTTP_BEGIN_ARGS(getResponseHeader
, 0, 0)
138 HTTP_ARG_VAL(name
, 0)
141 HTTP_BEGIN_ARGS(getResponseCookie
, 0, 0)
142 HTTP_ARG_VAL(name
, 0)
145 HTTP_EMPTY_ARGS(getResponseBody
, 0);
146 HTTP_EMPTY_ARGS(getResponseCode
, 0);
147 HTTP_BEGIN_ARGS(getResponseInfo
, 0, 0)
148 HTTP_ARG_VAL(name
, 0)
151 HTTP_EMPTY_ARGS(getResponseMessage
, 0);
152 HTTP_EMPTY_ARGS(getRawResponseMessage
, 0);
153 HTTP_EMPTY_ARGS(getRequestMessage
, 0);
154 HTTP_EMPTY_ARGS(getRawRequestMessage
, 0);
155 HTTP_EMPTY_ARGS(getHistory
, 0);
156 HTTP_EMPTY_ARGS(clearHistory
, 0);
157 HTTP_EMPTY_ARGS(send
, 0);
159 HTTP_BEGIN_ARGS(get
, 0, 1)
161 HTTP_ARG_VAL(options
, 0)
162 HTTP_ARG_VAL(info
, 1)
165 HTTP_BEGIN_ARGS(head
, 0, 1)
167 HTTP_ARG_VAL(options
, 0)
168 HTTP_ARG_VAL(info
, 1)
171 HTTP_BEGIN_ARGS(postData
, 0, 2)
173 HTTP_ARG_VAL(data
, 0)
174 HTTP_ARG_VAL(options
, 0)
175 HTTP_ARG_VAL(info
, 1)
178 HTTP_BEGIN_ARGS(postFields
, 0, 2)
180 HTTP_ARG_VAL(data
, 0)
181 HTTP_ARG_VAL(options
, 0)
182 HTTP_ARG_VAL(info
, 1)
185 HTTP_BEGIN_ARGS(putFile
, 0, 2)
187 HTTP_ARG_VAL(file
, 0)
188 HTTP_ARG_VAL(options
, 0)
189 HTTP_ARG_VAL(info
, 1)
192 HTTP_BEGIN_ARGS(putStream
, 0, 2)
194 HTTP_ARG_VAL(stream
, 0)
195 HTTP_ARG_VAL(options
, 0)
196 HTTP_ARG_VAL(info
, 1)
199 HTTP_BEGIN_ARGS(methodRegister
, 0, 1)
200 HTTP_ARG_VAL(method_name
, 0)
203 HTTP_BEGIN_ARGS(methodUnregister
, 0, 1)
204 HTTP_ARG_VAL(method
, 0)
207 HTTP_BEGIN_ARGS(methodName
, 0, 1)
208 HTTP_ARG_VAL(method_id
, 0)
211 HTTP_BEGIN_ARGS(methodExists
, 0, 1)
212 HTTP_ARG_VAL(method
, 0)
215 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
216 static inline void _http_request_object_declare_default_properties(TSRMLS_D
);
218 zend_class_entry
*http_request_object_ce
;
219 zend_function_entry http_request_object_fe
[] = {
220 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
222 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
223 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
224 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
225 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
226 HTTP_REQUEST_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
228 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
229 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
230 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
232 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
233 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
234 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
236 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
237 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
239 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
240 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
242 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
243 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
245 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
246 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
247 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
249 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
250 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
251 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
253 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
254 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
255 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
257 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
258 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
261 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
262 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
266 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(getResponseCookie
, ZEND_ACC_PUBLIC
)
269 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
270 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
272 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
273 HTTP_REQUEST_ME(getRawResponseMessage
, ZEND_ACC_PUBLIC
)
274 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
275 HTTP_REQUEST_ME(getRawRequestMessage
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
277 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
279 HTTP_REQUEST_ALIAS(get
, http_get
)
280 HTTP_REQUEST_ALIAS(head
, http_head
)
281 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
282 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
283 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
284 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
286 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
287 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
288 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
289 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
293 static zend_object_handlers http_request_object_handlers
;
295 PHP_MINIT_FUNCTION(http_request_object
)
297 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
298 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
302 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
304 return http_request_object_new_ex(ce
, curl_easy_init(), NULL
);
307 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
309 zend_object_value ov
;
310 http_request_object
*o
;
312 o
= ecalloc(1, sizeof(http_request_object
));
314 o
->request
= http_request_init_ex(NULL
, ch
, 0, NULL
);
315 phpstr_init(&o
->history
);
321 ALLOC_HASHTABLE(OBJ_PROP(o
));
322 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
323 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
325 ov
.handle
= putObject(http_request_object
, o
);
326 ov
.handlers
= &http_request_object_handlers
;
331 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
334 zend_object_value new_ov
;
335 http_request_object
*new_obj
;
336 getObject(http_request_object
, old_obj
);
338 old_zo
= zend_objects_get_address(this_ptr TSRMLS_CC
);
339 new_ov
= http_request_object_new_ex(old_zo
->ce
, curl_easy_duphandle(old_obj
->request
->ch
), &new_obj
);
341 zend_objects_clone_members(&new_obj
->zo
, new_ov
, old_zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
342 phpstr_append(&new_obj
->history
, old_obj
->history
.data
, old_obj
->history
.used
);
343 phpstr_append(&new_obj
->request
->conv
.request
, old_obj
->request
->conv
.request
.data
, old_obj
->request
->conv
.request
.used
);
344 phpstr_append(&new_obj
->request
->conv
.response
, old_obj
->request
->conv
.response
.data
, old_obj
->request
->conv
.response
.used
);
349 static inline void _http_request_object_declare_default_properties(TSRMLS_D
)
351 zend_class_entry
*ce
= http_request_object_ce
;
353 DCL_PROP_N(PRIVATE
, options
);
354 DCL_PROP_N(PRIVATE
, postFields
);
355 DCL_PROP_N(PRIVATE
, postFiles
);
356 DCL_PROP_N(PRIVATE
, responseInfo
);
357 DCL_PROP_N(PRIVATE
, responseData
);
358 DCL_PROP_N(PRIVATE
, responseMessage
);
359 DCL_PROP(PRIVATE
, long, responseCode
, 0);
360 DCL_PROP(PRIVATE
, long, method
, HTTP_GET
);
361 DCL_PROP(PRIVATE
, string
, url
, "");
362 DCL_PROP(PRIVATE
, string
, contentType
, "");
363 DCL_PROP(PRIVATE
, string
, rawPostData
, "");
364 DCL_PROP(PRIVATE
, string
, queryData
, "");
365 DCL_PROP(PRIVATE
, string
, putFile
, "");
367 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
371 * Request Method Constants
374 DCL_CONST(long, "METH_GET", HTTP_GET
);
375 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
376 DCL_CONST(long, "METH_POST", HTTP_POST
);
377 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
378 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
379 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
380 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
381 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
382 /* WebDAV - RFC 2518 */
383 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
384 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
385 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
386 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
387 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
388 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
389 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
390 /* WebDAV Versioning - RFC 3253 */
391 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
392 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
393 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
394 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
395 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
396 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
397 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
398 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
399 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
400 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
401 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
402 /* WebDAV Access Control - RFC 3744 */
403 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
408 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
409 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
410 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
411 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
415 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
417 http_request_object
*o
= (http_request_object
*) object
;
420 zend_hash_destroy(OBJ_PROP(o
));
421 FREE_HASHTABLE(OBJ_PROP(o
));
423 http_request_free(&o
->request
);
424 phpstr_dtor(&o
->history
);
428 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
430 STATUS status
= SUCCESS
;
432 http_request_reset(obj
->request
);
433 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, curl_easy_init(), return FAILURE
);
435 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(GET_PROP(obj
, url
)));
437 switch (obj
->request
->meth
= Z_LVAL_P(GET_PROP(obj
, method
)))
445 php_stream_statbuf ssb
;
446 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj
, putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
448 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
449 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
459 /* check for raw post data */
460 zval
*raw_data
= GET_PROP(obj
, rawPostData
);
462 if (Z_STRLEN_P(raw_data
)) {
463 zval
*ctype
= GET_PROP(obj
, contentType
);
465 if (Z_STRLEN_P(ctype
)) {
466 zval
**headers
, *opts
= GET_PROP(obj
, options
);
468 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
469 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) &&
470 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
473 /* only override if not already set */
474 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) && (Z_TYPE_PP(ct_header
) == IS_STRING
)) {
475 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
480 MAKE_STD_ZVAL(headers
);
482 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
483 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, headers
);
484 zval_ptr_dtor(&headers
);
488 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
489 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
492 zval
*zfields
= GET_PROP(obj
, postFields
), *zfiles
= GET_PROP(obj
, postFiles
);
496 fields
= (Z_TYPE_P(zfields
) == IS_ARRAY
) ? Z_ARRVAL_P(zfields
) : NULL
;
497 files
= (Z_TYPE_P(zfiles
) == IS_ARRAY
) ? Z_ARRVAL_P(zfiles
) : NULL
;
499 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
507 if (status
== SUCCESS
) {
508 zval
*qdata
= GET_PROP(obj
, queryData
);
509 zval
*options
= GET_PROP(obj
, options
);
511 if (Z_STRLEN_P(qdata
)) {
512 if (!strchr(obj
->request
->url
, '?')) {
513 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
515 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
517 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
520 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
526 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
530 phpstr_fix(&obj
->request
->conv
.request
);
531 phpstr_fix(&obj
->request
->conv
.response
);
533 msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
));
540 zval
*headers
, *message
, *resp
, *info
;
542 if (zval_is_true(GET_PROP(obj
, recordHistory
))) {
543 /* we need to act like a zipper, as we'll receive
544 * the requests and the responses in separate chains
547 http_message
*response
= msg
, *request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
548 http_message
*free_msg
= request
;
554 http_message_tostring(response
, &message
, &msglen
);
555 phpstr_append(&obj
->history
, message
, msglen
);
558 http_message_tostring(request
, &message
, &msglen
);
559 phpstr_append(&obj
->history
, message
, msglen
);
562 } while ((response
= response
->parent
) && (request
= request
->parent
));
564 http_message_free(&free_msg
);
565 phpstr_fix(&obj
->history
);
568 UPD_PROP(obj
, long, responseCode
, msg
->http
.info
.response
.code
);
572 MAKE_STD_ZVAL(headers
);
574 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
575 add_assoc_zval(resp
, "headers", headers
);
576 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
577 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
578 SET_PROP(obj
, responseData
, resp
);
579 zval_ptr_dtor(&resp
);
581 MAKE_STD_ZVAL(message
);
582 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
583 SET_PROP(obj
, responseMessage
, message
);
584 zval_ptr_dtor(&message
);
588 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
589 SET_PROP(obj
, responseInfo
, info
);
590 zval_ptr_dtor(&info
);
596 #define http_request_object_set_options_subr(key, ow) \
597 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
598 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
600 zval
*old_opts
, *new_opts
, *opts
, **entry
;
601 getObject(http_request_object
, obj
);
603 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
607 MAKE_STD_ZVAL(new_opts
);
608 array_init(new_opts
);
609 old_opts
= GET_PROP(obj
, options
);
610 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
611 array_copy(old_opts
, new_opts
);
614 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), key
, len
, (void **) &entry
)) {
616 zend_hash_clean(Z_ARRVAL_PP(entry
));
618 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
620 array_copy(opts
, *entry
);
622 array_merge(opts
, *entry
);
627 add_assoc_zval(new_opts
, key
, opts
);
629 SET_PROP(obj
, options
, new_opts
);
630 zval_ptr_dtor(&new_opts
);
635 #define http_request_object_get_options_subr(key) \
636 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
637 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
642 zval
*opts
, **options
;
643 getObject(http_request_object
, obj
);
645 opts
= GET_PROP(obj
, options
);
647 array_init(return_value
);
649 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
650 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
))) {
651 convert_to_array(*options
);
652 array_copy(*options
, return_value
);
658 /* ### USERLAND ### */
660 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
662 * Instantiate a new HttpRequest object.
664 * Accepts a string as optional parameter containing the target request url.
665 * Additianally accepts an optional int parameter specifying the request method
666 * to use and an associative array as optional third parameter which will be
667 * passed to HttpRequest::setOptions().
669 * Throws HttpException.
671 PHP_METHOD(HttpRequest
, __construct
)
676 zval
*options
= NULL
;
677 getObject(http_request_object
, obj
);
680 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
682 UPD_STRL(obj
, url
, URL
, URL_len
);
685 UPD_PROP(obj
, long, method
, meth
);
688 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
695 /* {{{ proto bool HttpRequest::setOptions([array options])
697 * Set the request options to use. See http_get() for a full list of available options.
699 * Accepts an array as optional parameters, wich values will overwrite the
700 * currently set request options. If the parameter is empty or mitted,
701 * the optoions of the HttpRequest object will be reset.
703 * Returns TRUE on success, or FALSE on failure.
705 PHP_METHOD(HttpRequest
, setOptions
)
710 zval
*opts
= NULL
, *old_opts
, *new_opts
, *add_opts
, **opt
;
711 getObject(http_request_object
, obj
);
713 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
717 MAKE_STD_ZVAL(new_opts
);
718 array_init(new_opts
);
720 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
721 SET_PROP(obj
, options
, new_opts
);
722 zval_ptr_dtor(&new_opts
);
726 MAKE_STD_ZVAL(add_opts
);
727 array_init(add_opts
);
728 /* some options need extra attention -- thus cannot use array_merge() directly */
729 FOREACH_KEYVAL(pos
, opts
, key
, idx
, opt
) {
731 if (!strcmp(key
, "headers")) {
732 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, *opt
);
733 } else if (!strcmp(key
, "cookies")) {
734 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addcookies", NULL
, *opt
);
735 } else if (!strcmp(key
, "ssl")) {
736 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addssloptions", NULL
, *opt
);
737 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
738 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "seturl", NULL
, *opt
);
739 } else if (!strcmp(key
, "method")) {
740 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setmethod", NULL
, *opt
);
743 add_assoc_zval(add_opts
, key
, *opt
);
750 old_opts
= GET_PROP(obj
, options
);
751 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
752 array_copy(old_opts
, new_opts
);
754 array_merge(add_opts
, new_opts
);
755 SET_PROP(obj
, options
, new_opts
);
756 zval_ptr_dtor(&new_opts
);
757 zval_ptr_dtor(&add_opts
);
763 /* {{{ proto array HttpRequest::getOptions()
765 * Get currently set options.
767 * Returns an associative array containing currently set options.
769 PHP_METHOD(HttpRequest
, getOptions
)
774 getObject(http_request_object
, obj
);
775 zval
*options
= GET_PROP(obj
, options
);
777 RETURN_ZVAL(options
, 1, 0);
782 /* {{{ proto bool HttpRequest::setSslOptions([array options])
786 * Accepts an associative array as parameter containing any SSL specific options.
787 * If the parameter is empty or omitted, the SSL options will be reset.
789 * Returns TRUE on success, or FALSE on failure.
791 PHP_METHOD(HttpRequest
, setSslOptions
)
793 http_request_object_set_options_subr("ssl", 1);
797 /* {{{ proto bool HttpRequest::addSslOptions(array options)
799 * Set additional SSL options.
801 * Expects an associative array as parameter containing additional SSL specific options.
803 * Returns TRUE on success, or FALSE on failure.
805 PHP_METHOD(HttpRequest
, addSslOptions
)
807 http_request_object_set_options_subr("ssl", 0);
811 /* {{{ proto array HttpRequest::getSslOtpions()
813 * Get previously set SSL options.
815 * Returns an associative array containing any previously set SSL options.
817 PHP_METHOD(HttpRequest
, getSslOptions
)
819 http_request_object_get_options_subr("ssl");
823 /* {{{ proto bool HttpRequest::addHeaders(array headers)
825 * Add request header name/value pairs.
827 * Expects an ssociative array as parameter containing additional header
830 * Returns TRUE on success, or FALSE on failure.
832 PHP_METHOD(HttpRequest
, addHeaders
)
834 http_request_object_set_options_subr("headers", 0);
837 /* {{{ proto bool HttpRequest::setHeaders([array headers])
839 * Set request header name/value pairs.
841 * Accepts an associative array as parameter containing header name/value pairs.
842 * If the parameter is empty or omitted, all previously set headers will be unset.
844 * Returns TRUE on success, or FALSE on failure.
846 PHP_METHOD(HttpRequest
, setHeaders
)
848 http_request_object_set_options_subr("headers", 1);
852 /* {{{ proto array HttpRequest::getHeaders()
854 * Get previously set request headers.
856 * Returns an associative array containing all currently set headers.
858 PHP_METHOD(HttpRequest
, getHeaders
)
860 http_request_object_get_options_subr("headers");
864 /* {{{ proto bool HttpRequest::setCookies([array cookies])
868 * Accepts an associative array as parameter containing cookie name/value pairs.
869 * If the parameter is empty or omitted, all previously set cookies will be unset.
871 * Returns TRUE on success, or FALSE on failure.
873 PHP_METHOD(HttpRequest
, setCookies
)
875 http_request_object_set_options_subr("cookies", 1);
879 /* {{{ proto bool HttpRequest::addCookies(array cookies)
883 * Expects an associative array as parameter containing any cookie name/value
886 * Returns TRUE on success, or FALSE on failure.
888 PHP_METHOD(HttpRequest
, addCookies
)
890 http_request_object_set_options_subr("cookies", 0);
894 /* {{{ proto array HttpRequest::getCookies()
896 * Get previously set cookies.
898 * Returns an associative array containing any previously set cookies.
900 PHP_METHOD(HttpRequest
, getCookies
)
902 http_request_object_get_options_subr("cookies");
906 /* {{{ proto bool HttpRequest::setUrl(string url)
908 * Set the request URL.
910 * Expects a string as parameter specifying the request url.
912 * Returns TRUE on success, or FALSE on failure.
914 PHP_METHOD(HttpRequest
, setUrl
)
918 getObject(http_request_object
, obj
);
920 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
924 UPD_STRL(obj
, url
, URL
, URL_len
);
929 /* {{{ proto string HttpRequest::getUrl()
931 * Get the previously set request URL.
933 * Returns the currently set request url as string.
935 PHP_METHOD(HttpRequest
, getUrl
)
940 getObject(http_request_object
, obj
);
941 zval
*url
= GET_PROP(obj
, url
);
943 RETURN_ZVAL(url
, 1, 0);
948 /* {{{ proto bool HttpRequest::setMethod(int request_method)
950 * Set the request method.
952 * Expects an int as parameter specifying the request method to use.
953 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
955 * Returns TRUE on success, or FALSE on failure.
957 PHP_METHOD(HttpRequest
, setMethod
)
960 getObject(http_request_object
, obj
);
962 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
966 UPD_PROP(obj
, long, method
, meth
);
971 /* {{{ proto int HttpRequest::getMethod()
973 * Get the previously set request method.
975 * Returns the currently set request method.
977 PHP_METHOD(HttpRequest
, getMethod
)
982 getObject(http_request_object
, obj
);
983 zval
*method
= GET_PROP(obj
, method
);
985 RETURN_ZVAL(method
, 1, 0);
990 /* {{{ proto bool HttpRequest::setContentType(string content_type)
992 * Set the content type the post request should have.
994 * Expects a string as parameters containing the content type of the request
995 * (primary/secondary).
997 * Returns TRUE on success, or FALSE if the content type does not seem to
998 * contain a primary and a secondary part.
1000 PHP_METHOD(HttpRequest
, setContentType
)
1004 getObject(http_request_object
, obj
);
1006 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1010 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1011 UPD_STRL(obj
, contentType
, ctype
, ct_len
);
1016 /* {{{ proto string HttpRequest::getContentType()
1018 * Get the previously content type.
1020 * Returns the previously set content type as string.
1022 PHP_METHOD(HttpRequest
, getContentType
)
1027 getObject(http_request_object
, obj
);
1028 zval
*ctype
= GET_PROP(obj
, contentType
);
1030 RETURN_ZVAL(ctype
, 1, 0);
1035 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1037 * Set the URL query parameters to use, overwriting previously set query parameters.
1038 * Affects any request types.
1040 * Accepts a string or associative array parameter containing the pre-encoded
1041 * query string or to be encoded query fields. If the parameter is empty or
1042 * omitted, the query data will be unset.
1044 * Returns TRUE on success, or FALSE on failure.
1046 PHP_METHOD(HttpRequest
, setQueryData
)
1049 getObject(http_request_object
, obj
);
1051 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1055 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1056 UPD_STRL(obj
, queryData
, "", 0);
1057 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1058 char *query_data
= NULL
;
1060 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1064 UPD_PROP(obj
, string
, queryData
, query_data
);
1069 convert_to_string_ex(&qdata
);
1070 UPD_STRL(obj
, queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1071 if (orig
!= qdata
) {
1072 zval_ptr_dtor(&qdata
);
1079 /* {{{ proto string HttpRequest::getQueryData()
1081 * Get the current query data in form of an urlencoded query string.
1083 * Returns a string containing the urlencoded query.
1085 PHP_METHOD(HttpRequest
, getQueryData
)
1090 getObject(http_request_object
, obj
);
1091 zval
*qdata
= GET_PROP(obj
, queryData
);
1093 RETURN_ZVAL(qdata
, 1, 0);
1098 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1100 * Add parameters to the query parameter list, leaving previously set unchanged.
1101 * Affects any request type.
1103 * Expects an associative array as parameter containing the query fields to add.
1105 * Returns TRUE on success, or FALSE on failure.
1107 PHP_METHOD(HttpRequest
, addQueryData
)
1109 zval
*qdata
, *old_qdata
;
1110 char *query_data
= NULL
;
1111 size_t query_data_len
= 0;
1112 getObject(http_request_object
, obj
);
1114 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &qdata
)) {
1118 old_qdata
= GET_PROP(obj
, queryData
);
1120 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
)) {
1124 UPD_STRL(obj
, queryData
, query_data
, query_data_len
);
1131 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1133 * Adds POST data entries, leaving previously set unchanged, unless a
1134 * post entry with the same name already exists.
1135 * Affects only POST and custom requests.
1137 * Expects an associative array as parameter containing the post fields.
1139 * Returns TRUE on success, or FALSE on failure.
1141 PHP_METHOD(HttpRequest
, addPostFields
)
1143 zval
*post_data
, *old_post
, *new_post
;
1144 getObject(http_request_object
, obj
);
1146 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &post_data
)) {
1150 if (zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1151 MAKE_STD_ZVAL(new_post
);
1152 array_init(new_post
);
1153 old_post
= GET_PROP(obj
, postFields
);
1154 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1155 array_copy(old_post
, new_post
);
1157 array_merge(post_data
, new_post
);
1158 SET_PROP(obj
, postFields
, new_post
);
1159 zval_ptr_dtor(&new_post
);
1166 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1168 * Set the POST data entries, overwriting previously set POST data.
1169 * Affects only POST and custom requests.
1171 * Accepts an associative array as parameter containing the post fields.
1172 * If the parameter is empty or omitted, the post data will be unset.
1174 * Returns TRUE on success, or FALSE on failure.
1176 PHP_METHOD(HttpRequest
, setPostFields
)
1178 zval
*post
, *post_data
= NULL
;
1179 getObject(http_request_object
, obj
);
1181 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &post_data
)) {
1185 MAKE_STD_ZVAL(post
);
1187 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1188 array_copy(post_data
, post
);
1190 SET_PROP(obj
, postFields
, post
);
1191 zval_ptr_dtor(&post
);
1197 /* {{{ proto array HttpRequest::getPostFields()
1199 * Get previously set POST data.
1201 * Returns the currently set post fields as associative array.
1203 PHP_METHOD(HttpRequest
, getPostFields
)
1208 getObject(http_request_object
, obj
);
1209 zval
*post
= GET_PROP(obj
, postFields
);
1211 RETURN_ZVAL(post
, 1, 0);
1216 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1218 * Set raw post data to send, overwriting previously set raw post data. Don't
1219 * forget to specify a content type. Affects only POST and custom requests.
1220 * Only either post fields or raw post data can be used for each request.
1221 * Raw post data has higher precedence and will be used even if post fields
1224 * Accepts a string as parameter containing the *raw* post data.
1226 * Returns TRUE on success, or FALSE on failure.
1228 PHP_METHOD(HttpRequest
, setRawPostData
)
1230 char *raw_data
= NULL
;
1232 getObject(http_request_object
, obj
);
1234 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1242 UPD_STRL(obj
, rawPostData
, raw_data
, data_len
);
1247 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1249 * Add raw post data, leaving previously set raw post data unchanged.
1250 * Affects only POST and custom requests.
1252 * Expects a string as parameter containing the raw post data to concatenate.
1254 * Returns TRUE on success, or FALSE on failure.
1256 PHP_METHOD(HttpRequest
, addRawPostData
)
1260 getObject(http_request_object
, obj
);
1262 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1267 zval
*data
= zval_copy(IS_STRING
, GET_PROP(obj
, rawPostData
));
1269 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1270 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1271 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, raw_data
, data_len
);
1272 SET_PROP(obj
, rawPostData
, data
);
1280 /* {{{ proto string HttpRequest::getRawPostData()
1282 * Get previously set raw post data.
1284 * Returns a string containing the currently set raw post data.
1286 PHP_METHOD(HttpRequest
, getRawPostData
)
1291 getObject(http_request_object
, obj
);
1292 zval
*post
= GET_PROP(obj
, rawPostData
);
1294 RETURN_ZVAL(post
, 1, 0);
1299 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1301 * Add a file to the POST request, leaving prefiously set files unchanged.
1302 * Affects only POST and custom requests. Cannot be used with raw post data.
1304 * Expects a string parameter containing the form element name, and a string
1305 * paremeter containing the path to the file which should be uploaded.
1306 * Additionally accepts an optional string parameter which chould contain
1307 * the content type of the file.
1309 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1310 * primary and a secondary content type part.
1312 PHP_METHOD(HttpRequest
, addPostFile
)
1314 zval
*entry
, *old_post
, *new_post
;
1315 char *name
, *file
, *type
= NULL
;
1316 int name_len
, file_len
, type_len
= 0;
1317 getObject(http_request_object
, obj
);
1319 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1324 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1326 type
= "application/x-octetstream";
1327 type_len
= sizeof("application/x-octetstream") - 1;
1330 MAKE_STD_ZVAL(entry
);
1333 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1334 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1335 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1337 MAKE_STD_ZVAL(new_post
);
1338 array_init(new_post
);
1339 old_post
= GET_PROP(obj
, postFiles
);
1340 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1341 array_copy(old_post
, new_post
);
1343 add_next_index_zval(new_post
, entry
);
1344 SET_PROP(obj
, postFiles
, new_post
);
1345 zval_ptr_dtor(&new_post
);
1351 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1353 * Set files to post, overwriting previously set post files.
1354 * Affects only POST and requests. Cannot be used with raw post data.
1356 * Accepts an array containing the files to post. Each entry should be an
1357 * associative array with "name", "file" and "type" keys. If the parameter
1358 * is empty or omitted the post files will be unset.
1360 * Returns TRUE on success, or FALSE on failure.
1362 PHP_METHOD(HttpRequest
, setPostFiles
)
1364 zval
*files
= NULL
, *post
;
1365 getObject(http_request_object
, obj
);
1367 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!/", &files
)) {
1371 MAKE_STD_ZVAL(post
);
1373 if (files
&& (Z_TYPE_P(files
) == IS_ARRAY
)) {
1374 array_copy(files
, post
);
1376 SET_PROP(obj
, postFiles
, post
);
1377 zval_ptr_dtor(&post
);
1383 /* {{{ proto array HttpRequest::getPostFiles()
1385 * Get all previously added POST files.
1387 * Returns an array containing currently set post files.
1389 PHP_METHOD(HttpRequest
, getPostFiles
)
1394 getObject(http_request_object
, obj
);
1395 zval
*files
= GET_PROP(obj
, postFiles
);
1397 RETURN_ZVAL(files
, 1, 0);
1402 /* {{{ proto bool HttpRequest::setPutFile([string file])
1404 * Set file to put. Affects only PUT requests.
1406 * Accepts a string as parameter referencing the path to file.
1407 * If the parameter is empty or omitted the put file will be unset.
1409 * Returns TRUE on success, or FALSE on failure.
1411 PHP_METHOD(HttpRequest
, setPutFile
)
1415 getObject(http_request_object
, obj
);
1417 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1421 UPD_STRL(obj
, putFile
, file
, file_len
);
1426 /* {{{ proto string HttpRequest::getPutFile()
1428 * Get previously set put file.
1430 * Returns a string containing the path to the currently set put file.
1432 PHP_METHOD(HttpRequest
, getPutFile
)
1437 getObject(http_request_object
, obj
);
1438 zval
*file
= GET_PROP(obj
, putFile
);
1440 RETVAL_ZVAL(file
, 1, 0);
1445 /* {{{ proto array HttpRequest::getResponseData()
1447 * Get all response data after the request has been sent.
1449 * Returns an associative array with the key "headers" containing an associative
1450 * array holding all response headers, as well as the ley "body" containing a
1451 * string with the response body.
1453 * If redirects were allowed and several responses were received, the data
1454 * references the last received response.
1456 PHP_METHOD(HttpRequest
, getResponseData
)
1461 getObject(http_request_object
, obj
);
1462 zval
*data
= GET_PROP(obj
, responseData
);
1464 RETURN_ZVAL(data
, 1, 0);
1469 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1471 * Get response header(s) after the request has been sent.
1473 * Accepts an string as optional parameter specifying a certain header to read.
1474 * If the parameter is empty or omitted all response headers will be returned.
1476 * Returns either a string with the value of the header matching name if requested,
1477 * FALSE on failure, or an associative array containing all reponse headers.
1479 * If redirects were allowed and several responses were received, the data
1480 * references the last received response.
1482 PHP_METHOD(HttpRequest
, getResponseHeader
)
1485 zval
*data
, **headers
, **header
;
1486 char *header_name
= NULL
;
1488 getObject(http_request_object
, obj
);
1490 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1494 data
= GET_PROP(obj
, responseData
);
1495 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1496 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) &&
1497 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1498 if (!header_len
|| !header_name
) {
1499 RETVAL_ZVAL(*headers
, 1, 0);
1500 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1501 RETVAL_ZVAL(*header
, 1, 0);
1512 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1514 * Get response cookie(s) after the request has been sent.
1516 * Accepts a string as optional parameter specifying the name of the cookie to read.
1517 * If the parameter is empty or omitted, an associative array with all received
1518 * cookies will be returned.
1520 * Returns either an associative array with the cookie's name, value and any
1521 * additional params of the cookie matching name if requested, FALSE on failure,
1522 * or an array containing all received cookies as arrays.
1524 * If redirects were allowed and several responses were received, the data
1525 * references the last received response.
1527 PHP_METHOD(HttpRequest
, getResponseCookie
)
1530 zval
*data
, **headers
;
1531 char *cookie_name
= NULL
;
1533 getObject(http_request_object
, obj
);
1535 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1539 array_init(return_value
);
1541 data
= GET_PROP(obj
, responseData
);
1542 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1543 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) &&
1544 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1547 zval
**header
= NULL
;
1550 convert_to_array(*headers
);
1551 FOREACH_HASH_KEYVAL(pos1
, Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1552 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1553 /* several cookies? */
1554 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1558 FOREACH_HASH_VAL(pos2
, Z_ARRVAL_PP(header
), cookie
) {
1560 MAKE_STD_ZVAL(cookie_hash
);
1561 array_init(cookie_hash
);
1563 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1565 add_next_index_zval(return_value
, cookie_hash
);
1569 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1570 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1571 add_next_index_zval(return_value
, cookie_hash
);
1572 return; /* <<< FOUND >>> */
1574 zval_dtor(cookie_hash
);
1579 zval_dtor(cookie_hash
);
1586 MAKE_STD_ZVAL(cookie_hash
);
1587 array_init(cookie_hash
);
1588 convert_to_string_ex(header
);
1590 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1592 add_next_index_zval(return_value
, cookie_hash
);
1596 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1597 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1598 add_next_index_zval(return_value
, cookie_hash
);
1600 zval_dtor(cookie_hash
);
1605 zval_dtor(cookie_hash
);
1619 /* {{{ proto string HttpRequest::getResponseBody()
1621 * Get the response body after the request has been sent.
1623 * Returns a string containing the response body.
1625 * If redirects were allowed and several responses were received, the data
1626 * references the last received response.
1628 PHP_METHOD(HttpRequest
, getResponseBody
)
1634 getObject(http_request_object
, obj
);
1635 zval
*data
= GET_PROP(obj
, responseData
);
1637 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1638 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
))) {
1639 RETURN_ZVAL(*body
, 1, 0);
1647 /* {{{ proto int HttpRequest::getResponseCode()
1649 * Get the response code after the request has been sent.
1651 * Returns an int representing the response code.
1653 * If redirects were allowed and several responses were received, the data
1654 * references the last received response.
1656 PHP_METHOD(HttpRequest
, getResponseCode
)
1661 getObject(http_request_object
, obj
);
1662 zval
*code
= GET_PROP(obj
, responseCode
);
1664 RETVAL_ZVAL(code
, 1, 0);
1669 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1671 * Get response info after the request has been sent.
1672 * See http_get() for a full list of returned info.
1674 * Accepts a string as optional parameter specifying the info to read.
1675 * If the parameter is empty or omitted, an associative array containing
1676 * all available info will be returned.
1678 * Returns either a scalar containing the value of the info matching name if
1679 * requested, FALSE on failure, or an associative array containing all
1682 * If redirects were allowed and several responses were received, the data
1683 * references the last received response.
1685 PHP_METHOD(HttpRequest
, getResponseInfo
)
1688 zval
*info
, **infop
;
1689 char *info_name
= NULL
;
1691 getObject(http_request_object
, obj
);
1693 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1697 info
= GET_PROP(obj
, responseInfo
);
1699 if (Z_TYPE_P(info
) != IS_ARRAY
) {
1703 if (info_len
&& info_name
) {
1704 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1705 RETURN_ZVAL(*infop
, 1, 0);
1707 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1711 RETURN_ZVAL(info
, 1, 0);
1717 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1719 * Get the full response as HttpMessage object after the request has been sent.
1721 * Returns an HttpMessage object of the response.
1723 * If redirects were allowed and several responses were received, the data
1724 * references the last received response. Use HttpMessage::getParentMessage()
1725 * to access the data of previously received responses whithin this request
1728 * Throws HttpException.
1730 PHP_METHOD(HttpRequest
, getResponseMessage
)
1736 getObject(http_request_object
, obj
);
1738 SET_EH_THROW_HTTP();
1739 message
= GET_PROP(obj
, responseMessage
);
1740 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1741 RETVAL_OBJECT(message
);
1750 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1752 * Get sent HTTP message.
1754 * Returns an HttpMessage object representing the sent request.
1756 * If redirects were allowed and several responses were received, the data
1757 * references the last received response. Use HttpMessage::getParentMessage()
1758 * to access the data of previously sent requests whithin this request
1761 * Note that the internal request message is immutable, that means that the
1762 * request message received through HttpRequest::getRequestMessage() will
1763 * always look the same for the same request, regardless of any changes you
1764 * may have made to the returned object.
1766 * Throws HttpMalformedHeadersException, HttpEncodingException.
1768 PHP_METHOD(HttpRequest
, getRequestMessage
)
1774 getObject(http_request_object
, obj
);
1776 SET_EH_THROW_HTTP();
1777 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1778 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1785 /* {{{ proto string HttpRequest::getRawRequestMessage()
1787 * Get sent HTTP message.
1789 * Returns an HttpMessage in a form of a string
1792 PHP_METHOD(HttpRequest
, getRawRequestMessage
)
1797 getObject(http_request_object
, obj
);
1799 RETURN_PHPSTR_DUP(&obj
->request
->conv
.request
);
1804 /* {{{ proto string HttpRequest::getRawResponseMessage()
1806 * Get the entire HTTP response.
1808 * Returns the complete web server response, including the headers in a form of a string.
1811 PHP_METHOD(HttpRequest
, getRawResponseMessage
)
1816 getObject(http_request_object
, obj
);
1818 RETURN_PHPSTR_DUP(&obj
->request
->conv
.response
);
1823 /* {{{ proto HttpMessage HttpRequest::getHistory()
1825 * Get all sent requests and received responses as an HttpMessage object.
1827 * If you don't want to record history at all, set the instance variable
1828 * HttpRequest::$recoedHistory to FALSE.
1830 * Returns an HttpMessage object representing the complete request/response
1833 * The object references the last received response, use HttpMessage::getParentMessage()
1834 * to access the data of previously sent requests and received responses.
1836 * Note that the internal history is immutable, that means that any changes
1837 * you make the the message list won't affect a history message list newly
1838 * created by another call to HttpRequest::getHistory().
1840 * Throws HttpMalformedHeaderException, HttpEncodingException.
1842 PHP_METHOD(HttpRequest
, getHistory
)
1848 getObject(http_request_object
, obj
);
1850 SET_EH_THROW_HTTP();
1851 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->history
), PHPSTR_LEN(&obj
->history
)))) {
1852 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1859 /* {{{ proto void HttpRequest::clearHistory()
1861 * Clear the history.
1863 PHP_METHOD(HttpRequest
, clearHistory
)
1866 getObject(http_request_object
, obj
);
1867 phpstr_dtor(&obj
->history
);
1872 /* {{{ proto HttpMessage HttpRequest::send()
1874 * Send the HTTP request.
1876 * Returns the received response as HttpMessage object.
1878 * NOTE: While an exception may be thrown, the transfer could have succeeded
1879 * at least partially, so you might want to check the return values of various
1880 * HttpRequest::getResponse*() methods.
1882 * Throws HttpRuntimeException, HttpRequestException,
1883 * HttpMalformedHeaderException, HttpEncodingException.
1888 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
1889 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1890 * $r->addQueryData(array('category' => 3));
1893 * if ($r->getResponseCode() == 200) {
1894 * file_put_contents('local.rss', $r->getResponseBody());
1896 * } catch (HttpException $ex) {
1905 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
1906 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1907 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1908 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1910 * echo $r->send()->getBody();
1911 * } catch (HttpException $ex) {
1917 PHP_METHOD(HttpRequest
, send
)
1919 getObject(http_request_object
, obj
);
1923 SET_EH_THROW_HTTP();
1928 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1929 } else if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
1930 http_request_exec(obj
->request
);
1931 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
1932 RETVAL_OBJECT(GET_PROP(obj
, responseMessage
));
1940 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1947 * vim600: noet sw=4 ts=4 fdm=marker
1948 * vim<600: noet sw=4 ts=4