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_cookie_api.h"
24 #include "php_http_exception_object.h"
25 #include "php_http_message_api.h"
26 #include "php_http_message_object.h"
27 #include "php_http_request_api.h"
28 #include "php_http_request_object.h"
29 #include "php_http_request_pool_api.h"
30 #include "php_http_url_api.h"
32 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, 0, req_args)
33 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpRequest, method, 0)
34 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
35 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
37 HTTP_BEGIN_ARGS(__construct
, 0)
39 HTTP_ARG_VAL(method
, 0)
40 HTTP_ARG_VAL(options
, 0)
43 HTTP_EMPTY_ARGS(getOptions
);
44 HTTP_BEGIN_ARGS(setOptions
, 0)
45 HTTP_ARG_VAL(options
, 0)
48 HTTP_EMPTY_ARGS(getSslOptions
);
49 HTTP_BEGIN_ARGS(setSslOptions
, 0)
50 HTTP_ARG_VAL(ssl_options
, 0)
53 HTTP_BEGIN_ARGS(addSslOptions
, 0)
54 HTTP_ARG_VAL(ssl_optins
, 0)
57 HTTP_EMPTY_ARGS(getHeaders
);
58 HTTP_BEGIN_ARGS(setHeaders
, 0)
59 HTTP_ARG_VAL(headers
, 0)
62 HTTP_BEGIN_ARGS(addHeaders
, 1)
63 HTTP_ARG_VAL(headers
, 0)
66 HTTP_EMPTY_ARGS(getCookies
);
67 HTTP_BEGIN_ARGS(setCookies
, 0)
68 HTTP_ARG_VAL(cookies
, 0)
71 HTTP_BEGIN_ARGS(addCookies
, 1)
72 HTTP_ARG_VAL(cookies
, 0)
75 HTTP_EMPTY_ARGS(getUrl
);
76 HTTP_BEGIN_ARGS(setUrl
, 1)
80 HTTP_EMPTY_ARGS(getMethod
);
81 HTTP_BEGIN_ARGS(setMethod
, 1)
82 HTTP_ARG_VAL(request_method
, 0)
85 HTTP_EMPTY_ARGS(getContentType
);
86 HTTP_BEGIN_ARGS(setContentType
, 1)
87 HTTP_ARG_VAL(content_type
, 0)
90 HTTP_EMPTY_ARGS(getQueryData
);
91 HTTP_BEGIN_ARGS(setQueryData
, 0)
92 HTTP_ARG_VAL(query_data
, 0)
95 HTTP_BEGIN_ARGS(addQueryData
, 1)
96 HTTP_ARG_VAL(query_data
, 0)
99 HTTP_EMPTY_ARGS(getPostFields
);
100 HTTP_BEGIN_ARGS(setPostFields
, 0)
101 HTTP_ARG_VAL(post_fields
, 0)
104 HTTP_BEGIN_ARGS(addPostFields
, 1)
105 HTTP_ARG_VAL(post_fields
, 0)
108 HTTP_EMPTY_ARGS(getPostFiles
);
109 HTTP_BEGIN_ARGS(setPostFiles
, 0)
110 HTTP_ARG_VAL(post_files
, 0)
113 HTTP_BEGIN_ARGS(addPostFile
, 2)
114 HTTP_ARG_VAL(formname
, 0)
115 HTTP_ARG_VAL(filename
, 0)
116 HTTP_ARG_VAL(content_type
, 0)
119 HTTP_EMPTY_ARGS(getRawPostData
);
120 HTTP_BEGIN_ARGS(setRawPostData
, 0)
121 HTTP_ARG_VAL(raw_post_data
, 0)
124 HTTP_BEGIN_ARGS(addRawPostData
, 1)
125 HTTP_ARG_VAL(raw_post_data
, 0)
128 HTTP_EMPTY_ARGS(getPutFile
);
129 HTTP_BEGIN_ARGS(setPutFile
, 0)
130 HTTP_ARG_VAL(filename
, 0)
133 HTTP_EMPTY_ARGS(getPutData
);
134 HTTP_BEGIN_ARGS(setPutData
, 0)
135 HTTP_ARG_VAL(put_data
, 0)
138 HTTP_BEGIN_ARGS(addPutData
, 1)
139 HTTP_ARG_VAL(put_data
, 0)
142 HTTP_EMPTY_ARGS(getResponseData
);
143 HTTP_BEGIN_ARGS(getResponseHeader
, 0)
144 HTTP_ARG_VAL(name
, 0)
147 HTTP_BEGIN_ARGS(getResponseCookies
, 0)
148 HTTP_ARG_VAL(flags
, 0)
149 HTTP_ARG_VAL(allowed_extras
, 0)
152 HTTP_EMPTY_ARGS(getResponseBody
);
153 HTTP_EMPTY_ARGS(getResponseCode
);
154 HTTP_EMPTY_ARGS(getResponseStatus
);
155 HTTP_BEGIN_ARGS(getResponseInfo
, 0)
156 HTTP_ARG_VAL(name
, 0)
159 HTTP_EMPTY_ARGS(getResponseMessage
);
160 HTTP_EMPTY_ARGS(getRawResponseMessage
);
161 HTTP_EMPTY_ARGS(getRequestMessage
);
162 HTTP_EMPTY_ARGS(getRawRequestMessage
);
163 HTTP_EMPTY_ARGS(getHistory
);
164 HTTP_EMPTY_ARGS(clearHistory
);
165 HTTP_EMPTY_ARGS(send
);
167 HTTP_BEGIN_ARGS(get
, 1)
169 HTTP_ARG_VAL(options
, 0)
170 HTTP_ARG_VAL(info
, 1)
173 HTTP_BEGIN_ARGS(head
, 1)
175 HTTP_ARG_VAL(options
, 0)
176 HTTP_ARG_VAL(info
, 1)
179 HTTP_BEGIN_ARGS(postData
, 2)
181 HTTP_ARG_VAL(data
, 0)
182 HTTP_ARG_VAL(options
, 0)
183 HTTP_ARG_VAL(info
, 1)
186 HTTP_BEGIN_ARGS(postFields
, 2)
188 HTTP_ARG_VAL(data
, 0)
189 HTTP_ARG_VAL(options
, 0)
190 HTTP_ARG_VAL(info
, 1)
193 HTTP_BEGIN_ARGS(putFile
, 2)
195 HTTP_ARG_VAL(file
, 0)
196 HTTP_ARG_VAL(options
, 0)
197 HTTP_ARG_VAL(info
, 1)
200 HTTP_BEGIN_ARGS(putStream
, 2)
202 HTTP_ARG_VAL(stream
, 0)
203 HTTP_ARG_VAL(options
, 0)
204 HTTP_ARG_VAL(info
, 1)
207 HTTP_BEGIN_ARGS(methodRegister
, 1)
208 HTTP_ARG_VAL(method_name
, 0)
211 HTTP_BEGIN_ARGS(methodUnregister
, 1)
212 HTTP_ARG_VAL(method
, 0)
215 HTTP_BEGIN_ARGS(methodName
, 1)
216 HTTP_ARG_VAL(method_id
, 0)
219 HTTP_BEGIN_ARGS(methodExists
, 1)
220 HTTP_ARG_VAL(method
, 0)
223 #define OBJ_PROP_CE http_request_object_ce
224 zend_class_entry
*http_request_object_ce
;
225 zend_function_entry http_request_object_fe
[] = {
226 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
228 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
229 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
230 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
231 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
232 HTTP_REQUEST_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
234 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
235 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
236 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
238 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
239 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
240 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
242 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
243 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
245 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
246 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
248 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
249 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
251 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
252 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
253 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
255 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
256 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
257 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
260 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
261 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
263 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
265 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
270 HTTP_REQUEST_ME(setPutData
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(getPutData
, ZEND_ACC_PUBLIC
)
272 HTTP_REQUEST_ME(addPutData
, ZEND_ACC_PUBLIC
)
274 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
277 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
278 HTTP_REQUEST_ME(getResponseCookies
, ZEND_ACC_PUBLIC
)
279 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
280 HTTP_REQUEST_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
281 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
282 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
283 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
284 HTTP_REQUEST_ME(getRawResponseMessage
, ZEND_ACC_PUBLIC
)
285 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
286 HTTP_REQUEST_ME(getRawRequestMessage
, ZEND_ACC_PUBLIC
)
287 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
288 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
290 HTTP_REQUEST_ALIAS(get
, http_get
)
291 HTTP_REQUEST_ALIAS(head
, http_head
)
292 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
293 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
294 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
295 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
297 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
298 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
299 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
300 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
304 static zend_object_handlers http_request_object_handlers
;
306 PHP_MINIT_FUNCTION(http_request_object
)
308 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
309 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
311 DCL_PROP_N(PRIVATE
, options
);
312 DCL_PROP_N(PRIVATE
, postFields
);
313 DCL_PROP_N(PRIVATE
, postFiles
);
314 DCL_PROP_N(PRIVATE
, responseInfo
);
315 DCL_PROP_N(PRIVATE
, responseData
);
316 DCL_PROP_N(PRIVATE
, responseMessage
);
317 DCL_PROP(PRIVATE
, long, responseCode
, 0);
318 DCL_PROP(PRIVATE
, string
, responseStatus
, "");
319 DCL_PROP(PRIVATE
, long, method
, HTTP_GET
);
320 DCL_PROP(PRIVATE
, string
, url
, "");
321 DCL_PROP(PRIVATE
, string
, contentType
, "");
322 DCL_PROP(PRIVATE
, string
, rawPostData
, "");
323 DCL_PROP(PRIVATE
, string
, queryData
, "");
324 DCL_PROP(PRIVATE
, string
, putFile
, "");
325 DCL_PROP(PRIVATE
, string
, putData
, "");
326 DCL_PROP_N(PRIVATE
, history
);
327 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
331 * Request Method Constants
334 DCL_CONST(long, "METH_GET", HTTP_GET
);
335 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
336 DCL_CONST(long, "METH_POST", HTTP_POST
);
337 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
338 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
339 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
340 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
341 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
342 /* WebDAV - RFC 2518 */
343 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
344 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
345 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
346 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
347 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
348 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
349 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
350 /* WebDAV Versioning - RFC 3253 */
351 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
352 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
353 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
354 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
355 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
356 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
357 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
358 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
359 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
360 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
361 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
362 /* WebDAV Access Control - RFC 3744 */
363 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
366 * HTTP Protocol Version Constants
368 DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0
);
369 DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1
);
370 DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE
);
375 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
376 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
377 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
378 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
381 * Proxy Type Constants
383 # if HTTP_CURL_VERSION(7,15,2)
384 DCL_CONST(long, "PROXY_SOCKS4", CURLPROXY_SOCKS4
);
386 DCL_CONST(long, "PROXY_SOCKS5", CURLPROXY_SOCKS5
);
387 DCL_CONST(long, "PROXY_HTTP", CURLPROXY_HTTP
);
393 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
395 return http_request_object_new_ex(ce
, NULL
, NULL
);
398 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
400 zend_object_value ov
;
401 http_request_object
*o
;
403 o
= ecalloc(1, sizeof(http_request_object
));
405 o
->request
= http_request_init_ex(NULL
, ch
, 0, NULL
);
411 ALLOC_HASHTABLE(OBJ_PROP(o
));
412 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
413 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
415 ov
.handle
= putObject(http_request_object
, o
);
416 ov
.handlers
= &http_request_object_handlers
;
421 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
424 zend_object_value new_ov
;
425 http_request_object
*new_obj
;
426 getObject(http_request_object
, old_obj
);
428 old_zo
= zend_objects_get_address(this_ptr TSRMLS_CC
);
429 new_ov
= http_request_object_new_ex(old_zo
->ce
, NULL
, &new_obj
);
430 if (old_obj
->request
->ch
) {
431 http_curl_init_ex(curl_easy_duphandle(old_obj
->request
->ch
), new_obj
->request
);
434 zend_objects_clone_members(&new_obj
->zo
, new_ov
, old_zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
435 phpstr_append(&new_obj
->request
->conv
.request
, old_obj
->request
->conv
.request
.data
, old_obj
->request
->conv
.request
.used
);
436 phpstr_append(&new_obj
->request
->conv
.response
, old_obj
->request
->conv
.response
.data
, old_obj
->request
->conv
.response
.used
);
441 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
443 http_request_object
*o
= (http_request_object
*) object
;
446 zend_hash_destroy(OBJ_PROP(o
));
447 FREE_HASHTABLE(OBJ_PROP(o
));
449 http_request_free(&o
->request
);
453 #define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC)
454 static inline void _http_request_object_check_request_content_type(zval
*this_ptr TSRMLS_DC
)
456 zval
*ctype
= GET_PROP(contentType
);
458 if (Z_STRLEN_P(ctype
)) {
459 zval
**headers
, *opts
= GET_PROP(options
);
461 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
462 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void *) &headers
)) &&
463 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
466 /* only override if not already set */
467 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void *) &ct_header
))) {
468 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
470 /* or not a string, zero length string or a string of spaces */
471 if ((Z_TYPE_PP(ct_header
) != IS_STRING
) || !Z_STRLEN_PP(ct_header
)) {
472 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
474 int i
, only_space
= 1;
476 /* check for spaces only */
477 for (i
= 0; i
< Z_STRLEN_PP(ct_header
); ++i
) {
478 if (!isspace(Z_STRVAL_PP(ct_header
)[i
])) {
484 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
490 MAKE_STD_ZVAL(headers
);
492 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
493 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, headers
);
494 zval_ptr_dtor(&headers
);
499 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
501 STATUS status
= SUCCESS
;
503 http_request_reset(obj
->request
);
504 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, http_curl_init(obj
->request
), return FAILURE
);
506 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(GET_PROP(url
)));
508 switch (obj
->request
->meth
= Z_LVAL_P(GET_PROP(method
)))
516 zval
*put_data
= GET_PROP(putData
);
518 http_request_object_check_request_content_type(getThis());
519 if (Z_STRLEN_P(put_data
)) {
520 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
521 estrndup(Z_STRVAL_P(put_data
), Z_STRLEN_P(put_data
)), Z_STRLEN_P(put_data
), 1);
523 php_stream_statbuf ssb
;
524 php_stream
*stream
= php_stream_open_wrapper_ex(Z_STRVAL_P(GET_PROP(putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
, HTTP_DEFAULT_STREAM_CONTEXT
);
526 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
527 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
538 /* check for raw post data */
539 zval
*raw_data
= GET_PROP(rawPostData
);
541 if (Z_STRLEN_P(raw_data
)) {
542 http_request_object_check_request_content_type(getThis());
543 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
544 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
546 zval
*zfields
= GET_PROP(postFields
), *zfiles
= GET_PROP(postFiles
);
550 fields
= (Z_TYPE_P(zfields
) == IS_ARRAY
) ? Z_ARRVAL_P(zfields
) : NULL
;
551 files
= (Z_TYPE_P(zfiles
) == IS_ARRAY
) ? Z_ARRVAL_P(zfiles
) : NULL
;
553 if ((fields
&& zend_hash_num_elements(fields
)) || (files
&& zend_hash_num_elements(files
))) {
554 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
563 if (status
== SUCCESS
) {
564 zval
*qdata
= GET_PROP(queryData
);
565 zval
*options
= GET_PROP(options
);
567 if (Z_STRLEN_P(qdata
)) {
568 if (!strchr(obj
->request
->url
, '?')) {
569 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
571 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
573 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
576 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
578 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
579 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onprogress", sizeof("onprogress"))) {
582 if ( (Z_TYPE_P(options
) != IS_ARRAY
)
583 || (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(options
), "onprogress", sizeof("onprogress"), (void *) &entry
)
584 || (!zval_is_true(*entry
)))) {
587 ZVAL_ADDREF(getThis());
588 add_next_index_zval(pcb
, getThis());
589 add_next_index_stringl(pcb
, "onprogress", lenof("onprogress"), 1);
590 http_request_set_progress_callback(obj
->request
, pcb
);
599 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
605 /* always fetch info */
608 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
609 SET_PROP(responseInfo
, info
);
610 zval_ptr_dtor(&info
);
612 /* parse response message */
613 phpstr_fix(&obj
->request
->conv
.request
);
614 phpstr_fix(&obj
->request
->conv
.response
);
616 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
)))) {
619 zval
*headers
, *message
, *resp
;
621 if (zval_is_true(GET_PROP(recordHistory
))) {
622 zval
*hist
, *history
= GET_PROP(history
);
623 http_message
*response
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
));
624 http_message
*request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
627 ZVAL_OBJVAL(hist
, http_message_object_new_ex(http_message_object_ce
, http_message_interconnect(response
, request
), NULL
), 0);
628 if (Z_TYPE_P(history
) == IS_OBJECT
) {
629 http_message_object_prepend(hist
, history
);
631 SET_PROP(history
, hist
);
632 zval_ptr_dtor(&hist
);
635 UPD_PROP(long, responseCode
, msg
->http
.info
.response
.code
);
636 UPD_PROP(string
, responseStatus
, msg
->http
.info
.response
.status
? msg
->http
.info
.response
.status
: "");
640 MAKE_STD_ZVAL(headers
);
642 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
643 add_assoc_zval(resp
, "headers", headers
);
644 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
645 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
646 SET_PROP(responseData
, resp
);
647 zval_ptr_dtor(&resp
);
649 MAKE_STD_ZVAL(message
);
650 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
651 SET_PROP(responseMessage
, message
);
652 zval_ptr_dtor(&message
);
656 /* update properties with empty values*/
657 zval
*resp
= GET_PROP(responseData
), *znull
;
659 MAKE_STD_ZVAL(znull
);
661 SET_PROP(responseMessage
, znull
);
662 zval_ptr_dtor(&znull
);
664 if (Z_TYPE_P(resp
) == IS_ARRAY
) {
665 zend_hash_clean(Z_ARRVAL_P(resp
));
668 UPD_PROP(long, responseCode
, 0);
669 UPD_PROP(string
, responseStatus
, "");
671 /* append request message to history */
672 if (zval_is_true(GET_PROP(recordHistory
))) {
673 http_message
*request
;
675 if ((request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
676 zval
*hist
, *history
= GET_PROP(history
);
679 ZVAL_OBJVAL(hist
, http_message_object_new_ex(http_message_object_ce
, request
, NULL
), 0);
680 if (Z_TYPE_P(history
) == IS_OBJECT
) {
681 http_message_object_prepend(hist
, history
);
683 SET_PROP(history
, hist
);
684 zval_ptr_dtor(&hist
);
691 if (!EG(exception
) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onfinish", sizeof("onfinish"))) {
694 MAKE_STD_ZVAL(param
);
695 ZVAL_BOOL(param
, ret
== SUCCESS
);
696 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "onfinish", NULL
, param
);
697 zval_ptr_dtor(¶m
);
703 static int apply_pretty_key(void *pDest
, int num_args
, va_list args
, zend_hash_key
*hash_key
)
705 if (hash_key
->nKeyLength
> 1) {
706 hash_key
->h
= zend_get_hash_value(pretty_key(hash_key
->arKey
, hash_key
->nKeyLength
- 1, 1, 0), hash_key
->nKeyLength
);
708 return ZEND_HASH_APPLY_KEEP
;
711 #define http_request_object_set_options_subr(key, ow, pk) \
712 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
713 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
, int prettify_keys
)
715 zval
*old_opts
, *new_opts
, *opts
= NULL
, **entry
= NULL
;
717 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
721 MAKE_STD_ZVAL(new_opts
);
722 array_init(new_opts
);
723 old_opts
= GET_PROP(options
);
724 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
725 array_copy(old_opts
, new_opts
);
728 if (prettify_keys
&& opts
) {
729 zend_hash_apply_with_arguments(Z_ARRVAL_P(opts
), apply_pretty_key
, 0);
731 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), key
, len
, (void *) &entry
)) {
733 zend_hash_clean(Z_ARRVAL_PP(entry
));
735 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
737 array_copy(opts
, *entry
);
739 array_merge(opts
, *entry
);
744 add_assoc_zval(new_opts
, key
, opts
);
746 SET_PROP(options
, new_opts
);
747 zval_ptr_dtor(&new_opts
);
752 #define http_request_object_get_options_subr(key) \
753 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
754 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
759 zval
*opts
, **options
;
761 opts
= GET_PROP(options
);
762 array_init(return_value
);
764 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
765 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void *) &options
))) {
766 convert_to_array(*options
);
767 array_copy(*options
, return_value
);
773 /* ### USERLAND ### */
775 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
777 * Instantiate a new HttpRequest object.
779 * Accepts a string as optional parameter containing the target request url.
780 * Additionally accepts an optional int parameter specifying the request method
781 * to use and an associative array as optional third parameter which will be
782 * passed to HttpRequest::setOptions().
784 * Throws HttpException.
786 PHP_METHOD(HttpRequest
, __construct
)
791 zval
*options
= NULL
;
794 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
796 UPD_STRL(url
, URL
, URL_len
);
799 UPD_PROP(long, method
, meth
);
802 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
809 /* {{{ proto bool HttpRequest::setOptions([array options])
811 * Set the request options to use. See http_get() for a full list of available options.
813 * Accepts an array as optional parameters, which values will overwrite the
814 * currently set request options. If the parameter is empty or omitted,
815 * the options of the HttpRequest object will be reset.
817 * Returns TRUE on success, or FALSE on failure.
819 PHP_METHOD(HttpRequest
, setOptions
)
824 zval
*opts
= NULL
, *old_opts
, *new_opts
, *add_opts
, **opt
;
826 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
830 MAKE_STD_ZVAL(new_opts
);
831 array_init(new_opts
);
833 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
834 SET_PROP(options
, new_opts
);
835 zval_ptr_dtor(&new_opts
);
839 MAKE_STD_ZVAL(add_opts
);
840 array_init(add_opts
);
841 /* some options need extra attention -- thus cannot use array_merge() directly */
842 FOREACH_KEYVAL(pos
, opts
, key
, idx
, opt
) {
844 if (!strcmp(key
, "headers")) {
845 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, *opt
);
846 } else if (!strcmp(key
, "cookies")) {
847 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addcookies", NULL
, *opt
);
848 } else if (!strcmp(key
, "ssl")) {
849 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addssloptions", NULL
, *opt
);
850 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
851 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "seturl", NULL
, *opt
);
852 } else if (!strcmp(key
, "method")) {
853 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setmethod", NULL
, *opt
);
856 add_assoc_zval(add_opts
, key
, *opt
);
863 old_opts
= GET_PROP(options
);
864 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
865 array_copy(old_opts
, new_opts
);
867 array_merge(add_opts
, new_opts
);
868 SET_PROP(options
, new_opts
);
869 zval_ptr_dtor(&new_opts
);
870 zval_ptr_dtor(&add_opts
);
876 /* {{{ proto array HttpRequest::getOptions()
878 * Get currently set options.
880 * Returns an associative array containing currently set options.
882 PHP_METHOD(HttpRequest
, getOptions
)
887 RETURN_PROP(options
);
892 /* {{{ proto bool HttpRequest::setSslOptions([array options])
896 * Accepts an associative array as parameter containing any SSL specific options.
897 * If the parameter is empty or omitted, the SSL options will be reset.
899 * Returns TRUE on success, or FALSE on failure.
901 PHP_METHOD(HttpRequest
, setSslOptions
)
903 http_request_object_set_options_subr("ssl", 1, 0);
907 /* {{{ proto bool HttpRequest::addSslOptions(array options)
909 * Set additional SSL options.
911 * Expects an associative array as parameter containing additional SSL specific options.
913 * Returns TRUE on success, or FALSE on failure.
915 PHP_METHOD(HttpRequest
, addSslOptions
)
917 http_request_object_set_options_subr("ssl", 0, 0);
921 /* {{{ proto array HttpRequest::getSslOtpions()
923 * Get previously set SSL options.
925 * Returns an associative array containing any previously set SSL options.
927 PHP_METHOD(HttpRequest
, getSslOptions
)
929 http_request_object_get_options_subr("ssl");
933 /* {{{ proto bool HttpRequest::addHeaders(array headers)
935 * Add request header name/value pairs.
937 * Expects an associative array as parameter containing additional header
940 * Returns TRUE on success, or FALSE on failure.
942 PHP_METHOD(HttpRequest
, addHeaders
)
944 http_request_object_set_options_subr("headers", 0, 1);
947 /* {{{ proto bool HttpRequest::setHeaders([array headers])
949 * Set request header name/value pairs.
951 * Accepts an associative array as parameter containing header name/value pairs.
952 * If the parameter is empty or omitted, all previously set headers will be unset.
954 * Returns TRUE on success, or FALSE on failure.
956 PHP_METHOD(HttpRequest
, setHeaders
)
958 http_request_object_set_options_subr("headers", 1, 1);
962 /* {{{ proto array HttpRequest::getHeaders()
964 * Get previously set request headers.
966 * Returns an associative array containing all currently set headers.
968 PHP_METHOD(HttpRequest
, getHeaders
)
970 http_request_object_get_options_subr("headers");
974 /* {{{ proto bool HttpRequest::setCookies([array cookies])
978 * Accepts an associative array as parameter containing cookie name/value pairs.
979 * If the parameter is empty or omitted, all previously set cookies will be unset.
981 * Returns TRUE on success, or FALSE on failure.
983 PHP_METHOD(HttpRequest
, setCookies
)
985 http_request_object_set_options_subr("cookies", 1, 0);
989 /* {{{ proto bool HttpRequest::addCookies(array cookies)
993 * Expects an associative array as parameter containing any cookie name/value
996 * Returns TRUE on success, or FALSE on failure.
998 PHP_METHOD(HttpRequest
, addCookies
)
1000 http_request_object_set_options_subr("cookies", 0, 0);
1004 /* {{{ proto array HttpRequest::getCookies()
1006 * Get previously set cookies.
1008 * Returns an associative array containing any previously set cookies.
1010 PHP_METHOD(HttpRequest
, getCookies
)
1012 http_request_object_get_options_subr("cookies");
1016 /* {{{ proto bool HttpRequest::setUrl(string url)
1018 * Set the request URL.
1020 * Expects a string as parameter specifying the request url.
1022 * Returns TRUE on success, or FALSE on failure.
1024 PHP_METHOD(HttpRequest
, setUrl
)
1029 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1033 UPD_STRL(url
, URL
, URL_len
);
1038 /* {{{ proto string HttpRequest::getUrl()
1040 * Get the previously set request URL.
1042 * Returns the currently set request url as string.
1044 PHP_METHOD(HttpRequest
, getUrl
)
1054 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1056 * Set the request method.
1058 * Expects an int as parameter specifying the request method to use.
1059 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
1061 * Returns TRUE on success, or FALSE on failure.
1063 PHP_METHOD(HttpRequest
, setMethod
)
1067 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1071 UPD_PROP(long, method
, meth
);
1076 /* {{{ proto int HttpRequest::getMethod()
1078 * Get the previously set request method.
1080 * Returns the currently set request method.
1082 PHP_METHOD(HttpRequest
, getMethod
)
1087 RETURN_PROP(method
);
1092 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1094 * Set the content type the post request should have.
1096 * Expects a string as parameters containing the content type of the request
1097 * (primary/secondary).
1099 * Returns TRUE on success, or FALSE if the content type does not seem to
1100 * contain a primary and a secondary part.
1102 PHP_METHOD(HttpRequest
, setContentType
)
1107 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1112 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1114 UPD_STRL(contentType
, ctype
, ct_len
);
1119 /* {{{ proto string HttpRequest::getContentType()
1121 * Get the previously content type.
1123 * Returns the previously set content type as string.
1125 PHP_METHOD(HttpRequest
, getContentType
)
1130 RETURN_PROP(contentType
);
1135 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1137 * Set the URL query parameters to use, overwriting previously set query parameters.
1138 * Affects any request types.
1140 * Accepts a string or associative array parameter containing the pre-encoded
1141 * query string or to be encoded query fields. If the parameter is empty or
1142 * omitted, the query data will be unset.
1144 * Returns TRUE on success, or FALSE on failure.
1146 PHP_METHOD(HttpRequest
, setQueryData
)
1150 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1154 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1155 UPD_STRL(queryData
, "", 0);
1156 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1157 char *query_data
= NULL
;
1159 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1163 UPD_PROP(string
, queryData
, query_data
);
1168 convert_to_string_ex(&qdata
);
1169 UPD_STRL(queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1170 if (orig
!= qdata
) {
1171 zval_ptr_dtor(&qdata
);
1178 /* {{{ proto string HttpRequest::getQueryData()
1180 * Get the current query data in form of an urlencoded query string.
1182 * Returns a string containing the urlencoded query.
1184 PHP_METHOD(HttpRequest
, getQueryData
)
1189 RETURN_PROP(queryData
);
1194 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1196 * Add parameters to the query parameter list, leaving previously set unchanged.
1197 * Affects any request type.
1199 * Expects an associative array as parameter containing the query fields to add.
1201 * Returns TRUE on success, or FALSE on failure.
1203 PHP_METHOD(HttpRequest
, addQueryData
)
1205 zval
*qdata
, *old_qdata
;
1206 char *query_data
= NULL
;
1207 size_t query_data_len
= 0;
1209 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &qdata
)) {
1213 old_qdata
= GET_PROP(queryData
);
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
)) {
1219 UPD_STRL(queryData
, query_data
, query_data_len
);
1226 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1228 * Adds POST data entries, leaving previously set unchanged, unless a
1229 * post entry with the same name already exists.
1230 * Affects only POST and custom requests.
1232 * Expects an associative array as parameter containing the post fields.
1234 * Returns TRUE on success, or FALSE on failure.
1236 PHP_METHOD(HttpRequest
, addPostFields
)
1238 zval
*post_data
, *old_post
, *new_post
;
1240 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &post_data
)) {
1244 if (zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1245 MAKE_STD_ZVAL(new_post
);
1246 array_init(new_post
);
1247 old_post
= GET_PROP(postFields
);
1248 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1249 array_copy(old_post
, new_post
);
1251 array_merge(post_data
, new_post
);
1252 SET_PROP(postFields
, new_post
);
1253 zval_ptr_dtor(&new_post
);
1260 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1262 * Set the POST data entries, overwriting previously set POST data.
1263 * Affects only POST and custom requests.
1265 * Accepts an associative array as parameter containing the post fields.
1266 * If the parameter is empty or omitted, the post data will be unset.
1268 * Returns TRUE on success, or FALSE on failure.
1270 PHP_METHOD(HttpRequest
, setPostFields
)
1272 zval
*post
, *post_data
= NULL
;
1274 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &post_data
)) {
1278 MAKE_STD_ZVAL(post
);
1280 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1281 array_copy(post_data
, post
);
1283 SET_PROP(postFields
, post
);
1284 zval_ptr_dtor(&post
);
1290 /* {{{ proto array HttpRequest::getPostFields()
1292 * Get previously set POST data.
1294 * Returns the currently set post fields as associative array.
1296 PHP_METHOD(HttpRequest
, getPostFields
)
1301 RETURN_PROP(postFields
);
1306 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1308 * Set raw post data to send, overwriting previously set raw post data. Don't
1309 * forget to specify a content type. Affects only POST and custom requests.
1310 * Only either post fields or raw post data can be used for each request.
1311 * Raw post data has higher precedence and will be used even if post fields
1314 * Accepts a string as parameter containing the *raw* post data.
1316 * Returns TRUE on success, or FALSE on failure.
1318 PHP_METHOD(HttpRequest
, setRawPostData
)
1320 char *raw_data
= NULL
;
1323 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1331 UPD_STRL(rawPostData
, raw_data
, data_len
);
1336 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1338 * Add raw post data, leaving previously set raw post data unchanged.
1339 * Affects only POST and custom requests.
1341 * Expects a string as parameter containing the raw post data to concatenate.
1343 * Returns TRUE on success, or FALSE on failure.
1345 PHP_METHOD(HttpRequest
, addRawPostData
)
1350 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1355 zval
*data
= GET_PROP(rawPostData
);
1357 if (Z_STRLEN_P(data
)) {
1358 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1359 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1360 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, raw_data
, data_len
);
1362 UPD_STRL(putData
, raw_data
, data_len
);
1370 /* {{{ proto string HttpRequest::getRawPostData()
1372 * Get previously set raw post data.
1374 * Returns a string containing the currently set raw post data.
1376 PHP_METHOD(HttpRequest
, getRawPostData
)
1381 RETURN_PROP(rawPostData
);
1386 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1388 * Add a file to the POST request, leaving previously set files unchanged.
1389 * Affects only POST and custom requests. Cannot be used with raw post data.
1391 * Expects a string parameter containing the form element name, and a string
1392 * paremeter containing the path to the file which should be uploaded.
1393 * Additionally accepts an optional string parameter which should contain
1394 * the content type of the file.
1396 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1397 * primary and a secondary content type part.
1399 PHP_METHOD(HttpRequest
, addPostFile
)
1401 zval
*entry
, *old_post
, *new_post
;
1402 char *name
, *file
, *type
= NULL
;
1403 int name_len
, file_len
, type_len
= 0;
1405 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1410 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1412 type
= "application/x-octetstream";
1413 type_len
= sizeof("application/x-octetstream") - 1;
1416 MAKE_STD_ZVAL(entry
);
1419 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1420 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1421 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1423 MAKE_STD_ZVAL(new_post
);
1424 array_init(new_post
);
1425 old_post
= GET_PROP(postFiles
);
1426 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1427 array_copy(old_post
, new_post
);
1429 add_next_index_zval(new_post
, entry
);
1430 SET_PROP(postFiles
, new_post
);
1431 zval_ptr_dtor(&new_post
);
1437 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1439 * Set files to post, overwriting previously set post files.
1440 * Affects only POST and requests. Cannot be used with raw post data.
1442 * Accepts an array containing the files to post. Each entry should be an
1443 * associative array with "name", "file" and "type" keys. If the parameter
1444 * is empty or omitted the post files will be unset.
1446 * Returns TRUE on success, or FALSE on failure.
1448 PHP_METHOD(HttpRequest
, setPostFiles
)
1450 zval
*files
= NULL
, *post
;
1452 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!/", &files
)) {
1456 MAKE_STD_ZVAL(post
);
1458 if (files
&& (Z_TYPE_P(files
) == IS_ARRAY
)) {
1459 array_copy(files
, post
);
1461 SET_PROP(postFiles
, post
);
1462 zval_ptr_dtor(&post
);
1468 /* {{{ proto array HttpRequest::getPostFiles()
1470 * Get all previously added POST files.
1472 * Returns an array containing currently set post files.
1474 PHP_METHOD(HttpRequest
, getPostFiles
)
1479 RETURN_PROP(postFiles
);
1484 /* {{{ proto bool HttpRequest::setPutFile([string file])
1486 * Set file to put. Affects only PUT requests.
1488 * Accepts a string as parameter referencing the path to file.
1489 * If the parameter is empty or omitted the put file will be unset.
1491 * Returns TRUE on success, or FALSE on failure.
1493 PHP_METHOD(HttpRequest
, setPutFile
)
1498 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1502 UPD_STRL(putFile
, file
, file_len
);
1507 /* {{{ proto string HttpRequest::getPutFile()
1509 * Get previously set put file.
1511 * Returns a string containing the path to the currently set put file.
1513 PHP_METHOD(HttpRequest
, getPutFile
)
1518 RETURN_PROP(putFile
);
1523 /* {{{ proto bool HttpRequest::setPutData([string put_data])
1525 * Set PUT data to send, overwriting previously set PUT data.
1526 * Affects only PUT requests.
1527 * Only either PUT data or PUT file can be used for each request.
1528 * PUT data has higher precedence and will be used even if a PUT
1531 * Accepts a string as parameter containing the data to upload.
1533 * Returns TRUE on success, or FALSE on failure.
1535 PHP_METHOD(HttpRequest
, setPutData
)
1537 char *put_data
= NULL
;
1540 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &put_data
, &data_len
)) {
1548 UPD_STRL(putData
, put_data
, data_len
);
1553 /* {{{ proto bool HttpRequest::addPutData(string put_data)
1555 * Add PUT data, leaving previously set PUT data unchanged.
1556 * Affects only PUT requests.
1558 * Expects a string as parameter containing the data to concatenate.
1560 * Returns TRUE on success, or FALSE on failure.
1562 PHP_METHOD(HttpRequest
, addPutData
)
1567 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &put_data
, &data_len
)) {
1572 zval
*data
= GET_PROP(putData
);
1574 if (Z_STRLEN_P(data
)) {
1575 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1576 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1577 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, put_data
, data_len
);
1579 UPD_STRL(putData
, put_data
, data_len
);
1587 /* {{{ proto string HttpRequest::getPutData()
1589 * Get previously set PUT data.
1591 * Returns a string containing the currently set raw post data.
1593 PHP_METHOD(HttpRequest
, getPutData
)
1598 RETURN_PROP(putData
);
1603 /* {{{ proto array HttpRequest::getResponseData()
1605 * Get all response data after the request has been sent.
1607 * Returns an associative array with the key "headers" containing an associative
1608 * array holding all response headers, as well as the key "body" containing a
1609 * string with the response body.
1611 * If redirects were allowed and several responses were received, the data
1612 * references the last received response.
1614 PHP_METHOD(HttpRequest
, getResponseData
)
1619 RETURN_PROP(responseData
);
1624 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1626 * Get response header(s) after the request has been sent.
1628 * Accepts an string as optional parameter specifying a certain header to read.
1629 * If the parameter is empty or omitted all response headers will be returned.
1631 * Returns either a string with the value of the header matching name if requested,
1632 * FALSE on failure, or an associative array containing all response headers.
1634 * If redirects were allowed and several responses were received, the data
1635 * references the last received response.
1637 PHP_METHOD(HttpRequest
, getResponseHeader
)
1640 zval
*data
, **headers
, **header
;
1641 char *header_name
= NULL
;
1644 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1648 data
= GET_PROP(responseData
);
1649 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1650 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void *) &headers
)) &&
1651 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1652 if (!header_len
|| !header_name
) {
1653 RETVAL_ZVAL(*headers
, 1, 0);
1654 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void *) &header
)) {
1655 RETVAL_ZVAL(*header
, 1, 0);
1666 /* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]])
1668 * Get response cookie(s) after the request has been sent.
1670 * Returns an array of stdClass objects like http_parse_cookie would return.
1672 * If redirects were allowed and several responses were received, the data
1673 * references the last received response.
1675 PHP_METHOD(HttpRequest
, getResponseCookies
)
1679 zval
*allowed_extras_array
= NULL
, *data
, **headers
;
1681 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|la", &flags
, &allowed_extras_array
)) {
1685 data
= GET_PROP(responseData
);
1686 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1687 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void *) &headers
)) &&
1688 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1691 char *key
= NULL
, **allowed_extras
= NULL
;
1692 zval
**header
= NULL
, **entry
= NULL
;
1693 HashPosition pos
, pos1
, pos2
;
1695 array_init(return_value
);
1697 if (allowed_extras_array
) {
1698 allowed_extras
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array
)) + 1, sizeof(char *));
1699 FOREACH_VAL(pos
, allowed_extras_array
, entry
) {
1700 ZVAL_ADDREF(*entry
);
1701 convert_to_string_ex(entry
);
1702 allowed_extras
[i
++] = estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
1703 zval_ptr_dtor(entry
);
1707 FOREACH_HASH_KEYVAL(pos1
, Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1708 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1709 http_cookie_list list
;
1711 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1712 zval
**single_header
;
1714 FOREACH_VAL(pos2
, *header
, single_header
) {
1715 ZVAL_ADDREF(*single_header
);
1716 convert_to_string_ex(single_header
);
1717 if (http_parse_cookie_ex(&list
, Z_STRVAL_PP(single_header
), flags
, allowed_extras
)) {
1720 MAKE_STD_ZVAL(cookie
);
1721 object_init(cookie
);
1722 http_cookie_list_tostruct(&list
, cookie
);
1723 add_next_index_zval(return_value
, cookie
);
1724 http_cookie_list_dtor(&list
);
1726 zval_ptr_dtor(single_header
);
1729 ZVAL_ADDREF(*header
);
1730 convert_to_string_ex(header
);
1731 if (http_parse_cookie_ex(&list
, Z_STRVAL_PP(header
), flags
, allowed_extras
)) {
1734 MAKE_STD_ZVAL(cookie
);
1735 object_init(cookie
);
1736 http_cookie_list_tostruct(&list
, cookie
);
1737 add_next_index_zval(return_value
, cookie
);
1738 http_cookie_list_dtor(&list
);
1740 zval_ptr_dtor(header
);
1747 if (allowed_extras
) {
1748 for (i
= 0; allowed_extras
[i
]; ++i
) {
1749 efree(allowed_extras
[i
]);
1751 efree(allowed_extras
);
1760 /* {{{ proto string HttpRequest::getResponseBody()
1762 * Get the response body after the request has been sent.
1764 * Returns a string containing the response body.
1766 * If redirects were allowed and several responses were received, the data
1767 * references the last received response.
1769 PHP_METHOD(HttpRequest
, getResponseBody
)
1775 zval
*data
= GET_PROP(responseData
);
1777 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1778 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void *) &body
))) {
1779 RETURN_ZVAL(*body
, 1, 0);
1787 /* {{{ proto int HttpRequest::getResponseCode()
1789 * Get the response code after the request has been sent.
1791 * Returns an int representing the response code.
1793 * If redirects were allowed and several responses were received, the data
1794 * references the last received response.
1796 PHP_METHOD(HttpRequest
, getResponseCode
)
1801 RETURN_PROP(responseCode
);
1806 /* {{{ proto string HttpRequest::getResponseStatus()
1808 * Get the response status (i.e. the string after the response code) after the message has been sent.
1810 * Returns a string containing the response status text.
1812 PHP_METHOD(HttpRequest
, getResponseStatus
)
1817 RETURN_PROP(responseStatus
);
1822 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1824 * Get response info after the request has been sent.
1825 * See http_get() for a full list of returned info.
1827 * Accepts a string as optional parameter specifying the info to read.
1828 * If the parameter is empty or omitted, an associative array containing
1829 * all available info will be returned.
1831 * Returns either a scalar containing the value of the info matching name if
1832 * requested, FALSE on failure, or an associative array containing all
1835 * If redirects were allowed and several responses were received, the data
1836 * references the last received response.
1838 PHP_METHOD(HttpRequest
, getResponseInfo
)
1841 zval
*info
, **infop
;
1842 char *info_name
= NULL
;
1845 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1849 info
= GET_PROP(responseInfo
);
1851 if (Z_TYPE_P(info
) != IS_ARRAY
) {
1855 if (info_len
&& info_name
) {
1856 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void *) &infop
)) {
1857 RETURN_ZVAL(*infop
, 1, 0);
1859 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1863 RETURN_ZVAL(info
, 1, 0);
1869 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1871 * Get the full response as HttpMessage object after the request has been sent.
1873 * Returns an HttpMessage object of the response.
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 received responses within this request
1880 * Throws HttpException, HttpRuntimeException.
1882 PHP_METHOD(HttpRequest
, getResponseMessage
)
1887 SET_EH_THROW_HTTP();
1888 message
= GET_PROP(responseMessage
);
1889 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1890 RETVAL_OBJECT(message
, 1);
1892 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HttpRequest does not contain a response message");
1899 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1901 * Get sent HTTP message.
1903 * Returns an HttpMessage object representing the sent request.
1905 * If redirects were allowed and several responses were received, the data
1906 * references the last received response. Use HttpMessage::getParentMessage()
1907 * to access the data of previously sent requests within this request
1910 * Note that the internal request message is immutable, that means that the
1911 * request message received through HttpRequest::getRequestMessage() will
1912 * always look the same for the same request, regardless of any changes you
1913 * may have made to the returned object.
1915 * Throws HttpMalformedHeadersException, HttpEncodingException.
1917 PHP_METHOD(HttpRequest
, getRequestMessage
)
1923 getObject(http_request_object
, obj
);
1925 SET_EH_THROW_HTTP();
1926 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1927 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
1934 /* {{{ proto string HttpRequest::getRawRequestMessage()
1936 * Get sent HTTP message.
1938 * Returns an HttpMessage in a form of a string
1940 PHP_METHOD(HttpRequest
, getRawRequestMessage
)
1945 getObject(http_request_object
, obj
);
1947 RETURN_PHPSTR_DUP(&obj
->request
->conv
.request
);
1952 /* {{{ proto string HttpRequest::getRawResponseMessage()
1954 * Get the entire HTTP response.
1956 * Returns the complete web server response, including the headers in a form of a string.
1958 PHP_METHOD(HttpRequest
, getRawResponseMessage
)
1963 getObject(http_request_object
, obj
);
1965 RETURN_PHPSTR_DUP(&obj
->request
->conv
.response
);
1970 /* {{{ proto HttpMessage HttpRequest::getHistory()
1972 * Get all sent requests and received responses as an HttpMessage object.
1974 * If you want to record history, set the instance variable
1975 * HttpRequest::$recordHistory to TRUE.
1977 * Returns an HttpMessage object representing the complete request/response
1980 * The object references the last received response, use HttpMessage::getParentMessage()
1981 * to access the data of previously sent requests and received responses.
1983 * Throws HttpRuntimeException.
1985 PHP_METHOD(HttpRequest
, getHistory
)
1992 SET_EH_THROW_HTTP();
1993 hist
= GET_PROP(history
);
1994 if (Z_TYPE_P(hist
) == IS_OBJECT
) {
1995 RETVAL_OBJECT(hist
, 1);
1997 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "The history is empty");
2004 /* {{{ proto void HttpRequest::clearHistory()
2006 * Clear the history.
2008 PHP_METHOD(HttpRequest
, clearHistory
)
2013 MAKE_STD_ZVAL(hist
);
2015 SET_PROP(history
, hist
);
2016 zval_ptr_dtor(&hist
);
2021 /* {{{ proto HttpMessage HttpRequest::send()
2023 * Send the HTTP request.
2025 * Returns the received response as HttpMessage object.
2027 * NOTE: While an exception may be thrown, the transfer could have succeeded
2028 * at least partially, so you might want to check the return values of various
2029 * HttpRequest::getResponse*() methods.
2031 * Throws HttpRuntimeException, HttpRequestException,
2032 * HttpMalformedHeaderException, HttpEncodingException.
2037 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
2038 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
2039 * $r->addQueryData(array('category' => 3));
2042 * if ($r->getResponseCode() == 200) {
2043 * file_put_contents('local.rss', $r->getResponseBody());
2045 * } catch (HttpException $ex) {
2054 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
2055 * $r->setOptions(array('cookies' => array('lang' => 'de')));
2056 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
2057 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
2059 * echo $r->send()->getBody();
2060 * } catch (HttpException $ex) {
2066 PHP_METHOD(HttpRequest
, send
)
2068 getObject(http_request_object
, obj
);
2072 SET_EH_THROW_HTTP();
2077 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
2078 } else if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
2079 http_request_exec(obj
->request
);
2080 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
2081 RETVAL_OBJECT(GET_PROP(responseMessage
), 1);
2089 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
2096 * vim600: noet sw=4 ts=4 fdm=marker
2097 * vim<600: noet sw=4 ts=4