2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_CURL
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
20 #include "zend_interfaces.h"
22 #include "php_http_api.h"
23 #include "php_http_cookie_api.h"
24 #include "php_http_exception_object.h"
25 #include "php_http_message_api.h"
26 #include "php_http_message_object.h"
27 #include "php_http_request_api.h"
28 #include "php_http_request_object.h"
29 #include "php_http_request_pool_api.h"
30 #include "php_http_url_api.h"
32 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, 0, req_args)
33 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpRequest, method, 0)
34 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
35 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
37 HTTP_BEGIN_ARGS(__construct
, 0)
39 HTTP_ARG_VAL(method
, 0)
40 HTTP_ARG_VAL(options
, 0)
43 HTTP_EMPTY_ARGS(getOptions
);
44 HTTP_BEGIN_ARGS(setOptions
, 0)
45 HTTP_ARG_VAL(options
, 0)
48 HTTP_EMPTY_ARGS(getSslOptions
);
49 HTTP_BEGIN_ARGS(setSslOptions
, 0)
50 HTTP_ARG_VAL(ssl_options
, 0)
53 HTTP_BEGIN_ARGS(addSslOptions
, 0)
54 HTTP_ARG_VAL(ssl_optins
, 0)
57 HTTP_EMPTY_ARGS(getHeaders
);
58 HTTP_BEGIN_ARGS(setHeaders
, 0)
59 HTTP_ARG_VAL(headers
, 0)
62 HTTP_BEGIN_ARGS(addHeaders
, 1)
63 HTTP_ARG_VAL(headers
, 0)
66 HTTP_EMPTY_ARGS(getCookies
);
67 HTTP_BEGIN_ARGS(setCookies
, 0)
68 HTTP_ARG_VAL(cookies
, 0)
71 HTTP_BEGIN_ARGS(addCookies
, 1)
72 HTTP_ARG_VAL(cookies
, 0)
75 HTTP_EMPTY_ARGS(getUrl
);
76 HTTP_BEGIN_ARGS(setUrl
, 1)
80 HTTP_EMPTY_ARGS(getMethod
);
81 HTTP_BEGIN_ARGS(setMethod
, 1)
82 HTTP_ARG_VAL(request_method
, 0)
85 HTTP_EMPTY_ARGS(getContentType
);
86 HTTP_BEGIN_ARGS(setContentType
, 1)
87 HTTP_ARG_VAL(content_type
, 0)
90 HTTP_EMPTY_ARGS(getQueryData
);
91 HTTP_BEGIN_ARGS(setQueryData
, 0)
92 HTTP_ARG_VAL(query_data
, 0)
95 HTTP_BEGIN_ARGS(addQueryData
, 1)
96 HTTP_ARG_VAL(query_data
, 0)
99 HTTP_EMPTY_ARGS(getPostFields
);
100 HTTP_BEGIN_ARGS(setPostFields
, 0)
101 HTTP_ARG_VAL(post_fields
, 0)
104 HTTP_BEGIN_ARGS(addPostFields
, 1)
105 HTTP_ARG_VAL(post_fields
, 0)
108 HTTP_EMPTY_ARGS(getPostFiles
);
109 HTTP_BEGIN_ARGS(setPostFiles
, 0)
110 HTTP_ARG_VAL(post_files
, 0)
113 HTTP_BEGIN_ARGS(addPostFile
, 2)
114 HTTP_ARG_VAL(formname
, 0)
115 HTTP_ARG_VAL(filename
, 0)
116 HTTP_ARG_VAL(content_type
, 0)
119 HTTP_EMPTY_ARGS(getRawPostData
);
120 HTTP_BEGIN_ARGS(setRawPostData
, 0)
121 HTTP_ARG_VAL(raw_post_data
, 0)
124 HTTP_BEGIN_ARGS(addRawPostData
, 1)
125 HTTP_ARG_VAL(raw_post_data
, 0)
128 HTTP_EMPTY_ARGS(getPutFile
);
129 HTTP_BEGIN_ARGS(setPutFile
, 0)
130 HTTP_ARG_VAL(filename
, 0)
133 HTTP_EMPTY_ARGS(getResponseData
);
134 HTTP_BEGIN_ARGS(getResponseHeader
, 0)
135 HTTP_ARG_VAL(name
, 0)
138 HTTP_BEGIN_ARGS(getResponseCookies
, 0)
139 HTTP_ARG_VAL(flags
, 0)
140 HTTP_ARG_VAL(allowed_extras
, 0)
143 HTTP_EMPTY_ARGS(getResponseBody
);
144 HTTP_EMPTY_ARGS(getResponseCode
);
145 HTTP_EMPTY_ARGS(getResponseStatus
);
146 HTTP_BEGIN_ARGS(getResponseInfo
, 0)
147 HTTP_ARG_VAL(name
, 0)
150 HTTP_EMPTY_ARGS(getResponseMessage
);
151 HTTP_EMPTY_ARGS(getRawResponseMessage
);
152 HTTP_EMPTY_ARGS(getRequestMessage
);
153 HTTP_EMPTY_ARGS(getRawRequestMessage
);
154 HTTP_EMPTY_ARGS(getHistory
);
155 HTTP_EMPTY_ARGS(clearHistory
);
156 HTTP_EMPTY_ARGS(send
);
158 HTTP_BEGIN_ARGS(get
, 1)
160 HTTP_ARG_VAL(options
, 0)
161 HTTP_ARG_VAL(info
, 1)
164 HTTP_BEGIN_ARGS(head
, 1)
166 HTTP_ARG_VAL(options
, 0)
167 HTTP_ARG_VAL(info
, 1)
170 HTTP_BEGIN_ARGS(postData
, 2)
172 HTTP_ARG_VAL(data
, 0)
173 HTTP_ARG_VAL(options
, 0)
174 HTTP_ARG_VAL(info
, 1)
177 HTTP_BEGIN_ARGS(postFields
, 2)
179 HTTP_ARG_VAL(data
, 0)
180 HTTP_ARG_VAL(options
, 0)
181 HTTP_ARG_VAL(info
, 1)
184 HTTP_BEGIN_ARGS(putFile
, 2)
186 HTTP_ARG_VAL(file
, 0)
187 HTTP_ARG_VAL(options
, 0)
188 HTTP_ARG_VAL(info
, 1)
191 HTTP_BEGIN_ARGS(putStream
, 2)
193 HTTP_ARG_VAL(stream
, 0)
194 HTTP_ARG_VAL(options
, 0)
195 HTTP_ARG_VAL(info
, 1)
198 HTTP_BEGIN_ARGS(methodRegister
, 1)
199 HTTP_ARG_VAL(method_name
, 0)
202 HTTP_BEGIN_ARGS(methodUnregister
, 1)
203 HTTP_ARG_VAL(method
, 0)
206 HTTP_BEGIN_ARGS(methodName
, 1)
207 HTTP_ARG_VAL(method_id
, 0)
210 HTTP_BEGIN_ARGS(methodExists
, 1)
211 HTTP_ARG_VAL(method
, 0)
214 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
215 static inline void _http_request_object_declare_default_properties(TSRMLS_D
);
217 #define OBJ_PROP_CE http_request_object_ce
218 zend_class_entry
*http_request_object_ce
;
219 zend_function_entry http_request_object_fe
[] = {
220 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
222 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
223 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
224 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
225 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
226 HTTP_REQUEST_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
228 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
229 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
230 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
232 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
233 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
234 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
236 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
237 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
239 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
240 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
242 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
243 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
245 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
246 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
247 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
249 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
250 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
251 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
253 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
254 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
255 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
257 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
258 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
261 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
262 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
264 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
266 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(getResponseCookies
, ZEND_ACC_PUBLIC
)
269 HTTP_REQUEST_ME(getResponseCode
, ZEND_ACC_PUBLIC
)
270 HTTP_REQUEST_ME(getResponseStatus
, ZEND_ACC_PUBLIC
)
271 HTTP_REQUEST_ME(getResponseBody
, ZEND_ACC_PUBLIC
)
272 HTTP_REQUEST_ME(getResponseInfo
, ZEND_ACC_PUBLIC
)
273 HTTP_REQUEST_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
274 HTTP_REQUEST_ME(getRawResponseMessage
, ZEND_ACC_PUBLIC
)
275 HTTP_REQUEST_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
276 HTTP_REQUEST_ME(getRawRequestMessage
, ZEND_ACC_PUBLIC
)
277 HTTP_REQUEST_ME(getHistory
, ZEND_ACC_PUBLIC
)
278 HTTP_REQUEST_ME(clearHistory
, ZEND_ACC_PUBLIC
)
280 HTTP_REQUEST_ALIAS(get
, http_get
)
281 HTTP_REQUEST_ALIAS(head
, http_head
)
282 HTTP_REQUEST_ALIAS(postData
, http_post_data
)
283 HTTP_REQUEST_ALIAS(postFields
, http_post_fields
)
284 HTTP_REQUEST_ALIAS(putFile
, http_put_file
)
285 HTTP_REQUEST_ALIAS(putStream
, http_put_stream
)
287 HTTP_REQUEST_ALIAS(methodRegister
, http_request_method_register
)
288 HTTP_REQUEST_ALIAS(methodUnregister
, http_request_method_unregister
)
289 HTTP_REQUEST_ALIAS(methodName
, http_request_method_name
)
290 HTTP_REQUEST_ALIAS(methodExists
, http_request_method_exists
)
294 static zend_object_handlers http_request_object_handlers
;
296 PHP_MINIT_FUNCTION(http_request_object
)
298 HTTP_REGISTER_CLASS_EX(HttpRequest
, http_request_object
, NULL
, 0);
299 http_request_object_handlers
.clone_obj
= _http_request_object_clone_obj
;
303 zend_object_value
_http_request_object_new(zend_class_entry
*ce TSRMLS_DC
)
305 return http_request_object_new_ex(ce
, NULL
, NULL
);
308 zend_object_value
_http_request_object_new_ex(zend_class_entry
*ce
, CURL
*ch
, http_request_object
**ptr TSRMLS_DC
)
310 zend_object_value ov
;
311 http_request_object
*o
;
313 o
= ecalloc(1, sizeof(http_request_object
));
315 o
->request
= http_request_init_ex(NULL
, ch
, 0, NULL
);
321 ALLOC_HASHTABLE(OBJ_PROP(o
));
322 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
323 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
325 ov
.handle
= putObject(http_request_object
, o
);
326 ov
.handlers
= &http_request_object_handlers
;
331 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
334 zend_object_value new_ov
;
335 http_request_object
*new_obj
;
336 getObject(http_request_object
, old_obj
);
338 old_zo
= zend_objects_get_address(this_ptr TSRMLS_CC
);
339 new_ov
= http_request_object_new_ex(old_zo
->ce
, NULL
, &new_obj
);
340 if (old_obj
->request
->ch
) {
341 http_curl_init_ex(curl_easy_duphandle(old_obj
->request
->ch
), new_obj
->request
);
344 zend_objects_clone_members(&new_obj
->zo
, new_ov
, old_zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
345 phpstr_append(&new_obj
->request
->conv
.request
, old_obj
->request
->conv
.request
.data
, old_obj
->request
->conv
.request
.used
);
346 phpstr_append(&new_obj
->request
->conv
.response
, old_obj
->request
->conv
.response
.data
, old_obj
->request
->conv
.response
.used
);
351 static inline void _http_request_object_declare_default_properties(TSRMLS_D
)
353 zend_class_entry
*ce
= http_request_object_ce
;
355 DCL_PROP_N(PRIVATE
, options
);
356 DCL_PROP_N(PRIVATE
, postFields
);
357 DCL_PROP_N(PRIVATE
, postFiles
);
358 DCL_PROP_N(PRIVATE
, responseInfo
);
359 DCL_PROP_N(PRIVATE
, responseData
);
360 DCL_PROP_N(PRIVATE
, responseMessage
);
361 DCL_PROP(PRIVATE
, long, responseCode
, 0);
362 DCL_PROP(PRIVATE
, string
, responseStatus
, "");
363 DCL_PROP(PRIVATE
, long, method
, HTTP_GET
);
364 DCL_PROP(PRIVATE
, string
, url
, "");
365 DCL_PROP(PRIVATE
, string
, contentType
, "");
366 DCL_PROP(PRIVATE
, string
, rawPostData
, "");
367 DCL_PROP(PRIVATE
, string
, queryData
, "");
368 DCL_PROP(PRIVATE
, string
, putFile
, "");
369 DCL_PROP_N(PRIVATE
, history
);
370 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
374 * Request Method Constants
377 DCL_CONST(long, "METH_GET", HTTP_GET
);
378 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
379 DCL_CONST(long, "METH_POST", HTTP_POST
);
380 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
381 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
382 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
383 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
384 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
385 /* WebDAV - RFC 2518 */
386 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
387 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
388 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
389 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
390 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
391 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
392 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
393 /* WebDAV Versioning - RFC 3253 */
394 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
395 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
396 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
397 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
398 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
399 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
400 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
401 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
402 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
403 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
404 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
405 /* WebDAV Access Control - RFC 3744 */
406 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
408 /* cURL HTTP protocol versions */
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
);
416 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
417 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
418 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
419 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
423 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
425 http_request_object
*o
= (http_request_object
*) object
;
428 zend_hash_destroy(OBJ_PROP(o
));
429 FREE_HASHTABLE(OBJ_PROP(o
));
431 http_request_free(&o
->request
);
435 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
437 STATUS status
= SUCCESS
;
439 http_request_reset(obj
->request
);
440 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, http_curl_init(obj
->request
), return FAILURE
);
442 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(GET_PROP(url
)));
444 switch (obj
->request
->meth
= Z_LVAL_P(GET_PROP(method
)))
452 php_stream_statbuf ssb
;
453 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
455 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
456 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
466 /* check for raw post data */
467 zval
*raw_data
= GET_PROP(rawPostData
);
469 if (Z_STRLEN_P(raw_data
)) {
470 zval
*ctype
= GET_PROP(contentType
);
472 if (Z_STRLEN_P(ctype
)) {
473 zval
**headers
, *opts
= GET_PROP(options
);
475 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
476 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) &&
477 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
480 /* only override if not already set */
481 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) && (Z_TYPE_PP(ct_header
) == IS_STRING
)) {
482 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
487 MAKE_STD_ZVAL(headers
);
489 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
490 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, headers
);
491 zval_ptr_dtor(&headers
);
495 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
496 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
499 zval
*zfields
= GET_PROP(postFields
), *zfiles
= GET_PROP(postFiles
);
503 fields
= (Z_TYPE_P(zfields
) == IS_ARRAY
) ? Z_ARRVAL_P(zfields
) : NULL
;
504 files
= (Z_TYPE_P(zfiles
) == IS_ARRAY
) ? Z_ARRVAL_P(zfiles
) : NULL
;
506 if ((fields
&& zend_hash_num_elements(fields
)) || (files
&& zend_hash_num_elements(files
))) {
507 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
516 if (status
== SUCCESS
) {
517 zval
*qdata
= GET_PROP(queryData
);
518 zval
*options
= GET_PROP(options
);
520 if (Z_STRLEN_P(qdata
)) {
521 if (!strchr(obj
->request
->url
, '?')) {
522 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
524 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
526 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
529 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
531 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
532 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onprogress", sizeof("onprogress"))) {
535 if ( (Z_TYPE_P(options
) != IS_ARRAY
)
536 || (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(options
), "onprogress", sizeof("onprogress"), (void **) &entry
)
537 || (!zval_is_true(*entry
)))) {
540 ZVAL_ADDREF(getThis());
541 add_next_index_zval(pcb
, getThis());
542 add_next_index_stringl(pcb
, "onprogress", lenof("onprogress"), 1);
543 http_request_set_progress_callback(obj
->request
, pcb
);
552 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
558 /* always fetch info */
561 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
562 SET_PROP(responseInfo
, info
);
563 zval_ptr_dtor(&info
);
565 /* parse response message */
566 phpstr_fix(&obj
->request
->conv
.request
);
567 phpstr_fix(&obj
->request
->conv
.response
);
569 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
)))) {
572 zval
*headers
, *message
, *resp
;
574 if (zval_is_true(GET_PROP(recordHistory
))) {
575 /* we need to act like a zipper, as we'll receive
576 * the requests and the responses in separate chains
579 http_message
*response
= http_message_dup(msg
);
580 http_message
*request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
582 if (request
&& response
) {
583 int num_req
, num_resp
;
585 http_message_count(num_req
, request
);
586 http_message_count(num_resp
, response
);
589 stuck request messages in between response messages
603 if there are more responses than requests (PUT etc),
604 begin add $diff response's parent message
606 if (num_req
<= num_resp
) {
608 zval
*prep
, *hist
, *history
= GET_PROP(history
);
609 http_message
*res_tmp
= response
, *req_tmp
= request
, *req_par
, *res_par
;
611 for (i
= 0; i
< (num_resp
- num_req
); ++i
) {
612 res_tmp
= res_tmp
->parent
;
614 for (i
= 0; i
< num_req
; ++i
) {
615 res_par
= res_tmp
->parent
;
616 req_par
= req_tmp
->parent
;
617 res_tmp
->parent
= req_tmp
;
618 req_tmp
->parent
= res_par
;
624 ZVAL_OBJVAL(hist
, http_message_object_new_ex(http_message_object_ce
, response
, NULL
), 0);
626 http_message_object_reverse(hist
, prep
);
627 if (Z_TYPE_P(history
) == IS_OBJECT
) {
628 http_message_object_prepend(prep
, history
);
630 SET_PROP(history
, prep
);
631 zval_ptr_dtor(&prep
);
632 zval_ptr_dtor(&hist
);
636 http_message_free(&response
);
637 http_message_free(&request
);
641 UPD_PROP(long, responseCode
, msg
->http
.info
.response
.code
);
642 UPD_PROP(string
, responseStatus
, msg
->http
.info
.response
.status
);
646 MAKE_STD_ZVAL(headers
);
648 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
649 add_assoc_zval(resp
, "headers", headers
);
650 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
651 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
652 SET_PROP(responseData
, resp
);
653 zval_ptr_dtor(&resp
);
655 MAKE_STD_ZVAL(message
);
656 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
657 SET_PROP(responseMessage
, message
);
658 zval_ptr_dtor(&message
);
662 /* update properties with empty values*/
663 zval
*resp
= GET_PROP(responseData
), *znull
;
665 MAKE_STD_ZVAL(znull
);
667 SET_PROP(responseMessage
, znull
);
668 zval_ptr_dtor(&znull
);
670 if (Z_TYPE_P(resp
) == IS_ARRAY
) {
671 zend_hash_clean(Z_ARRVAL_P(resp
));
674 UPD_PROP(long, responseCode
, 0);
675 UPD_PROP(string
, responseStatus
, "");
677 /* append request message to history */
678 if (zval_is_true(GET_PROP(recordHistory
))) {
679 http_message
*request
;
681 if ((request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
682 zval
*hist
, *history
= GET_PROP(history
);
685 ZVAL_OBJVAL(hist
, http_message_object_new_ex(http_message_object_ce
, request
, NULL
), 0);
686 if (Z_TYPE_P(history
) == IS_OBJECT
) {
687 http_message_object_prepend(hist
, history
);
689 SET_PROP(history
, hist
);
690 zval_ptr_dtor(&hist
);
697 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onfinish", sizeof("onfinish"))) {
700 MAKE_STD_ZVAL(param
);
701 ZVAL_BOOL(param
, ret
== SUCCESS
);
702 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "onfinish", NULL
, param
);
703 zval_ptr_dtor(¶m
);
709 #define http_request_object_set_options_subr(key, ow) \
710 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
711 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
713 zval
*old_opts
, *new_opts
, *opts
= NULL
, **entry
;
715 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
719 MAKE_STD_ZVAL(new_opts
);
720 array_init(new_opts
);
721 old_opts
= GET_PROP(options
);
722 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
723 array_copy(old_opts
, new_opts
);
726 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), key
, len
, (void **) &entry
)) {
728 zend_hash_clean(Z_ARRVAL_PP(entry
));
730 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
732 array_copy(opts
, *entry
);
734 array_merge(opts
, *entry
);
739 add_assoc_zval(new_opts
, key
, opts
);
741 SET_PROP(options
, new_opts
);
742 zval_ptr_dtor(&new_opts
);
747 #define http_request_object_get_options_subr(key) \
748 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
749 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
754 zval
*opts
, **options
;
756 opts
= GET_PROP(options
);
757 array_init(return_value
);
759 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
760 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
))) {
761 convert_to_array(*options
);
762 array_copy(*options
, return_value
);
768 /* ### USERLAND ### */
770 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
772 * Instantiate a new HttpRequest object.
774 * Accepts a string as optional parameter containing the target request url.
775 * Additionally accepts an optional int parameter specifying the request method
776 * to use and an associative array as optional third parameter which will be
777 * passed to HttpRequest::setOptions().
779 * Throws HttpException.
781 PHP_METHOD(HttpRequest
, __construct
)
786 zval
*options
= NULL
;
789 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
791 UPD_STRL(url
, URL
, URL_len
);
794 UPD_PROP(long, method
, meth
);
797 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
804 /* {{{ proto bool HttpRequest::setOptions([array options])
806 * Set the request options to use. See http_get() for a full list of available options.
808 * Accepts an array as optional parameters, which values will overwrite the
809 * currently set request options. If the parameter is empty or omitted,
810 * the options of the HttpRequest object will be reset.
812 * Returns TRUE on success, or FALSE on failure.
814 PHP_METHOD(HttpRequest
, setOptions
)
819 zval
*opts
= NULL
, *old_opts
, *new_opts
, *add_opts
, **opt
;
821 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
825 MAKE_STD_ZVAL(new_opts
);
826 array_init(new_opts
);
828 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
829 SET_PROP(options
, new_opts
);
830 zval_ptr_dtor(&new_opts
);
834 MAKE_STD_ZVAL(add_opts
);
835 array_init(add_opts
);
836 /* some options need extra attention -- thus cannot use array_merge() directly */
837 FOREACH_KEYVAL(pos
, opts
, key
, idx
, opt
) {
839 if (!strcmp(key
, "headers")) {
840 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, *opt
);
841 } else if (!strcmp(key
, "cookies")) {
842 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addcookies", NULL
, *opt
);
843 } else if (!strcmp(key
, "ssl")) {
844 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addssloptions", NULL
, *opt
);
845 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
846 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "seturl", NULL
, *opt
);
847 } else if (!strcmp(key
, "method")) {
848 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setmethod", NULL
, *opt
);
851 add_assoc_zval(add_opts
, key
, *opt
);
858 old_opts
= GET_PROP(options
);
859 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
860 array_copy(old_opts
, new_opts
);
862 array_merge(add_opts
, new_opts
);
863 SET_PROP(options
, new_opts
);
864 zval_ptr_dtor(&new_opts
);
865 zval_ptr_dtor(&add_opts
);
871 /* {{{ proto array HttpRequest::getOptions()
873 * Get currently set options.
875 * Returns an associative array containing currently set options.
877 PHP_METHOD(HttpRequest
, getOptions
)
882 RETURN_PROP(options
);
887 /* {{{ proto bool HttpRequest::setSslOptions([array options])
891 * Accepts an associative array as parameter containing any SSL specific options.
892 * If the parameter is empty or omitted, the SSL options will be reset.
894 * Returns TRUE on success, or FALSE on failure.
896 PHP_METHOD(HttpRequest
, setSslOptions
)
898 http_request_object_set_options_subr("ssl", 1);
902 /* {{{ proto bool HttpRequest::addSslOptions(array options)
904 * Set additional SSL options.
906 * Expects an associative array as parameter containing additional SSL specific options.
908 * Returns TRUE on success, or FALSE on failure.
910 PHP_METHOD(HttpRequest
, addSslOptions
)
912 http_request_object_set_options_subr("ssl", 0);
916 /* {{{ proto array HttpRequest::getSslOtpions()
918 * Get previously set SSL options.
920 * Returns an associative array containing any previously set SSL options.
922 PHP_METHOD(HttpRequest
, getSslOptions
)
924 http_request_object_get_options_subr("ssl");
928 /* {{{ proto bool HttpRequest::addHeaders(array headers)
930 * Add request header name/value pairs.
932 * Expects an associative array as parameter containing additional header
935 * Returns TRUE on success, or FALSE on failure.
937 PHP_METHOD(HttpRequest
, addHeaders
)
939 http_request_object_set_options_subr("headers", 0);
942 /* {{{ proto bool HttpRequest::setHeaders([array headers])
944 * Set request header name/value pairs.
946 * Accepts an associative array as parameter containing header name/value pairs.
947 * If the parameter is empty or omitted, all previously set headers will be unset.
949 * Returns TRUE on success, or FALSE on failure.
951 PHP_METHOD(HttpRequest
, setHeaders
)
953 http_request_object_set_options_subr("headers", 1);
957 /* {{{ proto array HttpRequest::getHeaders()
959 * Get previously set request headers.
961 * Returns an associative array containing all currently set headers.
963 PHP_METHOD(HttpRequest
, getHeaders
)
965 http_request_object_get_options_subr("headers");
969 /* {{{ proto bool HttpRequest::setCookies([array cookies])
973 * Accepts an associative array as parameter containing cookie name/value pairs.
974 * If the parameter is empty or omitted, all previously set cookies will be unset.
976 * Returns TRUE on success, or FALSE on failure.
978 PHP_METHOD(HttpRequest
, setCookies
)
980 http_request_object_set_options_subr("cookies", 1);
984 /* {{{ proto bool HttpRequest::addCookies(array cookies)
988 * Expects an associative array as parameter containing any cookie name/value
991 * Returns TRUE on success, or FALSE on failure.
993 PHP_METHOD(HttpRequest
, addCookies
)
995 http_request_object_set_options_subr("cookies", 0);
999 /* {{{ proto array HttpRequest::getCookies()
1001 * Get previously set cookies.
1003 * Returns an associative array containing any previously set cookies.
1005 PHP_METHOD(HttpRequest
, getCookies
)
1007 http_request_object_get_options_subr("cookies");
1011 /* {{{ proto bool HttpRequest::setUrl(string url)
1013 * Set the request URL.
1015 * Expects a string as parameter specifying the request url.
1017 * Returns TRUE on success, or FALSE on failure.
1019 PHP_METHOD(HttpRequest
, setUrl
)
1024 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
1028 UPD_STRL(url
, URL
, URL_len
);
1033 /* {{{ proto string HttpRequest::getUrl()
1035 * Get the previously set request URL.
1037 * Returns the currently set request url as string.
1039 PHP_METHOD(HttpRequest
, getUrl
)
1049 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1051 * Set the request method.
1053 * Expects an int as parameter specifying the request method to use.
1054 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
1056 * Returns TRUE on success, or FALSE on failure.
1058 PHP_METHOD(HttpRequest
, setMethod
)
1062 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
1066 UPD_PROP(long, method
, meth
);
1071 /* {{{ proto int HttpRequest::getMethod()
1073 * Get the previously set request method.
1075 * Returns the currently set request method.
1077 PHP_METHOD(HttpRequest
, getMethod
)
1082 RETURN_PROP(method
);
1087 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1089 * Set the content type the post request should have.
1091 * Expects a string as parameters containing the content type of the request
1092 * (primary/secondary).
1094 * Returns TRUE on success, or FALSE if the content type does not seem to
1095 * contain a primary and a secondary part.
1097 PHP_METHOD(HttpRequest
, setContentType
)
1102 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1106 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1107 UPD_STRL(contentType
, ctype
, ct_len
);
1112 /* {{{ proto string HttpRequest::getContentType()
1114 * Get the previously content type.
1116 * Returns the previously set content type as string.
1118 PHP_METHOD(HttpRequest
, getContentType
)
1123 RETURN_PROP(contentType
);
1128 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1130 * Set the URL query parameters to use, overwriting previously set query parameters.
1131 * Affects any request types.
1133 * Accepts a string or associative array parameter containing the pre-encoded
1134 * query string or to be encoded query fields. If the parameter is empty or
1135 * omitted, the query data will be unset.
1137 * Returns TRUE on success, or FALSE on failure.
1139 PHP_METHOD(HttpRequest
, setQueryData
)
1143 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1147 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1148 UPD_STRL(queryData
, "", 0);
1149 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1150 char *query_data
= NULL
;
1152 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1156 UPD_PROP(string
, queryData
, query_data
);
1161 convert_to_string_ex(&qdata
);
1162 UPD_STRL(queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1163 if (orig
!= qdata
) {
1164 zval_ptr_dtor(&qdata
);
1171 /* {{{ proto string HttpRequest::getQueryData()
1173 * Get the current query data in form of an urlencoded query string.
1175 * Returns a string containing the urlencoded query.
1177 PHP_METHOD(HttpRequest
, getQueryData
)
1182 RETURN_PROP(queryData
);
1187 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1189 * Add parameters to the query parameter list, leaving previously set unchanged.
1190 * Affects any request type.
1192 * Expects an associative array as parameter containing the query fields to add.
1194 * Returns TRUE on success, or FALSE on failure.
1196 PHP_METHOD(HttpRequest
, addQueryData
)
1198 zval
*qdata
, *old_qdata
;
1199 char *query_data
= NULL
;
1200 size_t query_data_len
= 0;
1202 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &qdata
)) {
1206 old_qdata
= GET_PROP(queryData
);
1208 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
)) {
1212 UPD_STRL(queryData
, query_data
, query_data_len
);
1219 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1221 * Adds POST data entries, leaving previously set unchanged, unless a
1222 * post entry with the same name already exists.
1223 * Affects only POST and custom requests.
1225 * Expects an associative array as parameter containing the post fields.
1227 * Returns TRUE on success, or FALSE on failure.
1229 PHP_METHOD(HttpRequest
, addPostFields
)
1231 zval
*post_data
, *old_post
, *new_post
;
1233 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &post_data
)) {
1237 if (zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1238 MAKE_STD_ZVAL(new_post
);
1239 array_init(new_post
);
1240 old_post
= GET_PROP(postFields
);
1241 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1242 array_copy(old_post
, new_post
);
1244 array_merge(post_data
, new_post
);
1245 SET_PROP(postFields
, new_post
);
1246 zval_ptr_dtor(&new_post
);
1253 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1255 * Set the POST data entries, overwriting previously set POST data.
1256 * Affects only POST and custom requests.
1258 * Accepts an associative array as parameter containing the post fields.
1259 * If the parameter is empty or omitted, the post data will be unset.
1261 * Returns TRUE on success, or FALSE on failure.
1263 PHP_METHOD(HttpRequest
, setPostFields
)
1265 zval
*post
, *post_data
= NULL
;
1267 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &post_data
)) {
1271 MAKE_STD_ZVAL(post
);
1273 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1274 array_copy(post_data
, post
);
1276 SET_PROP(postFields
, post
);
1277 zval_ptr_dtor(&post
);
1283 /* {{{ proto array HttpRequest::getPostFields()
1285 * Get previously set POST data.
1287 * Returns the currently set post fields as associative array.
1289 PHP_METHOD(HttpRequest
, getPostFields
)
1294 RETURN_PROP(postFields
);
1299 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1301 * Set raw post data to send, overwriting previously set raw post data. Don't
1302 * forget to specify a content type. Affects only POST and custom requests.
1303 * Only either post fields or raw post data can be used for each request.
1304 * Raw post data has higher precedence and will be used even if post fields
1307 * Accepts a string as parameter containing the *raw* post data.
1309 * Returns TRUE on success, or FALSE on failure.
1311 PHP_METHOD(HttpRequest
, setRawPostData
)
1313 char *raw_data
= NULL
;
1316 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1324 UPD_STRL(rawPostData
, raw_data
, data_len
);
1329 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1331 * Add raw post data, leaving previously set raw post data unchanged.
1332 * Affects only POST and custom requests.
1334 * Expects a string as parameter containing the raw post data to concatenate.
1336 * Returns TRUE on success, or FALSE on failure.
1338 PHP_METHOD(HttpRequest
, addRawPostData
)
1343 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1348 zval
*data
= zval_copy(IS_STRING
, GET_PROP(rawPostData
));
1350 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1351 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1352 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, raw_data
, data_len
);
1353 SET_PROP(rawPostData
, data
);
1361 /* {{{ proto string HttpRequest::getRawPostData()
1363 * Get previously set raw post data.
1365 * Returns a string containing the currently set raw post data.
1367 PHP_METHOD(HttpRequest
, getRawPostData
)
1372 RETURN_PROP(rawPostData
);
1377 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1379 * Add a file to the POST request, leaving previously set files unchanged.
1380 * Affects only POST and custom requests. Cannot be used with raw post data.
1382 * Expects a string parameter containing the form element name, and a string
1383 * paremeter containing the path to the file which should be uploaded.
1384 * Additionally accepts an optional string parameter which should contain
1385 * the content type of the file.
1387 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1388 * primary and a secondary content type part.
1390 PHP_METHOD(HttpRequest
, addPostFile
)
1392 zval
*entry
, *old_post
, *new_post
;
1393 char *name
, *file
, *type
= NULL
;
1394 int name_len
, file_len
, type_len
= 0;
1396 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1401 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1403 type
= "application/x-octetstream";
1404 type_len
= sizeof("application/x-octetstream") - 1;
1407 MAKE_STD_ZVAL(entry
);
1410 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1411 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1412 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1414 MAKE_STD_ZVAL(new_post
);
1415 array_init(new_post
);
1416 old_post
= GET_PROP(postFiles
);
1417 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1418 array_copy(old_post
, new_post
);
1420 add_next_index_zval(new_post
, entry
);
1421 SET_PROP(postFiles
, new_post
);
1422 zval_ptr_dtor(&new_post
);
1428 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1430 * Set files to post, overwriting previously set post files.
1431 * Affects only POST and requests. Cannot be used with raw post data.
1433 * Accepts an array containing the files to post. Each entry should be an
1434 * associative array with "name", "file" and "type" keys. If the parameter
1435 * is empty or omitted the post files will be unset.
1437 * Returns TRUE on success, or FALSE on failure.
1439 PHP_METHOD(HttpRequest
, setPostFiles
)
1441 zval
*files
= NULL
, *post
;
1443 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!/", &files
)) {
1447 MAKE_STD_ZVAL(post
);
1449 if (files
&& (Z_TYPE_P(files
) == IS_ARRAY
)) {
1450 array_copy(files
, post
);
1452 SET_PROP(postFiles
, post
);
1453 zval_ptr_dtor(&post
);
1459 /* {{{ proto array HttpRequest::getPostFiles()
1461 * Get all previously added POST files.
1463 * Returns an array containing currently set post files.
1465 PHP_METHOD(HttpRequest
, getPostFiles
)
1470 RETURN_PROP(postFiles
);
1475 /* {{{ proto bool HttpRequest::setPutFile([string file])
1477 * Set file to put. Affects only PUT requests.
1479 * Accepts a string as parameter referencing the path to file.
1480 * If the parameter is empty or omitted the put file will be unset.
1482 * Returns TRUE on success, or FALSE on failure.
1484 PHP_METHOD(HttpRequest
, setPutFile
)
1489 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1493 UPD_STRL(putFile
, file
, file_len
);
1498 /* {{{ proto string HttpRequest::getPutFile()
1500 * Get previously set put file.
1502 * Returns a string containing the path to the currently set put file.
1504 PHP_METHOD(HttpRequest
, getPutFile
)
1509 RETURN_PROP(putFile
);
1514 /* {{{ proto array HttpRequest::getResponseData()
1516 * Get all response data after the request has been sent.
1518 * Returns an associative array with the key "headers" containing an associative
1519 * array holding all response headers, as well as the key "body" containing a
1520 * string with the response body.
1522 * If redirects were allowed and several responses were received, the data
1523 * references the last received response.
1525 PHP_METHOD(HttpRequest
, getResponseData
)
1530 RETURN_PROP(responseData
);
1535 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1537 * Get response header(s) after the request has been sent.
1539 * Accepts an string as optional parameter specifying a certain header to read.
1540 * If the parameter is empty or omitted all response headers will be returned.
1542 * Returns either a string with the value of the header matching name if requested,
1543 * FALSE on failure, or an associative array containing all response headers.
1545 * If redirects were allowed and several responses were received, the data
1546 * references the last received response.
1548 PHP_METHOD(HttpRequest
, getResponseHeader
)
1551 zval
*data
, **headers
, **header
;
1552 char *header_name
= NULL
;
1555 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1559 data
= GET_PROP(responseData
);
1560 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1561 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) &&
1562 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1563 if (!header_len
|| !header_name
) {
1564 RETVAL_ZVAL(*headers
, 1, 0);
1565 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1566 RETVAL_ZVAL(*header
, 1, 0);
1577 /* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]])
1579 * Get response cookie(s) after the request has been sent.
1581 * Returns an array of stdClass objects like http_parse_cookie would return.
1583 * If redirects were allowed and several responses were received, the data
1584 * references the last received response.
1586 PHP_METHOD(HttpRequest
, getResponseCookies
)
1590 zval
*allowed_extras_array
= NULL
, *data
, **headers
;
1592 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|la", &flags
, &allowed_extras_array
)) {
1596 data
= GET_PROP(responseData
);
1597 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1598 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) &&
1599 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1602 char *key
= NULL
, **allowed_extras
= NULL
;
1603 zval
**header
= NULL
, **entry
= NULL
;
1604 HashPosition pos
, pos1
, pos2
;
1606 array_init(return_value
);
1608 if (allowed_extras_array
) {
1609 allowed_extras
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array
)) + 1, sizeof(char *));
1610 FOREACH_VAL(pos
, allowed_extras_array
, entry
) {
1611 ZVAL_ADDREF(*entry
);
1612 convert_to_string_ex(entry
);
1613 allowed_extras
[i
++] = estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
1614 zval_ptr_dtor(entry
);
1618 FOREACH_HASH_KEYVAL(pos1
, Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1619 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1620 http_cookie_list list
;
1622 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1623 zval
**single_header
;
1625 FOREACH_VAL(pos2
, *header
, single_header
) {
1626 ZVAL_ADDREF(*single_header
);
1627 convert_to_string_ex(single_header
);
1628 if (http_parse_cookie_ex(&list
, Z_STRVAL_PP(single_header
), flags
, allowed_extras
)) {
1631 MAKE_STD_ZVAL(cookie
);
1632 object_init(cookie
);
1633 http_cookie_list_tostruct(&list
, cookie
);
1634 add_next_index_zval(return_value
, cookie
);
1635 http_cookie_list_dtor(&list
);
1637 zval_ptr_dtor(single_header
);
1640 ZVAL_ADDREF(*header
);
1641 convert_to_string_ex(header
);
1642 if (http_parse_cookie_ex(&list
, Z_STRVAL_PP(header
), flags
, allowed_extras
)) {
1645 MAKE_STD_ZVAL(cookie
);
1646 object_init(cookie
);
1647 http_cookie_list_tostruct(&list
, cookie
);
1648 add_next_index_zval(return_value
, cookie
);
1649 http_cookie_list_dtor(&list
);
1651 zval_ptr_dtor(header
);
1658 if (allowed_extras
) {
1659 for (i
= 0; allowed_extras
[i
]; ++i
) {
1660 efree(allowed_extras
[i
]);
1662 efree(allowed_extras
);
1671 /* {{{ proto string HttpRequest::getResponseBody()
1673 * Get the response body after the request has been sent.
1675 * Returns a string containing the response body.
1677 * If redirects were allowed and several responses were received, the data
1678 * references the last received response.
1680 PHP_METHOD(HttpRequest
, getResponseBody
)
1686 zval
*data
= GET_PROP(responseData
);
1688 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1689 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
))) {
1690 RETURN_ZVAL(*body
, 1, 0);
1698 /* {{{ proto int HttpRequest::getResponseCode()
1700 * Get the response code after the request has been sent.
1702 * Returns an int representing the response code.
1704 * If redirects were allowed and several responses were received, the data
1705 * references the last received response.
1707 PHP_METHOD(HttpRequest
, getResponseCode
)
1712 RETURN_PROP(responseCode
);
1717 /* {{{ proto string HttpRequest::getResponseStatus()
1719 * Get the response status (i.e. the string after the response code) after the message has been sent.
1721 * Returns a string containing the response status text.
1723 PHP_METHOD(HttpRequest
, getResponseStatus
)
1728 RETURN_PROP(responseStatus
);
1733 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1735 * Get response info after the request has been sent.
1736 * See http_get() for a full list of returned info.
1738 * Accepts a string as optional parameter specifying the info to read.
1739 * If the parameter is empty or omitted, an associative array containing
1740 * all available info will be returned.
1742 * Returns either a scalar containing the value of the info matching name if
1743 * requested, FALSE on failure, or an associative array containing all
1746 * If redirects were allowed and several responses were received, the data
1747 * references the last received response.
1749 PHP_METHOD(HttpRequest
, getResponseInfo
)
1752 zval
*info
, **infop
;
1753 char *info_name
= NULL
;
1756 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1760 info
= GET_PROP(responseInfo
);
1762 if (Z_TYPE_P(info
) != IS_ARRAY
) {
1766 if (info_len
&& info_name
) {
1767 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1768 RETURN_ZVAL(*infop
, 1, 0);
1770 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1774 RETURN_ZVAL(info
, 1, 0);
1780 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1782 * Get the full response as HttpMessage object after the request has been sent.
1784 * Returns an HttpMessage object of the response.
1786 * If redirects were allowed and several responses were received, the data
1787 * references the last received response. Use HttpMessage::getParentMessage()
1788 * to access the data of previously received responses within this request
1791 * Throws HttpException.
1793 PHP_METHOD(HttpRequest
, getResponseMessage
)
1800 SET_EH_THROW_HTTP();
1801 message
= GET_PROP(responseMessage
);
1802 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1803 RETVAL_OBJECT(message
, 1);
1812 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1814 * Get sent HTTP message.
1816 * Returns an HttpMessage object representing the sent request.
1818 * If redirects were allowed and several responses were received, the data
1819 * references the last received response. Use HttpMessage::getParentMessage()
1820 * to access the data of previously sent requests within this request
1823 * Note that the internal request message is immutable, that means that the
1824 * request message received through HttpRequest::getRequestMessage() will
1825 * always look the same for the same request, regardless of any changes you
1826 * may have made to the returned object.
1828 * Throws HttpMalformedHeadersException, HttpEncodingException.
1830 PHP_METHOD(HttpRequest
, getRequestMessage
)
1836 getObject(http_request_object
, obj
);
1838 SET_EH_THROW_HTTP();
1839 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1840 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce
, msg
, NULL
), 0);
1847 /* {{{ proto string HttpRequest::getRawRequestMessage()
1849 * Get sent HTTP message.
1851 * Returns an HttpMessage in a form of a string
1854 PHP_METHOD(HttpRequest
, getRawRequestMessage
)
1859 getObject(http_request_object
, obj
);
1861 RETURN_PHPSTR_DUP(&obj
->request
->conv
.request
);
1866 /* {{{ proto string HttpRequest::getRawResponseMessage()
1868 * Get the entire HTTP response.
1870 * Returns the complete web server response, including the headers in a form of a string.
1873 PHP_METHOD(HttpRequest
, getRawResponseMessage
)
1878 getObject(http_request_object
, obj
);
1880 RETURN_PHPSTR_DUP(&obj
->request
->conv
.response
);
1885 /* {{{ proto HttpMessage HttpRequest::getHistory()
1887 * Get all sent requests and received responses as an HttpMessage object.
1889 * If you want to record history, set the instance variable
1890 * HttpRequest::$recordHistory to TRUE.
1892 * Returns an HttpMessage object representing the complete request/response
1895 * The object references the last received response, use HttpMessage::getParentMessage()
1896 * to access the data of previously sent requests and received responses.
1898 * Throws HttpRuntimeException.
1900 PHP_METHOD(HttpRequest
, getHistory
)
1907 SET_EH_THROW_HTTP();
1908 hist
= GET_PROP(history
);
1909 if (Z_TYPE_P(hist
) == IS_OBJECT
) {
1910 RETVAL_OBJECT(hist
, 1);
1912 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "The history is empty");
1919 /* {{{ proto void HttpRequest::clearHistory()
1921 * Clear the history.
1923 PHP_METHOD(HttpRequest
, clearHistory
)
1928 MAKE_STD_ZVAL(hist
);
1930 SET_PROP(history
, hist
);
1931 zval_ptr_dtor(&hist
);
1936 /* {{{ proto HttpMessage HttpRequest::send()
1938 * Send the HTTP request.
1940 * Returns the received response as HttpMessage object.
1942 * NOTE: While an exception may be thrown, the transfer could have succeeded
1943 * at least partially, so you might want to check the return values of various
1944 * HttpRequest::getResponse*() methods.
1946 * Throws HttpRuntimeException, HttpRequestException,
1947 * HttpMalformedHeaderException, HttpEncodingException.
1952 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
1953 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1954 * $r->addQueryData(array('category' => 3));
1957 * if ($r->getResponseCode() == 200) {
1958 * file_put_contents('local.rss', $r->getResponseBody());
1960 * } catch (HttpException $ex) {
1969 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
1970 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1971 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1972 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1974 * echo $r->send()->getBody();
1975 * } catch (HttpException $ex) {
1981 PHP_METHOD(HttpRequest
, send
)
1983 getObject(http_request_object
, obj
);
1987 SET_EH_THROW_HTTP();
1992 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1993 } else if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
1994 http_request_exec(obj
->request
);
1995 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
1996 RETVAL_OBJECT(GET_PROP(responseMessage
), 1);
2004 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
2011 * vim600: noet sw=4 ts=4 fdm=marker
2012 * vim<600: noet sw=4 ts=4