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 #if HTTP_CURL_VERSION(7,14,1)
76 HTTP_EMPTY_ARGS(resetCookies
);
79 HTTP_EMPTY_ARGS(getUrl
);
80 HTTP_BEGIN_ARGS(setUrl
, 1)
84 HTTP_EMPTY_ARGS(getMethod
);
85 HTTP_BEGIN_ARGS(setMethod
, 1)
86 HTTP_ARG_VAL(request_method
, 0)
89 HTTP_EMPTY_ARGS(getContentType
);
90 HTTP_BEGIN_ARGS(setContentType
, 1)
91 HTTP_ARG_VAL(content_type
, 0)
94 HTTP_EMPTY_ARGS(getQueryData
);
95 HTTP_BEGIN_ARGS(setQueryData
, 0)
96 HTTP_ARG_VAL(query_data
, 0)
99 HTTP_BEGIN_ARGS(addQueryData
, 1)
100 HTTP_ARG_VAL(query_data
, 0)
103 HTTP_EMPTY_ARGS(getPostFields
);
104 HTTP_BEGIN_ARGS(setPostFields
, 0)
105 HTTP_ARG_VAL(post_fields
, 0)
108 HTTP_BEGIN_ARGS(addPostFields
, 1)
109 HTTP_ARG_VAL(post_fields
, 0)
112 HTTP_EMPTY_ARGS(getPostFiles
);
113 HTTP_BEGIN_ARGS(setPostFiles
, 0)
114 HTTP_ARG_VAL(post_files
, 0)
117 HTTP_BEGIN_ARGS(addPostFile
, 2)
118 HTTP_ARG_VAL(formname
, 0)
119 HTTP_ARG_VAL(filename
, 0)
120 HTTP_ARG_VAL(content_type
, 0)
123 HTTP_EMPTY_ARGS(getRawPostData
);
124 HTTP_BEGIN_ARGS(setRawPostData
, 0)
125 HTTP_ARG_VAL(raw_post_data
, 0)
128 HTTP_BEGIN_ARGS(addRawPostData
, 1)
129 HTTP_ARG_VAL(raw_post_data
, 0)
132 HTTP_EMPTY_ARGS(getPutFile
);
133 HTTP_BEGIN_ARGS(setPutFile
, 0)
134 HTTP_ARG_VAL(filename
, 0)
137 HTTP_EMPTY_ARGS(getPutData
);
138 HTTP_BEGIN_ARGS(setPutData
, 0)
139 HTTP_ARG_VAL(put_data
, 0)
142 HTTP_BEGIN_ARGS(addPutData
, 1)
143 HTTP_ARG_VAL(put_data
, 0)
146 HTTP_EMPTY_ARGS(getResponseData
);
147 HTTP_BEGIN_ARGS(getResponseHeader
, 0)
148 HTTP_ARG_VAL(name
, 0)
151 HTTP_BEGIN_ARGS(getResponseCookies
, 0)
152 HTTP_ARG_VAL(flags
, 0)
153 HTTP_ARG_VAL(allowed_extras
, 0)
156 HTTP_EMPTY_ARGS(getResponseBody
);
157 HTTP_EMPTY_ARGS(getResponseCode
);
158 HTTP_EMPTY_ARGS(getResponseStatus
);
159 HTTP_BEGIN_ARGS(getResponseInfo
, 0)
160 HTTP_ARG_VAL(name
, 0)
163 HTTP_EMPTY_ARGS(getResponseMessage
);
164 HTTP_EMPTY_ARGS(getRawResponseMessage
);
165 HTTP_EMPTY_ARGS(getRequestMessage
);
166 HTTP_EMPTY_ARGS(getRawRequestMessage
);
167 HTTP_EMPTY_ARGS(getHistory
);
168 HTTP_EMPTY_ARGS(clearHistory
);
169 HTTP_EMPTY_ARGS(send
);
171 HTTP_BEGIN_ARGS(get
, 1)
173 HTTP_ARG_VAL(options
, 0)
174 HTTP_ARG_VAL(info
, 1)
177 HTTP_BEGIN_ARGS(head
, 1)
179 HTTP_ARG_VAL(options
, 0)
180 HTTP_ARG_VAL(info
, 1)
183 HTTP_BEGIN_ARGS(postData
, 2)
185 HTTP_ARG_VAL(data
, 0)
186 HTTP_ARG_VAL(options
, 0)
187 HTTP_ARG_VAL(info
, 1)
190 HTTP_BEGIN_ARGS(postFields
, 2)
192 HTTP_ARG_VAL(data
, 0)
193 HTTP_ARG_VAL(options
, 0)
194 HTTP_ARG_VAL(info
, 1)
197 HTTP_BEGIN_ARGS(putData
, 2)
199 HTTP_ARG_VAL(data
, 0)
200 HTTP_ARG_VAL(options
, 0)
201 HTTP_ARG_VAL(info
, 1)
204 HTTP_BEGIN_ARGS(putFile
, 2)
206 HTTP_ARG_VAL(file
, 0)
207 HTTP_ARG_VAL(options
, 0)
208 HTTP_ARG_VAL(info
, 1)
211 HTTP_BEGIN_ARGS(putStream
, 2)
213 HTTP_ARG_VAL(stream
, 0)
214 HTTP_ARG_VAL(options
, 0)
215 HTTP_ARG_VAL(info
, 1)
218 HTTP_BEGIN_ARGS(methodRegister
, 1)
219 HTTP_ARG_VAL(method_name
, 0)
222 HTTP_BEGIN_ARGS(methodUnregister
, 1)
223 HTTP_ARG_VAL(method
, 0)
226 HTTP_BEGIN_ARGS(methodName
, 1)
227 HTTP_ARG_VAL(method_id
, 0)
230 HTTP_BEGIN_ARGS(methodExists
, 1)
231 HTTP_ARG_VAL(method
, 0)
234 HTTP_BEGIN_ARGS(encodeBody
, 2)
235 HTTP_ARG_VAL(fields
, 0)
236 HTTP_ARG_VAL(files
, 0)
239 #define OBJ_PROP_CE http_request_object_ce
240 zend_class_entry
*http_request_object_ce
;
241 zend_function_entry http_request_object_fe
[] = {
242 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
244 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
245 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
246 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
247 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
248 HTTP_REQUEST_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
250 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
251 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
252 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
254 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
255 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
256 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
257 #if HTTP_CURL_VERSION(7,14,1)
258 HTTP_REQUEST_ME(resetCookies
, ZEND_ACC_PUBLIC
)
261 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
262 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
265 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
270 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
272 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
274 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
275 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
278 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
279 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
280 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
282 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
283 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
284 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
286 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
287 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
289 HTTP_REQUEST_ME(setPutData
, ZEND_ACC_PUBLIC
)
290 HTTP_REQUEST_ME(getPutData
, ZEND_ACC_PUBLIC
)
291 HTTP_REQUEST_ME(addPutData
, ZEND_ACC_PUBLIC
)
293 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
295 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
296 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
297 HTTP_REQUEST_ME(getResponseCookies
, ZEND_ACC_PUBLIC
)
298 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
299 HTTP_REQUEST_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
300 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
301 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
302 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
303 HTTP_REQUEST_ME(getRawResponseMessage
, ZEND_ACC_PUBLIC
)
304 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
305 HTTP_REQUEST_ME(getRawRequestMessage
, ZEND_ACC_PUBLIC
)
306 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
307 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
309 HTTP_REQUEST_ALIAS(get
, http_get
)
310 HTTP_REQUEST_ALIAS(head
, http_head
)
311 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
312 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
313 HTTP_REQUEST_ALIAS(putData
, http_put_data
)
314 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
315 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
317 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
318 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
319 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
320 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
322 HTTP_REQUEST_ALIAS(encodeBody
, http_request_body_encode
)
326 static zend_object_handlers http_request_object_handlers
;
328 PHP_MINIT_FUNCTION(http_request_object
)
330 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
331 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
333 DCL_PROP_N(PRIVATE
, options
);
334 DCL_PROP_N(PRIVATE
, postFields
);
335 DCL_PROP_N(PRIVATE
, postFiles
);
336 DCL_PROP_N(PRIVATE
, responseInfo
);
337 DCL_PROP_N(PRIVATE
, responseData
);
338 DCL_PROP_N(PRIVATE
, responseMessage
);
339 DCL_PROP(PRIVATE
, long, responseCode
, 0);
340 DCL_PROP(PRIVATE
, string
, responseStatus
, "");
341 DCL_PROP(PRIVATE
, long, method
, HTTP_GET
);
342 DCL_PROP(PRIVATE
, string
, url
, "");
343 DCL_PROP(PRIVATE
, string
, contentType
, "");
344 DCL_PROP(PRIVATE
, string
, rawPostData
, "");
345 DCL_PROP(PRIVATE
, string
, queryData
, "");
346 DCL_PROP(PRIVATE
, string
, putFile
, "");
347 DCL_PROP(PRIVATE
, string
, putData
, "");
348 DCL_PROP_N(PRIVATE
, history
);
349 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
353 * Request Method Constants
356 DCL_CONST(long, "METH_GET", HTTP_GET
);
357 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
358 DCL_CONST(long, "METH_POST", HTTP_POST
);
359 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
360 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
361 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
362 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
363 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
364 /* WebDAV - RFC 2518 */
365 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
366 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
367 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
368 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
369 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
370 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
371 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
372 /* WebDAV Versioning - RFC 3253 */
373 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
374 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
375 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
376 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
377 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
378 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
379 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
380 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
381 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
382 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
383 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
384 /* WebDAV Access Control - RFC 3744 */
385 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
388 * HTTP Protocol Version Constants
390 DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0
);
391 DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1
);
392 DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE
);
397 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
398 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
399 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
400 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
403 * Proxy Type Constants
405 # if HTTP_CURL_VERSION(7,15,2)
406 DCL_CONST(long, "PROXY_SOCKS4", CURLPROXY_SOCKS4
);
408 DCL_CONST(long, "PROXY_SOCKS5", CURLPROXY_SOCKS5
);
409 DCL_CONST(long, "PROXY_HTTP", CURLPROXY_HTTP
);
415 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
417 return http_request_object_new_ex(ce
, NULL
, NULL
);
420 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
422 zend_object_value ov
;
423 http_request_object
*o
;
425 o
= ecalloc(1, sizeof(http_request_object
));
427 o
->request
= http_request_init_ex(NULL
, ch
, 0, NULL
);
433 ALLOC_HASHTABLE(OBJ_PROP(o
));
434 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
435 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
437 ov
.handle
= putObject(http_request_object
, o
);
438 ov
.handlers
= &http_request_object_handlers
;
443 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
446 zend_object_value new_ov
;
447 http_request_object
*new_obj
;
448 getObject(http_request_object
, old_obj
);
450 old_zo
= zend_objects_get_address(this_ptr TSRMLS_CC
);
451 new_ov
= http_request_object_new_ex(old_zo
->ce
, NULL
, &new_obj
);
452 if (old_obj
->request
->ch
) {
453 http_curl_init_ex(curl_easy_duphandle(old_obj
->request
->ch
), new_obj
->request
);
456 zend_objects_clone_members(&new_obj
->zo
, new_ov
, old_zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
457 phpstr_append(&new_obj
->request
->conv
.request
, old_obj
->request
->conv
.request
.data
, old_obj
->request
->conv
.request
.used
);
458 phpstr_append(&new_obj
->request
->conv
.response
, old_obj
->request
->conv
.response
.data
, old_obj
->request
->conv
.response
.used
);
463 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
465 http_request_object
*o
= (http_request_object
*) object
;
468 zend_hash_destroy(OBJ_PROP(o
));
469 FREE_HASHTABLE(OBJ_PROP(o
));
471 http_request_free(&o
->request
);
475 #define http_request_object_resetcookies(o) _http_request_object_resetcookies((o) TSRMLS_CC)
476 static inline STATUS
_http_request_object_resetcookies(zval
*this_ptr TSRMLS_DC
)
478 getObject(http_request_object
, obj
);
479 return curl_easy_setopt(obj
->request
->ch
, CURLOPT_COOKIELIST
, "ALL");
482 #define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC)
483 static inline void _http_request_object_check_request_content_type(zval
*this_ptr TSRMLS_DC
)
485 zval
*ctype
= GET_PROP(contentType
);
487 if (Z_STRLEN_P(ctype
)) {
488 zval
**headers
, *opts
= GET_PROP(options
);
490 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
491 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void *) &headers
)) &&
492 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
495 /* only override if not already set */
496 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void *) &ct_header
))) {
497 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
499 /* or not a string, zero length string or a string of spaces */
500 if ((Z_TYPE_PP(ct_header
) != IS_STRING
) || !Z_STRLEN_PP(ct_header
)) {
501 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
503 int i
, only_space
= 1;
505 /* check for spaces only */
506 for (i
= 0; i
< Z_STRLEN_PP(ct_header
); ++i
) {
507 if (!isspace(Z_STRVAL_PP(ct_header
)[i
])) {
513 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
519 MAKE_STD_ZVAL(headers
);
521 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
522 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, headers
);
523 zval_ptr_dtor(&headers
);
528 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
530 STATUS status
= SUCCESS
;
532 http_request_reset(obj
->request
);
533 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, http_curl_init(obj
->request
), return FAILURE
);
535 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(GET_PROP(url
)));
537 switch (obj
->request
->meth
= Z_LVAL_P(GET_PROP(method
)))
545 zval
*put_data
= GET_PROP(putData
);
547 http_request_object_check_request_content_type(getThis());
548 if (Z_STRLEN_P(put_data
)) {
549 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
550 estrndup(Z_STRVAL_P(put_data
), Z_STRLEN_P(put_data
)), Z_STRLEN_P(put_data
), 1);
552 php_stream_statbuf ssb
;
553 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
);
555 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
556 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
567 /* check for raw post data */
568 zval
*raw_data
= GET_PROP(rawPostData
);
570 if (Z_STRLEN_P(raw_data
)) {
571 http_request_object_check_request_content_type(getThis());
572 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
573 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
575 zval
*zfields
= GET_PROP(postFields
), *zfiles
= GET_PROP(postFiles
);
579 fields
= (Z_TYPE_P(zfields
) == IS_ARRAY
) ? Z_ARRVAL_P(zfields
) : NULL
;
580 files
= (Z_TYPE_P(zfiles
) == IS_ARRAY
) ? Z_ARRVAL_P(zfiles
) : NULL
;
582 if ((fields
&& zend_hash_num_elements(fields
)) || (files
&& zend_hash_num_elements(files
))) {
583 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
592 if (status
== SUCCESS
) {
593 zval
*qdata
= GET_PROP(queryData
);
594 zval
*options
= GET_PROP(options
);
596 if (Z_STRLEN_P(qdata
)) {
597 if (!strchr(obj
->request
->url
, '?')) {
598 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
600 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
602 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
605 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
607 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
608 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onprogress", sizeof("onprogress"))) {
611 if ( (Z_TYPE_P(options
) != IS_ARRAY
)
612 || (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(options
), "onprogress", sizeof("onprogress"), (void *) &entry
)
613 || (!zval_is_true(*entry
)))) {
616 ZVAL_ADDREF(getThis());
617 add_next_index_zval(pcb
, getThis());
618 add_next_index_stringl(pcb
, "onprogress", lenof("onprogress"), 1);
619 http_request_set_progress_callback(obj
->request
, pcb
);
628 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
634 /* always fetch info */
637 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
638 SET_PROP(responseInfo
, info
);
639 zval_ptr_dtor(&info
);
641 /* parse response message */
642 phpstr_fix(&obj
->request
->conv
.request
);
643 phpstr_fix(&obj
->request
->conv
.response
);
645 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
)))) {
648 zval
*headers
, *message
, *resp
;
650 if (zval_is_true(GET_PROP(recordHistory
))) {
651 zval
*hist
, *history
= GET_PROP(history
);
652 http_message
*response
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
));
653 http_message
*request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
656 ZVAL_OBJVAL(hist
, http_message_object_new_ex(http_message_object_ce
, http_message_interconnect(response
, request
), NULL
), 0);
657 if (Z_TYPE_P(history
) == IS_OBJECT
) {
658 http_message_object_prepend(hist
, history
);
660 SET_PROP(history
, hist
);
661 zval_ptr_dtor(&hist
);
664 UPD_PROP(long, responseCode
, msg
->http
.info
.response
.code
);
665 UPD_PROP(string
, responseStatus
, msg
->http
.info
.response
.status
? msg
->http
.info
.response
.status
: "");
669 MAKE_STD_ZVAL(headers
);
671 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
672 add_assoc_zval(resp
, "headers", headers
);
673 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
674 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
675 SET_PROP(responseData
, resp
);
676 zval_ptr_dtor(&resp
);
678 MAKE_STD_ZVAL(message
);
679 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
680 SET_PROP(responseMessage
, message
);
681 zval_ptr_dtor(&message
);
685 /* update properties with empty values*/
686 zval
*resp
= GET_PROP(responseData
), *znull
;
688 MAKE_STD_ZVAL(znull
);
690 SET_PROP(responseMessage
, znull
);
691 zval_ptr_dtor(&znull
);
693 if (Z_TYPE_P(resp
) == IS_ARRAY
) {
694 zend_hash_clean(Z_ARRVAL_P(resp
));
697 UPD_PROP(long, responseCode
, 0);
698 UPD_PROP(string
, responseStatus
, "");
700 /* append request message to history */
701 if (zval_is_true(GET_PROP(recordHistory
))) {
702 http_message
*request
;
704 if ((request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
705 zval
*hist
, *history
= GET_PROP(history
);
708 ZVAL_OBJVAL(hist
, http_message_object_new_ex(http_message_object_ce
, request
, NULL
), 0);
709 if (Z_TYPE_P(history
) == IS_OBJECT
) {
710 http_message_object_prepend(hist
, history
);
712 SET_PROP(history
, hist
);
713 zval_ptr_dtor(&hist
);
720 if (!EG(exception
) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onfinish", sizeof("onfinish"))) {
723 MAKE_STD_ZVAL(param
);
724 ZVAL_BOOL(param
, ret
== SUCCESS
);
725 with_error_handling(EH_NORMAL
, NULL
) {
726 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "onfinish", NULL
, param
);
727 } end_error_handling();
728 zval_ptr_dtor(¶m
);
734 static int apply_pretty_key(void *pDest
, int num_args
, va_list args
, zend_hash_key
*hash_key
)
736 if (hash_key
->nKeyLength
> 1) {
737 hash_key
->h
= zend_get_hash_value(pretty_key(hash_key
->arKey
, hash_key
->nKeyLength
- 1, 1, 0), hash_key
->nKeyLength
);
739 return ZEND_HASH_APPLY_KEEP
;
742 #define http_request_object_set_options_subr(key, ow, pk) \
743 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
744 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
, int prettify_keys
)
746 zval
*old_opts
, *new_opts
, *opts
= NULL
, **entry
= NULL
;
748 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
752 MAKE_STD_ZVAL(new_opts
);
753 array_init(new_opts
);
754 old_opts
= GET_PROP(options
);
755 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
756 array_copy(old_opts
, new_opts
);
759 if (prettify_keys
&& opts
) {
760 zend_hash_apply_with_arguments(Z_ARRVAL_P(opts
), apply_pretty_key
, 0);
762 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), key
, len
, (void *) &entry
)) {
764 zend_hash_clean(Z_ARRVAL_PP(entry
));
766 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
768 array_copy(opts
, *entry
);
770 array_merge(opts
, *entry
);
775 add_assoc_zval(new_opts
, key
, opts
);
777 SET_PROP(options
, new_opts
);
778 zval_ptr_dtor(&new_opts
);
783 #define http_request_object_get_options_subr(key) \
784 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
785 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
789 if (return_value_used
) {
790 zval
*opts
, **options
;
792 opts
= GET_PROP(options
);
793 array_init(return_value
);
795 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
796 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void *) &options
))) {
797 convert_to_array(*options
);
798 array_copy(*options
, return_value
);
804 /* ### USERLAND ### */
806 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
808 * Instantiate a new HttpRequest object.
810 * Accepts a string as optional parameter containing the target request url.
811 * Additionally accepts an optional int parameter specifying the request method
812 * to use and an associative array as optional third parameter which will be
813 * passed to HttpRequest::setOptions().
815 * Throws HttpException.
817 PHP_METHOD(HttpRequest
, __construct
)
822 zval
*options
= NULL
;
825 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
827 UPD_STRL(url
, URL
, URL_len
);
830 UPD_PROP(long, method
, meth
);
833 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
840 /* {{{ proto bool HttpRequest::setOptions([array options])
842 * Set the request options to use. See http_get() for a full list of available options.
844 * Accepts an array as optional parameters, which values will overwrite the
845 * currently set request options. If the parameter is empty or omitted,
846 * the options of the HttpRequest object will be reset.
848 * Returns TRUE on success, or FALSE on failure.
850 PHP_METHOD(HttpRequest
, setOptions
)
855 zval
*opts
= NULL
, *old_opts
, *new_opts
, *add_opts
, **opt
;
857 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
861 MAKE_STD_ZVAL(new_opts
);
862 array_init(new_opts
);
864 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
865 SET_PROP(options
, new_opts
);
866 zval_ptr_dtor(&new_opts
);
870 MAKE_STD_ZVAL(add_opts
);
871 array_init(add_opts
);
872 /* some options need extra attention -- thus cannot use array_merge() directly */
873 FOREACH_KEYVAL(pos
, opts
, key
, idx
, opt
) {
875 if (!strcmp(key
, "headers")) {
876 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, *opt
);
877 } else if (!strcmp(key
, "cookies")) {
878 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addcookies", NULL
, *opt
);
879 } else if (!strcmp(key
, "ssl")) {
880 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addssloptions", NULL
, *opt
);
881 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
882 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "seturl", NULL
, *opt
);
883 } else if (!strcmp(key
, "method")) {
884 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setmethod", NULL
, *opt
);
885 } else if (!strcmp(key
, "resetcookies")) {
886 http_request_object_resetcookies(getThis());
889 add_assoc_zval(add_opts
, key
, *opt
);
896 old_opts
= GET_PROP(options
);
897 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
898 array_copy(old_opts
, new_opts
);
900 array_merge(add_opts
, new_opts
);
901 SET_PROP(options
, new_opts
);
902 zval_ptr_dtor(&new_opts
);
903 zval_ptr_dtor(&add_opts
);
909 /* {{{ proto array HttpRequest::getOptions()
911 * Get currently set options.
913 * Returns an associative array containing currently set options.
915 PHP_METHOD(HttpRequest
, getOptions
)
919 if (return_value_used
) {
920 RETURN_PROP(options
);
925 /* {{{ proto bool HttpRequest::setSslOptions([array options])
929 * Accepts an associative array as parameter containing any SSL specific options.
930 * If the parameter is empty or omitted, the SSL options will be reset.
932 * Returns TRUE on success, or FALSE on failure.
934 PHP_METHOD(HttpRequest
, setSslOptions
)
936 http_request_object_set_options_subr("ssl", 1, 0);
940 /* {{{ proto bool HttpRequest::addSslOptions(array options)
942 * Set additional SSL options.
944 * Expects an associative array as parameter containing additional SSL specific options.
946 * Returns TRUE on success, or FALSE on failure.
948 PHP_METHOD(HttpRequest
, addSslOptions
)
950 http_request_object_set_options_subr("ssl", 0, 0);
954 /* {{{ proto array HttpRequest::getSslOtpions()
956 * Get previously set SSL options.
958 * Returns an associative array containing any previously set SSL options.
960 PHP_METHOD(HttpRequest
, getSslOptions
)
962 http_request_object_get_options_subr("ssl");
966 /* {{{ proto bool HttpRequest::addHeaders(array headers)
968 * Add request header name/value pairs.
970 * Expects an associative array as parameter containing additional header
973 * Returns TRUE on success, or FALSE on failure.
975 PHP_METHOD(HttpRequest
, addHeaders
)
977 http_request_object_set_options_subr("headers", 0, 1);
980 /* {{{ proto bool HttpRequest::setHeaders([array headers])
982 * Set request header name/value pairs.
984 * Accepts an associative array as parameter containing header name/value pairs.
985 * If the parameter is empty or omitted, all previously set headers will be unset.
987 * Returns TRUE on success, or FALSE on failure.
989 PHP_METHOD(HttpRequest
, setHeaders
)
991 http_request_object_set_options_subr("headers", 1, 1);
995 /* {{{ proto array HttpRequest::getHeaders()
997 * Get previously set request headers.
999 * Returns an associative array containing all currently set headers.
1001 PHP_METHOD(HttpRequest
, getHeaders
)
1003 http_request_object_get_options_subr("headers");
1007 /* {{{ proto bool HttpRequest::setCookies([array cookies])
1011 * Accepts an associative array as parameter containing cookie name/value pairs.
1012 * If the parameter is empty or omitted, all previously set cookies will be unset.
1014 * Returns TRUE on success, or FALSE on failure.
1016 PHP_METHOD(HttpRequest
, setCookies
)
1018 http_request_object_set_options_subr("cookies", 1, 0);
1022 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1026 * Expects an associative array as parameter containing any cookie name/value
1029 * Returns TRUE on success, or FALSE on failure.
1031 PHP_METHOD(HttpRequest
, addCookies
)
1033 http_request_object_set_options_subr("cookies", 0, 0);
1037 /* {{{ proto array HttpRequest::getCookies()
1039 * Get previously set cookies.
1041 * Returns an associative array containing any previously set cookies.
1043 PHP_METHOD(HttpRequest
, getCookies
)
1045 http_request_object_get_options_subr("cookies");
1049 #if HTTP_CURL_VERSION(7,14,1)
1050 /* {{{ proto bool HttpRequest::resetCookies()
1052 * Reset all cookies. Note that customly set cookies are not affected.
1054 PHP_METHOD(HttpRequest
, resetCookies
)
1057 RETURN_SUCCESS(http_request_object_resetcookies(getThis()));
1062 /* {{{ proto bool HttpRequest::setUrl(string url)
1064 * Set the request URL.
1066 * Expects a string as parameter specifying the request url.
1068 * Returns TRUE on success, or FALSE on failure.
1070 PHP_METHOD(HttpRequest
, setUrl
)
1075 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1079 UPD_STRL(url
, URL
, URL_len
);
1084 /* {{{ proto string HttpRequest::getUrl()
1086 * Get the previously set request URL.
1088 * Returns the currently set request url as string.
1090 PHP_METHOD(HttpRequest
, getUrl
)
1094 if (return_value_used
) {
1100 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1102 * Set the request method.
1104 * Expects an int as parameter specifying the request method to use.
1105 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
1107 * Returns TRUE on success, or FALSE on failure.
1109 PHP_METHOD(HttpRequest
, setMethod
)
1113 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1117 UPD_PROP(long, method
, meth
);
1122 /* {{{ proto int HttpRequest::getMethod()
1124 * Get the previously set request method.
1126 * Returns the currently set request method.
1128 PHP_METHOD(HttpRequest
, getMethod
)
1132 if (return_value_used
) {
1133 RETURN_PROP(method
);
1138 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1140 * Set the content type the post request should have.
1142 * Expects a string as parameters containing the content type of the request
1143 * (primary/secondary).
1145 * Returns TRUE on success, or FALSE if the content type does not seem to
1146 * contain a primary and a secondary part.
1148 PHP_METHOD(HttpRequest
, setContentType
)
1153 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1158 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1160 UPD_STRL(contentType
, ctype
, ct_len
);
1165 /* {{{ proto string HttpRequest::getContentType()
1167 * Get the previously content type.
1169 * Returns the previously set content type as string.
1171 PHP_METHOD(HttpRequest
, getContentType
)
1175 if (return_value_used
) {
1176 RETURN_PROP(contentType
);
1181 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1183 * Set the URL query parameters to use, overwriting previously set query parameters.
1184 * Affects any request types.
1186 * Accepts a string or associative array parameter containing the pre-encoded
1187 * query string or to be encoded query fields. If the parameter is empty or
1188 * omitted, the query data will be unset.
1190 * Returns TRUE on success, or FALSE on failure.
1192 PHP_METHOD(HttpRequest
, setQueryData
)
1196 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1200 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1201 UPD_STRL(queryData
, "", 0);
1202 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1203 char *query_data
= NULL
;
1205 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1209 UPD_PROP(string
, queryData
, query_data
);
1214 convert_to_string_ex(&qdata
);
1215 UPD_STRL(queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1216 if (orig
!= qdata
) {
1217 zval_ptr_dtor(&qdata
);
1224 /* {{{ proto string HttpRequest::getQueryData()
1226 * Get the current query data in form of an urlencoded query string.
1228 * Returns a string containing the urlencoded query.
1230 PHP_METHOD(HttpRequest
, getQueryData
)
1234 if (return_value_used
) {
1235 RETURN_PROP(queryData
);
1240 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1242 * Add parameters to the query parameter list, leaving previously set unchanged.
1243 * Affects any request type.
1245 * Expects an associative array as parameter containing the query fields to add.
1247 * Returns TRUE on success, or FALSE on failure.
1249 PHP_METHOD(HttpRequest
, addQueryData
)
1251 zval
*qdata
, *old_qdata
;
1252 char *query_data
= NULL
;
1253 size_t query_data_len
= 0;
1255 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &qdata
)) {
1259 old_qdata
= GET_PROP(queryData
);
1261 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
)) {
1265 UPD_STRL(queryData
, query_data
, query_data_len
);
1272 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1274 * Adds POST data entries, leaving previously set unchanged, unless a
1275 * post entry with the same name already exists.
1276 * Affects only POST and custom requests.
1278 * Expects an associative array as parameter containing the post fields.
1280 * Returns TRUE on success, or FALSE on failure.
1282 PHP_METHOD(HttpRequest
, addPostFields
)
1284 zval
*post_data
, *old_post
, *new_post
;
1286 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &post_data
)) {
1290 if (zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1291 MAKE_STD_ZVAL(new_post
);
1292 array_init(new_post
);
1293 old_post
= GET_PROP(postFields
);
1294 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1295 array_copy(old_post
, new_post
);
1297 array_merge(post_data
, new_post
);
1298 SET_PROP(postFields
, new_post
);
1299 zval_ptr_dtor(&new_post
);
1306 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1308 * Set the POST data entries, overwriting previously set POST data.
1309 * Affects only POST and custom requests.
1311 * Accepts an associative array as parameter containing the post fields.
1312 * If the parameter is empty or omitted, the post data will be unset.
1314 * Returns TRUE on success, or FALSE on failure.
1316 PHP_METHOD(HttpRequest
, setPostFields
)
1318 zval
*post
, *post_data
= NULL
;
1320 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &post_data
)) {
1324 MAKE_STD_ZVAL(post
);
1326 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1327 array_copy(post_data
, post
);
1329 SET_PROP(postFields
, post
);
1330 zval_ptr_dtor(&post
);
1336 /* {{{ proto array HttpRequest::getPostFields()
1338 * Get previously set POST data.
1340 * Returns the currently set post fields as associative array.
1342 PHP_METHOD(HttpRequest
, getPostFields
)
1346 if (return_value_used
) {
1347 RETURN_PROP(postFields
);
1352 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1354 * Set raw post data to send, overwriting previously set raw post data. Don't
1355 * forget to specify a content type. Affects only POST and custom requests.
1356 * Only either post fields or raw post data can be used for each request.
1357 * Raw post data has higher precedence and will be used even if post fields
1360 * Accepts a string as parameter containing the *raw* post data.
1362 * Returns TRUE on success, or FALSE on failure.
1364 PHP_METHOD(HttpRequest
, setRawPostData
)
1366 char *raw_data
= NULL
;
1369 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1377 UPD_STRL(rawPostData
, raw_data
, data_len
);
1382 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1384 * Add raw post data, leaving previously set raw post data unchanged.
1385 * Affects only POST and custom requests.
1387 * Expects a string as parameter containing the raw post data to concatenate.
1389 * Returns TRUE on success, or FALSE on failure.
1391 PHP_METHOD(HttpRequest
, addRawPostData
)
1396 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1401 zval
*data
= GET_PROP(rawPostData
);
1403 if (Z_STRLEN_P(data
)) {
1404 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1405 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1406 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, raw_data
, data_len
);
1408 UPD_STRL(putData
, raw_data
, data_len
);
1416 /* {{{ proto string HttpRequest::getRawPostData()
1418 * Get previously set raw post data.
1420 * Returns a string containing the currently set raw post data.
1422 PHP_METHOD(HttpRequest
, getRawPostData
)
1426 if (return_value_used
) {
1427 RETURN_PROP(rawPostData
);
1432 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1434 * Add a file to the POST request, leaving previously set files unchanged.
1435 * Affects only POST and custom requests. Cannot be used with raw post data.
1437 * Expects a string parameter containing the form element name, and a string
1438 * paremeter containing the path to the file which should be uploaded.
1439 * Additionally accepts an optional string parameter which should contain
1440 * the content type of the file.
1442 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1443 * primary and a secondary content type part.
1445 PHP_METHOD(HttpRequest
, addPostFile
)
1447 zval
*entry
, *old_post
, *new_post
;
1448 char *name
, *file
, *type
= NULL
;
1449 int name_len
, file_len
, type_len
= 0;
1451 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1456 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1458 type
= "application/x-octetstream";
1459 type_len
= sizeof("application/x-octetstream") - 1;
1462 MAKE_STD_ZVAL(entry
);
1465 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1466 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1467 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1469 MAKE_STD_ZVAL(new_post
);
1470 array_init(new_post
);
1471 old_post
= GET_PROP(postFiles
);
1472 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1473 array_copy(old_post
, new_post
);
1475 add_next_index_zval(new_post
, entry
);
1476 SET_PROP(postFiles
, new_post
);
1477 zval_ptr_dtor(&new_post
);
1483 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1485 * Set files to post, overwriting previously set post files.
1486 * Affects only POST and requests. Cannot be used with raw post data.
1488 * Accepts an array containing the files to post. Each entry should be an
1489 * associative array with "name", "file" and "type" keys. If the parameter
1490 * is empty or omitted the post files will be unset.
1492 * Returns TRUE on success, or FALSE on failure.
1494 PHP_METHOD(HttpRequest
, setPostFiles
)
1496 zval
*files
= NULL
, *post
;
1498 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!/", &files
)) {
1502 MAKE_STD_ZVAL(post
);
1504 if (files
&& (Z_TYPE_P(files
) == IS_ARRAY
)) {
1505 array_copy(files
, post
);
1507 SET_PROP(postFiles
, post
);
1508 zval_ptr_dtor(&post
);
1514 /* {{{ proto array HttpRequest::getPostFiles()
1516 * Get all previously added POST files.
1518 * Returns an array containing currently set post files.
1520 PHP_METHOD(HttpRequest
, getPostFiles
)
1524 if (return_value_used
) {
1525 RETURN_PROP(postFiles
);
1530 /* {{{ proto bool HttpRequest::setPutFile([string file])
1532 * Set file to put. Affects only PUT requests.
1534 * Accepts a string as parameter referencing the path to file.
1535 * If the parameter is empty or omitted the put file will be unset.
1537 * Returns TRUE on success, or FALSE on failure.
1539 PHP_METHOD(HttpRequest
, setPutFile
)
1544 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1548 UPD_STRL(putFile
, file
, file_len
);
1553 /* {{{ proto string HttpRequest::getPutFile()
1555 * Get previously set put file.
1557 * Returns a string containing the path to the currently set put file.
1559 PHP_METHOD(HttpRequest
, getPutFile
)
1563 if (return_value_used
) {
1564 RETURN_PROP(putFile
);
1569 /* {{{ proto bool HttpRequest::setPutData([string put_data])
1571 * Set PUT data to send, overwriting previously set PUT data.
1572 * Affects only PUT requests.
1573 * Only either PUT data or PUT file can be used for each request.
1574 * PUT data has higher precedence and will be used even if a PUT
1577 * Accepts a string as parameter containing the data to upload.
1579 * Returns TRUE on success, or FALSE on failure.
1581 PHP_METHOD(HttpRequest
, setPutData
)
1583 char *put_data
= NULL
;
1586 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &put_data
, &data_len
)) {
1594 UPD_STRL(putData
, put_data
, data_len
);
1599 /* {{{ proto bool HttpRequest::addPutData(string put_data)
1601 * Add PUT data, leaving previously set PUT data unchanged.
1602 * Affects only PUT requests.
1604 * Expects a string as parameter containing the data to concatenate.
1606 * Returns TRUE on success, or FALSE on failure.
1608 PHP_METHOD(HttpRequest
, addPutData
)
1613 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &put_data
, &data_len
)) {
1618 zval
*data
= GET_PROP(putData
);
1620 if (Z_STRLEN_P(data
)) {
1621 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1622 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1623 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, put_data
, data_len
);
1625 UPD_STRL(putData
, put_data
, data_len
);
1633 /* {{{ proto string HttpRequest::getPutData()
1635 * Get previously set PUT data.
1637 * Returns a string containing the currently set raw post data.
1639 PHP_METHOD(HttpRequest
, getPutData
)
1643 if (return_value_used
) {
1644 RETURN_PROP(putData
);
1649 /* {{{ proto array HttpRequest::getResponseData()
1651 * Get all response data after the request has been sent.
1653 * Returns an associative array with the key "headers" containing an associative
1654 * array holding all response headers, as well as the key "body" containing a
1655 * string with the response body.
1657 * If redirects were allowed and several responses were received, the data
1658 * references the last received response.
1660 PHP_METHOD(HttpRequest
, getResponseData
)
1664 if (return_value_used
) {
1665 RETURN_PROP(responseData
);
1670 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1672 * Get response header(s) after the request has been sent.
1674 * Accepts an string as optional parameter specifying a certain header to read.
1675 * If the parameter is empty or omitted all response headers will be returned.
1677 * Returns either a string with the value of the header matching name if requested,
1678 * FALSE on failure, or an associative array containing all response headers.
1680 * If redirects were allowed and several responses were received, the data
1681 * references the last received response.
1683 PHP_METHOD(HttpRequest
, getResponseHeader
)
1685 if (return_value_used
) {
1686 zval
*data
, **headers
, **header
;
1687 char *header_name
= NULL
;
1690 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1694 data
= GET_PROP(responseData
);
1695 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1696 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void *) &headers
)) &&
1697 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1698 if (!header_len
|| !header_name
) {
1699 RETVAL_ZVAL(*headers
, 1, 0);
1700 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void *) &header
)) {
1701 RETVAL_ZVAL(*header
, 1, 0);
1712 /* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]])
1714 * Get response cookie(s) after the request has been sent.
1716 * Returns an array of stdClass objects like http_parse_cookie would return.
1718 * If redirects were allowed and several responses were received, the data
1719 * references the last received response.
1721 PHP_METHOD(HttpRequest
, getResponseCookies
)
1723 if (return_value_used
) {
1725 zval
*allowed_extras_array
= NULL
, *data
, **headers
;
1727 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|la", &flags
, &allowed_extras_array
)) {
1731 data
= GET_PROP(responseData
);
1732 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1733 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void *) &headers
)) &&
1734 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1737 char *key
= NULL
, **allowed_extras
= NULL
;
1738 zval
**header
= NULL
, **entry
= NULL
;
1739 HashPosition pos
, pos1
, pos2
;
1741 array_init(return_value
);
1743 if (allowed_extras_array
) {
1744 allowed_extras
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array
)) + 1, sizeof(char *));
1745 FOREACH_VAL(pos
, allowed_extras_array
, entry
) {
1746 ZVAL_ADDREF(*entry
);
1747 convert_to_string_ex(entry
);
1748 allowed_extras
[i
++] = estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
1749 zval_ptr_dtor(entry
);
1753 FOREACH_HASH_KEYVAL(pos1
, Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1754 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1755 http_cookie_list list
;
1757 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1758 zval
**single_header
;
1760 FOREACH_VAL(pos2
, *header
, single_header
) {
1761 ZVAL_ADDREF(*single_header
);
1762 convert_to_string_ex(single_header
);
1763 if (http_parse_cookie_ex(&list
, Z_STRVAL_PP(single_header
), flags
, allowed_extras
)) {
1766 MAKE_STD_ZVAL(cookie
);
1767 object_init(cookie
);
1768 http_cookie_list_tostruct(&list
, cookie
);
1769 add_next_index_zval(return_value
, cookie
);
1770 http_cookie_list_dtor(&list
);
1772 zval_ptr_dtor(single_header
);
1775 ZVAL_ADDREF(*header
);
1776 convert_to_string_ex(header
);
1777 if (http_parse_cookie_ex(&list
, Z_STRVAL_PP(header
), flags
, allowed_extras
)) {
1780 MAKE_STD_ZVAL(cookie
);
1781 object_init(cookie
);
1782 http_cookie_list_tostruct(&list
, cookie
);
1783 add_next_index_zval(return_value
, cookie
);
1784 http_cookie_list_dtor(&list
);
1786 zval_ptr_dtor(header
);
1793 if (allowed_extras
) {
1794 for (i
= 0; allowed_extras
[i
]; ++i
) {
1795 efree(allowed_extras
[i
]);
1797 efree(allowed_extras
);
1806 /* {{{ proto string HttpRequest::getResponseBody()
1808 * Get the response body after the request has been sent.
1810 * Returns a string containing the response body.
1812 * If redirects were allowed and several responses were received, the data
1813 * references the last received response.
1815 PHP_METHOD(HttpRequest
, getResponseBody
)
1819 if (return_value_used
) {
1821 zval
*data
= GET_PROP(responseData
);
1823 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1824 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void *) &body
))) {
1825 RETURN_ZVAL(*body
, 1, 0);
1833 /* {{{ proto int HttpRequest::getResponseCode()
1835 * Get the response code after the request has been sent.
1837 * Returns an int representing the response code.
1839 * If redirects were allowed and several responses were received, the data
1840 * references the last received response.
1842 PHP_METHOD(HttpRequest
, getResponseCode
)
1846 if (return_value_used
) {
1847 RETURN_PROP(responseCode
);
1852 /* {{{ proto string HttpRequest::getResponseStatus()
1854 * Get the response status (i.e. the string after the response code) after the message has been sent.
1856 * Returns a string containing the response status text.
1858 PHP_METHOD(HttpRequest
, getResponseStatus
)
1862 if (return_value_used
) {
1863 RETURN_PROP(responseStatus
);
1868 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1870 * Get response info after the request has been sent.
1871 * See http_get() for a full list of returned info.
1873 * Accepts a string as optional parameter specifying the info to read.
1874 * If the parameter is empty or omitted, an associative array containing
1875 * all available info will be returned.
1877 * Returns either a scalar containing the value of the info matching name if
1878 * requested, FALSE on failure, or an associative array containing all
1881 * If redirects were allowed and several responses were received, the data
1882 * references the last received response.
1884 PHP_METHOD(HttpRequest
, getResponseInfo
)
1886 if (return_value_used
) {
1887 zval
*info
, **infop
;
1888 char *info_name
= NULL
;
1891 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1895 info
= GET_PROP(responseInfo
);
1897 if (Z_TYPE_P(info
) != IS_ARRAY
) {
1901 if (info_len
&& info_name
) {
1902 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void *) &infop
)) {
1903 RETURN_ZVAL(*infop
, 1, 0);
1905 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1909 RETURN_ZVAL(info
, 1, 0);
1915 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1917 * Get the full response as HttpMessage object after the request has been sent.
1919 * Returns an HttpMessage object of the response.
1921 * If redirects were allowed and several responses were received, the data
1922 * references the last received response. Use HttpMessage::getParentMessage()
1923 * to access the data of previously received responses within this request
1926 * Throws HttpException, HttpRuntimeException.
1928 PHP_METHOD(HttpRequest
, getResponseMessage
)
1933 SET_EH_THROW_HTTP();
1934 message
= GET_PROP(responseMessage
);
1935 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1936 RETVAL_OBJECT(message
, 1);
1938 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HttpRequest does not contain a response message");
1945 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1947 * Get sent HTTP message.
1949 * Returns an HttpMessage object representing the sent request.
1951 * If redirects were allowed and several responses were received, the data
1952 * references the last received response. Use HttpMessage::getParentMessage()
1953 * to access the data of previously sent requests within this request
1956 * Note that the internal request message is immutable, that means that the
1957 * request message received through HttpRequest::getRequestMessage() will
1958 * always look the same for the same request, regardless of any changes you
1959 * may have made to the returned object.
1961 * Throws HttpMalformedHeadersException, HttpEncodingException.
1963 PHP_METHOD(HttpRequest
, getRequestMessage
)
1967 if (return_value_used
) {
1969 getObject(http_request_object
, obj
);
1971 SET_EH_THROW_HTTP();
1972 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1973 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
1980 /* {{{ proto string HttpRequest::getRawRequestMessage()
1982 * Get sent HTTP message.
1984 * Returns an HttpMessage in a form of a string
1986 PHP_METHOD(HttpRequest
, getRawRequestMessage
)
1990 if (return_value_used
) {
1991 getObject(http_request_object
, obj
);
1993 RETURN_PHPSTR_DUP(&obj
->request
->conv
.request
);
1998 /* {{{ proto string HttpRequest::getRawResponseMessage()
2000 * Get the entire HTTP response.
2002 * Returns the complete web server response, including the headers in a form of a string.
2004 PHP_METHOD(HttpRequest
, getRawResponseMessage
)
2008 if (return_value_used
) {
2009 getObject(http_request_object
, obj
);
2011 RETURN_PHPSTR_DUP(&obj
->request
->conv
.response
);
2016 /* {{{ proto HttpMessage HttpRequest::getHistory()
2018 * Get all sent requests and received responses as an HttpMessage object.
2020 * If you want to record history, set the instance variable
2021 * HttpRequest::$recordHistory to TRUE.
2023 * Returns an HttpMessage object representing the complete request/response
2026 * The object references the last received response, use HttpMessage::getParentMessage()
2027 * to access the data of previously sent requests and received responses.
2029 * Throws HttpRuntimeException.
2031 PHP_METHOD(HttpRequest
, getHistory
)
2035 if (return_value_used
) {
2038 SET_EH_THROW_HTTP();
2039 hist
= GET_PROP(history
);
2040 if (Z_TYPE_P(hist
) == IS_OBJECT
) {
2041 RETVAL_OBJECT(hist
, 1);
2043 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "The history is empty");
2050 /* {{{ proto void HttpRequest::clearHistory()
2052 * Clear the history.
2054 PHP_METHOD(HttpRequest
, clearHistory
)
2059 MAKE_STD_ZVAL(hist
);
2061 SET_PROP(history
, hist
);
2062 zval_ptr_dtor(&hist
);
2067 /* {{{ proto HttpMessage HttpRequest::send()
2069 * Send the HTTP request.
2071 * Returns the received response as HttpMessage object.
2073 * NOTE: While an exception may be thrown, the transfer could have succeeded
2074 * at least partially, so you might want to check the return values of various
2075 * HttpRequest::getResponse*() methods.
2077 * Throws HttpRuntimeException, HttpRequestException,
2078 * HttpMalformedHeaderException, HttpEncodingException.
2083 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
2084 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
2085 * $r->addQueryData(array('category' => 3));
2088 * if ($r->getResponseCode() == 200) {
2089 * file_put_contents('local.rss', $r->getResponseBody());
2091 * } catch (HttpException $ex) {
2100 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
2101 * $r->setOptions(array('cookies' => array('lang' => 'de')));
2102 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
2103 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
2105 * echo $r->send()->getBody();
2106 * } catch (HttpException $ex) {
2112 PHP_METHOD(HttpRequest
, send
)
2114 getObject(http_request_object
, obj
);
2118 SET_EH_THROW_HTTP();
2123 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
2124 } else if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
2125 http_request_exec(obj
->request
);
2126 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
2127 RETVAL_OBJECT(GET_PROP(responseMessage
), 1);
2135 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
2142 * vim600: noet sw=4 ts=4 fdm=marker
2143 * vim<600: noet sw=4 ts=4