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_BEGIN_ARGS(factory
, 0)
45 HTTP_ARG_VAL(method
, 0)
46 HTTP_ARG_VAL(options
, 0)
47 HTTP_ARG_VAL(class_name
, 0)
50 HTTP_EMPTY_ARGS(getOptions
);
51 HTTP_BEGIN_ARGS(setOptions
, 0)
52 HTTP_ARG_VAL(options
, 0)
55 HTTP_EMPTY_ARGS(getSslOptions
);
56 HTTP_BEGIN_ARGS(setSslOptions
, 0)
57 HTTP_ARG_VAL(ssl_options
, 0)
60 HTTP_BEGIN_ARGS(addSslOptions
, 0)
61 HTTP_ARG_VAL(ssl_optins
, 0)
64 HTTP_EMPTY_ARGS(getHeaders
);
65 HTTP_BEGIN_ARGS(setHeaders
, 0)
66 HTTP_ARG_VAL(headers
, 0)
69 HTTP_BEGIN_ARGS(addHeaders
, 1)
70 HTTP_ARG_VAL(headers
, 0)
73 HTTP_EMPTY_ARGS(getCookies
);
74 HTTP_BEGIN_ARGS(setCookies
, 0)
75 HTTP_ARG_VAL(cookies
, 0)
78 HTTP_BEGIN_ARGS(addCookies
, 1)
79 HTTP_ARG_VAL(cookies
, 0)
82 HTTP_EMPTY_ARGS(enableCookies
);
83 #if HTTP_CURL_VERSION(7,14,1)
84 HTTP_BEGIN_ARGS(resetCookies
, 0)
85 HTTP_ARG_VAL(session_only
, 0)
89 HTTP_EMPTY_ARGS(getUrl
);
90 HTTP_BEGIN_ARGS(setUrl
, 1)
94 HTTP_EMPTY_ARGS(getMethod
);
95 HTTP_BEGIN_ARGS(setMethod
, 1)
96 HTTP_ARG_VAL(request_method
, 0)
99 HTTP_EMPTY_ARGS(getContentType
);
100 HTTP_BEGIN_ARGS(setContentType
, 1)
101 HTTP_ARG_VAL(content_type
, 0)
104 HTTP_EMPTY_ARGS(getQueryData
);
105 HTTP_BEGIN_ARGS(setQueryData
, 0)
106 HTTP_ARG_VAL(query_data
, 0)
109 HTTP_BEGIN_ARGS(addQueryData
, 1)
110 HTTP_ARG_VAL(query_data
, 0)
113 HTTP_EMPTY_ARGS(getPostFields
);
114 HTTP_BEGIN_ARGS(setPostFields
, 0)
115 HTTP_ARG_VAL(post_fields
, 0)
118 HTTP_BEGIN_ARGS(addPostFields
, 1)
119 HTTP_ARG_VAL(post_fields
, 0)
122 HTTP_EMPTY_ARGS(getPostFiles
);
123 HTTP_BEGIN_ARGS(setPostFiles
, 0)
124 HTTP_ARG_VAL(post_files
, 0)
127 HTTP_BEGIN_ARGS(addPostFile
, 2)
128 HTTP_ARG_VAL(formname
, 0)
129 HTTP_ARG_VAL(filename
, 0)
130 HTTP_ARG_VAL(content_type
, 0)
133 HTTP_EMPTY_ARGS(getRawPostData
);
134 HTTP_BEGIN_ARGS(setRawPostData
, 0)
135 HTTP_ARG_VAL(raw_post_data
, 0)
138 HTTP_BEGIN_ARGS(addRawPostData
, 1)
139 HTTP_ARG_VAL(raw_post_data
, 0)
142 HTTP_EMPTY_ARGS(getPutFile
);
143 HTTP_BEGIN_ARGS(setPutFile
, 0)
144 HTTP_ARG_VAL(filename
, 0)
147 HTTP_EMPTY_ARGS(getPutData
);
148 HTTP_BEGIN_ARGS(setPutData
, 0)
149 HTTP_ARG_VAL(put_data
, 0)
152 HTTP_BEGIN_ARGS(addPutData
, 1)
153 HTTP_ARG_VAL(put_data
, 0)
156 HTTP_EMPTY_ARGS(getResponseData
);
157 HTTP_BEGIN_ARGS(getResponseHeader
, 0)
158 HTTP_ARG_VAL(name
, 0)
161 HTTP_BEGIN_ARGS(getResponseCookies
, 0)
162 HTTP_ARG_VAL(flags
, 0)
163 HTTP_ARG_VAL(allowed_extras
, 0)
166 HTTP_EMPTY_ARGS(getResponseBody
);
167 HTTP_EMPTY_ARGS(getResponseCode
);
168 HTTP_EMPTY_ARGS(getResponseStatus
);
169 HTTP_BEGIN_ARGS(getResponseInfo
, 0)
170 HTTP_ARG_VAL(name
, 0)
173 HTTP_EMPTY_ARGS(getResponseMessage
);
174 HTTP_EMPTY_ARGS(getRawResponseMessage
);
175 HTTP_EMPTY_ARGS(getRequestMessage
);
176 HTTP_EMPTY_ARGS(getRawRequestMessage
);
177 HTTP_EMPTY_ARGS(getHistory
);
178 HTTP_EMPTY_ARGS(clearHistory
);
179 HTTP_EMPTY_ARGS(send
);
181 HTTP_BEGIN_ARGS(get
, 1)
183 HTTP_ARG_VAL(options
, 0)
184 HTTP_ARG_VAL(info
, 1)
187 HTTP_BEGIN_ARGS(head
, 1)
189 HTTP_ARG_VAL(options
, 0)
190 HTTP_ARG_VAL(info
, 1)
193 HTTP_BEGIN_ARGS(postData
, 2)
195 HTTP_ARG_VAL(data
, 0)
196 HTTP_ARG_VAL(options
, 0)
197 HTTP_ARG_VAL(info
, 1)
200 HTTP_BEGIN_ARGS(postFields
, 2)
202 HTTP_ARG_VAL(data
, 0)
203 HTTP_ARG_VAL(options
, 0)
204 HTTP_ARG_VAL(info
, 1)
207 HTTP_BEGIN_ARGS(putData
, 2)
209 HTTP_ARG_VAL(data
, 0)
210 HTTP_ARG_VAL(options
, 0)
211 HTTP_ARG_VAL(info
, 1)
214 HTTP_BEGIN_ARGS(putFile
, 2)
216 HTTP_ARG_VAL(file
, 0)
217 HTTP_ARG_VAL(options
, 0)
218 HTTP_ARG_VAL(info
, 1)
221 HTTP_BEGIN_ARGS(putStream
, 2)
223 HTTP_ARG_VAL(stream
, 0)
224 HTTP_ARG_VAL(options
, 0)
225 HTTP_ARG_VAL(info
, 1)
228 HTTP_BEGIN_ARGS(methodRegister
, 1)
229 HTTP_ARG_VAL(method_name
, 0)
232 HTTP_BEGIN_ARGS(methodUnregister
, 1)
233 HTTP_ARG_VAL(method
, 0)
236 HTTP_BEGIN_ARGS(methodName
, 1)
237 HTTP_ARG_VAL(method_id
, 0)
240 HTTP_BEGIN_ARGS(methodExists
, 1)
241 HTTP_ARG_VAL(method
, 0)
244 #if defined(HAVE_CURL_GETFORMDATA) || defined(HAVE_CURL_FORMGET)
245 HTTP_BEGIN_ARGS(encodeBody
, 2)
246 HTTP_ARG_VAL(fields
, 0)
247 HTTP_ARG_VAL(files
, 0)
251 #define OBJ_PROP_CE http_request_object_ce
252 zend_class_entry
*http_request_object_ce
;
253 zend_function_entry http_request_object_fe
[] = {
254 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
256 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
257 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
258 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
260 HTTP_REQUEST_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
262 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
263 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
266 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
270 HTTP_REQUEST_ME(enableCookies
, ZEND_ACC_PUBLIC
)
271 #if HTTP_CURL_VERSION(7,14,1)
272 HTTP_REQUEST_ME(resetCookies
, ZEND_ACC_PUBLIC
)
275 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
278 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
279 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
281 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
282 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
284 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
285 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
286 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
288 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
289 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
290 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
292 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
293 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
294 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
296 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
297 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
298 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
300 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
301 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
303 HTTP_REQUEST_ME(setPutData
, ZEND_ACC_PUBLIC
)
304 HTTP_REQUEST_ME(getPutData
, ZEND_ACC_PUBLIC
)
305 HTTP_REQUEST_ME(addPutData
, ZEND_ACC_PUBLIC
)
307 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
309 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
310 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
311 HTTP_REQUEST_ME(getResponseCookies
, ZEND_ACC_PUBLIC
)
312 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
313 HTTP_REQUEST_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
314 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
315 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
316 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
317 HTTP_REQUEST_ME(getRawResponseMessage
, ZEND_ACC_PUBLIC
)
318 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
319 HTTP_REQUEST_ME(getRawRequestMessage
, ZEND_ACC_PUBLIC
)
320 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
321 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
323 HTTP_REQUEST_ME(factory
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
325 HTTP_REQUEST_ALIAS(get
, http_get
)
326 HTTP_REQUEST_ALIAS(head
, http_head
)
327 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
328 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
329 HTTP_REQUEST_ALIAS(putData
, http_put_data
)
330 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
331 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
333 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
334 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
335 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
336 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
337 #if defined(HAVE_CURL_GETFORMDATA) || defined(HAVE_CURL_FORMGET)
338 HTTP_REQUEST_ALIAS(encodeBody
, http_request_body_encode
)
342 static zend_object_handlers http_request_object_handlers
;
344 PHP_MINIT_FUNCTION(http_request_object
)
346 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
347 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
349 DCL_PROP_N(PRIVATE
, options
);
350 DCL_PROP_N(PRIVATE
, postFields
);
351 DCL_PROP_N(PRIVATE
, postFiles
);
352 DCL_PROP_N(PRIVATE
, responseInfo
);
353 DCL_PROP_N(PRIVATE
, responseMessage
);
354 DCL_PROP(PRIVATE
, long, responseCode
, 0);
355 DCL_PROP(PRIVATE
, string
, responseStatus
, "");
356 DCL_PROP(PRIVATE
, long, method
, HTTP_GET
);
357 DCL_PROP(PRIVATE
, string
, url
, "");
358 DCL_PROP(PRIVATE
, string
, contentType
, "");
359 DCL_PROP(PRIVATE
, string
, rawPostData
, "");
360 DCL_PROP(PRIVATE
, string
, queryData
, "");
361 DCL_PROP(PRIVATE
, string
, putFile
, "");
362 DCL_PROP(PRIVATE
, string
, putData
, "");
363 DCL_PROP_N(PRIVATE
, history
);
364 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
368 * Request Method Constants
371 DCL_CONST(long, "METH_GET", HTTP_GET
);
372 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
373 DCL_CONST(long, "METH_POST", HTTP_POST
);
374 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
375 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
376 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
377 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
378 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
379 /* WebDAV - RFC 2518 */
380 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
381 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
382 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
383 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
384 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
385 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
386 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
387 /* WebDAV Versioning - RFC 3253 */
388 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
389 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
390 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
391 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
392 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
393 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
394 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
395 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
396 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
397 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
398 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
399 /* WebDAV Access Control - RFC 3744 */
400 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
403 * HTTP Protocol Version Constants
405 DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0
);
406 DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1
);
407 DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE
); /* to be removed */
408 DCL_CONST(long, "VERSION_ANY", CURL_HTTP_VERSION_NONE
);
411 * SSL Version Constants
413 DCL_CONST(long, "SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1
);
414 DCL_CONST(long, "SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2
);
415 DCL_CONST(long, "SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3
);
416 DCL_CONST(long, "SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT
);
421 DCL_CONST(long, "IPRESOLVE_V4", CURL_IPRESOLVE_V4
);
422 DCL_CONST(long, "IPRESOLVE_V6", CURL_IPRESOLVE_V6
);
423 DCL_CONST(long, "IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER
);
428 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
429 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
430 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
431 DCL_CONST(long, "AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE
);
432 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
435 * Proxy Type Constants
437 # if HTTP_CURL_VERSION(7,15,2)
438 DCL_CONST(long, "PROXY_SOCKS4", CURLPROXY_SOCKS4
);
440 DCL_CONST(long, "PROXY_SOCKS5", CURLPROXY_SOCKS5
);
441 DCL_CONST(long, "PROXY_HTTP", CURLPROXY_HTTP
);
447 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
449 return http_request_object_new_ex(ce
, NULL
, NULL
);
452 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
454 zend_object_value ov
;
455 http_request_object
*o
;
457 o
= ecalloc(1, sizeof(http_request_object
));
459 o
->request
= http_request_init_ex(NULL
, ch
, 0, NULL
);
465 ALLOC_HASHTABLE(OBJ_PROP(o
));
466 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
467 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
469 ov
.handle
= putObject(http_request_object
, o
);
470 ov
.handlers
= &http_request_object_handlers
;
475 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
477 zend_object_value new_ov
;
478 http_request_object
*new_obj
;
479 getObject(http_request_object
, old_obj
);
481 new_ov
= http_request_object_new_ex(old_obj
->zo
.ce
, NULL
, &new_obj
);
482 if (old_obj
->request
->ch
) {
483 http_curl_init_ex(curl_easy_duphandle(old_obj
->request
->ch
), new_obj
->request
);
486 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
487 phpstr_append(&new_obj
->request
->conv
.request
, old_obj
->request
->conv
.request
.data
, old_obj
->request
->conv
.request
.used
);
488 phpstr_append(&new_obj
->request
->conv
.response
, old_obj
->request
->conv
.response
.data
, old_obj
->request
->conv
.response
.used
);
493 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
495 http_request_object
*o
= (http_request_object
*) object
;
497 http_request_free(&o
->request
);
501 #define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC)
502 static inline void _http_request_object_check_request_content_type(zval
*this_ptr TSRMLS_DC
)
504 zval
*ctype
= GET_PROP(contentType
);
506 if (Z_STRLEN_P(ctype
)) {
507 zval
**headers
, *opts
= GET_PROP(options
);
509 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
510 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void *) &headers
)) &&
511 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
514 /* only override if not already set */
515 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void *) &ct_header
))) {
516 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
518 /* or not a string, zero length string or a string of spaces */
519 if ((Z_TYPE_PP(ct_header
) != IS_STRING
) || !Z_STRLEN_PP(ct_header
)) {
520 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
522 int i
, only_space
= 1;
524 /* check for spaces only */
525 for (i
= 0; i
< Z_STRLEN_PP(ct_header
); ++i
) {
526 if (!HTTP_IS_CTYPE(space
, Z_STRVAL_PP(ct_header
)[i
])) {
532 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
538 MAKE_STD_ZVAL(headers
);
540 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
541 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, headers
);
542 zval_ptr_dtor(&headers
);
547 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
549 STATUS status
= SUCCESS
;
551 http_request_reset(obj
->request
);
552 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, http_curl_init(obj
->request
), return FAILURE
);
554 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(GET_PROP(url
)));
556 switch (obj
->request
->meth
= Z_LVAL_P(GET_PROP(method
)))
564 zval
*put_file
= GET_PROP(putFile
);
566 http_request_object_check_request_content_type(getThis());
568 if (Z_STRLEN_P(put_file
)) {
569 php_stream_statbuf ssb
;
570 php_stream
*stream
= php_stream_open_wrapper_ex(Z_STRVAL_P(put_file
), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
, HTTP_DEFAULT_STREAM_CONTEXT
);
572 if (stream
&& SUCCESS
== php_stream_stat(stream
, &ssb
)) {
573 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
578 zval
*put_data
= GET_PROP(putData
);
579 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
580 estrndup(Z_STRVAL_P(put_data
), Z_STRLEN_P(put_data
)), Z_STRLEN_P(put_data
), 1);
588 /* check for raw post data */
589 zval
*raw_data
= GET_PROP(rawPostData
);
591 if (Z_STRLEN_P(raw_data
)) {
592 http_request_object_check_request_content_type(getThis());
593 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
594 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
596 zval
*zfields
= GET_PROP(postFields
), *zfiles
= GET_PROP(postFiles
);
600 fields
= (Z_TYPE_P(zfields
) == IS_ARRAY
) ? Z_ARRVAL_P(zfields
) : NULL
;
601 files
= (Z_TYPE_P(zfiles
) == IS_ARRAY
) ? Z_ARRVAL_P(zfiles
) : NULL
;
603 if ((fields
&& zend_hash_num_elements(fields
)) || (files
&& zend_hash_num_elements(files
))) {
604 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
613 if (status
== SUCCESS
) {
614 zval
*qdata
= GET_PROP(queryData
);
615 zval
*options
= GET_PROP(options
);
617 if (Z_STRLEN_P(qdata
)) {
618 if (!strchr(obj
->request
->url
, '?')) {
619 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
621 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
623 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
626 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
628 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
629 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onprogress", sizeof("onprogress"))) {
632 if ( (Z_TYPE_P(options
) != IS_ARRAY
)
633 || (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(options
), "onprogress", sizeof("onprogress"), (void *) &entry
)
634 || (!zval_is_true(*entry
)))) {
637 ZVAL_ADDREF(getThis());
638 add_next_index_zval(pcb
, getThis());
639 add_next_index_stringl(pcb
, "onprogress", lenof("onprogress"), 1);
640 http_request_set_progress_callback(obj
->request
, pcb
);
649 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
655 /* always fetch info */
658 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
659 SET_PROP(responseInfo
, info
);
660 zval_ptr_dtor(&info
);
662 /* parse response message */
663 phpstr_fix(&obj
->request
->conv
.request
);
664 phpstr_fix(&obj
->request
->conv
.response
);
666 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
)))) {
669 if (zval_is_true(GET_PROP(recordHistory
))) {
670 zval
*hist
, *history
= GET_PROP(history
);
671 http_message
*response
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
));
672 http_message
*request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
675 ZVAL_OBJVAL(hist
, http_message_object_new_ex(http_message_object_ce
, http_message_interconnect(response
, request
), NULL
), 0);
676 if (Z_TYPE_P(history
) == IS_OBJECT
) {
677 http_message_object_prepend(hist
, history
);
679 SET_PROP(history
, hist
);
680 zval_ptr_dtor(&hist
);
683 UPD_PROP(long, responseCode
, msg
->http
.info
.response
.code
);
684 UPD_PROP(string
, responseStatus
, STR_PTR(msg
->http
.info
.response
.status
));
686 MAKE_STD_ZVAL(message
);
687 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
688 SET_PROP(responseMessage
, message
);
689 zval_ptr_dtor(&message
);
693 /* update properties with empty values*/
696 MAKE_STD_ZVAL(znull
);
698 SET_PROP(responseMessage
, znull
);
699 zval_ptr_dtor(&znull
);
701 UPD_PROP(long, responseCode
, 0);
702 UPD_PROP(string
, responseStatus
, "");
704 /* append request message to history */
705 if (zval_is_true(GET_PROP(recordHistory
))) {
706 http_message
*request
;
708 if ((request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
709 zval
*hist
, *history
= GET_PROP(history
);
712 ZVAL_OBJVAL(hist
, http_message_object_new_ex(http_message_object_ce
, request
, NULL
), 0);
713 if (Z_TYPE_P(history
) == IS_OBJECT
) {
714 http_message_object_prepend(hist
, history
);
716 SET_PROP(history
, hist
);
717 zval_ptr_dtor(&hist
);
724 http_request_set_progress_callback(obj
->request
, NULL
);
726 if (!EG(exception
) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onfinish", sizeof("onfinish"))) {
729 MAKE_STD_ZVAL(param
);
730 ZVAL_BOOL(param
, ret
== SUCCESS
);
731 with_error_handling(EH_NORMAL
, NULL
) {
732 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "onfinish", NULL
, param
);
733 } end_error_handling();
734 zval_ptr_dtor(¶m
);
740 static int apply_pretty_key(void *pDest
, int num_args
, va_list args
, zend_hash_key
*hash_key
)
742 if (hash_key
->nKeyLength
> 1) {
743 hash_key
->h
= zend_get_hash_value(pretty_key(hash_key
->arKey
, hash_key
->nKeyLength
- 1, 1, 0), hash_key
->nKeyLength
);
745 return ZEND_HASH_APPLY_KEEP
;
748 #define http_request_object_set_options_subr(key, ow, pk) \
749 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
750 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
, int prettify_keys
)
752 zval
*old_opts
, *new_opts
, *opts
= NULL
, **entry
= NULL
;
754 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
758 MAKE_STD_ZVAL(new_opts
);
759 array_init(new_opts
);
760 old_opts
= GET_PROP(options
);
761 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
762 array_copy(Z_ARRVAL_P(old_opts
), Z_ARRVAL_P(new_opts
));
765 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), key
, len
, (void *) &entry
)) {
767 zend_hash_clean(Z_ARRVAL_PP(entry
));
769 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
771 array_copy(Z_ARRVAL_P(opts
), Z_ARRVAL_PP(entry
));
773 array_join(Z_ARRVAL_P(opts
), Z_ARRVAL_PP(entry
), 0, prettify_keys
? ARRAY_JOIN_PRETTIFY
: 0);
778 zend_hash_apply_with_arguments(Z_ARRVAL_P(opts
), apply_pretty_key
, 0);
781 add_assoc_zval_ex(new_opts
, key
, len
, opts
);
783 SET_PROP(options
, new_opts
);
784 zval_ptr_dtor(&new_opts
);
789 #define http_request_object_get_options_subr(key) \
790 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
791 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
795 if (return_value_used
) {
796 zval
*opts
, **options
;
798 opts
= GET_PROP(options
);
799 array_init(return_value
);
801 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
802 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void *) &options
))) {
803 convert_to_array(*options
);
804 array_copy(Z_ARRVAL_PP(options
), Z_ARRVAL_P(return_value
));
810 /* ### USERLAND ### */
812 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
813 Create a new HttpRequest object instance. */
814 PHP_METHOD(HttpRequest
, __construct
)
819 zval
*options
= NULL
;
822 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla!", &URL
, &URL_len
, &meth
, &options
)) {
824 UPD_STRL(url
, URL
, URL_len
);
827 UPD_PROP(long, method
, meth
);
830 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
837 /* {{{ proto HttpRequest HttpRequest::factory([string url[, int request_method HTTP_METH_GET[, array options[, string class_name = "HttpRequest"]]]])
838 Create a new HttpRequest object instance. */
839 PHP_METHOD(HttpRequest
, factory
)
841 char *cn
= NULL
, *URL
= NULL
;
842 int cl
= 0, URL_len
= 0;
844 zval
*options
= NULL
;
845 zend_object_value ov
;
848 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla!s", &URL
, &URL_len
, &meth
, &options
, &cn
, &cl
) &&
849 SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_request_object_new_ex
, http_request_object_ce
, NULL
, NULL
)) {
850 RETVAL_OBJVAL(ov
, 0);
851 getThis() = return_value
;
853 UPD_STRL(url
, URL
, URL_len
);
856 UPD_PROP(long, method
, meth
);
859 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
866 /* {{{ proto bool HttpRequest::setOptions([array options])
867 Set the request options to use. See http_get() for a full list of available options. */
868 PHP_METHOD(HttpRequest
, setOptions
)
870 HashKey key
= initHashKey(0);
872 zval
*opts
= NULL
, *old_opts
, *new_opts
, *add_opts
, **opt
;
874 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
878 MAKE_STD_ZVAL(new_opts
);
879 array_init(new_opts
);
881 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
882 SET_PROP(options
, new_opts
);
883 zval_ptr_dtor(&new_opts
);
887 MAKE_STD_ZVAL(add_opts
);
888 array_init(add_opts
);
889 /* some options need extra attention -- thus cannot use array_merge() directly */
890 FOREACH_KEYVAL(pos
, opts
, key
, opt
) {
891 if (key
.type
== HASH_KEY_IS_STRING
) {
892 if (!strcmp(key
.str
, "headers")) {
893 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, *opt
);
894 } else if (!strcmp(key
.str
, "cookies")) {
895 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addcookies", NULL
, *opt
);
896 } else if (!strcmp(key
.str
, "ssl")) {
897 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addssloptions", NULL
, *opt
);
898 } else if ((!strcasecmp(key
.str
, "url")) || (!strcasecmp(key
.str
, "uri"))) {
899 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "seturl", NULL
, *opt
);
900 } else if (!strcmp(key
.str
, "method")) {
901 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setmethod", NULL
, *opt
);
902 #if HTTP_CURL_VERSION(7,14,1)
903 } else if (!strcmp(key
.str
, "resetcookies")) {
904 getObject(http_request_object
, obj
);
905 http_request_reset_cookies(obj
->request
, 0);
907 } else if (!strcmp(key
.str
, "enablecookies")) {
908 getObject(http_request_object
, obj
);
909 http_request_enable_cookies(obj
->request
);
910 } else if (!strcasecmp(key
.str
, "recordHistory")) {
911 UPD_PROP(bool, recordHistory
, 1);
914 add_assoc_zval_ex(add_opts
, key
.str
, key
.len
, *opt
);
919 old_opts
= GET_PROP(options
);
920 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
921 array_copy(Z_ARRVAL_P(old_opts
), Z_ARRVAL_P(new_opts
));
923 array_join(Z_ARRVAL_P(add_opts
), Z_ARRVAL_P(new_opts
), 0, 0);
924 SET_PROP(options
, new_opts
);
925 zval_ptr_dtor(&new_opts
);
926 zval_ptr_dtor(&add_opts
);
932 /* {{{ proto array HttpRequest::getOptions()
933 Get currently set options. */
934 PHP_METHOD(HttpRequest
, getOptions
)
938 if (return_value_used
) {
939 RETURN_PROP(options
);
944 /* {{{ proto bool HttpRequest::setSslOptions([array options])
946 PHP_METHOD(HttpRequest
, setSslOptions
)
948 http_request_object_set_options_subr("ssl", 1, 0);
952 /* {{{ proto bool HttpRequest::addSslOptions(array options)
953 Set additional SSL options. */
954 PHP_METHOD(HttpRequest
, addSslOptions
)
956 http_request_object_set_options_subr("ssl", 0, 0);
960 /* {{{ proto array HttpRequest::getSslOtpions()
961 Get previously set SSL options. */
962 PHP_METHOD(HttpRequest
, getSslOptions
)
964 http_request_object_get_options_subr("ssl");
968 /* {{{ proto bool HttpRequest::addHeaders(array headers)
969 Add request header name/value pairs. */
970 PHP_METHOD(HttpRequest
, addHeaders
)
972 http_request_object_set_options_subr("headers", 0, 1);
975 /* {{{ proto bool HttpRequest::setHeaders([array headers])
976 Set request header name/value pairs. */
977 PHP_METHOD(HttpRequest
, setHeaders
)
979 http_request_object_set_options_subr("headers", 1, 1);
983 /* {{{ proto array HttpRequest::getHeaders()
984 Get previously set request headers. */
985 PHP_METHOD(HttpRequest
, getHeaders
)
987 http_request_object_get_options_subr("headers");
991 /* {{{ proto bool HttpRequest::setCookies([array cookies])
993 PHP_METHOD(HttpRequest
, setCookies
)
995 http_request_object_set_options_subr("cookies", 1, 0);
999 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1001 PHP_METHOD(HttpRequest
, addCookies
)
1003 http_request_object_set_options_subr("cookies", 0, 0);
1007 /* {{{ proto array HttpRequest::getCookies()
1008 Get previously set cookies. */
1009 PHP_METHOD(HttpRequest
, getCookies
)
1011 http_request_object_get_options_subr("cookies");
1015 /* {{{ proto bool HttpRequest::enableCookies()
1016 Enable automatic sending of received cookies. Note that customly set cookies will be sent anyway. */
1017 PHP_METHOD(HttpRequest
, enableCookies
)
1020 getObject(http_request_object
, obj
);
1021 RETURN_SUCCESS(http_request_enable_cookies(obj
->request
));
1027 /* {{{ proto bool HttpRequest::resetCookies([bool session_only = FALSE])
1028 Reset all automatically received/sent cookies. Note that customly set cookies are not affected. */
1029 PHP_METHOD(HttpRequest
, resetCookies
)
1031 zend_bool session_only
= 0;
1032 getObject(http_request_object
, obj
);
1034 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &session_only
)) {
1037 RETURN_SUCCESS(http_request_reset_cookies(obj
->request
, session_only
));
1041 /* {{{ proto bool HttpRequest::setUrl(string url)
1042 Set the request URL. */
1043 PHP_METHOD(HttpRequest
, setUrl
)
1048 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1052 UPD_STRL(url
, URL
, URL_len
);
1057 /* {{{ proto string HttpRequest::getUrl()
1058 Get the previously set request URL. */
1059 PHP_METHOD(HttpRequest
, getUrl
)
1063 if (return_value_used
) {
1069 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1070 Set the request method. */
1071 PHP_METHOD(HttpRequest
, setMethod
)
1075 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1079 UPD_PROP(long, method
, meth
);
1084 /* {{{ proto int HttpRequest::getMethod()
1085 Get the previously set request method. */
1086 PHP_METHOD(HttpRequest
, getMethod
)
1090 if (return_value_used
) {
1091 RETURN_PROP(method
);
1096 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1097 Set the content type the post request should have. */
1098 PHP_METHOD(HttpRequest
, setContentType
)
1103 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1108 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1110 UPD_STRL(contentType
, ctype
, ct_len
);
1115 /* {{{ proto string HttpRequest::getContentType()
1116 Get the previously content type. */
1117 PHP_METHOD(HttpRequest
, getContentType
)
1121 if (return_value_used
) {
1122 RETURN_PROP(contentType
);
1127 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1128 Set the URL query parameters to use, overwriting previously set query parameters. */
1129 PHP_METHOD(HttpRequest
, setQueryData
)
1133 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1137 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1138 UPD_STRL(queryData
, "", 0);
1139 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1140 char *query_data
= NULL
;
1142 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1146 UPD_PROP(string
, queryData
, query_data
);
1151 convert_to_string_ex(&qdata
);
1152 UPD_STRL(queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1153 if (orig
!= qdata
) {
1154 zval_ptr_dtor(&qdata
);
1161 /* {{{ proto string HttpRequest::getQueryData()
1162 Get the current query data in form of an urlencoded query string. */
1163 PHP_METHOD(HttpRequest
, getQueryData
)
1167 if (return_value_used
) {
1168 RETURN_PROP(queryData
);
1173 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1174 Add parameters to the query parameter list, leaving previously set unchanged. */
1175 PHP_METHOD(HttpRequest
, addQueryData
)
1177 zval
*qdata
, *old_qdata
;
1178 char *query_data
= NULL
;
1179 size_t query_data_len
= 0;
1181 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &qdata
)) {
1185 old_qdata
= GET_PROP(queryData
);
1187 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
)) {
1191 UPD_STRL(queryData
, query_data
, query_data_len
);
1198 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1199 Adds POST data entries, leaving previously set unchanged, unless a post entry with the same name already exists. */
1200 PHP_METHOD(HttpRequest
, addPostFields
)
1202 zval
*post_data
, *old_post
, *new_post
;
1204 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &post_data
)) {
1208 if (zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1209 MAKE_STD_ZVAL(new_post
);
1210 array_init(new_post
);
1211 old_post
= GET_PROP(postFields
);
1212 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1213 array_copy(Z_ARRVAL_P(old_post
), Z_ARRVAL_P(new_post
));
1215 array_join(Z_ARRVAL_P(post_data
), Z_ARRVAL_P(new_post
), 0, 0);
1216 SET_PROP(postFields
, new_post
);
1217 zval_ptr_dtor(&new_post
);
1224 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1225 Set the POST data entries, overwriting previously set POST data. */
1226 PHP_METHOD(HttpRequest
, setPostFields
)
1228 zval
*post
, *post_data
= NULL
;
1230 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &post_data
)) {
1234 MAKE_STD_ZVAL(post
);
1236 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1237 array_copy(Z_ARRVAL_P(post_data
), Z_ARRVAL_P(post
));
1239 SET_PROP(postFields
, post
);
1240 zval_ptr_dtor(&post
);
1246 /* {{{ proto array HttpRequest::getPostFields()
1247 Get previously set POST data. */
1248 PHP_METHOD(HttpRequest
, getPostFields
)
1252 if (return_value_used
) {
1253 RETURN_PROP(postFields
);
1258 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1259 Set raw post data to send, overwriting previously set raw post data. Don't forget to specify a content type. */
1260 PHP_METHOD(HttpRequest
, setRawPostData
)
1262 char *raw_data
= NULL
;
1265 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1273 UPD_STRL(rawPostData
, raw_data
, data_len
);
1278 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1279 Add raw post data, leaving previously set raw post data unchanged. */
1280 PHP_METHOD(HttpRequest
, addRawPostData
)
1285 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1290 zval
*data
= GET_PROP(rawPostData
);
1292 if (Z_STRLEN_P(data
)) {
1293 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1294 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1295 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, raw_data
, data_len
);
1297 UPD_STRL(putData
, raw_data
, data_len
);
1305 /* {{{ proto string HttpRequest::getRawPostData()
1306 Get previously set raw post data. */
1307 PHP_METHOD(HttpRequest
, getRawPostData
)
1311 if (return_value_used
) {
1312 RETURN_PROP(rawPostData
);
1317 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1318 Add a file to the POST request, leaving previously set files unchanged. */
1319 PHP_METHOD(HttpRequest
, addPostFile
)
1321 zval
*entry
, *old_post
, *new_post
;
1322 char *name
, *file
, *type
= NULL
;
1323 int name_len
, file_len
, type_len
= 0;
1325 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1330 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1332 type
= "application/x-octetstream";
1333 type_len
= sizeof("application/x-octetstream") - 1;
1336 MAKE_STD_ZVAL(entry
);
1339 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1340 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1341 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1343 MAKE_STD_ZVAL(new_post
);
1344 array_init(new_post
);
1345 old_post
= GET_PROP(postFiles
);
1346 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1347 array_copy(Z_ARRVAL_P(old_post
), Z_ARRVAL_P(new_post
));
1349 add_next_index_zval(new_post
, entry
);
1350 SET_PROP(postFiles
, new_post
);
1351 zval_ptr_dtor(&new_post
);
1357 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1358 Set files to post, overwriting previously set post files. */
1359 PHP_METHOD(HttpRequest
, setPostFiles
)
1361 zval
*files
= NULL
, *post
;
1363 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!/", &files
)) {
1367 MAKE_STD_ZVAL(post
);
1369 if (files
&& (Z_TYPE_P(files
) == IS_ARRAY
)) {
1370 array_copy(Z_ARRVAL_P(files
), Z_ARRVAL_P(post
));
1372 SET_PROP(postFiles
, post
);
1373 zval_ptr_dtor(&post
);
1379 /* {{{ proto array HttpRequest::getPostFiles()
1380 Get all previously added POST files. */
1381 PHP_METHOD(HttpRequest
, getPostFiles
)
1385 if (return_value_used
) {
1386 RETURN_PROP(postFiles
);
1391 /* {{{ proto bool HttpRequest::setPutFile([string file])
1392 Set file to put. Affects only PUT requests. */
1393 PHP_METHOD(HttpRequest
, setPutFile
)
1398 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1402 UPD_STRL(putFile
, file
, file_len
);
1407 /* {{{ proto string HttpRequest::getPutFile()
1408 Get previously set put file. */
1409 PHP_METHOD(HttpRequest
, getPutFile
)
1413 if (return_value_used
) {
1414 RETURN_PROP(putFile
);
1419 /* {{{ proto bool HttpRequest::setPutData([string put_data])
1420 Set PUT data to send, overwriting previously set PUT data. */
1421 PHP_METHOD(HttpRequest
, setPutData
)
1423 char *put_data
= NULL
;
1426 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &put_data
, &data_len
)) {
1434 UPD_STRL(putData
, put_data
, data_len
);
1439 /* {{{ proto bool HttpRequest::addPutData(string put_data)
1440 Add PUT data, leaving previously set PUT data unchanged. */
1441 PHP_METHOD(HttpRequest
, addPutData
)
1446 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &put_data
, &data_len
)) {
1451 zval
*data
= GET_PROP(putData
);
1453 if (Z_STRLEN_P(data
)) {
1454 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1455 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1456 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, put_data
, data_len
);
1458 UPD_STRL(putData
, put_data
, data_len
);
1466 /* {{{ proto string HttpRequest::getPutData()
1467 Get previously set PUT data. */
1468 PHP_METHOD(HttpRequest
, getPutData
)
1472 if (return_value_used
) {
1473 RETURN_PROP(putData
);
1478 /* {{{ proto array HttpRequest::getResponseData()
1479 Get all response data after the request has been sent. */
1480 PHP_METHOD(HttpRequest
, getResponseData
)
1484 if (return_value_used
) {
1487 zval
*headers
, *message
= GET_PROP(responseMessage
);
1489 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1490 getObjectEx(http_message_object
, msg
, message
);
1492 array_init(return_value
);
1494 MAKE_STD_ZVAL(headers
);
1495 array_init(headers
);
1496 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1497 add_assoc_zval(return_value
, "headers", headers
);
1499 phpstr_data(PHPSTR(msg
->message
), &body
, &body_len
);
1500 add_assoc_stringl(return_value
, "body", body
, body_len
, 0);
1506 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1507 Get response header(s) after the request has been sent. */
1508 PHP_METHOD(HttpRequest
, getResponseHeader
)
1510 if (return_value_used
) {
1512 char *header_name
= NULL
;
1515 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1516 zval
*message
= GET_PROP(responseMessage
);
1518 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1519 getObjectEx(http_message_object
, msg
, message
);
1522 if ((header
= http_message_header_ex(msg
->message
, pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, 0))) {
1523 RETURN_ZVAL(header
, 1, 1);
1526 array_init(return_value
);
1527 zend_hash_copy(Z_ARRVAL_P(return_value
), &msg
->message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1537 /* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]])
1538 Get response cookie(s) after the request has been sent. */
1539 PHP_METHOD(HttpRequest
, getResponseCookies
)
1541 if (return_value_used
) {
1543 zval
*allowed_extras_array
= NULL
;
1545 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|la!", &flags
, &allowed_extras_array
)) {
1547 HashKey key
= initHashKey(0);
1548 char **allowed_extras
= NULL
;
1549 zval
**header
= NULL
, **entry
= NULL
, *message
= GET_PROP(responseMessage
);
1550 HashPosition pos
, pos1
, pos2
;
1552 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1553 getObjectEx(http_message_object
, msg
, message
);
1555 array_init(return_value
);
1557 if (allowed_extras_array
) {
1558 allowed_extras
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array
)) + 1, sizeof(char *));
1559 FOREACH_VAL(pos
, allowed_extras_array
, entry
) {
1560 ZVAL_ADDREF(*entry
);
1561 convert_to_string_ex(entry
);
1562 allowed_extras
[i
++] = estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
1563 zval_ptr_dtor(entry
);
1567 FOREACH_HASH_KEYVAL(pos1
, &msg
->message
->hdrs
, key
, header
) {
1568 if (key
.type
== HASH_KEY_IS_STRING
&& !strcasecmp(key
.str
, "Set-Cookie")) {
1569 http_cookie_list list
;
1571 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1572 zval
**single_header
;
1574 FOREACH_VAL(pos2
, *header
, single_header
) {
1575 ZVAL_ADDREF(*single_header
);
1576 convert_to_string_ex(single_header
);
1577 if (http_parse_cookie_ex(&list
, Z_STRVAL_PP(single_header
), flags
, allowed_extras
)) {
1580 MAKE_STD_ZVAL(cookie
);
1581 object_init(cookie
);
1582 http_cookie_list_tostruct(&list
, cookie
);
1583 add_next_index_zval(return_value
, cookie
);
1584 http_cookie_list_dtor(&list
);
1586 zval_ptr_dtor(single_header
);
1589 ZVAL_ADDREF(*header
);
1590 convert_to_string_ex(header
);
1591 if (http_parse_cookie_ex(&list
, Z_STRVAL_PP(header
), flags
, allowed_extras
)) {
1594 MAKE_STD_ZVAL(cookie
);
1595 object_init(cookie
);
1596 http_cookie_list_tostruct(&list
, cookie
);
1597 add_next_index_zval(return_value
, cookie
);
1598 http_cookie_list_dtor(&list
);
1600 zval_ptr_dtor(header
);
1605 if (allowed_extras
) {
1606 for (i
= 0; allowed_extras
[i
]; ++i
) {
1607 efree(allowed_extras
[i
]);
1609 efree(allowed_extras
);
1620 /* {{{ proto string HttpRequest::getResponseBody()
1621 Get the response body after the request has been sent. */
1622 PHP_METHOD(HttpRequest
, getResponseBody
)
1626 if (return_value_used
) {
1627 zval
*message
= GET_PROP(responseMessage
);
1629 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1630 getObjectEx(http_message_object
, msg
, message
);
1631 RETURN_PHPSTR_DUP(&msg
->message
->body
);
1639 /* {{{ proto int HttpRequest::getResponseCode()
1640 Get the response code after the request has been sent. */
1641 PHP_METHOD(HttpRequest
, getResponseCode
)
1645 if (return_value_used
) {
1646 RETURN_PROP(responseCode
);
1651 /* {{{ proto string HttpRequest::getResponseStatus()
1652 Get the response status (i.e. the string after the response code) after the message has been sent. */
1653 PHP_METHOD(HttpRequest
, getResponseStatus
)
1657 if (return_value_used
) {
1658 RETURN_PROP(responseStatus
);
1663 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1664 Get response info after the request has been sent. */
1665 PHP_METHOD(HttpRequest
, getResponseInfo
)
1667 if (return_value_used
) {
1668 zval
*info
, **infop
;
1669 char *info_name
= NULL
;
1672 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1676 info
= GET_PROP(responseInfo
);
1678 if (Z_TYPE_P(info
) != IS_ARRAY
) {
1682 if (info_len
&& info_name
) {
1683 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void *) &infop
)) {
1684 RETURN_ZVAL(*infop
, 1, 0);
1686 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1690 RETURN_ZVAL(info
, 1, 0);
1696 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1697 Get the full response as HttpMessage object after the request has been sent. */
1698 PHP_METHOD(HttpRequest
, getResponseMessage
)
1703 SET_EH_THROW_HTTP();
1704 message
= GET_PROP(responseMessage
);
1705 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1706 RETVAL_OBJECT(message
, 1);
1708 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "HttpRequest does not contain a response message");
1715 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1716 Get sent HTTP message. */
1717 PHP_METHOD(HttpRequest
, getRequestMessage
)
1721 if (return_value_used
) {
1723 getObject(http_request_object
, obj
);
1725 SET_EH_THROW_HTTP();
1726 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1727 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
1734 /* {{{ proto string HttpRequest::getRawRequestMessage()
1735 Get sent HTTP message. */
1736 PHP_METHOD(HttpRequest
, getRawRequestMessage
)
1740 if (return_value_used
) {
1741 getObject(http_request_object
, obj
);
1743 RETURN_PHPSTR_DUP(&obj
->request
->conv
.request
);
1748 /* {{{ proto string HttpRequest::getRawResponseMessage()
1749 Get the entire HTTP response. */
1750 PHP_METHOD(HttpRequest
, getRawResponseMessage
)
1754 if (return_value_used
) {
1755 getObject(http_request_object
, obj
);
1757 RETURN_PHPSTR_DUP(&obj
->request
->conv
.response
);
1762 /* {{{ proto HttpMessage HttpRequest::getHistory()
1763 Get all sent requests and received responses as an HttpMessage object. */
1764 PHP_METHOD(HttpRequest
, getHistory
)
1768 if (return_value_used
) {
1771 SET_EH_THROW_HTTP();
1772 hist
= GET_PROP(history
);
1773 if (Z_TYPE_P(hist
) == IS_OBJECT
) {
1774 RETVAL_OBJECT(hist
, 1);
1776 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "The history is empty");
1783 /* {{{ proto void HttpRequest::clearHistory()
1784 Clear the history. */
1785 PHP_METHOD(HttpRequest
, clearHistory
)
1790 MAKE_STD_ZVAL(hist
);
1792 SET_PROP(history
, hist
);
1793 zval_ptr_dtor(&hist
);
1798 /* {{{ proto HttpMessage HttpRequest::send()
1799 Send the HTTP request. */
1800 PHP_METHOD(HttpRequest
, send
)
1802 getObject(http_request_object
, obj
);
1806 SET_EH_THROW_HTTP();
1811 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1812 } else if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
1813 http_request_exec(obj
->request
);
1814 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
1815 RETVAL_OBJECT(GET_PROP(responseMessage
), 1);
1823 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1830 * vim600: noet sw=4 ts=4 fdm=marker
1831 * vim<600: noet sw=4 ts=4