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 +--------------------------------------------------------------------+
21 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
23 #include "zend_interfaces.h"
25 #include "php_http_std_defs.h"
26 #include "php_http_request_object.h"
27 #include "php_http_request_api.h"
28 #include "php_http_request_pool_api.h"
30 #include "php_http_api.h"
31 #include "php_http_url_api.h"
32 #include "php_http_message_api.h"
33 #include "php_http_message_object.h"
34 #include "php_http_exception_object.h"
39 # include <winsock2.h>
41 #include <curl/curl.h>
43 ZEND_EXTERN_MODULE_GLOBALS(http
);
45 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
46 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
47 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
48 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
50 HTTP_EMPTY_ARGS(__destruct
, 0);
51 HTTP_BEGIN_ARGS(__construct
, 0, 0)
53 HTTP_ARG_VAL(method
, 0)
54 HTTP_ARG_VAL(options
, 0)
57 HTTP_EMPTY_ARGS(getOptions
, 0);
58 HTTP_BEGIN_ARGS(setOptions
, 0, 0)
59 HTTP_ARG_VAL(options
, 0)
62 HTTP_EMPTY_ARGS(getSslOptions
, 0);
63 HTTP_BEGIN_ARGS(setSslOptions
, 0, 0)
64 HTTP_ARG_VAL(ssl_options
, 0)
67 HTTP_EMPTY_ARGS(getHeaders
, 0);
68 HTTP_BEGIN_ARGS(setHeaders
, 0, 0)
69 HTTP_ARG_VAL(headers
, 0)
72 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
73 HTTP_ARG_VAL(headers
, 0)
76 HTTP_EMPTY_ARGS(getCookies
, 0);
77 HTTP_BEGIN_ARGS(setCookies
, 0, 0)
78 HTTP_ARG_VAL(cookies
, 0)
81 HTTP_BEGIN_ARGS(addCookies
, 0, 1)
82 HTTP_ARG_VAL(cookies
, 0)
85 HTTP_EMPTY_ARGS(getUrl
, 0);
86 HTTP_BEGIN_ARGS(setUrl
, 0, 1)
90 HTTP_EMPTY_ARGS(getMethod
, 0);
91 HTTP_BEGIN_ARGS(setMethod
, 0, 1)
92 HTTP_ARG_VAL(request_method
, 0)
95 HTTP_EMPTY_ARGS(getContentType
, 0);
96 HTTP_BEGIN_ARGS(setContentType
, 0, 1)
97 HTTP_ARG_VAL(content_type
, 0)
100 HTTP_EMPTY_ARGS(getQueryData
, 0);
101 HTTP_BEGIN_ARGS(setQueryData
, 0, 0)
102 HTTP_ARG_VAL(query_data
, 0)
105 HTTP_BEGIN_ARGS(addQueryData
, 0, 1)
106 HTTP_ARG_VAL(query_data
, 0)
109 HTTP_EMPTY_ARGS(getPostFields
, 0);
110 HTTP_BEGIN_ARGS(setPostFields
, 0, 0)
111 HTTP_ARG_VAL(post_fields
, 0)
114 HTTP_BEGIN_ARGS(addPostFields
, 0, 1)
115 HTTP_ARG_VAL(post_fields
, 0)
118 HTTP_EMPTY_ARGS(getPostFiles
, 0);
119 HTTP_BEGIN_ARGS(setPostFiles
, 0, 0)
120 HTTP_ARG_VAL(post_files
, 0)
123 HTTP_BEGIN_ARGS(addPostFile
, 0, 2)
124 HTTP_ARG_VAL(formname
, 0)
125 HTTP_ARG_VAL(filename
, 0)
126 HTTP_ARG_VAL(content_type
, 0)
129 HTTP_EMPTY_ARGS(getRawPostData
, 0);
130 HTTP_BEGIN_ARGS(setRawPostData
, 0, 0)
131 HTTP_ARG_VAL(raw_post_data
, 0)
134 HTTP_BEGIN_ARGS(addRawPostData
, 0, 1)
135 HTTP_ARG_VAL(raw_post_data
, 0)
138 HTTP_EMPTY_ARGS(getPutFile
, 0);
139 HTTP_BEGIN_ARGS(setPutFile
, 0, 0)
140 HTTP_ARG_VAL(filename
, 0)
143 HTTP_EMPTY_ARGS(getResponseData
, 0);
144 HTTP_BEGIN_ARGS(getResponseHeader
, 0, 0)
145 HTTP_ARG_VAL(name
, 0)
148 HTTP_BEGIN_ARGS(getResponseCookie
, 0, 0)
149 HTTP_ARG_VAL(name
, 0)
152 HTTP_EMPTY_ARGS(getResponseBody
, 0);
153 HTTP_EMPTY_ARGS(getResponseCode
, 0);
154 HTTP_BEGIN_ARGS(getResponseInfo
, 0, 0)
155 HTTP_ARG_VAL(name
, 0)
158 HTTP_EMPTY_ARGS(getResponseMessage
, 1);
159 HTTP_EMPTY_ARGS(getRequestMessage
, 1);
160 HTTP_EMPTY_ARGS(getHistory
, 1);
161 HTTP_EMPTY_ARGS(clearHistory
, 0);
162 HTTP_EMPTY_ARGS(send
, 1);
164 HTTP_BEGIN_ARGS(get
, 0, 1)
166 HTTP_ARG_VAL(options
, 0)
167 HTTP_ARG_VAL(info
, 1)
170 HTTP_BEGIN_ARGS(head
, 0, 1)
172 HTTP_ARG_VAL(options
, 0)
173 HTTP_ARG_VAL(info
, 1)
176 HTTP_BEGIN_ARGS(postData
, 0, 2)
178 HTTP_ARG_VAL(data
, 0)
179 HTTP_ARG_VAL(options
, 0)
180 HTTP_ARG_VAL(info
, 1)
183 HTTP_BEGIN_ARGS(postFields
, 0, 2)
185 HTTP_ARG_VAL(data
, 0)
186 HTTP_ARG_VAL(options
, 0)
187 HTTP_ARG_VAL(info
, 1)
190 HTTP_BEGIN_ARGS(putFile
, 0, 2)
192 HTTP_ARG_VAL(file
, 0)
193 HTTP_ARG_VAL(options
, 0)
194 HTTP_ARG_VAL(info
, 1)
197 HTTP_BEGIN_ARGS(putStream
, 0, 2)
199 HTTP_ARG_VAL(stream
, 0)
200 HTTP_ARG_VAL(options
, 0)
201 HTTP_ARG_VAL(info
, 1)
204 HTTP_BEGIN_ARGS(methodRegister
, 0, 1)
205 HTTP_ARG_VAL(method_name
, 0)
208 HTTP_BEGIN_ARGS(methodUnregister
, 0, 1)
209 HTTP_ARG_VAL(method
, 0)
212 HTTP_BEGIN_ARGS(methodName
, 0, 1)
213 HTTP_ARG_VAL(method_id
, 0)
216 HTTP_BEGIN_ARGS(methodExists
, 0, 1)
217 HTTP_ARG_VAL(method
, 0)
220 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
221 static inline void _http_request_object_declare_default_properties(TSRMLS_D
);
222 #define http_request_object_clone_obj _http_request_object_clone_obj
223 static inline zend_object_value
_http_request_object_clone_obj(zval
*object TSRMLS_DC
);
225 zend_class_entry
*http_request_object_ce
;
226 zend_function_entry http_request_object_fe
[] = {
227 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
228 HTTP_REQUEST_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
230 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
231 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
232 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
233 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
235 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
236 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
237 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
239 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
240 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
241 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
243 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
244 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
246 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
247 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
249 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
250 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
252 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
253 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
254 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
256 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
257 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
258 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
260 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
261 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
262 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
265 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
266 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
269 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
273 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
274 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
275 HTTP_REQUEST_ME(getResponseCookie
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
277 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
278 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
279 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
280 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
281 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
282 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
284 HTTP_REQUEST_ALIAS(get
, http_get
)
285 HTTP_REQUEST_ALIAS(head
, http_head
)
286 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
287 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
288 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
289 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
291 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
292 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
293 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
294 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
298 static zend_object_handlers http_request_object_handlers
;
300 PHP_MINIT_FUNCTION(http_request_object
)
302 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
303 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
307 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
309 return http_request_object_new_ex(ce
, curl_easy_init(), NULL
);
312 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
314 zend_object_value ov
;
315 http_request_object
*o
;
317 o
= ecalloc(1, sizeof(http_request_object
));
325 phpstr_init(&o
->history
);
326 phpstr_init(&o
->request
);
327 phpstr_init_ex(&o
->response
, HTTP_CURLBUF_SIZE
, 0);
329 ALLOC_HASHTABLE(OBJ_PROP(o
));
330 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
331 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
333 ov
.handle
= putObject(http_request_object
, o
);
334 ov
.handlers
= &http_request_object_handlers
;
339 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
342 zend_object_value new_ov
;
343 http_request_object
*new_obj
;
344 getObject(http_request_object
, old_obj
);
346 old_zo
= zend_objects_get_address(this_ptr TSRMLS_CC
);
347 new_ov
= http_request_object_new_ex(old_zo
->ce
, curl_easy_duphandle(old_obj
->ch
), &new_obj
);
349 zend_objects_clone_members(&new_obj
->zo
, new_ov
, old_zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
350 phpstr_append(&new_obj
->history
, old_obj
->history
.data
, old_obj
->history
.used
);
351 phpstr_append(&new_obj
->request
, old_obj
->request
.data
, old_obj
->request
.used
);
352 phpstr_append(&new_obj
->response
, old_obj
->response
.data
, old_obj
->response
.used
);
357 static inline void _http_request_object_declare_default_properties(TSRMLS_D
)
359 zend_class_entry
*ce
= http_request_object_ce
;
361 DCL_PROP_N(PROTECTED
, options
);
362 DCL_PROP_N(PROTECTED
, responseInfo
);
363 DCL_PROP_N(PROTECTED
, responseData
);
364 DCL_PROP_N(PROTECTED
, responseCode
);
365 DCL_PROP_N(PROTECTED
, responseMessage
);
366 DCL_PROP_N(PROTECTED
, postFields
);
367 DCL_PROP_N(PROTECTED
, postFiles
);
369 DCL_PROP(PROTECTED
, long, method
, HTTP_GET
);
371 DCL_PROP(PROTECTED
, string
, url
, "");
372 DCL_PROP(PROTECTED
, string
, contentType
, "");
373 DCL_PROP(PROTECTED
, string
, rawPostData
, "");
374 DCL_PROP(PROTECTED
, string
, queryData
, "");
375 DCL_PROP(PROTECTED
, string
, putFile
, "");
377 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
381 * Request Method Constants
384 DCL_CONST(long, "METH_GET", HTTP_GET
);
385 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
386 DCL_CONST(long, "METH_POST", HTTP_POST
);
387 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
388 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
389 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
390 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
391 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
392 /* WebDAV - RFC 2518 */
393 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
394 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
395 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
396 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
397 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
398 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
399 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
400 /* WebDAV Versioning - RFC 3253 */
401 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
402 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
403 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
404 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
405 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
406 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
407 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
408 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
409 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
410 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
411 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
412 /* WebDAV Access Control - RFC 3744 */
413 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
418 # if LIBCURL_VERSION_NUM >= 0x070a05
419 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
420 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
421 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
422 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
423 # endif /* LIBCURL_VERSION_NUM */
427 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
429 http_request_object
*o
= (http_request_object
*) object
;
432 zend_hash_destroy(OBJ_PROP(o
));
433 FREE_HASHTABLE(OBJ_PROP(o
));
436 /* avoid nasty segfaults with already cleaned up callbacks */
437 curl_easy_setopt(o
->ch
, CURLOPT_NOPROGRESS
, 1);
438 curl_easy_setopt(o
->ch
, CURLOPT_PROGRESSFUNCTION
, NULL
);
439 curl_easy_setopt(o
->ch
, CURLOPT_VERBOSE
, 0);
440 curl_easy_setopt(o
->ch
, CURLOPT_DEBUGFUNCTION
, NULL
);
441 curl_easy_cleanup(o
->ch
);
443 phpstr_dtor(&o
->response
);
444 phpstr_dtor(&o
->request
);
445 phpstr_dtor(&o
->history
);
449 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr
, http_request_body
*body TSRMLS_DC
)
451 zval
*meth
, *URL
, *meth_p
, *URL_p
;
453 STATUS status
= SUCCESS
;
458 HTTP_CHECK_CURL_INIT(obj
->ch
, curl_easy_init(), return FAILURE
);
460 URL
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, url
), &URL_p
);
461 // HTTP_URI_MAXLEN+1 long char *
462 if (!(request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0))) {
464 zval_ptr_dtor(&URL_p
);
469 zval_ptr_dtor(&URL_p
);
472 meth
= convert_to_type_ex(IS_LONG
, GET_PROP(obj
, method
), &meth_p
);
473 switch (Z_LVAL_P(meth
))
483 php_stream_statbuf ssb
;
484 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj
, putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
486 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
487 body
->type
= HTTP_REQUEST_BODY_UPLOADFILE
;
489 body
->size
= ssb
.sb
.st_size
;
499 /* check for raw post data */
500 zval
*raw_data_p
, *raw_data
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
), &raw_data_p
);
502 if (Z_STRLEN_P(raw_data
)) {
503 zval
*ctype_p
, *ctype
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, contentType
), &ctype_p
);
505 if (Z_STRLEN_P(ctype
)) {
506 zval
**headers
, *opts
= GET_PROP(obj
, options
);
508 convert_to_array(opts
);
510 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
513 convert_to_array(*headers
);
514 /* only override if not already set */
515 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) {
516 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
521 MAKE_STD_ZVAL(headers
);
523 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
524 add_assoc_zval(opts
, "headers", headers
);
529 zval_ptr_dtor(&ctype_p
);
532 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
533 body
->data
= estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
));
534 body
->size
= Z_STRLEN_P(raw_data
);
536 status
= http_request_body_fill(body
, Z_ARRVAL_P(GET_PROP(obj
, postFields
)), Z_ARRVAL_P(GET_PROP(obj
, postFiles
)));
540 zval_ptr_dtor(&raw_data_p
);
547 zval_ptr_dtor(&meth_p
);
550 if (status
== SUCCESS
) {
551 zval
*qdata_p
, *qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
), &qdata_p
);
553 if (Z_STRLEN_P(qdata
)) {
554 if (!strchr(request_uri
, '?')) {
555 strlcat(request_uri
, "?", HTTP_URI_MAXLEN
);
557 strlcat(request_uri
, "&", HTTP_URI_MAXLEN
);
559 strlcat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
);
563 zval_ptr_dtor(&qdata_p
);
566 status
= http_request_init(obj
->ch
, Z_LVAL_P(meth
), request_uri
, body
, Z_ARRVAL_P(GET_PROP(obj
, options
)));
570 /* clean previous response */
571 phpstr_dtor(&obj
->response
);
572 /* clean previous request */
573 phpstr_dtor(&obj
->request
);
578 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
582 phpstr_fix(&obj
->request
);
583 phpstr_fix(&obj
->response
);
585 msg
= http_message_parse(PHPSTR_VAL(&obj
->response
), PHPSTR_LEN(&obj
->response
));
592 zval
*headers
, *message
,
593 *resp
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
)),
594 *info
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseInfo
));
599 if (zval_is_true(GET_PROP(obj
, recordHistory
))) {
600 /* we need to act like a zipper, as we'll receive
601 * the requests and the responses in separate chains
604 http_message
*response
= msg
, *request
= http_message_parse(PHPSTR_VAL(&obj
->request
), PHPSTR_LEN(&obj
->request
));
605 http_message
*free_msg
= request
;
611 http_message_tostring(response
, &message
, &msglen
);
612 phpstr_append(&obj
->history
, message
, msglen
);
615 http_message_tostring(request
, &message
, &msglen
);
616 phpstr_append(&obj
->history
, message
, msglen
);
619 } while ((response
= response
->parent
) && (request
= request
->parent
));
621 http_message_free(&free_msg
);
622 phpstr_fix(&obj
->history
);
625 UPD_PROP(obj
, long, responseCode
, msg
->http
.info
.response
.code
);
627 MAKE_STD_ZVAL(headers
)
630 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
631 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
633 add_assoc_zval(resp
, "headers", headers
);
634 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
635 SET_PROP(obj
, responseData
, resp
);
637 MAKE_STD_ZVAL(message
);
638 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
639 SET_PROP(obj
, responseMessage
, message
);
640 zval_ptr_dtor(&message
);
642 http_request_info(obj
->ch
, Z_ARRVAL_P(info
));
643 SET_PROP(obj
, responseInfo
, info
);
649 #define http_request_object_set_options_subr(key, ow) \
650 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
651 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
653 zval
*opts
, **options
, *new_options
= NULL
;
654 getObject(http_request_object
, obj
);
656 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &new_options
)) {
660 opts
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, options
));
662 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
663 convert_to_array(*options
);
665 zend_hash_clean(Z_ARRVAL_PP(options
));
667 if (new_options
&& zend_hash_num_elements(Z_ARRVAL_P(new_options
))) {
669 array_copy(new_options
, *options
);
671 array_merge(new_options
, *options
);
674 } else if (new_options
&& zend_hash_num_elements(Z_ARRVAL_P(new_options
))) {
675 ZVAL_ADDREF(new_options
);
676 add_assoc_zval(opts
, key
, new_options
);
682 #define http_request_object_get_options_subr(key) \
683 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
684 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
689 zval
*opts_p
, *opts
, **options
;
690 getObject(http_request_object
, obj
);
692 opts
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
), &opts_p
);
694 array_init(return_value
);
696 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
)) {
697 convert_to_array(*options
);
698 array_copy(*options
, return_value
);
702 zval_ptr_dtor(&opts_p
);
708 /* ### USERLAND ### */
710 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
712 * Instantiate a new HttpRequest object.
714 * Accepts a string as optional parameter containing the target request url.
715 * Additianally accepts an optional int parameter specifying the request method
716 * to use and an associative array as optional third parameter which will be
717 * passed to HttpRequest::setOptions().
719 * Throws HttpException.
721 PHP_METHOD(HttpRequest
, __construct
)
726 zval
*options
= NULL
;
727 getObject(http_request_object
, obj
);
730 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
731 INIT_PARR(obj
, options
);
732 INIT_PARR(obj
, responseInfo
);
733 INIT_PARR(obj
, responseData
);
734 INIT_PARR(obj
, postFields
);
735 INIT_PARR(obj
, postFiles
);
738 UPD_STRL(obj
, url
, URL
, URL_len
);
741 UPD_PROP(obj
, long, method
, meth
);
744 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
751 /* {{{ proto void HttpRequest::__destruct()
753 * Destroys the HttpRequest object.
755 PHP_METHOD(HttpRequest
, __destruct
)
757 getObject(http_request_object
, obj
);
761 FREE_PARR(obj
, options
);
762 FREE_PARR(obj
, responseInfo
);
763 FREE_PARR(obj
, responseData
);
764 FREE_PARR(obj
, postFields
);
765 FREE_PARR(obj
, postFiles
);
769 /* {{{ proto bool HttpRequest::setOptions([array options])
771 * Set the request options to use. See http_get() for a full list of available options.
773 * Accepts an array as optional parameters, wich values will overwrite the
774 * currently set request options. If the parameter is empty or mitted,
775 * the optoions of the HttpRequest object will be reset.
777 * Returns TRUE on success, or FALSE on failure.
779 PHP_METHOD(HttpRequest
, setOptions
)
783 zval
*opts
= NULL
, *old_opts
, **opt
;
784 getObject(http_request_object
, obj
);
786 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!", &opts
)) {
790 old_opts
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, options
));
793 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
794 zend_hash_clean(Z_ARRVAL_P(old_opts
));
795 SET_PROP(obj
, options
, old_opts
);
799 /* some options need extra attention -- thus cannot use array_merge() directly */
800 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
802 if (!strcmp(key
, "headers")) {
804 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
805 convert_to_array_ex(opt
);
806 convert_to_array(*headers
);
807 array_merge(*opt
, *headers
);
810 } else if (!strcmp(key
, "cookies")) {
812 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
813 convert_to_array_ex(opt
);
814 convert_to_array(*cookies
);
815 array_merge(*opt
, *cookies
);
818 } else if (!strcmp(key
, "ssl")) {
820 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl
)) {
821 convert_to_array_ex(opt
);
822 convert_to_array(*ssl
);
823 array_merge(*opt
, *ssl
);
826 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
827 if (Z_TYPE_PP(opt
) != IS_STRING
) {
828 convert_to_string_ex(opt
);
830 UPD_STRL(obj
, url
, Z_STRVAL_PP(opt
), Z_STRLEN_PP(opt
));
832 } else if (!strcmp(key
, "method")) {
833 if (Z_TYPE_PP(opt
) != IS_LONG
) {
834 convert_to_long_ex(opt
);
836 UPD_PROP(obj
, long, method
, Z_LVAL_PP(opt
));
841 add_assoc_zval(old_opts
, key
, *opt
);
847 SET_PROP(obj
, options
, old_opts
);
853 /* {{{ proto array HttpRequest::getOptions()
855 * Get currently set options.
857 * Returns an associative array containing currently set options.
859 PHP_METHOD(HttpRequest
, getOptions
)
865 getObject(http_request_object
, obj
);
867 opts
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, options
), &opts_p
);
868 array_init(return_value
);
869 array_copy(opts
, return_value
);
872 zval_ptr_dtor(&opts_p
);
878 /* {{{ proto bool HttpRequest::setSslOptions([array options])
882 * Accepts an associative array as parameter containing any SSL specific options.
883 * If the parameter is empty or omitted, the SSL options will be reset.
885 * Returns TRUE on success, or FALSE on failure.
887 PHP_METHOD(HttpRequest
, setSslOptions
)
889 http_request_object_set_options_subr("ssl", 1);
893 /* {{{ proto bool HttpRequest::addSslOptions(array options)
895 * Set additional SSL options.
897 * Expects an associative array as parameter containing additional SSL specific options.
899 * Returns TRUE on success, or FALSE on failure.
901 PHP_METHOD(HttpRequest
, addSslOptions
)
903 http_request_object_set_options_subr("ssl", 0);
907 /* {{{ proto array HttpRequest::getSslOtpions()
909 * Get previously set SSL options.
911 * Returns an associative array containing any previously set SSL options.
913 PHP_METHOD(HttpRequest
, getSslOptions
)
915 http_request_object_get_options_subr("ssl");
919 /* {{{ proto bool HttpRequest::addHeaders(array headers)
921 * Add request header name/value pairs.
923 * Expects an ssociative array as parameter containing additional header
926 * Returns TRUE on success, or FALSE on failure.
928 PHP_METHOD(HttpRequest
, addHeaders
)
930 http_request_object_set_options_subr("headers", 0);
933 /* {{{ proto bool HttpRequest::setHeaders([array headers])
935 * Set request header name/value pairs.
937 * Accepts an associative array as parameter containing header name/value pairs.
938 * If the parameter is empty or omitted, all previously set headers will be unset.
940 * Returns TRUE on success, or FALSE on failure.
942 PHP_METHOD(HttpRequest
, setHeaders
)
944 http_request_object_set_options_subr("headers", 1);
948 /* {{{ proto array HttpRequest::getHeaders()
950 * Get previously set request headers.
952 * Returns an associative array containing all currently set headers.
954 PHP_METHOD(HttpRequest
, getHeaders
)
956 http_request_object_get_options_subr("headers");
960 /* {{{ proto bool HttpRequest::setCookies([array cookies])
964 * Accepts an associative array as parameter containing cookie name/value pairs.
965 * If the parameter is empty or omitted, all previously set cookies will be unset.
967 * Returns TRUE on success, or FALSE on failure.
969 PHP_METHOD(HttpRequest
, setCookies
)
971 http_request_object_set_options_subr("cookies", 1);
975 /* {{{ proto bool HttpRequest::addCookies(array cookies)
979 * Expects an associative array as parameter containing any cookie name/value
982 * Returns TRUE on success, or FALSE on failure.
984 PHP_METHOD(HttpRequest
, addCookies
)
986 http_request_object_set_options_subr("cookies", 0);
990 /* {{{ proto array HttpRequest::getCookies()
992 * Get previously set cookies.
994 * Returns an associative array containing any previously set cookies.
996 PHP_METHOD(HttpRequest
, getCookies
)
998 http_request_object_get_options_subr("cookies");
1002 /* {{{ proto bool HttpRequest::setUrl(string url)
1004 * Set the request URL.
1006 * Expects a string as parameter specifying the request url.
1008 * Returns TRUE on success, or FALSE on failure.
1010 PHP_METHOD(HttpRequest
, setUrl
)
1014 getObject(http_request_object
, obj
);
1016 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1020 UPD_STRL(obj
, url
, URL
, URL_len
);
1025 /* {{{ proto string HttpRequest::getUrl()
1027 * Get the previously set request URL.
1029 * Returns the currently set request url as string.
1031 PHP_METHOD(HttpRequest
, getUrl
)
1036 getObject(http_request_object
, obj
);
1037 zval
*URL
= GET_PROP(obj
, url
);
1039 RETURN_ZVAL(URL
, 1, 0);
1044 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1046 * Set the request method.
1048 * Expects an int as parameter specifying the request method to use.
1049 * In PHP 5.1+ HttpRequest::METH, otherwise the HTTP_METH constants can be used.
1051 * Returns TRUE on success, or FALSE on failure.
1053 PHP_METHOD(HttpRequest
, setMethod
)
1056 getObject(http_request_object
, obj
);
1058 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1062 UPD_PROP(obj
, long, method
, meth
);
1067 /* {{{ proto int HttpRequest::getMethod()
1069 * Get the previously set request method.
1071 * Returns the currently set request method.
1073 PHP_METHOD(HttpRequest
, getMethod
)
1078 getObject(http_request_object
, obj
);
1079 zval
*meth
= GET_PROP(obj
, method
);
1081 RETURN_ZVAL(meth
, 1, 0);
1086 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1088 * Set the content type the post request should have.
1090 * Expects a string as parameters containing the content type of the request
1091 * (primary/secondary).
1093 * Returns TRUE on success, or FALSE if the content type does not seem to
1094 * contain a primary and a secondary part.
1096 PHP_METHOD(HttpRequest
, setContentType
)
1100 getObject(http_request_object
, obj
);
1102 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1106 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1107 UPD_STRL(obj
, contentType
, ctype
, ct_len
);
1112 /* {{{ proto string HttpRequest::getContentType()
1114 * Get the previously content type.
1116 * Returns the previously set content type as string.
1118 PHP_METHOD(HttpRequest
, getContentType
)
1123 getObject(http_request_object
, obj
);
1124 zval
*ctype
= GET_PROP(obj
, contentType
);
1126 RETURN_ZVAL(ctype
, 1, 0);
1131 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1133 * Set the URL query parameters to use, overwriting previously set query parameters.
1134 * Affects any request types.
1136 * Accepts a string or associative array parameter containing the pre-encoded
1137 * query string or to be encoded query fields. If the parameter is empty or
1138 * omitted, the query data will be unset.
1140 * Returns TRUE on success, or FALSE on failure.
1142 PHP_METHOD(HttpRequest
, setQueryData
)
1145 getObject(http_request_object
, obj
);
1147 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1151 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1152 UPD_STRL(obj
, queryData
, "", 0);
1153 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1154 char *query_data
= NULL
;
1156 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1160 UPD_PROP(obj
, string
, queryData
, query_data
);
1163 convert_to_string_ex(&qdata
);
1164 UPD_STRL(obj
, queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1170 /* {{{ proto string HttpRequest::getQueryData()
1172 * Get the current query data in form of an urlencoded query string.
1174 * Returns a string containing the urlencoded query.
1176 PHP_METHOD(HttpRequest
, getQueryData
)
1181 getObject(http_request_object
, obj
);
1182 zval
*qdata_p
, *qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
), &qdata_p
);
1184 RETURN_ZVAL(qdata
, 1, 0);
1187 zval_ptr_dtor(&qdata_p
);
1193 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1195 * Add parameters to the query parameter list, leaving previously set unchanged.
1196 * Affects any request type.
1198 * Expects an associative array as parameter containing the query fields to add.
1200 * Returns TRUE on success, or FALSE on failure.
1202 PHP_METHOD(HttpRequest
, addQueryData
)
1204 zval
*qdata
, *old_qdata
, *old_qdata_p
;
1205 char *query_data
= NULL
;
1206 size_t query_data_len
= 0;
1207 getObject(http_request_object
, obj
);
1209 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1213 old_qdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, queryData
), &old_qdata_p
);
1215 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
)) {
1217 zval_ptr_dtor(&old_qdata_p
);
1222 UPD_STRL(obj
, queryData
, query_data
, query_data_len
);
1226 zval_ptr_dtor(&old_qdata_p
);
1233 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1235 * Adds POST data entries, leaving previously set unchanged, unless a
1236 * post entry with the same name already exists.
1237 * Affects only POST and custom requests.
1239 * Expects an associative array as parameter containing the post fields.
1241 * Returns TRUE on success, or FALSE on failure.
1243 PHP_METHOD(HttpRequest
, addPostFields
)
1245 zval
*post
, *post_data
;
1246 getObject(http_request_object
, obj
);
1248 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1252 post
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFields
));
1253 array_merge(post_data
, post
);
1259 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1261 * Set the POST data entries, overwriting previously set POST data.
1262 * Affects only POST and custom requests.
1264 * Accepts an associative array as parameter containing the post fields.
1265 * If the parameter is empty or omitted, the post data will be unset.
1267 * Returns TRUE on success, or FALSE on failure.
1269 PHP_METHOD(HttpRequest
, setPostFields
)
1271 zval
*post
, *post_data
= NULL
;
1272 getObject(http_request_object
, obj
);
1274 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!", &post_data
)) {
1278 post
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFields
));
1281 zend_hash_clean(Z_ARRVAL_P(post
));
1282 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1283 array_copy(post_data
, post
);
1285 SET_PROP(obj
, postFields
, post
);
1291 /* {{{ proto array HttpRequest::getPostFields()
1293 * Get previously set POST data.
1295 * Returns the currently set post fields as associative array.
1297 PHP_METHOD(HttpRequest
, getPostFields
)
1302 getObject(http_request_object
, obj
);
1303 zval
*post_data_p
, *post_data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFields
), &post_data_p
);
1305 array_init(return_value
);
1306 array_copy(post_data
, return_value
);
1309 zval_ptr_dtor(&post_data_p
);
1315 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1317 * Set raw post data to send, overwriting previously set raw post data. Don't
1318 * forget to specify a content type. Affects only POST and custom requests.
1319 * Only either post fields or raw post data can be used for each request.
1320 * Raw post data has higher precedence and will be used even if post fields
1323 * Accepts a string as parameter containing the *raw* post data.
1325 * Returns TRUE on success, or FALSE on failure.
1327 PHP_METHOD(HttpRequest
, setRawPostData
)
1329 char *raw_data
= NULL
;
1331 getObject(http_request_object
, obj
);
1333 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1341 UPD_STRL(obj
, rawPostData
, raw_data
, data_len
);
1346 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1348 * Add raw post data, leaving previously set raw post data unchanged.
1349 * Affects only POST and custom requests.
1351 * Expects a string as parameter containing the raw post data to concatenate.
1353 * Returns TRUE on success, or FALSE on failure.
1355 PHP_METHOD(HttpRequest
, addRawPostData
)
1357 char *raw_data
, *new_data
;
1359 getObject(http_request_object
, obj
);
1361 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1366 zval
*zdata_p
, *zdata
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
), &zdata_p
);
1368 new_data
= emalloc(Z_STRLEN_P(zdata
) + data_len
+ 1);
1369 new_data
[Z_STRLEN_P(zdata
) + data_len
] = '\0';
1371 if (Z_STRLEN_P(zdata
)) {
1372 memcpy(new_data
, Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
));
1375 memcpy(new_data
+ Z_STRLEN_P(zdata
), raw_data
, data_len
);
1376 UPD_STRL(obj
, rawPostData
, new_data
, Z_STRLEN_P(zdata
) + data_len
);
1379 zval_ptr_dtor(&zdata_p
);
1387 /* {{{ proto string HttpRequest::getRawPostData()
1389 * Get previously set raw post data.
1391 * Returns a string containing the currently set raw post data.
1393 PHP_METHOD(HttpRequest
, getRawPostData
)
1398 getObject(http_request_object
, obj
);
1399 zval
*raw_data_p
, *raw_data
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, rawPostData
), &raw_data_p
);
1401 RETVAL_ZVAL(raw_data
, 1, 0);
1404 zval_ptr_dtor(&raw_data_p
);
1410 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1412 * Add a file to the POST request, leaving prefiously set files unchanged.
1413 * Affects only POST and custom requests. Cannot be used with raw post data.
1415 * Expects a string parameter containing the form element name, and a string
1416 * paremeter containing the path to the file which should be uploaded.
1417 * Additionally accepts an optional string parameter which chould contain
1418 * the content type of the file.
1420 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1421 * primary and a secondary content type part.
1423 PHP_METHOD(HttpRequest
, addPostFile
)
1425 zval
*files
, *entry
;
1426 char *name
, *file
, *type
= NULL
;
1427 int name_len
, file_len
, type_len
= 0;
1428 getObject(http_request_object
, obj
);
1430 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1435 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1437 type
= "application/x-octetstream";
1438 type_len
= sizeof("application/x-octetstream") - 1;
1441 MAKE_STD_ZVAL(entry
);
1444 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1445 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1446 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1448 files
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1449 add_next_index_zval(files
, entry
);
1455 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1457 * Set files to post, overwriting previously set post files.
1458 * Affects only POST and requests. Cannot be used with raw post data.
1460 * Accepts an array containing the files to post. Each entry should be an
1461 * associative array with "name", "file" and "type" keys. If the parameter
1462 * is empty or omitted the post files will be unset.
1464 * Returns TRUE on success, or FALSE on failure.
1466 PHP_METHOD(HttpRequest
, setPostFiles
)
1468 zval
*files
, *pFiles
;
1469 getObject(http_request_object
, obj
);
1471 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &files
)) {
1475 pFiles
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, postFiles
));
1478 zend_hash_clean(Z_ARRVAL_P(pFiles
));
1479 if (files
&& zend_hash_num_elements(Z_ARRVAL_P(files
))) {
1480 array_copy(files
, pFiles
);
1482 SET_PROP(obj
, postFiles
, pFiles
);
1488 /* {{{ proto array HttpRequest::getPostFiles()
1490 * Get all previously added POST files.
1492 * Returns an array containing currently set post files.
1494 PHP_METHOD(HttpRequest
, getPostFiles
)
1499 getObject(http_request_object
, obj
);
1500 zval
*files_p
, *files
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, postFiles
), &files_p
);
1502 array_init(return_value
);
1503 array_copy(files
, return_value
);
1506 zval_ptr_dtor(&files_p
);
1512 /* {{{ proto bool HttpRequest::setPutFile([string file])
1514 * Set file to put. Affects only PUT requests.
1516 * Accepts a string as parameter referencing the path to file.
1517 * If the parameter is empty or omitted the put file will be unset.
1519 * Returns TRUE on success, or FALSE on failure.
1521 PHP_METHOD(HttpRequest
, setPutFile
)
1525 getObject(http_request_object
, obj
);
1527 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1531 UPD_STRL(obj
, putFile
, file
, file_len
);
1536 /* {{{ proto string HttpRequest::getPutFile()
1538 * Get previously set put file.
1540 * Returns a string containing the path to the currently set put file.
1542 PHP_METHOD(HttpRequest
, getPutFile
)
1547 getObject(http_request_object
, obj
);
1548 zval
*putfile_p
, *putfile
= convert_to_type_ex(IS_STRING
, GET_PROP(obj
, putFile
), &putfile_p
);
1550 RETVAL_ZVAL(putfile
, 1, 0);
1553 zval_ptr_dtor(&putfile_p
);
1559 /* {{{ proto array HttpRequest::getResponseData()
1561 * Get all response data after the request has been sent.
1563 * Returns an associative array with the key "headers" containing an associative
1564 * array holding all response headers, as well as the ley "body" containing a
1565 * string with the response body.
1567 * If redirects were allowed and several responses were received, the data
1568 * references the last received response.
1570 PHP_METHOD(HttpRequest
, getResponseData
)
1575 getObject(http_request_object
, obj
);
1576 zval
*data_p
, *data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
), &data_p
);
1578 array_init(return_value
);
1579 array_copy(data
, return_value
);
1582 zval_ptr_dtor(&data_p
);
1588 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1590 * Get response header(s) after the request has been sent.
1592 * Accepts an string as optional parameter specifying a certain header to read.
1593 * If the parameter is empty or omitted all response headers will be returned.
1595 * Returns either a string with the value of the header matching name if requested,
1596 * FALSE on failure, or an associative array containing all reponse headers.
1598 * If redirects were allowed and several responses were received, the data
1599 * references the last received response.
1601 PHP_METHOD(HttpRequest
, getResponseHeader
)
1604 zval
*data_p
, *data
, **headers
, **header
;
1605 char *header_name
= NULL
;
1607 getObject(http_request_object
, obj
);
1609 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1613 data
= convert_to_type_ex(IS_ARRAY
, GET_PROP(obj
, responseData
), &data_p
);
1614 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1615 convert_to_array(*headers
);
1616 if (!header_len
|| !header_name
) {
1617 array_init(return_value
);
1618 array_copy(*headers
, return_value
);
1619 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1620 RETVAL_ZVAL(*header
, 1, 0);
1629 zval_ptr_dtor(&data_p
);
1635 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1637 * Get response cookie(s) after the request has been sent.
1639 * Accepts a string as optional parameter specifying the name of the cookie to read.
1640 * If the parameter is empty or omitted, an associative array with all received
1641 * cookies will be returned.
1643 * Returns either an associative array with the cookie's name, value and any
1644 * additional params of the cookie matching name if requested, FALSE on failure,
1645 * or an array containing all received cookies as arrays.
1647 * If redirects were allowed and several responses were received, the data
1648 * references the last received response.
1650 PHP_METHOD(HttpRequest
, getResponseCookie
)
1653 zval
*data
, **headers
;
1654 char *cookie_name
= NULL
;
1656 getObject(http_request_object
, obj
);
1658 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1662 array_init(return_value
);
1664 data
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
));
1665 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1668 zval
**header
= NULL
;
1670 convert_to_array_ex(headers
);
1671 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1672 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1673 /* several cookies? */
1674 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1677 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1679 MAKE_STD_ZVAL(cookie_hash
);
1680 array_init(cookie_hash
);
1682 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1684 add_next_index_zval(return_value
, cookie_hash
);
1688 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1689 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1690 add_next_index_zval(return_value
, cookie_hash
);
1691 return; /* <<< FOUND >>> */
1693 zval_dtor(cookie_hash
);
1698 zval_dtor(cookie_hash
);
1705 MAKE_STD_ZVAL(cookie_hash
);
1706 array_init(cookie_hash
);
1707 convert_to_string_ex(header
);
1709 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1711 add_next_index_zval(return_value
, cookie_hash
);
1715 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1716 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1717 add_next_index_zval(return_value
, cookie_hash
);
1719 zval_dtor(cookie_hash
);
1724 zval_dtor(cookie_hash
);
1738 /* {{{ proto string HttpRequest::getResponseBody()
1740 * Get the response body after the request has been sent.
1742 * Returns a string containing the response body.
1744 * If redirects were allowed and several responses were received, the data
1745 * references the last received response.
1747 PHP_METHOD(HttpRequest
, getResponseBody
)
1753 getObject(http_request_object
, obj
);
1754 zval
*data
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseData
));
1756 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1757 RETURN_ZVAL(*body
, 1, 0);
1765 /* {{{ proto int HttpRequest::getResponseCode()
1767 * Get the response code after the request has been sent.
1769 * Returns an int representing the response code.
1771 * If redirects were allowed and several responses were received, the data
1772 * references the last received response.
1774 PHP_METHOD(HttpRequest
, getResponseCode
)
1779 getObject(http_request_object
, obj
);
1780 zval
*code_p
, *code
= convert_to_type_ex(IS_LONG
, GET_PROP(obj
, responseCode
), &code_p
);
1782 RETVAL_ZVAL(code
, 1, 0);
1785 zval_ptr_dtor(&code_p
);
1791 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1793 * Get response info after the request has been sent.
1794 * See http_get() for a full list of returned info.
1796 * Accepts a string as optional parameter specifying the info to read.
1797 * If the parameter is empty or omitted, an associative array containing
1798 * all available info will be returned.
1800 * Returns either a scalar containing the value of the info matching name if
1801 * requested, FALSE on failure, or an associative array containing all
1804 * If redirects were allowed and several responses were received, the data
1805 * references the last received response.
1807 PHP_METHOD(HttpRequest
, getResponseInfo
)
1810 zval
*info
, **infop
;
1811 char *info_name
= NULL
;
1813 getObject(http_request_object
, obj
);
1815 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1819 info
= convert_to_type(IS_ARRAY
, GET_PROP(obj
, responseInfo
));
1821 if (info_len
&& info_name
) {
1822 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1823 RETURN_ZVAL(*infop
, 1, 0);
1825 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1829 array_init(return_value
);
1830 array_copy(info
, return_value
);
1836 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1838 * Get the full response as HttpMessage object after the request has been sent.
1840 * Returns an HttpMessage object of the response.
1842 * If redirects were allowed and several responses were received, the data
1843 * references the last received response. Use HttpMessage::getParentMessage()
1844 * to access the data of previously received responses whithin this request
1847 * Throws HttpException.
1849 PHP_METHOD(HttpRequest
, getResponseMessage
)
1855 getObject(http_request_object
, obj
);
1857 SET_EH_THROW_HTTP();
1858 message
= GET_PROP(obj
, responseMessage
);
1859 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1860 RETVAL_OBJECT(message
);
1869 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1871 * Get sent HTTP message.
1873 * Returns an HttpMessage object representing the sent request.
1875 * If redirects were allowed and several responses were received, the data
1876 * references the last received response. Use HttpMessage::getParentMessage()
1877 * to access the data of previously sent requests whithin this request
1880 PHP_METHOD(HttpRequest
, getRequestMessage
)
1886 getObject(http_request_object
, obj
);
1888 SET_EH_THROW_HTTP();
1889 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->request
), PHPSTR_LEN(&obj
->request
))) {
1890 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1897 /* {{{ proto HttpMessage HttpRequest::getHistory()
1899 * Get all sent requests and received responses as an HttpMessage object.
1901 * If you don't want to record history at all, set the instance variable
1902 * HttpRequest::$recoedHistory to FALSE.
1904 * Returns an HttpMessage object representing the complete request/response
1907 * The object references the last received response, use HttpMessage::getParentMessage()
1908 * to access the data of previously sent requests and received responses.
1910 * Throws HttpMalformedHeaderException.
1912 PHP_METHOD(HttpRequest
, getHistory
)
1918 getObject(http_request_object
, obj
);
1920 SET_EH_THROW_HTTP();
1921 if (msg
= http_message_parse(PHPSTR_VAL(&obj
->history
), PHPSTR_LEN(&obj
->history
))) {
1922 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1929 /* {{{ proto void HttpRequest::clearHistory()
1931 * Clear the history.
1933 PHP_METHOD(HttpRequest
, clearHistory
)
1936 getObject(http_request_object
, obj
);
1937 phpstr_dtor(&obj
->history
);
1942 /* {{{ proto HttpMessage HttpRequest::send()
1944 * Send the HTTP request.
1946 * Returns the received response as HttpMessage object.
1948 * Throws HttpRuntimeException, HttpRequestException,
1949 * HttpMalformedHeaderException, HttpEncodingException.
1954 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1955 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1956 * $r->addQueryData(array('category' => 3));
1959 * if ($r->getResponseCode() == 200) {
1960 * file_put_contents('local.rss', $r->getResponseBody());
1962 * } catch (HttpException $ex) {
1971 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1972 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1973 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1974 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1976 * echo $r->send()->getBody();
1977 * } catch (HttpException $ex) {
1983 PHP_METHOD(HttpRequest
, send
)
1985 http_request_body body
= {0, NULL
, 0};
1986 getObject(http_request_object
, obj
);
1990 SET_EH_THROW_HTTP();
1993 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
2000 if ( (SUCCESS
== http_request_object_requesthandler(obj
, getThis(), &body
)) &&
2001 (SUCCESS
== http_request_exec(obj
->ch
, NULL
, &obj
->response
, &obj
->request
)) &&
2002 (SUCCESS
== http_request_object_responsehandler(obj
, getThis()))) {
2003 RETVAL_OBJECT(GET_PROP(obj
, responseMessage
));
2005 http_request_body_dtor(&body
);
2011 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
2018 * vim600: noet sw=4 ts=4 fdm=marker
2019 * vim<600: noet sw=4 ts=4