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 +--------------------------------------------------------------------+
19 #define HTTP_WANT_CURL
22 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
24 #include "zend_interfaces.h"
26 #include "php_http_api.h"
27 #include "php_http_exception_object.h"
28 #include "php_http_message_api.h"
29 #include "php_http_message_object.h"
30 #include "php_http_request_api.h"
31 #include "php_http_request_object.h"
32 #include "php_http_request_pool_api.h"
33 #include "php_http_url_api.h"
35 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
36 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
37 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
38 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
40 HTTP_BEGIN_ARGS(__construct
, 0, 0)
42 HTTP_ARG_VAL(method
, 0)
43 HTTP_ARG_VAL(options
, 0)
46 HTTP_EMPTY_ARGS(getOptions
, 0);
47 HTTP_BEGIN_ARGS(setOptions
, 0, 0)
48 HTTP_ARG_VAL(options
, 0)
51 HTTP_EMPTY_ARGS(getSslOptions
, 0);
52 HTTP_BEGIN_ARGS(setSslOptions
, 0, 0)
53 HTTP_ARG_VAL(ssl_options
, 0)
56 HTTP_BEGIN_ARGS(addSslOptions
, 0, 0)
57 HTTP_ARG_VAL(ssl_optins
, 0)
60 HTTP_EMPTY_ARGS(getHeaders
, 0);
61 HTTP_BEGIN_ARGS(setHeaders
, 0, 0)
62 HTTP_ARG_VAL(headers
, 0)
65 HTTP_BEGIN_ARGS(addHeaders
, 0, 1)
66 HTTP_ARG_VAL(headers
, 0)
69 HTTP_EMPTY_ARGS(getCookies
, 0);
70 HTTP_BEGIN_ARGS(setCookies
, 0, 0)
71 HTTP_ARG_VAL(cookies
, 0)
74 HTTP_BEGIN_ARGS(addCookies
, 0, 1)
75 HTTP_ARG_VAL(cookies
, 0)
78 HTTP_EMPTY_ARGS(getUrl
, 0);
79 HTTP_BEGIN_ARGS(setUrl
, 0, 1)
83 HTTP_EMPTY_ARGS(getMethod
, 0);
84 HTTP_BEGIN_ARGS(setMethod
, 0, 1)
85 HTTP_ARG_VAL(request_method
, 0)
88 HTTP_EMPTY_ARGS(getContentType
, 0);
89 HTTP_BEGIN_ARGS(setContentType
, 0, 1)
90 HTTP_ARG_VAL(content_type
, 0)
93 HTTP_EMPTY_ARGS(getQueryData
, 0);
94 HTTP_BEGIN_ARGS(setQueryData
, 0, 0)
95 HTTP_ARG_VAL(query_data
, 0)
98 HTTP_BEGIN_ARGS(addQueryData
, 0, 1)
99 HTTP_ARG_VAL(query_data
, 0)
102 HTTP_EMPTY_ARGS(getPostFields
, 0);
103 HTTP_BEGIN_ARGS(setPostFields
, 0, 0)
104 HTTP_ARG_VAL(post_fields
, 0)
107 HTTP_BEGIN_ARGS(addPostFields
, 0, 1)
108 HTTP_ARG_VAL(post_fields
, 0)
111 HTTP_EMPTY_ARGS(getPostFiles
, 0);
112 HTTP_BEGIN_ARGS(setPostFiles
, 0, 0)
113 HTTP_ARG_VAL(post_files
, 0)
116 HTTP_BEGIN_ARGS(addPostFile
, 0, 2)
117 HTTP_ARG_VAL(formname
, 0)
118 HTTP_ARG_VAL(filename
, 0)
119 HTTP_ARG_VAL(content_type
, 0)
122 HTTP_EMPTY_ARGS(getRawPostData
, 0);
123 HTTP_BEGIN_ARGS(setRawPostData
, 0, 0)
124 HTTP_ARG_VAL(raw_post_data
, 0)
127 HTTP_BEGIN_ARGS(addRawPostData
, 0, 1)
128 HTTP_ARG_VAL(raw_post_data
, 0)
131 HTTP_EMPTY_ARGS(getPutFile
, 0);
132 HTTP_BEGIN_ARGS(setPutFile
, 0, 0)
133 HTTP_ARG_VAL(filename
, 0)
136 HTTP_EMPTY_ARGS(getResponseData
, 0);
137 HTTP_BEGIN_ARGS(getResponseHeader
, 0, 0)
138 HTTP_ARG_VAL(name
, 0)
141 HTTP_BEGIN_ARGS(getResponseCookie
, 0, 0)
142 HTTP_ARG_VAL(name
, 0)
145 HTTP_EMPTY_ARGS(getResponseBody
, 0);
146 HTTP_EMPTY_ARGS(getResponseCode
, 0);
147 HTTP_BEGIN_ARGS(getResponseInfo
, 0, 0)
148 HTTP_ARG_VAL(name
, 0)
151 HTTP_EMPTY_ARGS(getResponseMessage
, 0);
152 HTTP_EMPTY_ARGS(getRawResponseMessage
, 0);
153 HTTP_EMPTY_ARGS(getRequestMessage
, 0);
154 HTTP_EMPTY_ARGS(getRawRequestMessage
, 0);
155 HTTP_EMPTY_ARGS(getHistory
, 0);
156 HTTP_EMPTY_ARGS(clearHistory
, 0);
157 HTTP_EMPTY_ARGS(send
, 0);
159 HTTP_BEGIN_ARGS(get
, 0, 1)
161 HTTP_ARG_VAL(options
, 0)
162 HTTP_ARG_VAL(info
, 1)
165 HTTP_BEGIN_ARGS(head
, 0, 1)
167 HTTP_ARG_VAL(options
, 0)
168 HTTP_ARG_VAL(info
, 1)
171 HTTP_BEGIN_ARGS(postData
, 0, 2)
173 HTTP_ARG_VAL(data
, 0)
174 HTTP_ARG_VAL(options
, 0)
175 HTTP_ARG_VAL(info
, 1)
178 HTTP_BEGIN_ARGS(postFields
, 0, 2)
180 HTTP_ARG_VAL(data
, 0)
181 HTTP_ARG_VAL(options
, 0)
182 HTTP_ARG_VAL(info
, 1)
185 HTTP_BEGIN_ARGS(putFile
, 0, 2)
187 HTTP_ARG_VAL(file
, 0)
188 HTTP_ARG_VAL(options
, 0)
189 HTTP_ARG_VAL(info
, 1)
192 HTTP_BEGIN_ARGS(putStream
, 0, 2)
194 HTTP_ARG_VAL(stream
, 0)
195 HTTP_ARG_VAL(options
, 0)
196 HTTP_ARG_VAL(info
, 1)
199 HTTP_BEGIN_ARGS(methodRegister
, 0, 1)
200 HTTP_ARG_VAL(method_name
, 0)
203 HTTP_BEGIN_ARGS(methodUnregister
, 0, 1)
204 HTTP_ARG_VAL(method
, 0)
207 HTTP_BEGIN_ARGS(methodName
, 0, 1)
208 HTTP_ARG_VAL(method_id
, 0)
211 HTTP_BEGIN_ARGS(methodExists
, 0, 1)
212 HTTP_ARG_VAL(method
, 0)
215 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
216 static inline void _http_request_object_declare_default_properties(TSRMLS_D
);
218 #define OBJ_PROP_CE http_request_object_ce
219 zend_class_entry
*http_request_object_ce
;
220 zend_function_entry http_request_object_fe
[] = {
221 HTTP_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
223 HTTP_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
224 HTTP_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
225 HTTP_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
226 HTTP_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
227 HTTP_REQUEST_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
229 HTTP_REQUEST_ME(addHeaders
, ZEND_ACC_PUBLIC
)
230 HTTP_REQUEST_ME(getHeaders
, ZEND_ACC_PUBLIC
)
231 HTTP_REQUEST_ME(setHeaders
, ZEND_ACC_PUBLIC
)
233 HTTP_REQUEST_ME(addCookies
, ZEND_ACC_PUBLIC
)
234 HTTP_REQUEST_ME(getCookies
, ZEND_ACC_PUBLIC
)
235 HTTP_REQUEST_ME(setCookies
, ZEND_ACC_PUBLIC
)
237 HTTP_REQUEST_ME(setMethod
, ZEND_ACC_PUBLIC
)
238 HTTP_REQUEST_ME(getMethod
, ZEND_ACC_PUBLIC
)
240 HTTP_REQUEST_ME(setUrl
, ZEND_ACC_PUBLIC
)
241 HTTP_REQUEST_ME(getUrl
, ZEND_ACC_PUBLIC
)
243 HTTP_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
244 HTTP_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
246 HTTP_REQUEST_ME(setQueryData
, ZEND_ACC_PUBLIC
)
247 HTTP_REQUEST_ME(getQueryData
, ZEND_ACC_PUBLIC
)
248 HTTP_REQUEST_ME(addQueryData
, ZEND_ACC_PUBLIC
)
250 HTTP_REQUEST_ME(setPostFields
, ZEND_ACC_PUBLIC
)
251 HTTP_REQUEST_ME(getPostFields
, ZEND_ACC_PUBLIC
)
252 HTTP_REQUEST_ME(addPostFields
, ZEND_ACC_PUBLIC
)
254 HTTP_REQUEST_ME(setRawPostData
, ZEND_ACC_PUBLIC
)
255 HTTP_REQUEST_ME(getRawPostData
, ZEND_ACC_PUBLIC
)
256 HTTP_REQUEST_ME(addRawPostData
, ZEND_ACC_PUBLIC
)
258 HTTP_REQUEST_ME(setPostFiles
, ZEND_ACC_PUBLIC
)
259 HTTP_REQUEST_ME(addPostFile
, ZEND_ACC_PUBLIC
)
260 HTTP_REQUEST_ME(getPostFiles
, ZEND_ACC_PUBLIC
)
262 HTTP_REQUEST_ME(setPutFile
, ZEND_ACC_PUBLIC
)
263 HTTP_REQUEST_ME(getPutFile
, ZEND_ACC_PUBLIC
)
265 HTTP_REQUEST_ME(send
, ZEND_ACC_PUBLIC
)
267 HTTP_REQUEST_ME(getResponseData
, ZEND_ACC_PUBLIC
)
268 HTTP_REQUEST_ME(getResponseHeader
, ZEND_ACC_PUBLIC
)
269 HTTP_REQUEST_ME(getResponseCookie
, ZEND_ACC_PUBLIC
)
270 HTTP_REQUEST_ME(getResponseCode
, 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
);
316 phpstr_init(&o
->history
);
322 ALLOC_HASHTABLE(OBJ_PROP(o
));
323 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
324 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
326 ov
.handle
= putObject(http_request_object
, o
);
327 ov
.handlers
= &http_request_object_handlers
;
332 zend_object_value
_http_request_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
335 zend_object_value new_ov
;
336 http_request_object
*new_obj
;
337 getObject(http_request_object
, old_obj
);
339 old_zo
= zend_objects_get_address(this_ptr TSRMLS_CC
);
340 new_ov
= http_request_object_new_ex(old_zo
->ce
, NULL
, &new_obj
);
341 if (old_obj
->request
->ch
) {
342 http_curl_init_ex(curl_easy_duphandle(old_obj
->request
->ch
), new_obj
->request
);
345 zend_objects_clone_members(&new_obj
->zo
, new_ov
, old_zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
346 phpstr_append(&new_obj
->history
, old_obj
->history
.data
, old_obj
->history
.used
);
347 phpstr_append(&new_obj
->request
->conv
.request
, old_obj
->request
->conv
.request
.data
, old_obj
->request
->conv
.request
.used
);
348 phpstr_append(&new_obj
->request
->conv
.response
, old_obj
->request
->conv
.response
.data
, old_obj
->request
->conv
.response
.used
);
353 static inline void _http_request_object_declare_default_properties(TSRMLS_D
)
355 zend_class_entry
*ce
= http_request_object_ce
;
357 DCL_PROP_N(PRIVATE
, options
);
358 DCL_PROP_N(PRIVATE
, postFields
);
359 DCL_PROP_N(PRIVATE
, postFiles
);
360 DCL_PROP_N(PRIVATE
, responseInfo
);
361 DCL_PROP_N(PRIVATE
, responseData
);
362 DCL_PROP_N(PRIVATE
, responseMessage
);
363 DCL_PROP(PRIVATE
, long, responseCode
, 0);
364 DCL_PROP(PRIVATE
, long, method
, HTTP_GET
);
365 DCL_PROP(PRIVATE
, string
, url
, "");
366 DCL_PROP(PRIVATE
, string
, contentType
, "");
367 DCL_PROP(PRIVATE
, string
, rawPostData
, "");
368 DCL_PROP(PRIVATE
, string
, queryData
, "");
369 DCL_PROP(PRIVATE
, string
, putFile
, "");
371 DCL_PROP(PUBLIC
, bool, recordHistory
, 0);
375 * Request Method Constants
378 DCL_CONST(long, "METH_GET", HTTP_GET
);
379 DCL_CONST(long, "METH_HEAD", HTTP_HEAD
);
380 DCL_CONST(long, "METH_POST", HTTP_POST
);
381 DCL_CONST(long, "METH_PUT", HTTP_PUT
);
382 DCL_CONST(long, "METH_DELETE", HTTP_DELETE
);
383 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS
);
384 DCL_CONST(long, "METH_TRACE", HTTP_TRACE
);
385 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT
);
386 /* WebDAV - RFC 2518 */
387 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND
);
388 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH
);
389 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL
);
390 DCL_CONST(long, "METH_COPY", HTTP_COPY
);
391 DCL_CONST(long, "METH_MOVE", HTTP_MOVE
);
392 DCL_CONST(long, "METH_LOCK", HTTP_LOCK
);
393 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK
);
394 /* WebDAV Versioning - RFC 3253 */
395 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL
);
396 DCL_CONST(long, "METH_REPORT", HTTP_REPORT
);
397 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT
);
398 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN
);
399 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT
);
400 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE
);
401 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE
);
402 DCL_CONST(long, "METH_LABEL", HTTP_LABEL
);
403 DCL_CONST(long, "METH_MERGE", HTTP_MERGE
);
404 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL
);
405 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY
);
406 /* WebDAV Access Control - RFC 3744 */
407 DCL_CONST(long, "METH_ACL", HTTP_ACL
);
409 /* cURL HTTP protocol versions */
410 DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0
);
411 DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1
);
412 DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE
);
417 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC
);
418 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST
);
419 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM
);
420 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY
);
424 void _http_request_object_free(zend_object
*object TSRMLS_DC
)
426 http_request_object
*o
= (http_request_object
*) object
;
429 zend_hash_destroy(OBJ_PROP(o
));
430 FREE_HASHTABLE(OBJ_PROP(o
));
432 http_request_free(&o
->request
);
433 phpstr_dtor(&o
->history
);
437 STATUS
_http_request_object_requesthandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
439 STATUS status
= SUCCESS
;
441 http_request_reset(obj
->request
);
442 HTTP_CHECK_CURL_INIT(obj
->request
->ch
, http_curl_init(obj
->request
), return FAILURE
);
444 obj
->request
->url
= http_absolute_url(Z_STRVAL_P(GET_PROP(url
)));
446 switch (obj
->request
->meth
= Z_LVAL_P(GET_PROP(method
)))
454 php_stream_statbuf ssb
;
455 php_stream
*stream
= php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(putFile
)), "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
457 if (stream
&& !php_stream_stat(stream
, &ssb
)) {
458 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
468 /* check for raw post data */
469 zval
*raw_data
= GET_PROP(rawPostData
);
471 if (Z_STRLEN_P(raw_data
)) {
472 zval
*ctype
= GET_PROP(contentType
);
474 if (Z_STRLEN_P(ctype
)) {
475 zval
**headers
, *opts
= GET_PROP(options
);
477 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
478 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) &&
479 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
482 /* only override if not already set */
483 if ((SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(headers
), "Content-Type", sizeof("Content-Type"), (void **) &ct_header
)) && (Z_TYPE_PP(ct_header
) == IS_STRING
)) {
484 add_assoc_stringl(*headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
489 MAKE_STD_ZVAL(headers
);
491 add_assoc_stringl(headers
, "Content-Type", Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
492 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, headers
);
493 zval_ptr_dtor(&headers
);
497 obj
->request
->body
= http_request_body_init_ex(obj
->request
->body
, HTTP_REQUEST_BODY_CSTRING
,
498 estrndup(Z_STRVAL_P(raw_data
), Z_STRLEN_P(raw_data
)), Z_STRLEN_P(raw_data
), 1);
501 zval
*zfields
= GET_PROP(postFields
), *zfiles
= GET_PROP(postFiles
);
505 fields
= (Z_TYPE_P(zfields
) == IS_ARRAY
) ? Z_ARRVAL_P(zfields
) : NULL
;
506 files
= (Z_TYPE_P(zfiles
) == IS_ARRAY
) ? Z_ARRVAL_P(zfiles
) : NULL
;
508 if ((fields
&& zend_hash_num_elements(fields
)) || (files
&& zend_hash_num_elements(files
))) {
509 if (!(obj
->request
->body
= http_request_body_fill(obj
->request
->body
, fields
, files
))) {
518 if (status
== SUCCESS
) {
519 zval
*qdata
= GET_PROP(queryData
);
520 zval
*options
= GET_PROP(options
);
522 if (Z_STRLEN_P(qdata
)) {
523 if (!strchr(obj
->request
->url
, '?')) {
524 strlcat(obj
->request
->url
, "?", HTTP_URL_MAXLEN
);
526 strlcat(obj
->request
->url
, "&", HTTP_URL_MAXLEN
);
528 strlcat(obj
->request
->url
, Z_STRVAL_P(qdata
), HTTP_URL_MAXLEN
);
531 http_request_prepare(obj
->request
, Z_ARRVAL_P(options
));
533 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
534 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onprogress", sizeof("onprogress"))) {
537 if ( (Z_TYPE_P(options
) != IS_ARRAY
)
538 || (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(options
), "onprogress", sizeof("onprogress"), (void **) &entry
)
539 || (!zval_is_true(*entry
)))) {
542 ZVAL_ADDREF(getThis());
543 add_next_index_zval(pcb
, getThis());
544 add_next_index_stringl(pcb
, "onprogress", lenof("onprogress"), 1);
545 http_request_set_progress_callback(obj
->request
, pcb
);
554 STATUS
_http_request_object_responsehandler(http_request_object
*obj
, zval
*this_ptr TSRMLS_DC
)
558 phpstr_fix(&obj
->request
->conv
.request
);
559 phpstr_fix(&obj
->request
->conv
.response
);
561 msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.response
), PHPSTR_LEN(&obj
->request
->conv
.response
));
568 zval
*headers
, *message
, *resp
, *info
;
570 if (zval_is_true(GET_PROP(recordHistory
))) {
571 /* we need to act like a zipper, as we'll receive
572 * the requests and the responses in separate chains
575 http_message
*response
= msg
, *request
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
));
576 http_message
*free_msg
= request
;
582 http_message_tostring(response
, &message
, &msglen
);
583 phpstr_append(&obj
->history
, message
, msglen
);
586 http_message_tostring(request
, &message
, &msglen
);
587 phpstr_append(&obj
->history
, message
, msglen
);
590 } while ((response
= response
->parent
) && (request
= request
->parent
));
592 http_message_free(&free_msg
);
593 phpstr_fix(&obj
->history
);
596 UPD_PROP(long, responseCode
, msg
->http
.info
.response
.code
);
600 MAKE_STD_ZVAL(headers
);
602 zend_hash_copy(Z_ARRVAL_P(headers
), &msg
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
603 add_assoc_zval(resp
, "headers", headers
);
604 phpstr_data(PHPSTR(msg
), &body
, &body_len
);
605 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
606 SET_PROP(responseData
, resp
);
607 zval_ptr_dtor(&resp
);
609 MAKE_STD_ZVAL(message
);
610 ZVAL_OBJVAL(message
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
611 SET_PROP(responseMessage
, message
);
612 zval_ptr_dtor(&message
);
616 http_request_info(obj
->request
, Z_ARRVAL_P(info
));
617 SET_PROP(responseInfo
, info
);
618 zval_ptr_dtor(&info
);
620 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table
, "onfinish", sizeof("onfinish"))) {
621 zend_call_method_with_0_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "onfinish", NULL
);
628 #define http_request_object_set_options_subr(key, ow) \
629 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
630 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
, int overwrite
)
632 zval
*old_opts
, *new_opts
, *opts
= NULL
, **entry
;
633 getObject(http_request_object
, obj
);
635 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &opts
)) {
639 MAKE_STD_ZVAL(new_opts
);
640 array_init(new_opts
);
641 old_opts
= GET_PROP(options
);
642 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
643 array_copy(old_opts
, new_opts
);
646 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(new_opts
), key
, len
, (void **) &entry
)) {
648 zend_hash_clean(Z_ARRVAL_PP(entry
));
650 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
652 array_copy(opts
, *entry
);
654 array_merge(opts
, *entry
);
659 add_assoc_zval(new_opts
, key
, opts
);
661 SET_PROP(options
, new_opts
);
662 zval_ptr_dtor(&new_opts
);
667 #define http_request_object_get_options_subr(key) \
668 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
669 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS
, char *key
, size_t len
)
674 zval
*opts
, **options
;
675 getObject(http_request_object
, obj
);
677 opts
= GET_PROP(options
);
679 array_init(return_value
);
681 if ( (Z_TYPE_P(opts
) == IS_ARRAY
) &&
682 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), key
, len
, (void **) &options
))) {
683 convert_to_array(*options
);
684 array_copy(*options
, return_value
);
690 /* ### USERLAND ### */
692 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
694 * Instantiate a new HttpRequest object.
696 * Accepts a string as optional parameter containing the target request url.
697 * Additianally accepts an optional int parameter specifying the request method
698 * to use and an associative array as optional third parameter which will be
699 * passed to HttpRequest::setOptions().
701 * Throws HttpException.
703 PHP_METHOD(HttpRequest
, __construct
)
708 zval
*options
= NULL
;
709 getObject(http_request_object
, obj
);
712 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sla", &URL
, &URL_len
, &meth
, &options
)) {
714 UPD_STRL(url
, URL
, URL_len
);
717 UPD_PROP(long, method
, meth
);
720 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setoptions", NULL
, options
);
727 /* {{{ proto bool HttpRequest::setOptions([array options])
729 * Set the request options to use. See http_get() for a full list of available options.
731 * Accepts an array as optional parameters, wich values will overwrite the
732 * currently set request options. If the parameter is empty or mitted,
733 * the optoions of the HttpRequest object will be reset.
735 * Returns TRUE on success, or FALSE on failure.
737 PHP_METHOD(HttpRequest
, setOptions
)
742 zval
*opts
= NULL
, *old_opts
, *new_opts
, *add_opts
, **opt
;
743 getObject(http_request_object
, obj
);
745 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
749 MAKE_STD_ZVAL(new_opts
);
750 array_init(new_opts
);
752 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
753 SET_PROP(options
, new_opts
);
754 zval_ptr_dtor(&new_opts
);
758 MAKE_STD_ZVAL(add_opts
);
759 array_init(add_opts
);
760 /* some options need extra attention -- thus cannot use array_merge() directly */
761 FOREACH_KEYVAL(pos
, opts
, key
, idx
, opt
) {
763 if (!strcmp(key
, "headers")) {
764 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addheaders", NULL
, *opt
);
765 } else if (!strcmp(key
, "cookies")) {
766 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addcookies", NULL
, *opt
);
767 } else if (!strcmp(key
, "ssl")) {
768 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "addssloptions", NULL
, *opt
);
769 } else if ((!strcasecmp(key
, "url")) || (!strcasecmp(key
, "uri"))) {
770 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "seturl", NULL
, *opt
);
771 } else if (!strcmp(key
, "method")) {
772 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "setmethod", NULL
, *opt
);
775 add_assoc_zval(add_opts
, key
, *opt
);
782 old_opts
= GET_PROP(options
);
783 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
784 array_copy(old_opts
, new_opts
);
786 array_merge(add_opts
, new_opts
);
787 SET_PROP(options
, new_opts
);
788 zval_ptr_dtor(&new_opts
);
789 zval_ptr_dtor(&add_opts
);
795 /* {{{ proto array HttpRequest::getOptions()
797 * Get currently set options.
799 * Returns an associative array containing currently set options.
801 PHP_METHOD(HttpRequest
, getOptions
)
806 RETURN_PROP(options
);
811 /* {{{ proto bool HttpRequest::setSslOptions([array options])
815 * Accepts an associative array as parameter containing any SSL specific options.
816 * If the parameter is empty or omitted, the SSL options will be reset.
818 * Returns TRUE on success, or FALSE on failure.
820 PHP_METHOD(HttpRequest
, setSslOptions
)
822 http_request_object_set_options_subr("ssl", 1);
826 /* {{{ proto bool HttpRequest::addSslOptions(array options)
828 * Set additional SSL options.
830 * Expects an associative array as parameter containing additional SSL specific options.
832 * Returns TRUE on success, or FALSE on failure.
834 PHP_METHOD(HttpRequest
, addSslOptions
)
836 http_request_object_set_options_subr("ssl", 0);
840 /* {{{ proto array HttpRequest::getSslOtpions()
842 * Get previously set SSL options.
844 * Returns an associative array containing any previously set SSL options.
846 PHP_METHOD(HttpRequest
, getSslOptions
)
848 http_request_object_get_options_subr("ssl");
852 /* {{{ proto bool HttpRequest::addHeaders(array headers)
854 * Add request header name/value pairs.
856 * Expects an ssociative array as parameter containing additional header
859 * Returns TRUE on success, or FALSE on failure.
861 PHP_METHOD(HttpRequest
, addHeaders
)
863 http_request_object_set_options_subr("headers", 0);
866 /* {{{ proto bool HttpRequest::setHeaders([array headers])
868 * Set request header name/value pairs.
870 * Accepts an associative array as parameter containing header name/value pairs.
871 * If the parameter is empty or omitted, all previously set headers will be unset.
873 * Returns TRUE on success, or FALSE on failure.
875 PHP_METHOD(HttpRequest
, setHeaders
)
877 http_request_object_set_options_subr("headers", 1);
881 /* {{{ proto array HttpRequest::getHeaders()
883 * Get previously set request headers.
885 * Returns an associative array containing all currently set headers.
887 PHP_METHOD(HttpRequest
, getHeaders
)
889 http_request_object_get_options_subr("headers");
893 /* {{{ proto bool HttpRequest::setCookies([array cookies])
897 * Accepts an associative array as parameter containing cookie name/value pairs.
898 * If the parameter is empty or omitted, all previously set cookies will be unset.
900 * Returns TRUE on success, or FALSE on failure.
902 PHP_METHOD(HttpRequest
, setCookies
)
904 http_request_object_set_options_subr("cookies", 1);
908 /* {{{ proto bool HttpRequest::addCookies(array cookies)
912 * Expects an associative array as parameter containing any cookie name/value
915 * Returns TRUE on success, or FALSE on failure.
917 PHP_METHOD(HttpRequest
, addCookies
)
919 http_request_object_set_options_subr("cookies", 0);
923 /* {{{ proto array HttpRequest::getCookies()
925 * Get previously set cookies.
927 * Returns an associative array containing any previously set cookies.
929 PHP_METHOD(HttpRequest
, getCookies
)
931 http_request_object_get_options_subr("cookies");
935 /* {{{ proto bool HttpRequest::setUrl(string url)
937 * Set the request URL.
939 * Expects a string as parameter specifying the request url.
941 * Returns TRUE on success, or FALSE on failure.
943 PHP_METHOD(HttpRequest
, setUrl
)
947 getObject(http_request_object
, obj
);
949 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
953 UPD_STRL(url
, URL
, URL_len
);
958 /* {{{ proto string HttpRequest::getUrl()
960 * Get the previously set request URL.
962 * Returns the currently set request url as string.
964 PHP_METHOD(HttpRequest
, getUrl
)
974 /* {{{ proto bool HttpRequest::setMethod(int request_method)
976 * Set the request method.
978 * Expects an int as parameter specifying the request method to use.
979 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
981 * Returns TRUE on success, or FALSE on failure.
983 PHP_METHOD(HttpRequest
, setMethod
)
986 getObject(http_request_object
, obj
);
988 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
992 UPD_PROP(long, method
, meth
);
997 /* {{{ proto int HttpRequest::getMethod()
999 * Get the previously set request method.
1001 * Returns the currently set request method.
1003 PHP_METHOD(HttpRequest
, getMethod
)
1008 RETURN_PROP(method
);
1013 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1015 * Set the content type the post request should have.
1017 * Expects a string as parameters containing the content type of the request
1018 * (primary/secondary).
1020 * Returns TRUE on success, or FALSE if the content type does not seem to
1021 * contain a primary and a secondary part.
1023 PHP_METHOD(HttpRequest
, setContentType
)
1027 getObject(http_request_object
, obj
);
1029 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
1033 HTTP_CHECK_CONTENT_TYPE(ctype
, RETURN_FALSE
);
1034 UPD_STRL(contentType
, ctype
, ct_len
);
1039 /* {{{ proto string HttpRequest::getContentType()
1041 * Get the previously content type.
1043 * Returns the previously set content type as string.
1045 PHP_METHOD(HttpRequest
, getContentType
)
1050 RETURN_PROP(contentType
);
1055 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1057 * Set the URL query parameters to use, overwriting previously set query parameters.
1058 * Affects any request types.
1060 * Accepts a string or associative array parameter containing the pre-encoded
1061 * query string or to be encoded query fields. If the parameter is empty or
1062 * omitted, the query data will be unset.
1064 * Returns TRUE on success, or FALSE on failure.
1066 PHP_METHOD(HttpRequest
, setQueryData
)
1069 getObject(http_request_object
, obj
);
1071 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
1075 if ((!qdata
) || Z_TYPE_P(qdata
) == IS_NULL
) {
1076 UPD_STRL(queryData
, "", 0);
1077 } else if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1078 char *query_data
= NULL
;
1080 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1084 UPD_PROP(string
, queryData
, query_data
);
1089 convert_to_string_ex(&qdata
);
1090 UPD_STRL(queryData
, Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
));
1091 if (orig
!= qdata
) {
1092 zval_ptr_dtor(&qdata
);
1099 /* {{{ proto string HttpRequest::getQueryData()
1101 * Get the current query data in form of an urlencoded query string.
1103 * Returns a string containing the urlencoded query.
1105 PHP_METHOD(HttpRequest
, getQueryData
)
1110 RETURN_PROP(queryData
);
1115 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1117 * Add parameters to the query parameter list, leaving previously set unchanged.
1118 * Affects any request type.
1120 * Expects an associative array as parameter containing the query fields to add.
1122 * Returns TRUE on success, or FALSE on failure.
1124 PHP_METHOD(HttpRequest
, addQueryData
)
1126 zval
*qdata
, *old_qdata
;
1127 char *query_data
= NULL
;
1128 size_t query_data_len
= 0;
1129 getObject(http_request_object
, obj
);
1131 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &qdata
)) {
1135 old_qdata
= GET_PROP(queryData
);
1137 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
)) {
1141 UPD_STRL(queryData
, query_data
, query_data_len
);
1148 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1150 * Adds POST data entries, leaving previously set unchanged, unless a
1151 * post entry with the same name already exists.
1152 * Affects only POST and custom requests.
1154 * Expects an associative array as parameter containing the post fields.
1156 * Returns TRUE on success, or FALSE on failure.
1158 PHP_METHOD(HttpRequest
, addPostFields
)
1160 zval
*post_data
, *old_post
, *new_post
;
1161 getObject(http_request_object
, obj
);
1163 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &post_data
)) {
1167 if (zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1168 MAKE_STD_ZVAL(new_post
);
1169 array_init(new_post
);
1170 old_post
= GET_PROP(postFields
);
1171 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1172 array_copy(old_post
, new_post
);
1174 array_merge(post_data
, new_post
);
1175 SET_PROP(postFields
, new_post
);
1176 zval_ptr_dtor(&new_post
);
1183 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1185 * Set the POST data entries, overwriting previously set POST data.
1186 * Affects only POST and custom requests.
1188 * Accepts an associative array as parameter containing the post fields.
1189 * If the parameter is empty or omitted, the post data will be unset.
1191 * Returns TRUE on success, or FALSE on failure.
1193 PHP_METHOD(HttpRequest
, setPostFields
)
1195 zval
*post
, *post_data
= NULL
;
1196 getObject(http_request_object
, obj
);
1198 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/!", &post_data
)) {
1202 MAKE_STD_ZVAL(post
);
1204 if (post_data
&& zend_hash_num_elements(Z_ARRVAL_P(post_data
))) {
1205 array_copy(post_data
, post
);
1207 SET_PROP(postFields
, post
);
1208 zval_ptr_dtor(&post
);
1214 /* {{{ proto array HttpRequest::getPostFields()
1216 * Get previously set POST data.
1218 * Returns the currently set post fields as associative array.
1220 PHP_METHOD(HttpRequest
, getPostFields
)
1225 RETURN_PROP(postFields
);
1230 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1232 * Set raw post data to send, overwriting previously set raw post data. Don't
1233 * forget to specify a content type. Affects only POST and custom requests.
1234 * Only either post fields or raw post data can be used for each request.
1235 * Raw post data has higher precedence and will be used even if post fields
1238 * Accepts a string as parameter containing the *raw* post data.
1240 * Returns TRUE on success, or FALSE on failure.
1242 PHP_METHOD(HttpRequest
, setRawPostData
)
1244 char *raw_data
= NULL
;
1246 getObject(http_request_object
, obj
);
1248 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &raw_data
, &data_len
)) {
1256 UPD_STRL(rawPostData
, raw_data
, data_len
);
1261 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1263 * Add raw post data, leaving previously set raw post data unchanged.
1264 * Affects only POST and custom requests.
1266 * Expects a string as parameter containing the raw post data to concatenate.
1268 * Returns TRUE on success, or FALSE on failure.
1270 PHP_METHOD(HttpRequest
, addRawPostData
)
1274 getObject(http_request_object
, obj
);
1276 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &raw_data
, &data_len
)) {
1281 zval
*data
= zval_copy(IS_STRING
, GET_PROP(rawPostData
));
1283 Z_STRVAL_P(data
) = erealloc(Z_STRVAL_P(data
), (Z_STRLEN_P(data
) += data_len
) + 1);
1284 Z_STRVAL_P(data
)[Z_STRLEN_P(data
)] = '\0';
1285 memcpy(Z_STRVAL_P(data
) + Z_STRLEN_P(data
) - data_len
, raw_data
, data_len
);
1286 SET_PROP(rawPostData
, data
);
1294 /* {{{ proto string HttpRequest::getRawPostData()
1296 * Get previously set raw post data.
1298 * Returns a string containing the currently set raw post data.
1300 PHP_METHOD(HttpRequest
, getRawPostData
)
1305 RETURN_PROP(rawPostData
);
1310 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1312 * Add a file to the POST request, leaving prefiously set files unchanged.
1313 * Affects only POST and custom requests. Cannot be used with raw post data.
1315 * Expects a string parameter containing the form element name, and a string
1316 * paremeter containing the path to the file which should be uploaded.
1317 * Additionally accepts an optional string parameter which chould contain
1318 * the content type of the file.
1320 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1321 * primary and a secondary content type part.
1323 PHP_METHOD(HttpRequest
, addPostFile
)
1325 zval
*entry
, *old_post
, *new_post
;
1326 char *name
, *file
, *type
= NULL
;
1327 int name_len
, file_len
, type_len
= 0;
1328 getObject(http_request_object
, obj
);
1330 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1335 HTTP_CHECK_CONTENT_TYPE(type
, RETURN_FALSE
);
1337 type
= "application/x-octetstream";
1338 type_len
= sizeof("application/x-octetstream") - 1;
1341 MAKE_STD_ZVAL(entry
);
1344 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1345 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1346 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1348 MAKE_STD_ZVAL(new_post
);
1349 array_init(new_post
);
1350 old_post
= GET_PROP(postFiles
);
1351 if (Z_TYPE_P(old_post
) == IS_ARRAY
) {
1352 array_copy(old_post
, new_post
);
1354 add_next_index_zval(new_post
, entry
);
1355 SET_PROP(postFiles
, new_post
);
1356 zval_ptr_dtor(&new_post
);
1362 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1364 * Set files to post, overwriting previously set post files.
1365 * Affects only POST and requests. Cannot be used with raw post data.
1367 * Accepts an array containing the files to post. Each entry should be an
1368 * associative array with "name", "file" and "type" keys. If the parameter
1369 * is empty or omitted the post files will be unset.
1371 * Returns TRUE on success, or FALSE on failure.
1373 PHP_METHOD(HttpRequest
, setPostFiles
)
1375 zval
*files
= NULL
, *post
;
1376 getObject(http_request_object
, obj
);
1378 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!/", &files
)) {
1382 MAKE_STD_ZVAL(post
);
1384 if (files
&& (Z_TYPE_P(files
) == IS_ARRAY
)) {
1385 array_copy(files
, post
);
1387 SET_PROP(postFiles
, post
);
1388 zval_ptr_dtor(&post
);
1394 /* {{{ proto array HttpRequest::getPostFiles()
1396 * Get all previously added POST files.
1398 * Returns an array containing currently set post files.
1400 PHP_METHOD(HttpRequest
, getPostFiles
)
1405 RETURN_PROP(postFiles
);
1410 /* {{{ proto bool HttpRequest::setPutFile([string file])
1412 * Set file to put. Affects only PUT requests.
1414 * Accepts a string as parameter referencing the path to file.
1415 * If the parameter is empty or omitted the put file will be unset.
1417 * Returns TRUE on success, or FALSE on failure.
1419 PHP_METHOD(HttpRequest
, setPutFile
)
1423 getObject(http_request_object
, obj
);
1425 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &file
, &file_len
)) {
1429 UPD_STRL(putFile
, file
, file_len
);
1434 /* {{{ proto string HttpRequest::getPutFile()
1436 * Get previously set put file.
1438 * Returns a string containing the path to the currently set put file.
1440 PHP_METHOD(HttpRequest
, getPutFile
)
1445 RETURN_PROP(putFile
);
1450 /* {{{ proto array HttpRequest::getResponseData()
1452 * Get all response data after the request has been sent.
1454 * Returns an associative array with the key "headers" containing an associative
1455 * array holding all response headers, as well as the ley "body" containing a
1456 * string with the response body.
1458 * If redirects were allowed and several responses were received, the data
1459 * references the last received response.
1461 PHP_METHOD(HttpRequest
, getResponseData
)
1466 RETURN_PROP(responseData
);
1471 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1473 * Get response header(s) after the request has been sent.
1475 * Accepts an string as optional parameter specifying a certain header to read.
1476 * If the parameter is empty or omitted all response headers will be returned.
1478 * Returns either a string with the value of the header matching name if requested,
1479 * FALSE on failure, or an associative array containing all reponse headers.
1481 * If redirects were allowed and several responses were received, the data
1482 * references the last received response.
1484 PHP_METHOD(HttpRequest
, getResponseHeader
)
1487 zval
*data
, **headers
, **header
;
1488 char *header_name
= NULL
;
1490 getObject(http_request_object
, obj
);
1492 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1496 data
= GET_PROP(responseData
);
1497 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1498 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) &&
1499 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1500 if (!header_len
|| !header_name
) {
1501 RETVAL_ZVAL(*headers
, 1, 0);
1502 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1503 RETVAL_ZVAL(*header
, 1, 0);
1514 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1516 * Get response cookie(s) after the request has been sent.
1518 * Accepts a string as optional parameter specifying the name of the cookie to read.
1519 * If the parameter is empty or omitted, an associative array with all received
1520 * cookies will be returned.
1522 * Returns either an associative array with the cookie's name, value and any
1523 * additional params of the cookie matching name if requested, FALSE on failure,
1524 * or an array containing all received cookies as arrays.
1526 * If redirects were allowed and several responses were received, the data
1527 * references the last received response.
1529 PHP_METHOD(HttpRequest
, getResponseCookie
)
1532 zval
*data
, **headers
;
1533 char *cookie_name
= NULL
;
1535 getObject(http_request_object
, obj
);
1537 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1541 array_init(return_value
);
1543 data
= GET_PROP(responseData
);
1544 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1545 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) &&
1546 (Z_TYPE_PP(headers
) == IS_ARRAY
)) {
1549 zval
**header
= NULL
;
1552 convert_to_array(*headers
);
1553 FOREACH_HASH_KEYVAL(pos1
, Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1554 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1555 /* several cookies? */
1556 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1560 FOREACH_HASH_VAL(pos2
, Z_ARRVAL_PP(header
), cookie
) {
1562 MAKE_STD_ZVAL(cookie_hash
);
1563 array_init(cookie_hash
);
1565 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1567 add_next_index_zval(return_value
, cookie_hash
);
1571 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1572 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1573 add_next_index_zval(return_value
, cookie_hash
);
1574 return; /* <<< FOUND >>> */
1576 zval_dtor(cookie_hash
);
1581 zval_dtor(cookie_hash
);
1588 MAKE_STD_ZVAL(cookie_hash
);
1589 array_init(cookie_hash
);
1590 convert_to_string_ex(header
);
1592 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1594 add_next_index_zval(return_value
, cookie_hash
);
1598 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1599 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1600 add_next_index_zval(return_value
, cookie_hash
);
1602 zval_dtor(cookie_hash
);
1607 zval_dtor(cookie_hash
);
1621 /* {{{ proto string HttpRequest::getResponseBody()
1623 * Get the response body after the request has been sent.
1625 * Returns a string containing the response body.
1627 * If redirects were allowed and several responses were received, the data
1628 * references the last received response.
1630 PHP_METHOD(HttpRequest
, getResponseBody
)
1636 getObject(http_request_object
, obj
);
1637 zval
*data
= GET_PROP(responseData
);
1639 if ( (Z_TYPE_P(data
) == IS_ARRAY
) &&
1640 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
))) {
1641 RETURN_ZVAL(*body
, 1, 0);
1649 /* {{{ proto int HttpRequest::getResponseCode()
1651 * Get the response code after the request has been sent.
1653 * Returns an int representing the response code.
1655 * If redirects were allowed and several responses were received, the data
1656 * references the last received response.
1658 PHP_METHOD(HttpRequest
, getResponseCode
)
1663 RETURN_PROP(responseCode
);
1668 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1670 * Get response info after the request has been sent.
1671 * See http_get() for a full list of returned info.
1673 * Accepts a string as optional parameter specifying the info to read.
1674 * If the parameter is empty or omitted, an associative array containing
1675 * all available info will be returned.
1677 * Returns either a scalar containing the value of the info matching name if
1678 * requested, FALSE on failure, or an associative array containing all
1681 * If redirects were allowed and several responses were received, the data
1682 * references the last received response.
1684 PHP_METHOD(HttpRequest
, getResponseInfo
)
1687 zval
*info
, **infop
;
1688 char *info_name
= NULL
;
1690 getObject(http_request_object
, obj
);
1692 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1696 info
= GET_PROP(responseInfo
);
1698 if (Z_TYPE_P(info
) != IS_ARRAY
) {
1702 if (info_len
&& info_name
) {
1703 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1704 RETURN_ZVAL(*infop
, 1, 0);
1706 http_error_ex(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Could not find response info named %s", info_name
);
1710 RETURN_ZVAL(info
, 1, 0);
1716 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1718 * Get the full response as HttpMessage object after the request has been sent.
1720 * Returns an HttpMessage object of the response.
1722 * If redirects were allowed and several responses were received, the data
1723 * references the last received response. Use HttpMessage::getParentMessage()
1724 * to access the data of previously received responses whithin this request
1727 * Throws HttpException.
1729 PHP_METHOD(HttpRequest
, getResponseMessage
)
1735 getObject(http_request_object
, obj
);
1737 SET_EH_THROW_HTTP();
1738 message
= GET_PROP(responseMessage
);
1739 if (Z_TYPE_P(message
) == IS_OBJECT
) {
1740 RETVAL_OBJECT(message
);
1749 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1751 * Get sent HTTP message.
1753 * Returns an HttpMessage object representing the sent request.
1755 * If redirects were allowed and several responses were received, the data
1756 * references the last received response. Use HttpMessage::getParentMessage()
1757 * to access the data of previously sent requests whithin this request
1760 * Note that the internal request message is immutable, that means that the
1761 * request message received through HttpRequest::getRequestMessage() will
1762 * always look the same for the same request, regardless of any changes you
1763 * may have made to the returned object.
1765 * Throws HttpMalformedHeadersException, HttpEncodingException.
1767 PHP_METHOD(HttpRequest
, getRequestMessage
)
1773 getObject(http_request_object
, obj
);
1775 SET_EH_THROW_HTTP();
1776 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->request
->conv
.request
), PHPSTR_LEN(&obj
->request
->conv
.request
)))) {
1777 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1784 /* {{{ proto string HttpRequest::getRawRequestMessage()
1786 * Get sent HTTP message.
1788 * Returns an HttpMessage in a form of a string
1791 PHP_METHOD(HttpRequest
, getRawRequestMessage
)
1796 getObject(http_request_object
, obj
);
1798 RETURN_PHPSTR_DUP(&obj
->request
->conv
.request
);
1803 /* {{{ proto string HttpRequest::getRawResponseMessage()
1805 * Get the entire HTTP response.
1807 * Returns the complete web server response, including the headers in a form of a string.
1810 PHP_METHOD(HttpRequest
, getRawResponseMessage
)
1815 getObject(http_request_object
, obj
);
1817 RETURN_PHPSTR_DUP(&obj
->request
->conv
.response
);
1822 /* {{{ proto HttpMessage HttpRequest::getHistory()
1824 * Get all sent requests and received responses as an HttpMessage object.
1826 * If you don't want to record history at all, set the instance variable
1827 * HttpRequest::$recoedHistory to FALSE.
1829 * Returns an HttpMessage object representing the complete request/response
1832 * The object references the last received response, use HttpMessage::getParentMessage()
1833 * to access the data of previously sent requests and received responses.
1835 * Note that the internal history is immutable, that means that any changes
1836 * you make the the message list won't affect a history message list newly
1837 * created by another call to HttpRequest::getHistory().
1839 * Throws HttpMalformedHeaderException, HttpEncodingException.
1841 PHP_METHOD(HttpRequest
, getHistory
)
1847 getObject(http_request_object
, obj
);
1849 SET_EH_THROW_HTTP();
1850 if ((msg
= http_message_parse(PHPSTR_VAL(&obj
->history
), PHPSTR_LEN(&obj
->history
)))) {
1851 ZVAL_OBJVAL(return_value
, http_message_object_new_ex(http_message_object_ce
, msg
, NULL
));
1858 /* {{{ proto void HttpRequest::clearHistory()
1860 * Clear the history.
1862 PHP_METHOD(HttpRequest
, clearHistory
)
1865 getObject(http_request_object
, obj
);
1866 phpstr_dtor(&obj
->history
);
1871 /* {{{ proto HttpMessage HttpRequest::send()
1873 * Send the HTTP request.
1875 * Returns the received response as HttpMessage object.
1877 * NOTE: While an exception may be thrown, the transfer could have succeeded
1878 * at least partially, so you might want to check the return values of various
1879 * HttpRequest::getResponse*() methods.
1881 * Throws HttpRuntimeException, HttpRequestException,
1882 * HttpMalformedHeaderException, HttpEncodingException.
1887 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
1888 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1889 * $r->addQueryData(array('category' => 3));
1892 * if ($r->getResponseCode() == 200) {
1893 * file_put_contents('local.rss', $r->getResponseBody());
1895 * } catch (HttpException $ex) {
1904 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
1905 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1906 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1907 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1909 * echo $r->send()->getBody();
1910 * } catch (HttpException $ex) {
1916 PHP_METHOD(HttpRequest
, send
)
1918 getObject(http_request_object
, obj
);
1922 SET_EH_THROW_HTTP();
1927 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1928 } else if (SUCCESS
== http_request_object_requesthandler(obj
, getThis())) {
1929 http_request_exec(obj
->request
);
1930 if (SUCCESS
== http_request_object_responsehandler(obj
, getThis())) {
1931 RETVAL_OBJECT(GET_PROP(responseMessage
));
1939 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1946 * vim600: noet sw=4 ts=4 fdm=marker
1947 * vim<600: noet sw=4 ts=4