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-2007, 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))
36 #define HTTP_REQUEST_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpRequest_##al), HTTP_ARGS(HttpRequest, al), vis)
38 HTTP_BEGIN_ARGS(__construct
, 0)
40 HTTP_ARG_VAL(method
, 0)
41 HTTP_ARG_VAL(options
, 0)
44 HTTP_BEGIN_ARGS(factory
, 0)
46 HTTP_ARG_VAL(method
, 0)
47 HTTP_ARG_VAL(options
, 0)
48 HTTP_ARG_VAL(class_name
, 0)
51 HTTP_EMPTY_ARGS(getOptions
);
52 HTTP_BEGIN_ARGS(setOptions
, 0)
53 HTTP_ARG_VAL(options
, 0)
56 HTTP_EMPTY_ARGS(getSslOptions
);
57 HTTP_BEGIN_ARGS(setSslOptions
, 0)
58 HTTP_ARG_VAL(ssl_options
, 0)
61 HTTP_BEGIN_ARGS(addSslOptions
, 0)
62 HTTP_ARG_VAL(ssl_optins
, 0)
65 HTTP_EMPTY_ARGS(getHeaders
);
66 HTTP_BEGIN_ARGS(setHeaders
, 0)
67 HTTP_ARG_VAL(headers
, 0)
70 HTTP_BEGIN_ARGS(addHeaders
, 1)
71 HTTP_ARG_VAL(headers
, 0)
74 HTTP_EMPTY_ARGS(getCookies
);
75 HTTP_BEGIN_ARGS(setCookies
, 0)
76 HTTP_ARG_VAL(cookies
, 0)
79 HTTP_BEGIN_ARGS(addCookies
, 1)
80 HTTP_ARG_VAL(cookies
, 0)
83 HTTP_EMPTY_ARGS(enableCookies
);
84 #if HTTP_CURL_VERSION(7,14,1)
85 HTTP_BEGIN_ARGS(resetCookies
, 0)
86 HTTP_ARG_VAL(session_only
, 0)
90 HTTP_EMPTY_ARGS(getUrl
);
91 HTTP_BEGIN_ARGS(setUrl
, 1)
95 HTTP_EMPTY_ARGS(getMethod
);
96 HTTP_BEGIN_ARGS(setMethod
, 1)
97 HTTP_ARG_VAL(request_method
, 0)
100 HTTP_EMPTY_ARGS(getContentType
);
101 HTTP_BEGIN_ARGS(setContentType
, 1)
102 HTTP_ARG_VAL(content_type
, 0)
105 HTTP_EMPTY_ARGS(getQueryData
);
106 HTTP_BEGIN_ARGS(setQueryData
, 0)
107 HTTP_ARG_VAL(query_data
, 0)
110 HTTP_BEGIN_ARGS(addQueryData
, 1)
111 HTTP_ARG_VAL(query_data
, 0)
114 HTTP_EMPTY_ARGS(getPostFields
);
115 HTTP_BEGIN_ARGS(setPostFields
, 0)
116 HTTP_ARG_VAL(post_fields
, 0)
119 HTTP_BEGIN_ARGS(addPostFields
, 1)
120 HTTP_ARG_VAL(post_fields
, 0)
123 HTTP_EMPTY_ARGS(getPostFiles
);
124 HTTP_BEGIN_ARGS(setPostFiles
, 0)
125 HTTP_ARG_VAL(post_files
, 0)
128 HTTP_BEGIN_ARGS(addPostFile
, 2)
129 HTTP_ARG_VAL(formname
, 0)
130 HTTP_ARG_VAL(filename
, 0)
131 HTTP_ARG_VAL(content_type
, 0)
134 HTTP_EMPTY_ARGS(getBody
);
135 HTTP_BEGIN_ARGS(setBody
, 0)
136 HTTP_ARG_VAL(request_body_data
, 0)
139 HTTP_BEGIN_ARGS(addBody
, 1)
140 HTTP_ARG_VAL(request_body_data
, 0)
143 HTTP_EMPTY_ARGS(getPutFile
);
144 HTTP_BEGIN_ARGS(setPutFile
, 0)
145 HTTP_ARG_VAL(filename
, 0)
148 HTTP_EMPTY_ARGS(getPutData
);
149 HTTP_BEGIN_ARGS(setPutData
, 0)
150 HTTP_ARG_VAL(put_data
, 0)
153 HTTP_BEGIN_ARGS(addPutData
, 1)
154 HTTP_ARG_VAL(put_data
, 0)
157 HTTP_EMPTY_ARGS(getResponseData
);
158 HTTP_BEGIN_ARGS(getResponseHeader
, 0)
159 HTTP_ARG_VAL(name
, 0)
162 HTTP_BEGIN_ARGS(getResponseCookies
, 0)
163 HTTP_ARG_VAL(flags
, 0)
164 HTTP_ARG_VAL(allowed_extras
, 0)
167 HTTP_EMPTY_ARGS(getResponseBody
);
168 HTTP_EMPTY_ARGS(getResponseCode
);
169 HTTP_EMPTY_ARGS(getResponseStatus
);
170 HTTP_BEGIN_ARGS(getResponseInfo
, 0)
171 HTTP_ARG_VAL(name
, 0)
174 HTTP_EMPTY_ARGS(getResponseMessage
);
175 HTTP_EMPTY_ARGS(getRawResponseMessage
);
176 HTTP_EMPTY_ARGS(getRequestMessage
);
177 HTTP_EMPTY_ARGS(getRawRequestMessage
);
178 HTTP_EMPTY_ARGS(getHistory
);
179 HTTP_EMPTY_ARGS(clearHistory
);
180 HTTP_EMPTY_ARGS(send
);
182 HTTP_BEGIN_ARGS(get
, 1)
184 HTTP_ARG_VAL(options
, 0)
185 HTTP_ARG_VAL(info
, 1)
188 HTTP_BEGIN_ARGS(head
, 1)
190 HTTP_ARG_VAL(options
, 0)
191 HTTP_ARG_VAL(info
, 1)
194 HTTP_BEGIN_ARGS(postData
, 2)
196 HTTP_ARG_VAL(data
, 0)
197 HTTP_ARG_VAL(options
, 0)
198 HTTP_ARG_VAL(info
, 1)
201 HTTP_BEGIN_ARGS(postFields
, 2)
203 HTTP_ARG_VAL(data
, 0)
204 HTTP_ARG_VAL(options
, 0)
205 HTTP_ARG_VAL(info
, 1)
208 HTTP_BEGIN_ARGS(putData
, 2)
210 HTTP_ARG_VAL(data
, 0)
211 HTTP_ARG_VAL(options
, 0)
212 HTTP_ARG_VAL(info
, 1)
215 HTTP_BEGIN_ARGS(putFile
, 2)
217 HTTP_ARG_VAL(file
, 0)
218 HTTP_ARG_VAL(options
, 0)
219 HTTP_ARG_VAL(info
, 1)
222 HTTP_BEGIN_ARGS(putStream
, 2)
224 HTTP_ARG_VAL(stream
, 0)
225 HTTP_ARG_VAL(options
, 0)
226 HTTP_ARG_VAL(info
, 1)
229 HTTP_BEGIN_ARGS(methodRegister
, 1)
230 HTTP_ARG_VAL(method_name
, 0)
233 HTTP_BEGIN_ARGS(methodUnregister
, 1)
234 HTTP_ARG_VAL(method
, 0)
237 HTTP_BEGIN_ARGS(methodName
, 1)
238 HTTP_ARG_VAL(method_id
, 0)
241 HTTP_BEGIN_ARGS(methodExists
, 1)
242 HTTP_ARG_VAL(method
, 0)
245 #if defined(HAVE_CURL_GETFORMDATA) || defined(HAVE_CURL_FORMGET)
246 HTTP_BEGIN_ARGS(encodeBody
, 2)
247 HTTP_ARG_VAL(fields
, 0)
248 HTTP_ARG_VAL(files
, 0)
252 #define OBJ_PROP_CE http_request_object_ce
253 zend_class_entry
*http_request_object_ce
;
254 zend_function_entry http_request_object_fe
[] = {
255 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
257 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
258 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
260 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
261 HTTP_REQUEST_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
263 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
265 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
269 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(enableCookies
, ZEND_ACC_PUBLIC
)
272 #if HTTP_CURL_VERSION(7,14,1)
273 HTTP_REQUEST_ME(resetCookies
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
277 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
279 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
280 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
282 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
283 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
285 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
286 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
287 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
289 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
290 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
291 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
293 HTTP_REQUEST_ME(setBody
, ZEND_ACC_PUBLIC
)
294 HTTP_REQUEST_ME(getBody
, ZEND_ACC_PUBLIC
)
295 HTTP_REQUEST_ME(addBody
, ZEND_ACC_PUBLIC
)
296 HTTP_REQUEST_MALIAS(setRawPostData
, setBody
, ZEND_ACC_PUBLIC
|ZEND_ACC_DEPRECATED
)
297 HTTP_REQUEST_MALIAS(getRawPostData
, getBody
, ZEND_ACC_PUBLIC
|ZEND_ACC_DEPRECATED
)
298 HTTP_REQUEST_MALIAS(addRawPostData
, addBody
, ZEND_ACC_PUBLIC
|ZEND_ACC_DEPRECATED
)
300 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
301 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
302 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
304 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
305 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
307 HTTP_REQUEST_ME(setPutData
, ZEND_ACC_PUBLIC
)
308 HTTP_REQUEST_ME(getPutData
, ZEND_ACC_PUBLIC
)
309 HTTP_REQUEST_ME(addPutData
, ZEND_ACC_PUBLIC
)
311 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
313 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
314 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
315 HTTP_REQUEST_ME(getResponseCookies
, ZEND_ACC_PUBLIC
)
316 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
317 HTTP_REQUEST_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
318 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
319 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
320 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
321 HTTP_REQUEST_ME(getRawResponseMessage
, ZEND_ACC_PUBLIC
)
322 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
323 HTTP_REQUEST_ME(getRawRequestMessage
, ZEND_ACC_PUBLIC
)
324 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
325 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
327 HTTP_REQUEST_ME(factory
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
329 HTTP_REQUEST_ALIAS(get
, http_get
)
330 HTTP_REQUEST_ALIAS(head
, http_head
)
331 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
332 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
333 HTTP_REQUEST_ALIAS(putData
, http_put_data
)
334 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
335 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
337 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
338 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
339 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
340 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
341 #if defined(HAVE_CURL_GETFORMDATA) || defined(HAVE_CURL_FORMGET)
342 HTTP_REQUEST_ALIAS(encodeBody
, http_request_body_encode
)
346 static zend_object_handlers http_request_object_handlers
;
348 PHP_MINIT_FUNCTION(http_request_object
)
350 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
351 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
353 DCL_PROP_N(PRIVATE
, options
);
354 DCL_PROP_N(PRIVATE
, postFields
);
355 DCL_PROP_N(PRIVATE
, postFiles
);
356 DCL_PROP_N(PRIVATE
, responseInfo
);
357 DCL_PROP_N(PRIVATE
, responseMessage
);
358 DCL_PROP(PRIVATE
, long, responseCode
, 0);
359 DCL_PROP(PRIVATE
, string
, responseStatus
, "");
360 DCL_PROP(PRIVATE
, long, method
, HTTP_GET
);
361 DCL_PROP(PRIVATE
, string
, url
, "");
362 DCL_PROP(PRIVATE
, string
, contentType
, "");
363 DCL_PROP(PRIVATE
, string
, requestBody
, "");
364 DCL_PROP(PRIVATE
, string
, queryData
, "");
365 DCL_PROP(PRIVATE
, string
, putFile
, "");
366 DCL_PROP(PRIVATE
, string
, putData
, "");
367 DCL_PROP_N(PRIVATE
, history
);
368 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
372 * Request Method Constants
375 DCL_CONST(long, "METH_GET", HTTP_GET
);
376 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
377 DCL_CONST(long, "METH_POST", HTTP_POST
);
378 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
379 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
380 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
381 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
382 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
383 /* WebDAV - RFC 2518 */
384 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
385 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
386 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
387 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
388 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
389 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
390 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
391 /* WebDAV Versioning - RFC 3253 */
392 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
393 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
394 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
395 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
396 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
397 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
398 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
399 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
400 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
401 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
402 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
403 /* WebDAV Access Control - RFC 3744 */
404 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
407 * HTTP Protocol Version Constants
409 DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0
);
410 DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1
);
411 DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE
); /* to be removed */
412 DCL_CONST(long, "VERSION_ANY", CURL_HTTP_VERSION_NONE
);
415 * SSL Version Constants
417 DCL_CONST(long, "SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1
);
418 DCL_CONST(long, "SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2
);
419 DCL_CONST(long, "SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3
);
420 DCL_CONST(long, "SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT
);
425 DCL_CONST(long, "IPRESOLVE_V4", CURL_IPRESOLVE_V4
);
426 DCL_CONST(long, "IPRESOLVE_V6", CURL_IPRESOLVE_V6
);
427 DCL_CONST(long, "IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER
);
432 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
433 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
434 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
435 DCL_CONST(long, "AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE
);
436 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
439 * Proxy Type Constants
441 # if HTTP_CURL_VERSION(7,15,2)
442 DCL_CONST(long, "PROXY_SOCKS4", CURLPROXY_SOCKS4
);
444 DCL_CONST(long, "PROXY_SOCKS5", CURLPROXY_SOCKS5
);
445 DCL_CONST(long, "PROXY_HTTP", CURLPROXY_HTTP
);
451 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
453 return http_request_object_new_ex(ce
, NULL
, NULL
);
456 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
458 zend_object_value ov
;
459 http_request_object
*o
;
461 o
= ecalloc(1, sizeof(http_request_object
));
463 o
->request
= http_request_init_ex(NULL
, ch
, 0, NULL
);
469 ALLOC_HASHTABLE(OBJ_PROP(o
));
470 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
471 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
473 ov
.handle
= putObject(http_request_object
, o
);
474 ov
.handlers
= &http_request_object_handlers
;
479 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
481 zend_object_value new_ov
;
482 http_request_object
*new_obj
;
483 getObject(http_request_object
, old_obj
);
485 new_ov
= http_request_object_new_ex(old_obj
->zo
.ce
, NULL
, &new_obj
);
486 if (old_obj
->request
->ch
) {
487 http_curl_init_ex(http_curl_copy(old_obj
->request
->ch
), new_obj
->request
);
490 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
491 phpstr_append(&new_obj
->request
->conv
.request
, old_obj
->request
->conv
.request
.data
, old_obj
->request
->conv
.request
.used
);
492 phpstr_append(&new_obj
->request
->conv
.response
, old_obj
->request
->conv
.response
.data
, old_obj
->request
->conv
.response
.used
);
497 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
499 http_request_object
*o
= (http_request_object
*) object
;
501 http_request_free(&o
->request
);
505 #define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC)
506 static inline void _http_request_object_check_request_content_type(zval
*this_ptr TSRMLS_DC
)
508 zval
*ctype
= GET_PROP(contentType
);
510 if (Z_STRLEN_P(ctype
)) {
511 zval
**headers
, *opts
= GET_PROP(options
);
513 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
514 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void *) &headers
)) &&
515 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
518 /* only override if not already set */
519 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void *) &ct_header
))) {
520 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
522 /* or not a string, zero length string or a string of spaces */
523 if ((Z_TYPE_PP(ct_header
) != IS_STRING
) || !Z_STRLEN_PP(ct_header
)) {
524 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
526 int i
, only_space
= 1;
528 /* check for spaces only */
529 for (i
= 0; i
< Z_STRLEN_PP(ct_header
); ++i
) {
530 if (!HTTP_IS_CTYPE(space
, Z_STRVAL_PP(ct_header
)[i
])) {
536 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
542 MAKE_STD_ZVAL(headers
);
544 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
545 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, headers
);
546 zval_ptr_dtor(&headers
);
551 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
553 STATUS status
= SUCCESS
;
555 http_request_reset(obj
->request
);
556 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, http_curl_init(obj
->request
), return FAILURE
);
558 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(GET_PROP(url
)));
560 switch (obj
->request
->meth
= Z_LVAL_P(GET_PROP(method
)))
568 zval
*put_file
= GET_PROP(putFile
);
570 http_request_object_check_request_content_type(getThis());
572 if (Z_STRLEN_P(put_file
)) {
573 php_stream_statbuf ssb
;
574 php_stream
*stream
= php_stream_open_wrapper_ex(Z_STRVAL_P(put_file
), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
, HTTP_DEFAULT_STREAM_CONTEXT
);
576 if (stream
&& SUCCESS
== php_stream_stat(stream
, &ssb
)) {
577 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
582 zval
*put_data
= GET_PROP(putData
);
583 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
584 estrndup(Z_STRVAL_P(put_data
), Z_STRLEN_P(put_data
)), Z_STRLEN_P(put_data
), 1);
592 /* check for raw request body */
593 zval
*raw_data
= GET_PROP(requestBody
);
595 if (Z_STRLEN_P(raw_data
)) {
596 http_request_object_check_request_content_type(getThis());
597 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
598 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
600 zval
*zfields
= GET_PROP(postFields
), *zfiles
= GET_PROP(postFiles
);
604 fields
= (Z_TYPE_P(zfields
) == IS_ARRAY
) ? Z_ARRVAL_P(zfields
) : NULL
;
605 files
= (Z_TYPE_P(zfiles
) == IS_ARRAY
) ? Z_ARRVAL_P(zfiles
) : NULL
;
607 if ((fields
&& zend_hash_num_elements(fields
)) || (files
&& zend_hash_num_elements(files
))) {
608 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
617 if (status
== SUCCESS
) {
618 zval
*qdata
= GET_PROP(queryData
);
619 zval
*options
= GET_PROP(options
);
621 if (Z_STRLEN_P(qdata
)) {
622 if (!strchr(obj
->request
->url
, '?')) {
623 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
625 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
627 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
630 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
632 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
633 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onprogress", sizeof("onprogress"))) {
636 if ( (Z_TYPE_P(options
) != IS_ARRAY
)
637 || (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(options
), "onprogress", sizeof("onprogress"), (void *) &entry
)
638 || (!zval_is_true(*entry
)))) {
641 ZVAL_ADDREF(getThis());
642 add_next_index_zval(pcb
, getThis());
643 add_next_index_stringl(pcb
, "onprogress", lenof("onprogress"), 1);
644 http_request_set_progress_callback(obj
->request
, pcb
);
653 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
659 /* always fetch info */
662 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
663 SET_PROP(responseInfo
, info
);
664 zval_ptr_dtor(&info
);
666 /* parse response message */
667 phpstr_fix(&obj
->request
->conv
.request
);
668 phpstr_fix(&obj
->request
->conv
.response
);
670 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
)))) {
673 if (zval_is_true(GET_PROP(recordHistory
))) {
674 zval
*hist
, *history
= GET_PROP(history
);
675 http_message
*response
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
));
676 http_message
*request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
679 ZVAL_OBJVAL(hist
, http_message_object_new_ex(http_message_object_ce
, http_message_interconnect(response
, 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
);
687 UPD_PROP(long, responseCode
, msg
->http
.info
.response
.code
);
688 UPD_PROP(string
, responseStatus
, STR_PTR(msg
->http
.info
.response
.status
));
690 MAKE_STD_ZVAL(message
);
691 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
692 SET_PROP(responseMessage
, message
);
693 zval_ptr_dtor(&message
);
697 /* update properties with empty values*/
700 MAKE_STD_ZVAL(znull
);
702 SET_PROP(responseMessage
, znull
);
703 zval_ptr_dtor(&znull
);
705 UPD_PROP(long, responseCode
, 0);
706 UPD_PROP(string
, responseStatus
, "");
708 /* append request message to history */
709 if (zval_is_true(GET_PROP(recordHistory
))) {
710 http_message
*request
;
712 if ((request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
713 zval
*hist
, *history
= GET_PROP(history
);
716 ZVAL_OBJVAL(hist
, http_message_object_new_ex(http_message_object_ce
, request
, NULL
), 0);
717 if (Z_TYPE_P(history
) == IS_OBJECT
) {
718 http_message_object_prepend(hist
, history
);
720 SET_PROP(history
, hist
);
721 zval_ptr_dtor(&hist
);
728 http_request_set_progress_callback(obj
->request
, NULL
);
730 if (!EG(exception
) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onfinish", sizeof("onfinish"))) {
733 MAKE_STD_ZVAL(param
);
734 ZVAL_BOOL(param
, ret
== SUCCESS
);
735 with_error_handling(EH_NORMAL
, NULL
) {
736 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "onfinish", NULL
, param
);
737 } end_error_handling();
738 zval_ptr_dtor(¶m
);
744 static int apply_pretty_key(void *pDest
, int num_args
, va_list args
, zend_hash_key
*hash_key
)
746 if (hash_key
->nKeyLength
> 1) {
747 hash_key
->h
= zend_hash_func(pretty_key(hash_key
->arKey
, hash_key
->nKeyLength
- 1, 1, 0), hash_key
->nKeyLength
);
749 return ZEND_HASH_APPLY_KEEP
;
752 #define http_request_object_set_options_subr(key, ow, pk) \
753 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
754 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
, int prettify_keys
)
756 zval
*old_opts
, *new_opts
, *opts
= NULL
, **entry
= NULL
;
758 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
762 MAKE_STD_ZVAL(new_opts
);
763 array_init(new_opts
);
764 old_opts
= GET_PROP(options
);
765 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
766 array_copy(Z_ARRVAL_P(old_opts
), Z_ARRVAL_P(new_opts
));
769 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), key
, len
, (void *) &entry
)) {
771 zend_hash_clean(Z_ARRVAL_PP(entry
));
773 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
775 array_copy(Z_ARRVAL_P(opts
), Z_ARRVAL_PP(entry
));
777 array_join(Z_ARRVAL_P(opts
), Z_ARRVAL_PP(entry
), 0, prettify_keys
? ARRAY_JOIN_PRETTIFY
: 0);
782 zend_hash_apply_with_arguments(Z_ARRVAL_P(opts
), apply_pretty_key
, 0);
785 add_assoc_zval_ex(new_opts
, key
, len
, opts
);
787 SET_PROP(options
, new_opts
);
788 zval_ptr_dtor(&new_opts
);
793 #define http_request_object_get_options_subr(key) \
794 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
795 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
799 if (return_value_used
) {
800 zval
*opts
, **options
;
802 opts
= GET_PROP(options
);
803 array_init(return_value
);
805 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
806 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void *) &options
))) {
807 convert_to_array(*options
);
808 array_copy(Z_ARRVAL_PP(options
), Z_ARRVAL_P(return_value
));
814 /* ### USERLAND ### */
816 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
817 Create a new HttpRequest object instance. */
818 PHP_METHOD(HttpRequest
, __construct
)
823 zval
*options
= NULL
;
826 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla!", &URL
, &URL_len
, &meth
, &options
)) {
828 UPD_STRL(url
, URL
, URL_len
);
831 UPD_PROP(long, method
, meth
);
834 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
841 /* {{{ proto HttpRequest HttpRequest::factory([string url[, int request_method HTTP_METH_GET[, array options[, string class_name = "HttpRequest"]]]])
842 Create a new HttpRequest object instance. */
843 PHP_METHOD(HttpRequest
, factory
)
845 char *cn
= NULL
, *URL
= NULL
;
846 int cl
= 0, URL_len
= 0;
848 zval
*options
= NULL
;
849 zend_object_value ov
;
852 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla!s", &URL
, &URL_len
, &meth
, &options
, &cn
, &cl
) &&
853 SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_request_object_new_ex
, http_request_object_ce
, NULL
, NULL
)) {
854 RETVAL_OBJVAL(ov
, 0);
855 getThis() = return_value
;
857 UPD_STRL(url
, URL
, URL_len
);
860 UPD_PROP(long, method
, meth
);
863 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
870 /* {{{ proto bool HttpRequest::setOptions([array options])
871 Set the request options to use. See http_get() for a full list of available options. */
872 PHP_METHOD(HttpRequest
, setOptions
)
874 HashKey key
= initHashKey(0);
876 zval
*opts
= NULL
, *old_opts
, *new_opts
, *add_opts
, **opt
;
878 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
882 MAKE_STD_ZVAL(new_opts
);
883 array_init(new_opts
);
885 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
886 SET_PROP(options
, new_opts
);
887 zval_ptr_dtor(&new_opts
);
891 MAKE_STD_ZVAL(add_opts
);
892 array_init(add_opts
);
893 /* some options need extra attention -- thus cannot use array_merge() directly */
894 FOREACH_KEYVAL(pos
, opts
, key
, opt
) {
895 if (key
.type
== HASH_KEY_IS_STRING
) {
896 if (!strcmp(key
.str
, "headers")) {
897 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, *opt
);
898 } else if (!strcmp(key
.str
, "cookies")) {
899 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addcookies", NULL
, *opt
);
900 } else if (!strcmp(key
.str
, "ssl")) {
901 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addssloptions", NULL
, *opt
);
902 } else if ((!strcasecmp(key
.str
, "url")) || (!strcasecmp(key
.str
, "uri"))) {
903 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "seturl", NULL
, *opt
);
904 } else if (!strcmp(key
.str
, "method")) {
905 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setmethod", NULL
, *opt
);
906 #if HTTP_CURL_VERSION(7,14,1)
907 } else if (!strcmp(key
.str
, "resetcookies")) {
908 getObject(http_request_object
, obj
);
909 http_request_reset_cookies(obj
->request
, 0);
911 } else if (!strcmp(key
.str
, "enablecookies")) {
912 getObject(http_request_object
, obj
);
913 http_request_enable_cookies(obj
->request
);
914 } else if (!strcasecmp(key
.str
, "recordHistory")) {
915 UPD_PROP(bool, recordHistory
, 1);
918 add_assoc_zval_ex(add_opts
, key
.str
, key
.len
, *opt
);
923 old_opts
= GET_PROP(options
);
924 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
925 array_copy(Z_ARRVAL_P(old_opts
), Z_ARRVAL_P(new_opts
));
927 array_join(Z_ARRVAL_P(add_opts
), Z_ARRVAL_P(new_opts
), 0, 0);
928 SET_PROP(options
, new_opts
);
929 zval_ptr_dtor(&new_opts
);
930 zval_ptr_dtor(&add_opts
);
936 /* {{{ proto array HttpRequest::getOptions()
937 Get currently set options. */
938 PHP_METHOD(HttpRequest
, getOptions
)
942 if (return_value_used
) {
943 RETURN_PROP(options
);
948 /* {{{ proto bool HttpRequest::setSslOptions([array options])
950 PHP_METHOD(HttpRequest
, setSslOptions
)
952 http_request_object_set_options_subr("ssl", 1, 0);
956 /* {{{ proto bool HttpRequest::addSslOptions(array options)
957 Set additional SSL options. */
958 PHP_METHOD(HttpRequest
, addSslOptions
)
960 http_request_object_set_options_subr("ssl", 0, 0);
964 /* {{{ proto array HttpRequest::getSslOtpions()
965 Get previously set SSL options. */
966 PHP_METHOD(HttpRequest
, getSslOptions
)
968 http_request_object_get_options_subr("ssl");
972 /* {{{ proto bool HttpRequest::addHeaders(array headers)
973 Add request header name/value pairs. */
974 PHP_METHOD(HttpRequest
, addHeaders
)
976 http_request_object_set_options_subr("headers", 0, 1);
979 /* {{{ proto bool HttpRequest::setHeaders([array headers])
980 Set request header name/value pairs. */
981 PHP_METHOD(HttpRequest
, setHeaders
)
983 http_request_object_set_options_subr("headers", 1, 1);
987 /* {{{ proto array HttpRequest::getHeaders()
988 Get previously set request headers. */
989 PHP_METHOD(HttpRequest
, getHeaders
)
991 http_request_object_get_options_subr("headers");
995 /* {{{ proto bool HttpRequest::setCookies([array cookies])
997 PHP_METHOD(HttpRequest
, setCookies
)
999 http_request_object_set_options_subr("cookies", 1, 0);
1003 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1005 PHP_METHOD(HttpRequest
, addCookies
)
1007 http_request_object_set_options_subr("cookies", 0, 0);
1011 /* {{{ proto array HttpRequest::getCookies()
1012 Get previously set cookies. */
1013 PHP_METHOD(HttpRequest
, getCookies
)
1015 http_request_object_get_options_subr("cookies");
1019 /* {{{ proto bool HttpRequest::enableCookies()
1020 Enable automatic sending of received cookies. Note that customly set cookies will be sent anyway. */
1021 PHP_METHOD(HttpRequest
, enableCookies
)
1024 getObject(http_request_object
, obj
);
1025 RETURN_SUCCESS(http_request_enable_cookies(obj
->request
));
1031 /* {{{ proto bool HttpRequest::resetCookies([bool session_only = FALSE])
1032 Reset all automatically received/sent cookies. Note that customly set cookies are not affected. */
1033 PHP_METHOD(HttpRequest
, resetCookies
)
1035 zend_bool session_only
= 0;
1036 getObject(http_request_object
, obj
);
1038 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &session_only
)) {
1041 RETURN_SUCCESS(http_request_reset_cookies(obj
->request
, session_only
));
1045 /* {{{ proto bool HttpRequest::setUrl(string url)
1046 Set the request URL. */
1047 PHP_METHOD(HttpRequest
, setUrl
)
1052 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1056 UPD_STRL(url
, URL
, URL_len
);
1061 /* {{{ proto string HttpRequest::getUrl()
1062 Get the previously set request URL. */
1063 PHP_METHOD(HttpRequest
, getUrl
)
1067 if (return_value_used
) {
1073 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1074 Set the request method. */
1075 PHP_METHOD(HttpRequest
, setMethod
)
1079 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1083 UPD_PROP(long, method
, meth
);
1088 /* {{{ proto int HttpRequest::getMethod()
1089 Get the previously set request method. */
1090 PHP_METHOD(HttpRequest
, getMethod
)
1094 if (return_value_used
) {
1095 RETURN_PROP(method
);
1100 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1101 Set the content type the post request should have. */
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()
1120 Get the previously content type. */
1121 PHP_METHOD(HttpRequest
, getContentType
)
1125 if (return_value_used
) {
1126 RETURN_PROP(contentType
);
1131 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1132 Set the URL query parameters to use, overwriting previously set query parameters. */
1133 PHP_METHOD(HttpRequest
, setQueryData
)
1137 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1141 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1142 UPD_STRL(queryData
, "", 0);
1143 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1144 char *query_data
= NULL
;
1146 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1150 UPD_PROP(string
, queryData
, query_data
);
1155 convert_to_string_ex(&qdata
);
1156 UPD_STRL(queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1157 if (orig
!= qdata
) {
1158 zval_ptr_dtor(&qdata
);
1165 /* {{{ proto string HttpRequest::getQueryData()
1166 Get the current query data in form of an urlencoded query string. */
1167 PHP_METHOD(HttpRequest
, getQueryData
)
1171 if (return_value_used
) {
1172 RETURN_PROP(queryData
);
1177 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1178 Add parameters to the query parameter list, leaving previously set unchanged. */
1179 PHP_METHOD(HttpRequest
, addQueryData
)
1181 zval
*qdata
, *old_qdata
;
1182 char *query_data
= NULL
;
1183 size_t query_data_len
= 0;
1185 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &qdata
)) {
1189 old_qdata
= GET_PROP(queryData
);
1191 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
)) {
1195 UPD_STRL(queryData
, query_data
, query_data_len
);
1202 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1203 Adds POST data entries, leaving previously set unchanged, unless a post entry with the same name already exists. */
1204 PHP_METHOD(HttpRequest
, addPostFields
)
1206 zval
*post_data
, *old_post
, *new_post
;
1208 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &post_data
)) {
1212 if (zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1213 MAKE_STD_ZVAL(new_post
);
1214 array_init(new_post
);
1215 old_post
= GET_PROP(postFields
);
1216 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1217 array_copy(Z_ARRVAL_P(old_post
), Z_ARRVAL_P(new_post
));
1219 array_join(Z_ARRVAL_P(post_data
), Z_ARRVAL_P(new_post
), 0, 0);
1220 SET_PROP(postFields
, new_post
);
1221 zval_ptr_dtor(&new_post
);
1228 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1229 Set the POST data entries, overwriting previously set POST data. */
1230 PHP_METHOD(HttpRequest
, setPostFields
)
1232 zval
*post
, *post_data
= NULL
;
1234 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &post_data
)) {
1238 MAKE_STD_ZVAL(post
);
1240 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1241 array_copy(Z_ARRVAL_P(post_data
), Z_ARRVAL_P(post
));
1243 SET_PROP(postFields
, post
);
1244 zval_ptr_dtor(&post
);
1250 /* {{{ proto array HttpRequest::getPostFields()
1251 Get previously set POST data. */
1252 PHP_METHOD(HttpRequest
, getPostFields
)
1256 if (return_value_used
) {
1257 RETURN_PROP(postFields
);
1262 /* {{{ proto bool HttpRequest::setBody([string request_body_data])
1263 Set request body to send, overwriting previously set request body. Don't forget to specify a content type. */
1264 PHP_METHOD(HttpRequest
, setBody
)
1266 char *raw_data
= NULL
;
1269 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1277 UPD_STRL(requestBody
, raw_data
, data_len
);
1282 /* {{{ proto bool HttpRequest::addBody(string request_body_data)
1283 Add request body data, leaving previously set request body data unchanged. */
1284 PHP_METHOD(HttpRequest
, addBody
)
1289 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1294 zval
*data
= GET_PROP(rrequestBody
);
1296 if (Z_STRLEN_P(data
)) {
1297 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1298 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1299 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, raw_data
, data_len
);
1301 UPD_STRL(putData
, raw_data
, data_len
);
1309 /* {{{ proto string HttpRequest::getBody()
1310 Get previously set request body data. */
1311 PHP_METHOD(HttpRequest
, getBody
)
1315 if (return_value_used
) {
1316 RETURN_PROP(requestBody
);
1321 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1322 Add a file to the POST request, leaving previously set files unchanged. */
1323 PHP_METHOD(HttpRequest
, addPostFile
)
1325 zval
*entry
, *old_post
, *new_post
;
1326 char *name
, *file
, *type
= NULL
;
1327 int name_len
, file_len
, type_len
= 0;
1329 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1334 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1336 type
= "application/x-octetstream";
1337 type_len
= sizeof("application/x-octetstream") - 1;
1340 MAKE_STD_ZVAL(entry
);
1343 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1344 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1345 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1347 MAKE_STD_ZVAL(new_post
);
1348 array_init(new_post
);
1349 old_post
= GET_PROP(postFiles
);
1350 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1351 array_copy(Z_ARRVAL_P(old_post
), Z_ARRVAL_P(new_post
));
1353 add_next_index_zval(new_post
, entry
);
1354 SET_PROP(postFiles
, new_post
);
1355 zval_ptr_dtor(&new_post
);
1361 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1362 Set files to post, overwriting previously set post files. */
1363 PHP_METHOD(HttpRequest
, setPostFiles
)
1365 zval
*files
= NULL
, *post
;
1367 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!/", &files
)) {
1371 MAKE_STD_ZVAL(post
);
1373 if (files
&& (Z_TYPE_P(files
) == IS_ARRAY
)) {
1374 array_copy(Z_ARRVAL_P(files
), Z_ARRVAL_P(post
));
1376 SET_PROP(postFiles
, post
);
1377 zval_ptr_dtor(&post
);
1383 /* {{{ proto array HttpRequest::getPostFiles()
1384 Get all previously added POST files. */
1385 PHP_METHOD(HttpRequest
, getPostFiles
)
1389 if (return_value_used
) {
1390 RETURN_PROP(postFiles
);
1395 /* {{{ proto bool HttpRequest::setPutFile([string file])
1396 Set file to put. Affects only PUT requests. */
1397 PHP_METHOD(HttpRequest
, setPutFile
)
1402 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1406 UPD_STRL(putFile
, file
, file_len
);
1411 /* {{{ proto string HttpRequest::getPutFile()
1412 Get previously set put file. */
1413 PHP_METHOD(HttpRequest
, getPutFile
)
1417 if (return_value_used
) {
1418 RETURN_PROP(putFile
);
1423 /* {{{ proto bool HttpRequest::setPutData([string put_data])
1424 Set PUT data to send, overwriting previously set PUT data. */
1425 PHP_METHOD(HttpRequest
, setPutData
)
1427 char *put_data
= NULL
;
1430 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &put_data
, &data_len
)) {
1438 UPD_STRL(putData
, put_data
, data_len
);
1443 /* {{{ proto bool HttpRequest::addPutData(string put_data)
1444 Add PUT data, leaving previously set PUT data unchanged. */
1445 PHP_METHOD(HttpRequest
, addPutData
)
1450 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &put_data
, &data_len
)) {
1455 zval
*data
= GET_PROP(putData
);
1457 if (Z_STRLEN_P(data
)) {
1458 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1459 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1460 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, put_data
, data_len
);
1462 UPD_STRL(putData
, put_data
, data_len
);
1470 /* {{{ proto string HttpRequest::getPutData()
1471 Get previously set PUT data. */
1472 PHP_METHOD(HttpRequest
, getPutData
)
1476 if (return_value_used
) {
1477 RETURN_PROP(putData
);
1482 /* {{{ proto array HttpRequest::getResponseData()
1483 Get all response data after the request has been sent. */
1484 PHP_METHOD(HttpRequest
, getResponseData
)
1488 if (return_value_used
) {
1491 zval
*headers
, *message
= GET_PROP(responseMessage
);
1493 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1494 getObjectEx(http_message_object
, msg
, message
);
1496 array_init(return_value
);
1498 MAKE_STD_ZVAL(headers
);
1499 array_init(headers
);
1500 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1501 add_assoc_zval(return_value
, "headers", headers
);
1503 phpstr_data(PHPSTR(msg
->message
), &body
, &body_len
);
1504 add_assoc_stringl(return_value
, "body", body
, body_len
, 0);
1510 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1511 Get response header(s) after the request has been sent. */
1512 PHP_METHOD(HttpRequest
, getResponseHeader
)
1514 if (return_value_used
) {
1516 char *header_name
= NULL
;
1519 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1520 zval
*message
= GET_PROP(responseMessage
);
1522 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1523 getObjectEx(http_message_object
, msg
, message
);
1526 if ((header
= http_message_header_ex(msg
->message
, pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, 0))) {
1527 RETURN_ZVAL(header
, 1, 1);
1530 array_init(return_value
);
1531 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1541 /* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]])
1542 Get response cookie(s) after the request has been sent. */
1543 PHP_METHOD(HttpRequest
, getResponseCookies
)
1545 if (return_value_used
) {
1547 zval
*allowed_extras_array
= NULL
;
1549 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|la!", &flags
, &allowed_extras_array
)) {
1551 HashKey key
= initHashKey(0);
1552 char **allowed_extras
= NULL
;
1553 zval
**header
= NULL
, **entry
= NULL
, *message
= GET_PROP(responseMessage
);
1554 HashPosition pos
, pos1
, pos2
;
1556 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1557 getObjectEx(http_message_object
, msg
, message
);
1559 array_init(return_value
);
1561 if (allowed_extras_array
) {
1562 allowed_extras
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array
)) + 1, sizeof(char *));
1563 FOREACH_VAL(pos
, allowed_extras_array
, entry
) {
1564 ZVAL_ADDREF(*entry
);
1565 convert_to_string_ex(entry
);
1566 allowed_extras
[i
++] = estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
1567 zval_ptr_dtor(entry
);
1571 FOREACH_HASH_KEYVAL(pos1
, &msg
->message
->hdrs
, key
, header
) {
1572 if (key
.type
== HASH_KEY_IS_STRING
&& !strcasecmp(key
.str
, "Set-Cookie")) {
1573 http_cookie_list list
;
1575 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1576 zval
**single_header
;
1578 FOREACH_VAL(pos2
, *header
, single_header
) {
1579 ZVAL_ADDREF(*single_header
);
1580 convert_to_string_ex(single_header
);
1581 if (http_parse_cookie_ex(&list
, Z_STRVAL_PP(single_header
), flags
, allowed_extras
)) {
1584 MAKE_STD_ZVAL(cookie
);
1585 object_init(cookie
);
1586 http_cookie_list_tostruct(&list
, cookie
);
1587 add_next_index_zval(return_value
, cookie
);
1588 http_cookie_list_dtor(&list
);
1590 zval_ptr_dtor(single_header
);
1593 ZVAL_ADDREF(*header
);
1594 convert_to_string_ex(header
);
1595 if (http_parse_cookie_ex(&list
, Z_STRVAL_PP(header
), flags
, allowed_extras
)) {
1598 MAKE_STD_ZVAL(cookie
);
1599 object_init(cookie
);
1600 http_cookie_list_tostruct(&list
, cookie
);
1601 add_next_index_zval(return_value
, cookie
);
1602 http_cookie_list_dtor(&list
);
1604 zval_ptr_dtor(header
);
1609 if (allowed_extras
) {
1610 for (i
= 0; allowed_extras
[i
]; ++i
) {
1611 efree(allowed_extras
[i
]);
1613 efree(allowed_extras
);
1624 /* {{{ proto string HttpRequest::getResponseBody()
1625 Get the response body after the request has been sent. */
1626 PHP_METHOD(HttpRequest
, getResponseBody
)
1630 if (return_value_used
) {
1631 zval
*message
= GET_PROP(responseMessage
);
1633 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1634 getObjectEx(http_message_object
, msg
, message
);
1635 RETURN_PHPSTR_DUP(&msg
->message
->body
);
1643 /* {{{ proto int HttpRequest::getResponseCode()
1644 Get the response code after the request has been sent. */
1645 PHP_METHOD(HttpRequest
, getResponseCode
)
1649 if (return_value_used
) {
1650 RETURN_PROP(responseCode
);
1655 /* {{{ proto string HttpRequest::getResponseStatus()
1656 Get the response status (i.e. the string after the response code) after the message has been sent. */
1657 PHP_METHOD(HttpRequest
, getResponseStatus
)
1661 if (return_value_used
) {
1662 RETURN_PROP(responseStatus
);
1667 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1668 Get response info after the request has been sent. */
1669 PHP_METHOD(HttpRequest
, getResponseInfo
)
1671 if (return_value_used
) {
1672 zval
*info
, **infop
;
1673 char *info_name
= NULL
;
1676 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1680 info
= GET_PROP(responseInfo
);
1682 if (Z_TYPE_P(info
) != IS_ARRAY
) {
1686 if (info_len
&& info_name
) {
1687 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void *) &infop
)) {
1688 RETURN_ZVAL(*infop
, 1, 0);
1690 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1694 RETURN_ZVAL(info
, 1, 0);
1700 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1701 Get the full response as HttpMessage object after the request has been sent. */
1702 PHP_METHOD(HttpRequest
, getResponseMessage
)
1707 SET_EH_THROW_HTTP();
1708 message
= GET_PROP(responseMessage
);
1709 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1710 RETVAL_OBJECT(message
, 1);
1712 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HttpRequest does not contain a response message");
1719 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1720 Get sent HTTP message. */
1721 PHP_METHOD(HttpRequest
, getRequestMessage
)
1725 if (return_value_used
) {
1727 getObject(http_request_object
, obj
);
1729 SET_EH_THROW_HTTP();
1730 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1731 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
1738 /* {{{ proto string HttpRequest::getRawRequestMessage()
1739 Get sent HTTP message. */
1740 PHP_METHOD(HttpRequest
, getRawRequestMessage
)
1744 if (return_value_used
) {
1745 getObject(http_request_object
, obj
);
1747 RETURN_PHPSTR_DUP(&obj
->request
->conv
.request
);
1752 /* {{{ proto string HttpRequest::getRawResponseMessage()
1753 Get the entire HTTP response. */
1754 PHP_METHOD(HttpRequest
, getRawResponseMessage
)
1758 if (return_value_used
) {
1759 getObject(http_request_object
, obj
);
1761 RETURN_PHPSTR_DUP(&obj
->request
->conv
.response
);
1766 /* {{{ proto HttpMessage HttpRequest::getHistory()
1767 Get all sent requests and received responses as an HttpMessage object. */
1768 PHP_METHOD(HttpRequest
, getHistory
)
1772 if (return_value_used
) {
1775 SET_EH_THROW_HTTP();
1776 hist
= GET_PROP(history
);
1777 if (Z_TYPE_P(hist
) == IS_OBJECT
) {
1778 RETVAL_OBJECT(hist
, 1);
1780 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "The history is empty");
1787 /* {{{ proto void HttpRequest::clearHistory()
1788 Clear the history. */
1789 PHP_METHOD(HttpRequest
, clearHistory
)
1794 MAKE_STD_ZVAL(hist
);
1796 SET_PROP(history
, hist
);
1797 zval_ptr_dtor(&hist
);
1802 /* {{{ proto HttpMessage HttpRequest::send()
1803 Send the HTTP request. */
1804 PHP_METHOD(HttpRequest
, send
)
1806 getObject(http_request_object
, obj
);
1810 SET_EH_THROW_HTTP();
1815 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1816 } else if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
1817 http_request_exec(obj
->request
);
1818 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
1819 RETVAL_OBJECT(GET_PROP(responseMessage
), 1);
1827 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1834 * vim600: noet sw=4 ts=4 fdm=marker
1835 * vim<600: noet sw=4 ts=4