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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_CURL
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
20 #include "zend_interfaces.h"
22 #include "php_http_api.h"
23 #include "php_http_exception_object.h"
24 #include "php_http_message_api.h"
25 #include "php_http_message_object.h"
26 #include "php_http_request_api.h"
27 #include "php_http_request_object.h"
28 #include "php_http_request_pool_api.h"
29 #include "php_http_url_api.h"
31 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, 0, req_args)
32 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpRequest, method, 0)
33 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
34 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
36 HTTP_BEGIN_ARGS(__construct
, 0)
38 HTTP_ARG_VAL(method
, 0)
39 HTTP_ARG_VAL(options
, 0)
42 HTTP_EMPTY_ARGS(getOptions
);
43 HTTP_BEGIN_ARGS(setOptions
, 0)
44 HTTP_ARG_VAL(options
, 0)
47 HTTP_EMPTY_ARGS(getSslOptions
);
48 HTTP_BEGIN_ARGS(setSslOptions
, 0)
49 HTTP_ARG_VAL(ssl_options
, 0)
52 HTTP_BEGIN_ARGS(addSslOptions
, 0)
53 HTTP_ARG_VAL(ssl_optins
, 0)
56 HTTP_EMPTY_ARGS(getHeaders
);
57 HTTP_BEGIN_ARGS(setHeaders
, 0)
58 HTTP_ARG_VAL(headers
, 0)
61 HTTP_BEGIN_ARGS(addHeaders
, 1)
62 HTTP_ARG_VAL(headers
, 0)
65 HTTP_EMPTY_ARGS(getCookies
);
66 HTTP_BEGIN_ARGS(setCookies
, 0)
67 HTTP_ARG_VAL(cookies
, 0)
70 HTTP_BEGIN_ARGS(addCookies
, 1)
71 HTTP_ARG_VAL(cookies
, 0)
74 HTTP_EMPTY_ARGS(getUrl
);
75 HTTP_BEGIN_ARGS(setUrl
, 1)
79 HTTP_EMPTY_ARGS(getMethod
);
80 HTTP_BEGIN_ARGS(setMethod
, 1)
81 HTTP_ARG_VAL(request_method
, 0)
84 HTTP_EMPTY_ARGS(getContentType
);
85 HTTP_BEGIN_ARGS(setContentType
, 1)
86 HTTP_ARG_VAL(content_type
, 0)
89 HTTP_EMPTY_ARGS(getQueryData
);
90 HTTP_BEGIN_ARGS(setQueryData
, 0)
91 HTTP_ARG_VAL(query_data
, 0)
94 HTTP_BEGIN_ARGS(addQueryData
, 1)
95 HTTP_ARG_VAL(query_data
, 0)
98 HTTP_EMPTY_ARGS(getPostFields
);
99 HTTP_BEGIN_ARGS(setPostFields
, 0)
100 HTTP_ARG_VAL(post_fields
, 0)
103 HTTP_BEGIN_ARGS(addPostFields
, 1)
104 HTTP_ARG_VAL(post_fields
, 0)
107 HTTP_EMPTY_ARGS(getPostFiles
);
108 HTTP_BEGIN_ARGS(setPostFiles
, 0)
109 HTTP_ARG_VAL(post_files
, 0)
112 HTTP_BEGIN_ARGS(addPostFile
, 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
);
119 HTTP_BEGIN_ARGS(setRawPostData
, 0)
120 HTTP_ARG_VAL(raw_post_data
, 0)
123 HTTP_BEGIN_ARGS(addRawPostData
, 1)
124 HTTP_ARG_VAL(raw_post_data
, 0)
127 HTTP_EMPTY_ARGS(getPutFile
);
128 HTTP_BEGIN_ARGS(setPutFile
, 0)
129 HTTP_ARG_VAL(filename
, 0)
132 HTTP_EMPTY_ARGS(getResponseData
);
133 HTTP_BEGIN_ARGS(getResponseHeader
, 0)
134 HTTP_ARG_VAL(name
, 0)
137 HTTP_BEGIN_ARGS(getResponseCookie
, 0)
138 HTTP_ARG_VAL(name
, 0)
141 HTTP_EMPTY_ARGS(getResponseBody
);
142 HTTP_EMPTY_ARGS(getResponseCode
);
143 HTTP_EMPTY_ARGS(getResponseStatus
);
144 HTTP_BEGIN_ARGS(getResponseInfo
, 0)
145 HTTP_ARG_VAL(name
, 0)
148 HTTP_EMPTY_ARGS(getResponseMessage
);
149 HTTP_EMPTY_ARGS(getRawResponseMessage
);
150 HTTP_EMPTY_ARGS(getRequestMessage
);
151 HTTP_EMPTY_ARGS(getRawRequestMessage
);
152 HTTP_EMPTY_ARGS(getHistory
);
153 HTTP_EMPTY_ARGS(clearHistory
);
154 HTTP_EMPTY_ARGS(send
);
156 HTTP_BEGIN_ARGS(get
, 1)
158 HTTP_ARG_VAL(options
, 0)
159 HTTP_ARG_VAL(info
, 1)
162 HTTP_BEGIN_ARGS(head
, 1)
164 HTTP_ARG_VAL(options
, 0)
165 HTTP_ARG_VAL(info
, 1)
168 HTTP_BEGIN_ARGS(postData
, 2)
170 HTTP_ARG_VAL(data
, 0)
171 HTTP_ARG_VAL(options
, 0)
172 HTTP_ARG_VAL(info
, 1)
175 HTTP_BEGIN_ARGS(postFields
, 2)
177 HTTP_ARG_VAL(data
, 0)
178 HTTP_ARG_VAL(options
, 0)
179 HTTP_ARG_VAL(info
, 1)
182 HTTP_BEGIN_ARGS(putFile
, 2)
184 HTTP_ARG_VAL(file
, 0)
185 HTTP_ARG_VAL(options
, 0)
186 HTTP_ARG_VAL(info
, 1)
189 HTTP_BEGIN_ARGS(putStream
, 2)
191 HTTP_ARG_VAL(stream
, 0)
192 HTTP_ARG_VAL(options
, 0)
193 HTTP_ARG_VAL(info
, 1)
196 HTTP_BEGIN_ARGS(methodRegister
, 1)
197 HTTP_ARG_VAL(method_name
, 0)
200 HTTP_BEGIN_ARGS(methodUnregister
, 1)
201 HTTP_ARG_VAL(method
, 0)
204 HTTP_BEGIN_ARGS(methodName
, 1)
205 HTTP_ARG_VAL(method_id
, 0)
208 HTTP_BEGIN_ARGS(methodExists
, 1)
209 HTTP_ARG_VAL(method
, 0)
212 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
213 static inline void _http_request_object_declare_default_properties(TSRMLS_D
);
215 #define OBJ_PROP_CE http_request_object_ce
216 zend_class_entry
*http_request_object_ce
;
217 zend_function_entry http_request_object_fe
[] = {
218 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
220 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
221 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
222 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
223 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
224 HTTP_REQUEST_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
226 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
227 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
228 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
230 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
231 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
232 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
234 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
235 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
237 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
238 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
240 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
241 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
243 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
244 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
245 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
247 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
248 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
249 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
251 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
252 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
253 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
255 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
256 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
257 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
260 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
262 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
265 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
266 HTTP_REQUEST_ME(getResponseCookie
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
269 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
270 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
272 HTTP_REQUEST_ME(getRawResponseMessage
, ZEND_ACC_PUBLIC
)
273 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
274 HTTP_REQUEST_ME(getRawRequestMessage
, ZEND_ACC_PUBLIC
)
275 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
278 HTTP_REQUEST_ALIAS(get
, http_get
)
279 HTTP_REQUEST_ALIAS(head
, http_head
)
280 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
281 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
282 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
283 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
285 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
286 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
287 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
288 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
292 static zend_object_handlers http_request_object_handlers
;
294 PHP_MINIT_FUNCTION(http_request_object
)
296 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
297 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
301 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
303 return http_request_object_new_ex(ce
, NULL
, NULL
);
306 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
308 zend_object_value ov
;
309 http_request_object
*o
;
311 o
= ecalloc(1, sizeof(http_request_object
));
313 o
->request
= http_request_init_ex(NULL
, ch
, 0, NULL
);
314 phpstr_init(&o
->history
);
320 ALLOC_HASHTABLE(OBJ_PROP(o
));
321 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
322 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
324 ov
.handle
= putObject(http_request_object
, o
);
325 ov
.handlers
= &http_request_object_handlers
;
330 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
333 zend_object_value new_ov
;
334 http_request_object
*new_obj
;
335 getObject(http_request_object
, old_obj
);
337 old_zo
= zend_objects_get_address(this_ptr TSRMLS_CC
);
338 new_ov
= http_request_object_new_ex(old_zo
->ce
, NULL
, &new_obj
);
339 if (old_obj
->request
->ch
) {
340 http_curl_init_ex(curl_easy_duphandle(old_obj
->request
->ch
), new_obj
->request
);
343 zend_objects_clone_members(&new_obj
->zo
, new_ov
, old_zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
344 phpstr_append(&new_obj
->history
, old_obj
->history
.data
, old_obj
->history
.used
);
345 phpstr_append(&new_obj
->request
->conv
.request
, old_obj
->request
->conv
.request
.data
, old_obj
->request
->conv
.request
.used
);
346 phpstr_append(&new_obj
->request
->conv
.response
, old_obj
->request
->conv
.response
.data
, old_obj
->request
->conv
.response
.used
);
351 static inline void _http_request_object_declare_default_properties(TSRMLS_D
)
353 zend_class_entry
*ce
= http_request_object_ce
;
355 DCL_PROP_N(PRIVATE
, options
);
356 DCL_PROP_N(PRIVATE
, postFields
);
357 DCL_PROP_N(PRIVATE
, postFiles
);
358 DCL_PROP_N(PRIVATE
, responseInfo
);
359 DCL_PROP_N(PRIVATE
, responseData
);
360 DCL_PROP_N(PRIVATE
, responseMessage
);
361 DCL_PROP(PRIVATE
, long, responseCode
, 0);
362 DCL_PROP(PRIVATE
, string
, responseStatus
, "");
363 DCL_PROP(PRIVATE
, long, method
, HTTP_GET
);
364 DCL_PROP(PRIVATE
, string
, url
, "");
365 DCL_PROP(PRIVATE
, string
, contentType
, "");
366 DCL_PROP(PRIVATE
, string
, rawPostData
, "");
367 DCL_PROP(PRIVATE
, string
, queryData
, "");
368 DCL_PROP(PRIVATE
, string
, putFile
, "");
370 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
374 * Request Method Constants
377 DCL_CONST(long, "METH_GET", HTTP_GET
);
378 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
379 DCL_CONST(long, "METH_POST", HTTP_POST
);
380 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
381 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
382 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
383 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
384 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
385 /* WebDAV - RFC 2518 */
386 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
387 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
388 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
389 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
390 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
391 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
392 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
393 /* WebDAV Versioning - RFC 3253 */
394 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
395 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
396 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
397 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
398 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
399 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
400 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
401 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
402 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
403 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
404 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
405 /* WebDAV Access Control - RFC 3744 */
406 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
408 /* cURL HTTP protocol versions */
409 DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0
);
410 DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1
);
411 DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE
);
416 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
417 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
418 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
419 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
423 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
425 http_request_object
*o
= (http_request_object
*) object
;
428 zend_hash_destroy(OBJ_PROP(o
));
429 FREE_HASHTABLE(OBJ_PROP(o
));
431 http_request_free(&o
->request
);
432 phpstr_dtor(&o
->history
);
436 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
438 STATUS status
= SUCCESS
;
440 http_request_reset(obj
->request
);
441 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, http_curl_init(obj
->request
), return FAILURE
);
443 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(GET_PROP(url
)));
445 switch (obj
->request
->meth
= Z_LVAL_P(GET_PROP(method
)))
453 php_stream_statbuf ssb
;
454 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
456 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
457 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
467 /* check for raw post data */
468 zval
*raw_data
= GET_PROP(rawPostData
);
470 if (Z_STRLEN_P(raw_data
)) {
471 zval
*ctype
= GET_PROP(contentType
);
473 if (Z_STRLEN_P(ctype
)) {
474 zval
**headers
, *opts
= GET_PROP(options
);
476 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
477 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) &&
478 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
481 /* only override if not already set */
482 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) && (Z_TYPE_PP(ct_header
) == IS_STRING
)) {
483 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
488 MAKE_STD_ZVAL(headers
);
490 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
491 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, headers
);
492 zval_ptr_dtor(&headers
);
496 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
497 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
500 zval
*zfields
= GET_PROP(postFields
), *zfiles
= GET_PROP(postFiles
);
504 fields
= (Z_TYPE_P(zfields
) == IS_ARRAY
) ? Z_ARRVAL_P(zfields
) : NULL
;
505 files
= (Z_TYPE_P(zfiles
) == IS_ARRAY
) ? Z_ARRVAL_P(zfiles
) : NULL
;
507 if ((fields
&& zend_hash_num_elements(fields
)) || (files
&& zend_hash_num_elements(files
))) {
508 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
517 if (status
== SUCCESS
) {
518 zval
*qdata
= GET_PROP(queryData
);
519 zval
*options
= GET_PROP(options
);
521 if (Z_STRLEN_P(qdata
)) {
522 if (!strchr(obj
->request
->url
, '?')) {
523 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
525 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
527 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
530 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
532 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
533 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onprogress", sizeof("onprogress"))) {
536 if ( (Z_TYPE_P(options
) != IS_ARRAY
)
537 || (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(options
), "onprogress", sizeof("onprogress"), (void **) &entry
)
538 || (!zval_is_true(*entry
)))) {
541 ZVAL_ADDREF(getThis());
542 add_next_index_zval(pcb
, getThis());
543 add_next_index_stringl(pcb
, "onprogress", lenof("onprogress"), 1);
544 http_request_set_progress_callback(obj
->request
, pcb
);
553 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
557 phpstr_fix(&obj
->request
->conv
.request
);
558 phpstr_fix(&obj
->request
->conv
.response
);
560 msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
));
567 zval
*headers
, *message
, *resp
, *info
;
569 if (zval_is_true(GET_PROP(recordHistory
))) {
570 /* we need to act like a zipper, as we'll receive
571 * the requests and the responses in separate chains
574 http_message
*response
= msg
, *request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
575 http_message
*free_msg
= request
;
581 http_message_tostring(response
, &message
, &msglen
);
582 phpstr_append(&obj
->history
, message
, msglen
);
585 http_message_tostring(request
, &message
, &msglen
);
586 phpstr_append(&obj
->history
, message
, msglen
);
589 } while ((response
= response
->parent
) && (request
= request
->parent
));
591 http_message_free(&free_msg
);
592 phpstr_fix(&obj
->history
);
595 UPD_PROP(long, responseCode
, msg
->http
.info
.response
.code
);
596 UPD_PROP(string
, responseStatus
, msg
->http
.info
.response
.status
);
600 MAKE_STD_ZVAL(headers
);
602 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
603 add_assoc_zval(resp
, "headers", headers
);
604 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
605 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
606 SET_PROP(responseData
, resp
);
607 zval_ptr_dtor(&resp
);
609 MAKE_STD_ZVAL(message
);
610 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
611 SET_PROP(responseMessage
, message
);
612 zval_ptr_dtor(&message
);
616 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
617 SET_PROP(responseInfo
, info
);
618 zval_ptr_dtor(&info
);
620 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onfinish", sizeof("onfinish"))) {
621 zend_call_method_with_0_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "onfinish", NULL
);
628 #define http_request_object_set_options_subr(key, ow) \
629 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
630 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
632 zval
*old_opts
, *new_opts
, *opts
= NULL
, **entry
;
634 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
638 MAKE_STD_ZVAL(new_opts
);
639 array_init(new_opts
);
640 old_opts
= GET_PROP(options
);
641 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
642 array_copy(old_opts
, new_opts
);
645 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), key
, len
, (void **) &entry
)) {
647 zend_hash_clean(Z_ARRVAL_PP(entry
));
649 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
651 array_copy(opts
, *entry
);
653 array_merge(opts
, *entry
);
658 add_assoc_zval(new_opts
, key
, opts
);
660 SET_PROP(options
, new_opts
);
661 zval_ptr_dtor(&new_opts
);
666 #define http_request_object_get_options_subr(key) \
667 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
668 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
673 zval
*opts
, **options
;
675 opts
= GET_PROP(options
);
676 array_init(return_value
);
678 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
679 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
))) {
680 convert_to_array(*options
);
681 array_copy(*options
, return_value
);
687 /* ### USERLAND ### */
689 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
691 * Instantiate a new HttpRequest object.
693 * Accepts a string as optional parameter containing the target request url.
694 * Additionally accepts an optional int parameter specifying the request method
695 * to use and an associative array as optional third parameter which will be
696 * passed to HttpRequest::setOptions().
698 * Throws HttpException.
700 PHP_METHOD(HttpRequest
, __construct
)
705 zval
*options
= NULL
;
708 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
710 UPD_STRL(url
, URL
, URL_len
);
713 UPD_PROP(long, method
, meth
);
716 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
723 /* {{{ proto bool HttpRequest::setOptions([array options])
725 * Set the request options to use. See http_get() for a full list of available options.
727 * Accepts an array as optional parameters, which values will overwrite the
728 * currently set request options. If the parameter is empty or omitted,
729 * the options of the HttpRequest object will be reset.
731 * Returns TRUE on success, or FALSE on failure.
733 PHP_METHOD(HttpRequest
, setOptions
)
738 zval
*opts
= NULL
, *old_opts
, *new_opts
, *add_opts
, **opt
;
740 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
744 MAKE_STD_ZVAL(new_opts
);
745 array_init(new_opts
);
747 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
748 SET_PROP(options
, new_opts
);
749 zval_ptr_dtor(&new_opts
);
753 MAKE_STD_ZVAL(add_opts
);
754 array_init(add_opts
);
755 /* some options need extra attention -- thus cannot use array_merge() directly */
756 FOREACH_KEYVAL(pos
, opts
, key
, idx
, opt
) {
758 if (!strcmp(key
, "headers")) {
759 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, *opt
);
760 } else if (!strcmp(key
, "cookies")) {
761 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addcookies", NULL
, *opt
);
762 } else if (!strcmp(key
, "ssl")) {
763 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addssloptions", NULL
, *opt
);
764 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
765 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "seturl", NULL
, *opt
);
766 } else if (!strcmp(key
, "method")) {
767 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setmethod", NULL
, *opt
);
770 add_assoc_zval(add_opts
, key
, *opt
);
777 old_opts
= GET_PROP(options
);
778 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
779 array_copy(old_opts
, new_opts
);
781 array_merge(add_opts
, new_opts
);
782 SET_PROP(options
, new_opts
);
783 zval_ptr_dtor(&new_opts
);
784 zval_ptr_dtor(&add_opts
);
790 /* {{{ proto array HttpRequest::getOptions()
792 * Get currently set options.
794 * Returns an associative array containing currently set options.
796 PHP_METHOD(HttpRequest
, getOptions
)
801 RETURN_PROP(options
);
806 /* {{{ proto bool HttpRequest::setSslOptions([array options])
810 * Accepts an associative array as parameter containing any SSL specific options.
811 * If the parameter is empty or omitted, the SSL options will be reset.
813 * Returns TRUE on success, or FALSE on failure.
815 PHP_METHOD(HttpRequest
, setSslOptions
)
817 http_request_object_set_options_subr("ssl", 1);
821 /* {{{ proto bool HttpRequest::addSslOptions(array options)
823 * Set additional SSL options.
825 * Expects an associative array as parameter containing additional SSL specific options.
827 * Returns TRUE on success, or FALSE on failure.
829 PHP_METHOD(HttpRequest
, addSslOptions
)
831 http_request_object_set_options_subr("ssl", 0);
835 /* {{{ proto array HttpRequest::getSslOtpions()
837 * Get previously set SSL options.
839 * Returns an associative array containing any previously set SSL options.
841 PHP_METHOD(HttpRequest
, getSslOptions
)
843 http_request_object_get_options_subr("ssl");
847 /* {{{ proto bool HttpRequest::addHeaders(array headers)
849 * Add request header name/value pairs.
851 * Expects an associative array as parameter containing additional header
854 * Returns TRUE on success, or FALSE on failure.
856 PHP_METHOD(HttpRequest
, addHeaders
)
858 http_request_object_set_options_subr("headers", 0);
861 /* {{{ proto bool HttpRequest::setHeaders([array headers])
863 * Set request header name/value pairs.
865 * Accepts an associative array as parameter containing header name/value pairs.
866 * If the parameter is empty or omitted, all previously set headers will be unset.
868 * Returns TRUE on success, or FALSE on failure.
870 PHP_METHOD(HttpRequest
, setHeaders
)
872 http_request_object_set_options_subr("headers", 1);
876 /* {{{ proto array HttpRequest::getHeaders()
878 * Get previously set request headers.
880 * Returns an associative array containing all currently set headers.
882 PHP_METHOD(HttpRequest
, getHeaders
)
884 http_request_object_get_options_subr("headers");
888 /* {{{ proto bool HttpRequest::setCookies([array cookies])
892 * Accepts an associative array as parameter containing cookie name/value pairs.
893 * If the parameter is empty or omitted, all previously set cookies will be unset.
895 * Returns TRUE on success, or FALSE on failure.
897 PHP_METHOD(HttpRequest
, setCookies
)
899 http_request_object_set_options_subr("cookies", 1);
903 /* {{{ proto bool HttpRequest::addCookies(array cookies)
907 * Expects an associative array as parameter containing any cookie name/value
910 * Returns TRUE on success, or FALSE on failure.
912 PHP_METHOD(HttpRequest
, addCookies
)
914 http_request_object_set_options_subr("cookies", 0);
918 /* {{{ proto array HttpRequest::getCookies()
920 * Get previously set cookies.
922 * Returns an associative array containing any previously set cookies.
924 PHP_METHOD(HttpRequest
, getCookies
)
926 http_request_object_get_options_subr("cookies");
930 /* {{{ proto bool HttpRequest::setUrl(string url)
932 * Set the request URL.
934 * Expects a string as parameter specifying the request url.
936 * Returns TRUE on success, or FALSE on failure.
938 PHP_METHOD(HttpRequest
, setUrl
)
943 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
947 UPD_STRL(url
, URL
, URL_len
);
952 /* {{{ proto string HttpRequest::getUrl()
954 * Get the previously set request URL.
956 * Returns the currently set request url as string.
958 PHP_METHOD(HttpRequest
, getUrl
)
968 /* {{{ proto bool HttpRequest::setMethod(int request_method)
970 * Set the request method.
972 * Expects an int as parameter specifying the request method to use.
973 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
975 * Returns TRUE on success, or FALSE on failure.
977 PHP_METHOD(HttpRequest
, setMethod
)
981 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
985 UPD_PROP(long, method
, meth
);
990 /* {{{ proto int HttpRequest::getMethod()
992 * Get the previously set request method.
994 * Returns the currently set request method.
996 PHP_METHOD(HttpRequest
, getMethod
)
1001 RETURN_PROP(method
);
1006 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1008 * Set the content type the post request should have.
1010 * Expects a string as parameters containing the content type of the request
1011 * (primary/secondary).
1013 * Returns TRUE on success, or FALSE if the content type does not seem to
1014 * contain a primary and a secondary part.
1016 PHP_METHOD(HttpRequest
, setContentType
)
1021 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1025 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1026 UPD_STRL(contentType
, ctype
, ct_len
);
1031 /* {{{ proto string HttpRequest::getContentType()
1033 * Get the previously content type.
1035 * Returns the previously set content type as string.
1037 PHP_METHOD(HttpRequest
, getContentType
)
1042 RETURN_PROP(contentType
);
1047 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1049 * Set the URL query parameters to use, overwriting previously set query parameters.
1050 * Affects any request types.
1052 * Accepts a string or associative array parameter containing the pre-encoded
1053 * query string or to be encoded query fields. If the parameter is empty or
1054 * omitted, the query data will be unset.
1056 * Returns TRUE on success, or FALSE on failure.
1058 PHP_METHOD(HttpRequest
, setQueryData
)
1062 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1066 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1067 UPD_STRL(queryData
, "", 0);
1068 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1069 char *query_data
= NULL
;
1071 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1075 UPD_PROP(string
, queryData
, query_data
);
1080 convert_to_string_ex(&qdata
);
1081 UPD_STRL(queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1082 if (orig
!= qdata
) {
1083 zval_ptr_dtor(&qdata
);
1090 /* {{{ proto string HttpRequest::getQueryData()
1092 * Get the current query data in form of an urlencoded query string.
1094 * Returns a string containing the urlencoded query.
1096 PHP_METHOD(HttpRequest
, getQueryData
)
1101 RETURN_PROP(queryData
);
1106 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1108 * Add parameters to the query parameter list, leaving previously set unchanged.
1109 * Affects any request type.
1111 * Expects an associative array as parameter containing the query fields to add.
1113 * Returns TRUE on success, or FALSE on failure.
1115 PHP_METHOD(HttpRequest
, addQueryData
)
1117 zval
*qdata
, *old_qdata
;
1118 char *query_data
= NULL
;
1119 size_t query_data_len
= 0;
1121 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &qdata
)) {
1125 old_qdata
= GET_PROP(queryData
);
1127 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
)) {
1131 UPD_STRL(queryData
, query_data
, query_data_len
);
1138 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1140 * Adds POST data entries, leaving previously set unchanged, unless a
1141 * post entry with the same name already exists.
1142 * Affects only POST and custom requests.
1144 * Expects an associative array as parameter containing the post fields.
1146 * Returns TRUE on success, or FALSE on failure.
1148 PHP_METHOD(HttpRequest
, addPostFields
)
1150 zval
*post_data
, *old_post
, *new_post
;
1152 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &post_data
)) {
1156 if (zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1157 MAKE_STD_ZVAL(new_post
);
1158 array_init(new_post
);
1159 old_post
= GET_PROP(postFields
);
1160 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1161 array_copy(old_post
, new_post
);
1163 array_merge(post_data
, new_post
);
1164 SET_PROP(postFields
, new_post
);
1165 zval_ptr_dtor(&new_post
);
1172 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1174 * Set the POST data entries, overwriting previously set POST data.
1175 * Affects only POST and custom requests.
1177 * Accepts an associative array as parameter containing the post fields.
1178 * If the parameter is empty or omitted, the post data will be unset.
1180 * Returns TRUE on success, or FALSE on failure.
1182 PHP_METHOD(HttpRequest
, setPostFields
)
1184 zval
*post
, *post_data
= NULL
;
1186 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &post_data
)) {
1190 MAKE_STD_ZVAL(post
);
1192 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1193 array_copy(post_data
, post
);
1195 SET_PROP(postFields
, post
);
1196 zval_ptr_dtor(&post
);
1202 /* {{{ proto array HttpRequest::getPostFields()
1204 * Get previously set POST data.
1206 * Returns the currently set post fields as associative array.
1208 PHP_METHOD(HttpRequest
, getPostFields
)
1213 RETURN_PROP(postFields
);
1218 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1220 * Set raw post data to send, overwriting previously set raw post data. Don't
1221 * forget to specify a content type. Affects only POST and custom requests.
1222 * Only either post fields or raw post data can be used for each request.
1223 * Raw post data has higher precedence and will be used even if post fields
1226 * Accepts a string as parameter containing the *raw* post data.
1228 * Returns TRUE on success, or FALSE on failure.
1230 PHP_METHOD(HttpRequest
, setRawPostData
)
1232 char *raw_data
= NULL
;
1235 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1243 UPD_STRL(rawPostData
, raw_data
, data_len
);
1248 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1250 * Add raw post data, leaving previously set raw post data unchanged.
1251 * Affects only POST and custom requests.
1253 * Expects a string as parameter containing the raw post data to concatenate.
1255 * Returns TRUE on success, or FALSE on failure.
1257 PHP_METHOD(HttpRequest
, addRawPostData
)
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(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(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 RETURN_PROP(rawPostData
);
1296 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1298 * Add a file to the POST request, leaving previously set files unchanged.
1299 * Affects only POST and custom requests. Cannot be used with raw post data.
1301 * Expects a string parameter containing the form element name, and a string
1302 * paremeter containing the path to the file which should be uploaded.
1303 * Additionally accepts an optional string parameter which should contain
1304 * the content type of the file.
1306 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1307 * primary and a secondary content type part.
1309 PHP_METHOD(HttpRequest
, addPostFile
)
1311 zval
*entry
, *old_post
, *new_post
;
1312 char *name
, *file
, *type
= NULL
;
1313 int name_len
, file_len
, type_len
= 0;
1315 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1320 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1322 type
= "application/x-octetstream";
1323 type_len
= sizeof("application/x-octetstream") - 1;
1326 MAKE_STD_ZVAL(entry
);
1329 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1330 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1331 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1333 MAKE_STD_ZVAL(new_post
);
1334 array_init(new_post
);
1335 old_post
= GET_PROP(postFiles
);
1336 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1337 array_copy(old_post
, new_post
);
1339 add_next_index_zval(new_post
, entry
);
1340 SET_PROP(postFiles
, new_post
);
1341 zval_ptr_dtor(&new_post
);
1347 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1349 * Set files to post, overwriting previously set post files.
1350 * Affects only POST and requests. Cannot be used with raw post data.
1352 * Accepts an array containing the files to post. Each entry should be an
1353 * associative array with "name", "file" and "type" keys. If the parameter
1354 * is empty or omitted the post files will be unset.
1356 * Returns TRUE on success, or FALSE on failure.
1358 PHP_METHOD(HttpRequest
, setPostFiles
)
1360 zval
*files
= NULL
, *post
;
1362 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!/", &files
)) {
1366 MAKE_STD_ZVAL(post
);
1368 if (files
&& (Z_TYPE_P(files
) == IS_ARRAY
)) {
1369 array_copy(files
, post
);
1371 SET_PROP(postFiles
, post
);
1372 zval_ptr_dtor(&post
);
1378 /* {{{ proto array HttpRequest::getPostFiles()
1380 * Get all previously added POST files.
1382 * Returns an array containing currently set post files.
1384 PHP_METHOD(HttpRequest
, getPostFiles
)
1389 RETURN_PROP(postFiles
);
1394 /* {{{ proto bool HttpRequest::setPutFile([string file])
1396 * Set file to put. Affects only PUT requests.
1398 * Accepts a string as parameter referencing the path to file.
1399 * If the parameter is empty or omitted the put file will be unset.
1401 * Returns TRUE on success, or FALSE on failure.
1403 PHP_METHOD(HttpRequest
, setPutFile
)
1408 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1412 UPD_STRL(putFile
, file
, file_len
);
1417 /* {{{ proto string HttpRequest::getPutFile()
1419 * Get previously set put file.
1421 * Returns a string containing the path to the currently set put file.
1423 PHP_METHOD(HttpRequest
, getPutFile
)
1428 RETURN_PROP(putFile
);
1433 /* {{{ proto array HttpRequest::getResponseData()
1435 * Get all response data after the request has been sent.
1437 * Returns an associative array with the key "headers" containing an associative
1438 * array holding all response headers, as well as the key "body" containing a
1439 * string with the response body.
1441 * If redirects were allowed and several responses were received, the data
1442 * references the last received response.
1444 PHP_METHOD(HttpRequest
, getResponseData
)
1449 RETURN_PROP(responseData
);
1454 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1456 * Get response header(s) after the request has been sent.
1458 * Accepts an string as optional parameter specifying a certain header to read.
1459 * If the parameter is empty or omitted all response headers will be returned.
1461 * Returns either a string with the value of the header matching name if requested,
1462 * FALSE on failure, or an associative array containing all response headers.
1464 * If redirects were allowed and several responses were received, the data
1465 * references the last received response.
1467 PHP_METHOD(HttpRequest
, getResponseHeader
)
1470 zval
*data
, **headers
, **header
;
1471 char *header_name
= NULL
;
1474 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1478 data
= GET_PROP(responseData
);
1479 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1480 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) &&
1481 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1482 if (!header_len
|| !header_name
) {
1483 RETVAL_ZVAL(*headers
, 1, 0);
1484 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1485 RETVAL_ZVAL(*header
, 1, 0);
1496 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1498 * Get response cookie(s) after the request has been sent.
1500 * Accepts a string as optional parameter specifying the name of the cookie to read.
1501 * If the parameter is empty or omitted, an associative array with all received
1502 * cookies will be returned.
1504 * Returns either an associative array with the cookie's name, value and any
1505 * additional params of the cookie matching name if requested, FALSE on failure,
1506 * or an array containing all received cookies as arrays.
1508 * If redirects were allowed and several responses were received, the data
1509 * references the last received response.
1511 PHP_METHOD(HttpRequest
, getResponseCookie
)
1514 zval
*data
, **headers
;
1515 char *cookie_name
= NULL
;
1518 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1522 array_init(return_value
);
1524 data
= GET_PROP(responseData
);
1525 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1526 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) &&
1527 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1530 zval
**header
= NULL
;
1533 convert_to_array(*headers
);
1534 FOREACH_HASH_KEYVAL(pos1
, Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1535 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1536 /* several cookies? */
1537 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1541 FOREACH_HASH_VAL(pos2
, Z_ARRVAL_PP(header
), cookie
) {
1543 MAKE_STD_ZVAL(cookie_hash
);
1544 array_init(cookie_hash
);
1546 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1548 add_next_index_zval(return_value
, cookie_hash
);
1552 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1553 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1554 add_next_index_zval(return_value
, cookie_hash
);
1555 return; /* <<< FOUND >>> */
1557 zval_dtor(cookie_hash
);
1562 zval_dtor(cookie_hash
);
1569 MAKE_STD_ZVAL(cookie_hash
);
1570 array_init(cookie_hash
);
1571 convert_to_string_ex(header
);
1573 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1575 add_next_index_zval(return_value
, cookie_hash
);
1579 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1580 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1581 add_next_index_zval(return_value
, cookie_hash
);
1583 zval_dtor(cookie_hash
);
1588 zval_dtor(cookie_hash
);
1602 /* {{{ proto string HttpRequest::getResponseBody()
1604 * Get the response body after the request has been sent.
1606 * Returns a string containing the response body.
1608 * If redirects were allowed and several responses were received, the data
1609 * references the last received response.
1611 PHP_METHOD(HttpRequest
, getResponseBody
)
1617 zval
*data
= GET_PROP(responseData
);
1619 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1620 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
))) {
1621 RETURN_ZVAL(*body
, 1, 0);
1629 /* {{{ proto int HttpRequest::getResponseCode()
1631 * Get the response code after the request has been sent.
1633 * Returns an int representing the response code.
1635 * If redirects were allowed and several responses were received, the data
1636 * references the last received response.
1638 PHP_METHOD(HttpRequest
, getResponseCode
)
1643 RETURN_PROP(responseCode
);
1648 /* {{{ proto string HttpRequest::getResponseStatus()
1650 * Get the response status (i.e. the string after the response code) after the message has been sent.
1652 * Returns a string containing the response status text.
1654 PHP_METHOD(HttpRequest
, getResponseStatus
)
1659 RETURN_PROP(responseStatus
);
1664 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1666 * Get response info after the request has been sent.
1667 * See http_get() for a full list of returned info.
1669 * Accepts a string as optional parameter specifying the info to read.
1670 * If the parameter is empty or omitted, an associative array containing
1671 * all available info will be returned.
1673 * Returns either a scalar containing the value of the info matching name if
1674 * requested, FALSE on failure, or an associative array containing all
1677 * If redirects were allowed and several responses were received, the data
1678 * references the last received response.
1680 PHP_METHOD(HttpRequest
, getResponseInfo
)
1683 zval
*info
, **infop
;
1684 char *info_name
= NULL
;
1687 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1691 info
= GET_PROP(responseInfo
);
1693 if (Z_TYPE_P(info
) != IS_ARRAY
) {
1697 if (info_len
&& info_name
) {
1698 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1699 RETURN_ZVAL(*infop
, 1, 0);
1701 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1705 RETURN_ZVAL(info
, 1, 0);
1711 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1713 * Get the full response as HttpMessage object after the request has been sent.
1715 * Returns an HttpMessage object of the response.
1717 * If redirects were allowed and several responses were received, the data
1718 * references the last received response. Use HttpMessage::getParentMessage()
1719 * to access the data of previously received responses within this request
1722 * Throws HttpException.
1724 PHP_METHOD(HttpRequest
, getResponseMessage
)
1731 SET_EH_THROW_HTTP();
1732 message
= GET_PROP(responseMessage
);
1733 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1734 RETVAL_OBJECT(message
, 1);
1743 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1745 * Get sent HTTP message.
1747 * Returns an HttpMessage object representing the sent request.
1749 * If redirects were allowed and several responses were received, the data
1750 * references the last received response. Use HttpMessage::getParentMessage()
1751 * to access the data of previously sent requests within this request
1754 * Note that the internal request message is immutable, that means that the
1755 * request message received through HttpRequest::getRequestMessage() will
1756 * always look the same for the same request, regardless of any changes you
1757 * may have made to the returned object.
1759 * Throws HttpMalformedHeadersException, HttpEncodingException.
1761 PHP_METHOD(HttpRequest
, getRequestMessage
)
1767 getObject(http_request_object
, obj
);
1769 SET_EH_THROW_HTTP();
1770 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1771 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
1778 /* {{{ proto string HttpRequest::getRawRequestMessage()
1780 * Get sent HTTP message.
1782 * Returns an HttpMessage in a form of a string
1785 PHP_METHOD(HttpRequest
, getRawRequestMessage
)
1790 getObject(http_request_object
, obj
);
1792 RETURN_PHPSTR_DUP(&obj
->request
->conv
.request
);
1797 /* {{{ proto string HttpRequest::getRawResponseMessage()
1799 * Get the entire HTTP response.
1801 * Returns the complete web server response, including the headers in a form of a string.
1804 PHP_METHOD(HttpRequest
, getRawResponseMessage
)
1809 getObject(http_request_object
, obj
);
1811 RETURN_PHPSTR_DUP(&obj
->request
->conv
.response
);
1816 /* {{{ proto HttpMessage HttpRequest::getHistory()
1818 * Get all sent requests and received responses as an HttpMessage object.
1820 * If you don't want to record history at all, set the instance variable
1821 * HttpRequest::$recordHistory to FALSE.
1823 * Returns an HttpMessage object representing the complete request/response
1826 * The object references the last received response, use HttpMessage::getParentMessage()
1827 * to access the data of previously sent requests and received responses.
1829 * Note that the internal history is immutable, that means that any changes
1830 * you make the the message list won't affect a history message list newly
1831 * created by another call to HttpRequest::getHistory().
1833 * Throws HttpMalformedHeaderException, HttpEncodingException.
1835 PHP_METHOD(HttpRequest
, getHistory
)
1841 getObject(http_request_object
, obj
);
1843 SET_EH_THROW_HTTP();
1844 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->history
), PHPSTR_LEN(&obj
->history
)))) {
1845 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
1852 /* {{{ proto void HttpRequest::clearHistory()
1854 * Clear the history.
1856 PHP_METHOD(HttpRequest
, clearHistory
)
1859 getObject(http_request_object
, obj
);
1860 phpstr_dtor(&obj
->history
);
1865 /* {{{ proto HttpMessage HttpRequest::send()
1867 * Send the HTTP request.
1869 * Returns the received response as HttpMessage object.
1871 * NOTE: While an exception may be thrown, the transfer could have succeeded
1872 * at least partially, so you might want to check the return values of various
1873 * HttpRequest::getResponse*() methods.
1875 * Throws HttpRuntimeException, HttpRequestException,
1876 * HttpMalformedHeaderException, HttpEncodingException.
1881 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
1882 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1883 * $r->addQueryData(array('category' => 3));
1886 * if ($r->getResponseCode() == 200) {
1887 * file_put_contents('local.rss', $r->getResponseBody());
1889 * } catch (HttpException $ex) {
1898 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
1899 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1900 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1901 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1903 * echo $r->send()->getBody();
1904 * } catch (HttpException $ex) {
1910 PHP_METHOD(HttpRequest
, send
)
1912 getObject(http_request_object
, obj
);
1916 SET_EH_THROW_HTTP();
1921 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1922 } else if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
1923 http_request_exec(obj
->request
);
1924 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
1925 RETVAL_OBJECT(GET_PROP(responseMessage
), 1);
1933 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1940 * vim600: noet sw=4 ts=4 fdm=marker
1941 * vim<600: noet sw=4 ts=4