479e0b699978fcffd017a281be207d28ad1995af
[m6w6/ext-http] / http_request_object.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22 #include "php.h"
23
24 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
25
26 #include "zend_interfaces.h"
27
28 #include "php_http_std_defs.h"
29 #include "php_http_request_object.h"
30 #include "php_http_request_api.h"
31 #include "php_http_request_pool_api.h"
32 #include "php_http.h"
33 #include "php_http_api.h"
34 #include "php_http_url_api.h"
35 #include "php_http_message_api.h"
36 #include "php_http_message_object.h"
37 #include "php_http_exception_object.h"
38
39 #include "missing.h"
40
41 #ifdef PHP_WIN32
42 # include <winsock2.h>
43 #endif
44 #include <curl/curl.h>
45
46 ZEND_EXTERN_MODULE_GLOBALS(http);
47
48 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args)
49 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref)
50 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
51 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
52
53 HTTP_EMPTY_ARGS(__destruct, 0);
54 HTTP_BEGIN_ARGS(__construct, 0, 0)
55 HTTP_ARG_VAL(url, 0)
56 HTTP_ARG_VAL(method, 0)
57 HTTP_ARG_VAL(options, 0)
58 HTTP_END_ARGS;
59
60 HTTP_EMPTY_ARGS(getOptions, 0);
61 HTTP_BEGIN_ARGS(setOptions, 0, 0)
62 HTTP_ARG_VAL(options, 0)
63 HTTP_END_ARGS;
64
65 HTTP_EMPTY_ARGS(getSslOptions, 0);
66 HTTP_BEGIN_ARGS(setSslOptions, 0, 0)
67 HTTP_ARG_VAL(ssl_options, 0)
68 HTTP_END_ARGS;
69
70 HTTP_EMPTY_ARGS(getHeaders, 0);
71 HTTP_BEGIN_ARGS(setHeaders, 0, 0)
72 HTTP_ARG_VAL(headers, 0)
73 HTTP_END_ARGS;
74
75 HTTP_BEGIN_ARGS(addHeaders, 0, 1)
76 HTTP_ARG_VAL(headers, 0)
77 HTTP_END_ARGS;
78
79 HTTP_EMPTY_ARGS(getCookies, 0);
80 HTTP_BEGIN_ARGS(setCookies, 0, 0)
81 HTTP_ARG_VAL(cookies, 0)
82 HTTP_END_ARGS;
83
84 HTTP_BEGIN_ARGS(addCookies, 0, 1)
85 HTTP_ARG_VAL(cookies, 0)
86 HTTP_END_ARGS;
87
88 HTTP_EMPTY_ARGS(getUrl, 0);
89 HTTP_BEGIN_ARGS(setUrl, 0, 1)
90 HTTP_ARG_VAL(url, 0)
91 HTTP_END_ARGS;
92
93 HTTP_EMPTY_ARGS(getMethod, 0);
94 HTTP_BEGIN_ARGS(setMethod, 0, 1)
95 HTTP_ARG_VAL(request_method, 0)
96 HTTP_END_ARGS;
97
98 HTTP_EMPTY_ARGS(getContentType, 0);
99 HTTP_BEGIN_ARGS(setContentType, 0, 1)
100 HTTP_ARG_VAL(content_type, 0)
101 HTTP_END_ARGS;
102
103 HTTP_EMPTY_ARGS(getQueryData, 0);
104 HTTP_BEGIN_ARGS(setQueryData, 0, 0)
105 HTTP_ARG_VAL(query_data, 0)
106 HTTP_END_ARGS;
107
108 HTTP_BEGIN_ARGS(addQueryData, 0, 1)
109 HTTP_ARG_VAL(query_data, 0)
110 HTTP_END_ARGS;
111
112 HTTP_EMPTY_ARGS(getPostFields, 0);
113 HTTP_BEGIN_ARGS(setPostFields, 0, 0)
114 HTTP_ARG_VAL(post_fields, 0)
115 HTTP_END_ARGS;
116
117 HTTP_BEGIN_ARGS(addPostFields, 0, 1)
118 HTTP_ARG_VAL(post_fields, 0)
119 HTTP_END_ARGS;
120
121 HTTP_EMPTY_ARGS(getPostFiles, 0);
122 HTTP_BEGIN_ARGS(setPostFiles, 0, 0)
123 HTTP_ARG_VAL(post_files, 0)
124 HTTP_END_ARGS;
125
126 HTTP_BEGIN_ARGS(addPostFile, 0, 2)
127 HTTP_ARG_VAL(formname, 0)
128 HTTP_ARG_VAL(filename, 0)
129 HTTP_ARG_VAL(content_type, 0)
130 HTTP_END_ARGS;
131
132 HTTP_EMPTY_ARGS(getRawPostData, 0);
133 HTTP_BEGIN_ARGS(setRawPostData, 0, 0)
134 HTTP_ARG_VAL(raw_post_data, 0)
135 HTTP_END_ARGS;
136
137 HTTP_BEGIN_ARGS(addRawPostData, 0, 1)
138 HTTP_ARG_VAL(raw_post_data, 0)
139 HTTP_END_ARGS;
140
141 HTTP_EMPTY_ARGS(getPutFile, 0);
142 HTTP_BEGIN_ARGS(setPutFile, 0, 0)
143 HTTP_ARG_VAL(filename, 0)
144 HTTP_END_ARGS;
145
146 HTTP_EMPTY_ARGS(getResponseData, 0);
147 HTTP_BEGIN_ARGS(getResponseHeader, 0, 0)
148 HTTP_ARG_VAL(name, 0)
149 HTTP_END_ARGS;
150
151 HTTP_BEGIN_ARGS(getResponseCookie, 0, 0)
152 HTTP_ARG_VAL(name, 0)
153 HTTP_END_ARGS;
154
155 HTTP_EMPTY_ARGS(getResponseBody, 0);
156 HTTP_EMPTY_ARGS(getResponseCode, 0);
157 HTTP_BEGIN_ARGS(getResponseInfo, 0, 0)
158 HTTP_ARG_VAL(name, 0)
159 HTTP_END_ARGS;
160
161 HTTP_EMPTY_ARGS(getResponseMessage, 1);
162 HTTP_EMPTY_ARGS(getRequestMessage, 1);
163 HTTP_EMPTY_ARGS(getHistory, 1);
164 HTTP_EMPTY_ARGS(clearHistory, 0);
165 HTTP_EMPTY_ARGS(send, 1);
166
167 HTTP_BEGIN_ARGS(get, 0, 1)
168 HTTP_ARG_VAL(url, 0)
169 HTTP_ARG_VAL(options, 0)
170 HTTP_ARG_VAL(info, 1)
171 HTTP_END_ARGS;
172
173 HTTP_BEGIN_ARGS(head, 0, 1)
174 HTTP_ARG_VAL(url, 0)
175 HTTP_ARG_VAL(options, 0)
176 HTTP_ARG_VAL(info, 1)
177 HTTP_END_ARGS;
178
179 HTTP_BEGIN_ARGS(postData, 0, 2)
180 HTTP_ARG_VAL(url, 0)
181 HTTP_ARG_VAL(data, 0)
182 HTTP_ARG_VAL(options, 0)
183 HTTP_ARG_VAL(info, 1)
184 HTTP_END_ARGS;
185
186 HTTP_BEGIN_ARGS(postFields, 0, 2)
187 HTTP_ARG_VAL(url, 0)
188 HTTP_ARG_VAL(data, 0)
189 HTTP_ARG_VAL(options, 0)
190 HTTP_ARG_VAL(info, 1)
191 HTTP_END_ARGS;
192
193 HTTP_BEGIN_ARGS(putFile, 0, 2)
194 HTTP_ARG_VAL(url, 0)
195 HTTP_ARG_VAL(file, 0)
196 HTTP_ARG_VAL(options, 0)
197 HTTP_ARG_VAL(info, 1)
198 HTTP_END_ARGS;
199
200 HTTP_BEGIN_ARGS(putStream, 0, 2)
201 HTTP_ARG_VAL(url, 0)
202 HTTP_ARG_VAL(stream, 0)
203 HTTP_ARG_VAL(options, 0)
204 HTTP_ARG_VAL(info, 1)
205 HTTP_END_ARGS;
206
207 HTTP_BEGIN_ARGS(methodRegister, 0, 1)
208 HTTP_ARG_VAL(method_name, 0)
209 HTTP_END_ARGS;
210
211 HTTP_BEGIN_ARGS(methodUnregister, 0, 1)
212 HTTP_ARG_VAL(method, 0)
213 HTTP_END_ARGS;
214
215 HTTP_BEGIN_ARGS(methodName, 0, 1)
216 HTTP_ARG_VAL(method_id, 0)
217 HTTP_END_ARGS;
218
219 HTTP_BEGIN_ARGS(methodExists, 0, 1)
220 HTTP_ARG_VAL(method, 0)
221 HTTP_END_ARGS;
222
223 #define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
224 static inline void _http_request_object_declare_default_properties(TSRMLS_D);
225
226 zend_class_entry *http_request_object_ce;
227 zend_function_entry http_request_object_fe[] = {
228 HTTP_REQUEST_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
229 HTTP_REQUEST_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
230
231 HTTP_REQUEST_ME(setOptions, ZEND_ACC_PUBLIC)
232 HTTP_REQUEST_ME(getOptions, ZEND_ACC_PUBLIC)
233 HTTP_REQUEST_ME(setSslOptions, ZEND_ACC_PUBLIC)
234 HTTP_REQUEST_ME(getSslOptions, ZEND_ACC_PUBLIC)
235
236 HTTP_REQUEST_ME(addHeaders, ZEND_ACC_PUBLIC)
237 HTTP_REQUEST_ME(getHeaders, ZEND_ACC_PUBLIC)
238 HTTP_REQUEST_ME(setHeaders, ZEND_ACC_PUBLIC)
239
240 HTTP_REQUEST_ME(addCookies, ZEND_ACC_PUBLIC)
241 HTTP_REQUEST_ME(getCookies, ZEND_ACC_PUBLIC)
242 HTTP_REQUEST_ME(setCookies, ZEND_ACC_PUBLIC)
243
244 HTTP_REQUEST_ME(setMethod, ZEND_ACC_PUBLIC)
245 HTTP_REQUEST_ME(getMethod, ZEND_ACC_PUBLIC)
246
247 HTTP_REQUEST_ME(setUrl, ZEND_ACC_PUBLIC)
248 HTTP_REQUEST_ME(getUrl, ZEND_ACC_PUBLIC)
249
250 HTTP_REQUEST_ME(setContentType, ZEND_ACC_PUBLIC)
251 HTTP_REQUEST_ME(getContentType, ZEND_ACC_PUBLIC)
252
253 HTTP_REQUEST_ME(setQueryData, ZEND_ACC_PUBLIC)
254 HTTP_REQUEST_ME(getQueryData, ZEND_ACC_PUBLIC)
255 HTTP_REQUEST_ME(addQueryData, ZEND_ACC_PUBLIC)
256
257 HTTP_REQUEST_ME(setPostFields, ZEND_ACC_PUBLIC)
258 HTTP_REQUEST_ME(getPostFields, ZEND_ACC_PUBLIC)
259 HTTP_REQUEST_ME(addPostFields, ZEND_ACC_PUBLIC)
260
261 HTTP_REQUEST_ME(setRawPostData, ZEND_ACC_PUBLIC)
262 HTTP_REQUEST_ME(getRawPostData, ZEND_ACC_PUBLIC)
263 HTTP_REQUEST_ME(addRawPostData, ZEND_ACC_PUBLIC)
264
265 HTTP_REQUEST_ME(setPostFiles, ZEND_ACC_PUBLIC)
266 HTTP_REQUEST_ME(addPostFile, ZEND_ACC_PUBLIC)
267 HTTP_REQUEST_ME(getPostFiles, ZEND_ACC_PUBLIC)
268
269 HTTP_REQUEST_ME(setPutFile, ZEND_ACC_PUBLIC)
270 HTTP_REQUEST_ME(getPutFile, ZEND_ACC_PUBLIC)
271
272 HTTP_REQUEST_ME(send, ZEND_ACC_PUBLIC)
273
274 HTTP_REQUEST_ME(getResponseData, ZEND_ACC_PUBLIC)
275 HTTP_REQUEST_ME(getResponseHeader, ZEND_ACC_PUBLIC)
276 HTTP_REQUEST_ME(getResponseCookie, ZEND_ACC_PUBLIC)
277 HTTP_REQUEST_ME(getResponseCode, ZEND_ACC_PUBLIC)
278 HTTP_REQUEST_ME(getResponseBody, ZEND_ACC_PUBLIC)
279 HTTP_REQUEST_ME(getResponseInfo, ZEND_ACC_PUBLIC)
280 HTTP_REQUEST_ME(getResponseMessage, ZEND_ACC_PUBLIC)
281 HTTP_REQUEST_ME(getRequestMessage, ZEND_ACC_PUBLIC)
282 HTTP_REQUEST_ME(getHistory, ZEND_ACC_PUBLIC)
283 HTTP_REQUEST_ME(clearHistory, ZEND_ACC_PUBLIC)
284
285 HTTP_REQUEST_ALIAS(get, http_get)
286 HTTP_REQUEST_ALIAS(head, http_head)
287 HTTP_REQUEST_ALIAS(postData, http_post_data)
288 HTTP_REQUEST_ALIAS(postFields, http_post_fields)
289 HTTP_REQUEST_ALIAS(putFile, http_put_file)
290 HTTP_REQUEST_ALIAS(putStream, http_put_stream)
291
292 HTTP_REQUEST_ALIAS(methodRegister, http_request_method_register)
293 HTTP_REQUEST_ALIAS(methodUnregister, http_request_method_unregister)
294 HTTP_REQUEST_ALIAS(methodName, http_request_method_name)
295 HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists)
296
297 EMPTY_FUNCTION_ENTRY
298 };
299 static zend_object_handlers http_request_object_handlers;
300
301 void _http_request_object_init(INIT_FUNC_ARGS)
302 {
303 HTTP_REGISTER_CLASS_EX(HttpRequest, http_request_object, NULL, 0);
304 }
305
306 zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC)
307 {
308 zend_object_value ov;
309 http_request_object *o;
310
311 o = ecalloc(1, sizeof(http_request_object));
312 o->zo.ce = ce;
313 o->ch = curl_easy_init();
314
315 phpstr_init(&o->history);
316 phpstr_init(&o->request);
317 phpstr_init_ex(&o->response, HTTP_CURLBUF_SIZE, 0);
318
319 ALLOC_HASHTABLE(OBJ_PROP(o));
320 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
321 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
322
323 ov.handle = putObject(http_request_object, o);
324 ov.handlers = &http_request_object_handlers;
325
326 return ov;
327 }
328
329 static inline void _http_request_object_declare_default_properties(TSRMLS_D)
330 {
331 zend_class_entry *ce = http_request_object_ce;
332
333 DCL_PROP_N(PROTECTED, options);
334 DCL_PROP_N(PROTECTED, responseInfo);
335 DCL_PROP_N(PROTECTED, responseData);
336 DCL_PROP_N(PROTECTED, responseCode);
337 DCL_PROP_N(PROTECTED, responseMessage);
338 DCL_PROP_N(PROTECTED, postFields);
339 DCL_PROP_N(PROTECTED, postFiles);
340
341 DCL_PROP(PROTECTED, long, method, HTTP_GET);
342
343 DCL_PROP(PROTECTED, string, url, "");
344 DCL_PROP(PROTECTED, string, contentType, "");
345 DCL_PROP(PROTECTED, string, rawPostData, "");
346 DCL_PROP(PROTECTED, string, queryData, "");
347 DCL_PROP(PROTECTED, string, putFile, "");
348
349 DCL_PROP(PUBLIC, bool, recordHistory, 1);
350
351 #ifndef WONKY
352 /*
353 * Request Method Constants
354 */
355 /* HTTP/1.1 */
356 DCL_CONST(long, "METH_GET", HTTP_GET);
357 DCL_CONST(long, "METH_HEAD", HTTP_HEAD);
358 DCL_CONST(long, "METH_POST", HTTP_POST);
359 DCL_CONST(long, "METH_PUT", HTTP_PUT);
360 DCL_CONST(long, "METH_DELETE", HTTP_DELETE);
361 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS);
362 DCL_CONST(long, "METH_TRACE", HTTP_TRACE);
363 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT);
364 /* WebDAV - RFC 2518 */
365 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND);
366 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH);
367 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL);
368 DCL_CONST(long, "METH_COPY", HTTP_COPY);
369 DCL_CONST(long, "METH_MOVE", HTTP_MOVE);
370 DCL_CONST(long, "METH_LOCK", HTTP_LOCK);
371 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK);
372 /* WebDAV Versioning - RFC 3253 */
373 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL);
374 DCL_CONST(long, "METH_REPORT", HTTP_REPORT);
375 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT);
376 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN);
377 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT);
378 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE);
379 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE);
380 DCL_CONST(long, "METH_LABEL", HTTP_LABEL);
381 DCL_CONST(long, "METH_MERGE", HTTP_MERGE);
382 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL);
383 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY);
384 /* WebDAV Access Control - RFC 3744 */
385 DCL_CONST(long, "METH_ACL", HTTP_ACL);
386
387 /*
388 * Auth Constants
389 */
390 # if LIBCURL_VERSION_NUM >= 0x070a05
391 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC);
392 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST);
393 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM);
394 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY);
395 # endif /* LIBCURL_VERSION_NUM */
396 #endif /* WONKY */
397 }
398
399 void _http_request_object_free(zend_object *object TSRMLS_DC)
400 {
401 http_request_object *o = (http_request_object *) object;
402
403 if (OBJ_PROP(o)) {
404 zend_hash_destroy(OBJ_PROP(o));
405 FREE_HASHTABLE(OBJ_PROP(o));
406 }
407 if (o->ch) {
408 /* avoid nasty segfaults with already cleaned up callbacks */
409 curl_easy_setopt(o->ch, CURLOPT_NOPROGRESS, 1);
410 curl_easy_setopt(o->ch, CURLOPT_PROGRESSFUNCTION, NULL);
411 curl_easy_setopt(o->ch, CURLOPT_VERBOSE, 0);
412 curl_easy_setopt(o->ch, CURLOPT_DEBUGFUNCTION, NULL);
413 curl_easy_cleanup(o->ch);
414 }
415 phpstr_dtor(&o->response);
416 phpstr_dtor(&o->request);
417 phpstr_dtor(&o->history);
418 efree(o);
419 }
420
421 STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ptr, http_request_body *body TSRMLS_DC)
422 {
423 zval *meth, *URL;
424 char *request_uri;
425 STATUS status = SUCCESS;
426
427 if (!body) {
428 return FAILURE;
429 }
430 HTTP_CHECK_CURL_INIT(obj->ch, curl_easy_init(), return FAILURE);
431
432 URL = convert_to_type_ex(IS_STRING, GET_PROP(obj, url));
433 // HTTP_URI_MAXLEN+1 long char *
434 if (!(request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0))) {
435 return FAILURE;
436 }
437
438 meth = convert_to_type_ex(IS_LONG, GET_PROP(obj, method));
439 switch (Z_LVAL_P(meth))
440 {
441 case HTTP_GET:
442 case HTTP_HEAD:
443 body->type = -1;
444 body = NULL;
445 break;
446
447 case HTTP_PUT:
448 {
449 php_stream_statbuf ssb;
450 php_stream *stream = php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj, putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
451
452 if (stream && !php_stream_stat(stream, &ssb)) {
453 body->type = HTTP_REQUEST_BODY_UPLOADFILE;
454 body->data = stream;
455 body->size = ssb.sb.st_size;
456 } else {
457 status = FAILURE;
458 }
459 }
460 break;
461
462 case HTTP_POST:
463 default:
464 {
465 /* check for raw post data */
466 zval *raw_data = convert_to_type_ex(IS_STRING, GET_PROP(obj, rawPostData));
467
468 if (Z_STRLEN_P(raw_data)) {
469 zval *ctype = convert_to_type_ex(IS_STRING, GET_PROP(obj, contentType));
470
471 if (Z_STRLEN_P(ctype)) {
472 zval **headers, *opts = GET_PROP(obj, options);
473
474 convert_to_array(opts);
475
476 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
477 zval **ct_header;
478
479 convert_to_array(*headers);
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)) {
482 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
483 }
484 } else {
485 zval *headers;
486
487 MAKE_STD_ZVAL(headers);
488 array_init(headers);
489 add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
490 add_assoc_zval(opts, "headers", headers);
491 }
492 }
493
494 body->type = HTTP_REQUEST_BODY_CSTRING;
495 body->data = estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data));
496 body->size = Z_STRLEN_P(raw_data);
497 } else {
498 status = http_request_body_fill(body, Z_ARRVAL_P(GET_PROP(obj, postFields)), Z_ARRVAL_P(GET_PROP(obj, postFiles)));
499 }
500 }
501 break;
502 }
503
504 if (status == SUCCESS) {
505 zval *qdata = convert_to_type_ex(IS_STRING, GET_PROP(obj, queryData));
506
507 if (Z_STRLEN_P(qdata)) {
508 if (!strchr(request_uri, '?')) {
509 strlcat(request_uri, "?", HTTP_URI_MAXLEN);
510 } else {
511 strlcat(request_uri, "&", HTTP_URI_MAXLEN);
512 }
513 strlcat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN);
514 }
515
516 status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, body, Z_ARRVAL_P(GET_PROP(obj, options)));
517 }
518 efree(request_uri);
519
520 /* clean previous response */
521 phpstr_dtor(&obj->response);
522 /* clean previous request */
523 phpstr_dtor(&obj->request);
524
525 return status;
526 }
527
528 STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
529 {
530 http_message *msg;
531
532 phpstr_fix(&obj->request);
533 phpstr_fix(&obj->response);
534
535 msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response));
536
537 if (!msg) {
538 return FAILURE;
539 } else {
540 char *body;
541 size_t body_len;
542 zval *headers, *message,
543 *resp = convert_to_type(IS_ARRAY, GET_PROP(obj, responseData)),
544 *info = convert_to_type(IS_ARRAY, GET_PROP(obj, responseInfo));
545
546 if (zval_is_true(GET_PROP(obj, recordHistory))) {
547 /* we need to act like a zipper, as we'll receive
548 * the requests and the responses in separate chains
549 * for redirects
550 */
551 http_message *response = msg, *request = http_message_parse(PHPSTR_VAL(&obj->request), PHPSTR_LEN(&obj->request));
552 http_message *free_msg = request;
553
554 do {
555 char *message;
556 size_t msglen;
557
558 http_message_tostring(response, &message, &msglen);
559 phpstr_append(&obj->history, message, msglen);
560 efree(message);
561
562 http_message_tostring(request, &message, &msglen);
563 phpstr_append(&obj->history, message, msglen);
564 efree(message);
565
566 } while ((response = response->parent) && (request = request->parent));
567
568 http_message_free(&free_msg);
569 phpstr_fix(&obj->history);
570 }
571
572 UPD_PROP(obj, long, responseCode, msg->http.info.response.code);
573
574 MAKE_STD_ZVAL(headers)
575 array_init(headers);
576
577 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
578 phpstr_data(PHPSTR(msg), &body, &body_len);
579
580 add_assoc_zval(resp, "headers", headers);
581 add_assoc_stringl(resp, "body", body, body_len, 0);
582
583 MAKE_STD_ZVAL(message);
584 ZVAL_OBJVAL(message, http_message_object_from_msg(msg));
585 SET_PROP(obj, responseMessage, message);
586 zval_ptr_dtor(&message);
587
588 http_request_info(obj->ch, Z_ARRVAL_P(info));
589 SET_PROP(obj, responseInfo, info);
590
591 return SUCCESS;
592 }
593 }
594
595 #define http_request_object_set_options_subr(key, ow) \
596 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
597 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite)
598 {
599 zval *opts, **options, *new_options = NULL;
600 getObject(http_request_object, obj);
601
602 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &new_options)) {
603 RETURN_FALSE;
604 }
605
606 opts = convert_to_type(IS_ARRAY, GET_PROP(obj, options));
607
608 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options)) {
609 convert_to_array(*options);
610 if (overwrite) {
611 zend_hash_clean(Z_ARRVAL_PP(options));
612 }
613 if (new_options && zend_hash_num_elements(Z_ARRVAL_P(new_options))) {
614 if (overwrite) {
615 array_copy(new_options, *options);
616 } else {
617 array_merge(new_options, *options);
618 }
619 }
620 } else if (new_options && zend_hash_num_elements(Z_ARRVAL_P(new_options))) {
621 zval_add_ref(&new_options);
622 add_assoc_zval(opts, key, new_options);
623 }
624
625 RETURN_TRUE;
626 }
627
628 #define http_request_object_get_options_subr(key) \
629 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
630 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
631 {
632 NO_ARGS;
633
634 IF_RETVAL_USED {
635 zval *opts, **options;
636 getObject(http_request_object, obj);
637
638 opts = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, options));
639
640 array_init(return_value);
641
642 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options)) {
643 convert_to_array(*options);
644 array_copy(*options, return_value);
645 }
646 }
647 }
648
649
650 /* ### USERLAND ### */
651
652 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
653 *
654 * Instantiate a new HttpRequest object.
655 *
656 * Accepts a string as optional parameter containing the target request url.
657 * Additianally accepts an optional int parameter specifying the request method
658 * to use and an associative array as optional third parameter which will be
659 * passed to HttpRequest::setOptions().
660 *
661 * Throws HttpException.
662 */
663 PHP_METHOD(HttpRequest, __construct)
664 {
665 char *URL = NULL;
666 int URL_len;
667 long meth = -1;
668 zval *options = NULL;
669 getObject(http_request_object, obj);
670
671 SET_EH_THROW_HTTP();
672 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla", &URL, &URL_len, &meth, &options)) {
673 INIT_PARR(obj, options);
674 INIT_PARR(obj, responseInfo);
675 INIT_PARR(obj, responseData);
676 INIT_PARR(obj, postFields);
677 INIT_PARR(obj, postFiles);
678
679 if (URL) {
680 UPD_STRL(obj, url, URL, URL_len);
681 }
682 if (meth > -1) {
683 UPD_PROP(obj, long, method, meth);
684 }
685 if (options) {
686 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
687 }
688 }
689 SET_EH_NORMAL();
690 }
691 /* }}} */
692
693 /* {{{ proto void HttpRequest::__destruct()
694 *
695 * Destroys the HttpRequest object.
696 */
697 PHP_METHOD(HttpRequest, __destruct)
698 {
699 getObject(http_request_object, obj);
700
701 NO_ARGS;
702
703 FREE_PARR(obj, options);
704 FREE_PARR(obj, responseInfo);
705 FREE_PARR(obj, responseData);
706 FREE_PARR(obj, postFields);
707 FREE_PARR(obj, postFiles);
708 }
709 /* }}} */
710
711 /* {{{ proto bool HttpRequest::setOptions([array options])
712 *
713 * Set the request options to use. See http_get() for a full list of available options.
714 *
715 * Accepts an array as optional parameters, wich values will overwrite the
716 * currently set request options. If the parameter is empty or mitted,
717 * the optoions of the HttpRequest object will be reset.
718 *
719 * Returns TRUE on success, or FALSE on failure.
720 */
721 PHP_METHOD(HttpRequest, setOptions)
722 {
723 char *key = NULL;
724 ulong idx = 0;
725 zval *opts = NULL, *old_opts, **opt;
726 getObject(http_request_object, obj);
727
728 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
729 RETURN_FALSE;
730 }
731
732 old_opts = convert_to_type(IS_ARRAY, GET_PROP(obj, options));
733
734 if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
735 zend_hash_clean(Z_ARRVAL_P(old_opts));
736 RETURN_TRUE;
737 }
738
739 /* some options need extra attention -- thus cannot use array_merge() directly */
740 FOREACH_KEYVAL(opts, key, idx, opt) {
741 if (key) {
742 if (!strcmp(key, "headers")) {
743 zval **headers;
744 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
745 convert_to_array(*opt);
746 convert_to_array(*headers);
747 array_merge(*opt, *headers);
748 continue;
749 }
750 } else if (!strcmp(key, "cookies")) {
751 zval **cookies;
752 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
753 convert_to_array(*opt);
754 convert_to_array(*cookies);
755 array_merge(*opt, *cookies);
756 continue;
757 }
758 } else if (!strcmp(key, "ssl")) {
759 zval **ssl;
760 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl)) {
761 convert_to_array(*opt);
762 convert_to_array(*ssl);
763 array_merge(*opt, *ssl);
764 continue;
765 }
766 } else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) {
767 if (Z_TYPE_PP(opt) != IS_STRING) {
768 convert_to_string_ex(opt);
769 }
770 UPD_STRL(obj, url, Z_STRVAL_PP(opt), Z_STRLEN_PP(opt));
771 continue;
772 } else if (!strcmp(key, "method")) {
773 if (Z_TYPE_PP(opt) != IS_LONG) {
774 convert_to_long_ex(opt);
775 }
776 UPD_PROP(obj, long, method, Z_LVAL_PP(opt));
777 continue;
778 }
779
780 zval_add_ref(opt);
781 add_assoc_zval(old_opts, key, *opt);
782
783 /* reset */
784 key = NULL;
785 }
786 }
787
788 RETURN_TRUE;
789 }
790 /* }}} */
791
792 /* {{{ proto array HttpRequest::getOptions()
793 *
794 * Get currently set options.
795 *
796 * Returns an associative array containing currently set options.
797 */
798 PHP_METHOD(HttpRequest, getOptions)
799 {
800 NO_ARGS;
801
802 IF_RETVAL_USED {
803 zval *opts;
804 getObject(http_request_object, obj);
805
806 opts = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, options));
807 array_init(return_value);
808 array_copy(opts, return_value);
809 }
810 }
811 /* }}} */
812
813 /* {{{ proto bool HttpRequest::setSslOptions([array options])
814 *
815 * Set SSL options.
816 *
817 * Accepts an associative array as parameter containing any SSL specific options.
818 * If the parameter is empty or omitted, the SSL options will be reset.
819 *
820 * Returns TRUE on success, or FALSE on failure.
821 */
822 PHP_METHOD(HttpRequest, setSslOptions)
823 {
824 http_request_object_set_options_subr("ssl", 1);
825 }
826 /* }}} */
827
828 /* {{{ proto bool HttpRequest::addSslOptions(array options)
829 *
830 * Set additional SSL options.
831 *
832 * Expects an associative array as parameter containing additional SSL specific options.
833 *
834 * Returns TRUE on success, or FALSE on failure.
835 */
836 PHP_METHOD(HttpRequest, addSslOptions)
837 {
838 http_request_object_set_options_subr("ssl", 0);
839 }
840 /* }}} */
841
842 /* {{{ proto array HttpRequest::getSslOtpions()
843 *
844 * Get previously set SSL options.
845 *
846 * Returns an associative array containing any previously set SSL options.
847 */
848 PHP_METHOD(HttpRequest, getSslOptions)
849 {
850 http_request_object_get_options_subr("ssl");
851 }
852 /* }}} */
853
854 /* {{{ proto bool HttpRequest::addHeaders(array headers)
855 *
856 * Add request header name/value pairs.
857 *
858 * Expects an ssociative array as parameter containing additional header
859 * name/value pairs.
860 *
861 * Returns TRUE on success, or FALSE on failure.
862 */
863 PHP_METHOD(HttpRequest, addHeaders)
864 {
865 http_request_object_set_options_subr("headers", 0);
866 }
867
868 /* {{{ proto bool HttpRequest::setHeaders([array headers])
869 *
870 * Set request header name/value pairs.
871 *
872 * Accepts an associative array as parameter containing header name/value pairs.
873 * If the parameter is empty or omitted, all previously set headers will be unset.
874 *
875 * Returns TRUE on success, or FALSE on failure.
876 */
877 PHP_METHOD(HttpRequest, setHeaders)
878 {
879 http_request_object_set_options_subr("headers", 1);
880 }
881 /* }}} */
882
883 /* {{{ proto array HttpRequest::getHeaders()
884 *
885 * Get previously set request headers.
886 *
887 * Returns an associative array containing all currently set headers.
888 */
889 PHP_METHOD(HttpRequest, getHeaders)
890 {
891 http_request_object_get_options_subr("headers");
892 }
893 /* }}} */
894
895 /* {{{ proto bool HttpRequest::setCookies([array cookies])
896 *
897 * Set cookies.
898 *
899 * Accepts an associative array as parameter containing cookie name/value pairs.
900 * If the parameter is empty or omitted, all previously set cookies will be unset.
901 *
902 * Returns TRUE on success, or FALSE on failure.
903 */
904 PHP_METHOD(HttpRequest, setCookies)
905 {
906 http_request_object_set_options_subr("cookies", 1);
907 }
908 /* }}} */
909
910 /* {{{ proto bool HttpRequest::addCookies(array cookies)
911 *
912 * Add cookies.
913 *
914 * Expects an associative array as parameter containing any cookie name/value
915 * pairs to add.
916 *
917 * Returns TRUE on success, or FALSE on failure.
918 */
919 PHP_METHOD(HttpRequest, addCookies)
920 {
921 http_request_object_set_options_subr("cookies", 0);
922 }
923 /* }}} */
924
925 /* {{{ proto array HttpRequest::getCookies()
926 *
927 * Get previously set cookies.
928 *
929 * Returns an associative array containing any previously set cookies.
930 */
931 PHP_METHOD(HttpRequest, getCookies)
932 {
933 http_request_object_get_options_subr("cookies");
934 }
935 /* }}} */
936
937 /* {{{ proto bool HttpRequest::setUrl(string url)
938 *
939 * Set the request URL.
940 *
941 * Expects a string as parameter specifying the request url.
942 *
943 * Returns TRUE on success, or FALSE on failure.
944 */
945 PHP_METHOD(HttpRequest, setUrl)
946 {
947 char *URL = NULL;
948 int URL_len;
949 getObject(http_request_object, obj);
950
951 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
952 RETURN_FALSE;
953 }
954
955 UPD_STRL(obj, url, URL, URL_len);
956 RETURN_TRUE;
957 }
958 /* }}} */
959
960 /* {{{ proto string HttpRequest::getUrl()
961 *
962 * Get the previously set request URL.
963 *
964 * Returns the currently set request url as string.
965 */
966 PHP_METHOD(HttpRequest, getUrl)
967 {
968 NO_ARGS;
969
970 IF_RETVAL_USED {
971 getObject(http_request_object, obj);
972 zval *URL = GET_PROP(obj, url);
973
974 RETURN_ZVAL(URL, 1, 0);
975 }
976 }
977 /* }}} */
978
979 /* {{{ proto bool HttpRequest::setMethod(int request_method)
980 *
981 * Set the request method.
982 *
983 * Expects an int as parameter specifying the request method to use.
984 * In PHP 5.1+ HttpRequest::METH, otherwise the HTTP_METH constants can be used.
985 *
986 * Returns TRUE on success, or FALSE on failure.
987 */
988 PHP_METHOD(HttpRequest, setMethod)
989 {
990 long meth;
991 getObject(http_request_object, obj);
992
993 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
994 RETURN_FALSE;
995 }
996
997 UPD_PROP(obj, long, method, meth);
998 RETURN_TRUE;
999 }
1000 /* }}} */
1001
1002 /* {{{ proto int HttpRequest::getMethod()
1003 *
1004 * Get the previously set request method.
1005 *
1006 * Returns the currently set request method.
1007 */
1008 PHP_METHOD(HttpRequest, getMethod)
1009 {
1010 NO_ARGS;
1011
1012 IF_RETVAL_USED {
1013 getObject(http_request_object, obj);
1014 zval *meth = GET_PROP(obj, method);
1015
1016 RETURN_ZVAL(meth, 1, 0);
1017 }
1018 }
1019 /* }}} */
1020
1021 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1022 *
1023 * Set the content type the post request should have.
1024 *
1025 * Expects a string as parameters containing the content type of the request
1026 * (primary/secondary).
1027 *
1028 * Returns TRUE on success, or FALSE if the content type does not seem to
1029 * contain a primary and a secondary part.
1030 */
1031 PHP_METHOD(HttpRequest, setContentType)
1032 {
1033 char *ctype;
1034 int ct_len;
1035 getObject(http_request_object, obj);
1036
1037 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1038 RETURN_FALSE;
1039 }
1040
1041 HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
1042 UPD_STRL(obj, contentType, ctype, ct_len);
1043 RETURN_TRUE;
1044 }
1045 /* }}} */
1046
1047 /* {{{ proto string HttpRequest::getContentType()
1048 *
1049 * Get the previously content type.
1050 *
1051 * Returns the previously set content type as string.
1052 */
1053 PHP_METHOD(HttpRequest, getContentType)
1054 {
1055 NO_ARGS;
1056
1057 IF_RETVAL_USED {
1058 getObject(http_request_object, obj);
1059 zval *ctype = GET_PROP(obj, contentType);
1060
1061 RETURN_ZVAL(ctype, 1, 0);
1062 }
1063 }
1064 /* }}} */
1065
1066 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1067 *
1068 * Set the URL query parameters to use, overwriting previously set query parameters.
1069 * Affects any request types.
1070 *
1071 * Accepts a string or associative array parameter containing the pre-encoded
1072 * query string or to be encoded query fields. If the parameter is empty or
1073 * omitted, the query data will be unset.
1074 *
1075 * Returns TRUE on success, or FALSE on failure.
1076 */
1077 PHP_METHOD(HttpRequest, setQueryData)
1078 {
1079 zval *qdata = NULL;
1080 getObject(http_request_object, obj);
1081
1082 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
1083 RETURN_FALSE;
1084 }
1085
1086 if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
1087 UPD_STRL(obj, queryData, "", 0);
1088 } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1089 char *query_data = NULL;
1090
1091 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1092 RETURN_FALSE;
1093 }
1094
1095 UPD_PROP(obj, string, queryData, query_data);
1096 efree(query_data);
1097 } else {
1098 convert_to_string_ex(&qdata);
1099 UPD_STRL(obj, queryData, Z_STRVAL_P(qdata), Z_STRLEN_P(qdata));
1100 }
1101 RETURN_TRUE;
1102 }
1103 /* }}} */
1104
1105 /* {{{ proto string HttpRequest::getQueryData()
1106 *
1107 * Get the current query data in form of an urlencoded query string.
1108 *
1109 * Returns a string containing the urlencoded query.
1110 */
1111 PHP_METHOD(HttpRequest, getQueryData)
1112 {
1113 NO_ARGS;
1114
1115 IF_RETVAL_USED {
1116 getObject(http_request_object, obj);
1117 zval *qdata = convert_to_type_ex(IS_STRING, GET_PROP(obj, queryData));
1118
1119 RETURN_ZVAL(qdata, 1, 0);
1120 }
1121 }
1122 /* }}} */
1123
1124 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1125 *
1126 * Add parameters to the query parameter list, leaving previously set unchanged.
1127 * Affects any request type.
1128 *
1129 * Expects an associative array as parameter containing the query fields to add.
1130 *
1131 * Returns TRUE on success, or FALSE on failure.
1132 */
1133 PHP_METHOD(HttpRequest, addQueryData)
1134 {
1135 zval *qdata, *old_qdata;
1136 char *query_data = NULL;
1137 size_t query_data_len = 0;
1138 getObject(http_request_object, obj);
1139
1140 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1141 RETURN_FALSE;
1142 }
1143
1144 old_qdata = convert_to_type_ex(IS_STRING, GET_PROP(obj, queryData));
1145
1146 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)) {
1147 RETURN_FALSE;
1148 }
1149
1150 UPD_STRL(obj, queryData, query_data, query_data_len);
1151 efree(query_data);
1152
1153 RETURN_TRUE;
1154 }
1155 /* }}} */
1156
1157 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1158 *
1159 * Adds POST data entries, leaving previously set unchanged, unless a
1160 * post entry with the same name already exists.
1161 * Affects only POST and custom requests.
1162 *
1163 * Expects an associative array as parameter containing the post fields.
1164 *
1165 * Returns TRUE on success, or FALSE on failure.
1166 */
1167 PHP_METHOD(HttpRequest, addPostFields)
1168 {
1169 zval *post, *post_data;
1170 getObject(http_request_object, obj);
1171
1172 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1173 RETURN_FALSE;
1174 }
1175
1176 post = convert_to_type(IS_ARRAY, GET_PROP(obj, postFields));
1177 array_merge(post_data, post);
1178
1179 RETURN_TRUE;
1180 }
1181 /* }}} */
1182
1183 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1184 *
1185 * Set the POST data entries, overwriting previously set POST data.
1186 * Affects only POST and custom requests.
1187 *
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.
1190 *
1191 * Returns TRUE on success, or FALSE on failure.
1192 */
1193 PHP_METHOD(HttpRequest, setPostFields)
1194 {
1195 zval *post, *post_data = NULL;
1196 getObject(http_request_object, obj);
1197
1198 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!", &post_data)) {
1199 RETURN_FALSE;
1200 }
1201
1202 post = convert_to_type(IS_ARRAY, GET_PROP(obj, postFields));
1203 zend_hash_clean(Z_ARRVAL_P(post));
1204
1205 if (post_data && zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1206 array_copy(post_data, post);
1207 }
1208
1209 RETURN_TRUE;
1210 }
1211 /* }}}*/
1212
1213 /* {{{ proto array HttpRequest::getPostFields()
1214 *
1215 * Get previously set POST data.
1216 *
1217 * Returns the currently set post fields as associative array.
1218 */
1219 PHP_METHOD(HttpRequest, getPostFields)
1220 {
1221 NO_ARGS;
1222
1223 IF_RETVAL_USED {
1224 getObject(http_request_object, obj);
1225 zval *post_data = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, postFields));
1226
1227 array_init(return_value);
1228 array_copy(post_data, return_value);
1229 }
1230 }
1231 /* }}} */
1232
1233 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1234 *
1235 * Set raw post data to send, overwriting previously set raw post data. Don't
1236 * forget to specify a content type. Affects only POST and custom requests.
1237 * Only either post fields or raw post data can be used for each request.
1238 * Raw post data has higher precedence and will be used even if post fields
1239 * are set.
1240 *
1241 * Accepts a string as parameter containing the *raw* post data.
1242 *
1243 * Returns TRUE on success, or FALSE on failure.
1244 */
1245 PHP_METHOD(HttpRequest, setRawPostData)
1246 {
1247 char *raw_data = NULL;
1248 int data_len = 0;
1249 getObject(http_request_object, obj);
1250
1251 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &raw_data, &data_len)) {
1252 RETURN_FALSE;
1253 }
1254
1255 if (!raw_data) {
1256 raw_data = "";
1257 }
1258
1259 UPD_STRL(obj, rawPostData, raw_data, data_len);
1260 RETURN_TRUE;
1261 }
1262 /* }}} */
1263
1264 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1265 *
1266 * Add raw post data, leaving previously set raw post data unchanged.
1267 * Affects only POST and custom requests.
1268 *
1269 * Expects a string as parameter containing the raw post data to concatenate.
1270 *
1271 * Returns TRUE on success, or FALSE on failure.
1272 */
1273 PHP_METHOD(HttpRequest, addRawPostData)
1274 {
1275 char *raw_data, *new_data;
1276 int data_len;
1277 getObject(http_request_object, obj);
1278
1279 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw_data, &data_len)) {
1280 RETURN_FALSE;
1281 }
1282
1283 if (data_len) {
1284 zval *zdata = convert_to_type_ex(IS_STRING, GET_PROP(obj, rawPostData));
1285
1286 new_data = emalloc(Z_STRLEN_P(zdata) + data_len + 1);
1287 new_data[Z_STRLEN_P(zdata) + data_len] = '\0';
1288
1289 if (Z_STRLEN_P(zdata)) {
1290 memcpy(new_data, Z_STRVAL_P(zdata), Z_STRLEN_P(zdata));
1291 }
1292
1293 memcpy(new_data + Z_STRLEN_P(zdata), raw_data, data_len);
1294 UPD_STRL(obj, rawPostData, new_data, Z_STRLEN_P(zdata) + data_len);
1295 }
1296
1297 RETURN_TRUE;
1298 }
1299 /* }}} */
1300
1301 /* {{{ proto string HttpRequest::getRawPostData()
1302 *
1303 * Get previously set raw post data.
1304 *
1305 * Returns a string containing the currently set raw post data.
1306 */
1307 PHP_METHOD(HttpRequest, getRawPostData)
1308 {
1309 NO_ARGS;
1310
1311 IF_RETVAL_USED {
1312 getObject(http_request_object, obj);
1313 zval *raw_data = convert_to_type_ex(IS_STRING, GET_PROP(obj, rawPostData));
1314
1315 RETURN_ZVAL(raw_data, 1, 0);
1316 }
1317 }
1318 /* }}} */
1319
1320 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1321 *
1322 * Add a file to the POST request, leaving prefiously set files unchanged.
1323 * Affects only POST and custom requests. Cannot be used with raw post data.
1324 *
1325 * Expects a string parameter containing the form element name, and a string
1326 * paremeter containing the path to the file which should be uploaded.
1327 * Additionally accepts an optional string parameter which chould contain
1328 * the content type of the file.
1329 *
1330 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1331 * primary and a secondary content type part.
1332 */
1333 PHP_METHOD(HttpRequest, addPostFile)
1334 {
1335 zval *files, *entry;
1336 char *name, *file, *type = NULL;
1337 int name_len, file_len, type_len = 0;
1338 getObject(http_request_object, obj);
1339
1340 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1341 RETURN_FALSE;
1342 }
1343
1344 if (type_len) {
1345 HTTP_CHECK_CONTENT_TYPE(type, RETURN_FALSE);
1346 } else {
1347 type = "application/x-octetstream";
1348 type_len = sizeof("application/x-octetstream") - 1;
1349 }
1350
1351 MAKE_STD_ZVAL(entry);
1352 array_init(entry);
1353
1354 add_assoc_stringl(entry, "name", name, name_len, 1);
1355 add_assoc_stringl(entry, "type", type, type_len, 1);
1356 add_assoc_stringl(entry, "file", file, file_len, 1);
1357
1358 files = convert_to_type(IS_ARRAY, GET_PROP(obj, postFiles));
1359 add_next_index_zval(files, entry);
1360
1361 RETURN_TRUE;
1362 }
1363 /* }}} */
1364
1365 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1366 *
1367 * Set files to post, overwriting previously set post files.
1368 * Affects only POST and requests. Cannot be used with raw post data.
1369 *
1370 * Accepts an array containing the files to post. Each entry should be an
1371 * associative array with "name", "file" and "type" keys. If the parameter
1372 * is empty or omitted the post files will be unset.
1373 *
1374 * Returns TRUE on success, or FALSE on failure.
1375 */
1376 PHP_METHOD(HttpRequest, setPostFiles)
1377 {
1378 zval *files, *pFiles;
1379 getObject(http_request_object, obj);
1380
1381 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &files)) {
1382 RETURN_FALSE;
1383 }
1384
1385 pFiles = convert_to_type(IS_ARRAY, GET_PROP(obj, postFiles));
1386 zend_hash_clean(Z_ARRVAL_P(pFiles));
1387
1388 if (files && zend_hash_num_elements(Z_ARRVAL_P(files))) {
1389 array_copy(files, pFiles);
1390 }
1391
1392 RETURN_TRUE;
1393 }
1394 /* }}} */
1395
1396 /* {{{ proto array HttpRequest::getPostFiles()
1397 *
1398 * Get all previously added POST files.
1399 *
1400 * Returns an array containing currently set post files.
1401 */
1402 PHP_METHOD(HttpRequest, getPostFiles)
1403 {
1404 NO_ARGS;
1405
1406 IF_RETVAL_USED {
1407 getObject(http_request_object, obj);
1408 zval *files = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, postFiles));
1409
1410 array_init(return_value);
1411 array_copy(files, return_value);
1412 }
1413 }
1414 /* }}} */
1415
1416 /* {{{ proto bool HttpRequest::setPutFile([string file])
1417 *
1418 * Set file to put. Affects only PUT requests.
1419 *
1420 * Accepts a string as parameter referencing the path to file.
1421 * If the parameter is empty or omitted the put file will be unset.
1422 *
1423 * Returns TRUE on success, or FALSE on failure.
1424 */
1425 PHP_METHOD(HttpRequest, setPutFile)
1426 {
1427 char *file = "";
1428 int file_len = 0;
1429 getObject(http_request_object, obj);
1430
1431 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &file, &file_len)) {
1432 RETURN_FALSE;
1433 }
1434
1435 UPD_STRL(obj, putFile, file, file_len);
1436 RETURN_TRUE;
1437 }
1438 /* }}} */
1439
1440 /* {{{ proto string HttpRequest::getPutFile()
1441 *
1442 * Get previously set put file.
1443 *
1444 * Returns a string containing the path to the currently set put file.
1445 */
1446 PHP_METHOD(HttpRequest, getPutFile)
1447 {
1448 NO_ARGS;
1449
1450 IF_RETVAL_USED {
1451 getObject(http_request_object, obj);
1452 zval *putfile = convert_to_type_ex(IS_STRING, GET_PROP(obj, putFile));
1453
1454 RETURN_ZVAL(putfile, 1, 0);
1455 }
1456 }
1457 /* }}} */
1458
1459 /* {{{ proto array HttpRequest::getResponseData()
1460 *
1461 * Get all response data after the request has been sent.
1462 *
1463 * Returns an associative array with the key "headers" containing an associative
1464 * array holding all response headers, as well as the ley "body" containing a
1465 * string with the response body.
1466 *
1467 * If redirects were allowed and several responses were received, the data
1468 * references the last received response.
1469 */
1470 PHP_METHOD(HttpRequest, getResponseData)
1471 {
1472 NO_ARGS;
1473
1474 IF_RETVAL_USED {
1475 getObject(http_request_object, obj);
1476 zval *data = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, responseData));
1477
1478 array_init(return_value);
1479 array_copy(data, return_value);
1480 }
1481 }
1482 /* }}} */
1483
1484 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1485 *
1486 * Get response header(s) after the request has been sent.
1487 *
1488 * Accepts an string as optional parameter specifying a certain header to read.
1489 * If the parameter is empty or omitted all response headers will be returned.
1490 *
1491 * Returns either a string with the value of the header matching name if requested,
1492 * FALSE on failure, or an associative array containing all reponse headers.
1493 *
1494 * If redirects were allowed and several responses were received, the data
1495 * references the last received response.
1496 */
1497 PHP_METHOD(HttpRequest, getResponseHeader)
1498 {
1499 IF_RETVAL_USED {
1500 zval *data, **headers, **header;
1501 char *header_name = NULL;
1502 int header_len = 0;
1503 getObject(http_request_object, obj);
1504
1505 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1506 RETURN_FALSE;
1507 }
1508
1509 data = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, responseData));
1510 if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1511 RETURN_FALSE;
1512 }
1513 convert_to_array_ex(headers);
1514 if (!header_len || !header_name) {
1515 array_init(return_value);
1516 array_copy(*headers, return_value);
1517 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1518 RETURN_ZVAL(*header, 1, 0);
1519 } else {
1520 RETURN_FALSE;
1521 }
1522 }
1523 }
1524 /* }}} */
1525
1526 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1527 *
1528 * Get response cookie(s) after the request has been sent.
1529 *
1530 * Accepts a string as optional parameter specifying the name of the cookie to read.
1531 * If the parameter is empty or omitted, an associative array with all received
1532 * cookies will be returned.
1533 *
1534 * Returns either an associative array with the cookie's name, value and any
1535 * additional params of the cookie matching name if requested, FALSE on failure,
1536 * or an array containing all received cookies as arrays.
1537 *
1538 * If redirects were allowed and several responses were received, the data
1539 * references the last received response.
1540 */
1541 PHP_METHOD(HttpRequest, getResponseCookie)
1542 {
1543 IF_RETVAL_USED {
1544 zval *data, **headers;
1545 char *cookie_name = NULL;
1546 int cookie_len = 0;
1547 getObject(http_request_object, obj);
1548
1549 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
1550 RETURN_FALSE;
1551 }
1552
1553 array_init(return_value);
1554
1555 data = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, responseData));
1556 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1557 ulong idx = 0;
1558 char *key = NULL;
1559 zval **header = NULL;
1560
1561 convert_to_array_ex(headers);
1562 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers), key, idx, header) {
1563 if (key && !strcasecmp(key, "Set-Cookie")) {
1564 /* several cookies? */
1565 if (Z_TYPE_PP(header) == IS_ARRAY) {
1566 zval **cookie;
1567
1568 FOREACH_HASH_VAL(Z_ARRVAL_PP(header), cookie) {
1569 zval *cookie_hash;
1570 MAKE_STD_ZVAL(cookie_hash);
1571 array_init(cookie_hash);
1572
1573 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
1574 if (!cookie_len) {
1575 add_next_index_zval(return_value, cookie_hash);
1576 } else {
1577 zval **name;
1578
1579 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1580 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1581 add_next_index_zval(return_value, cookie_hash);
1582 return; /* <<< FOUND >>> */
1583 } else {
1584 zval_dtor(cookie_hash);
1585 efree(cookie_hash);
1586 }
1587 }
1588 } else {
1589 zval_dtor(cookie_hash);
1590 efree(cookie_hash);
1591 }
1592 }
1593 } else {
1594 zval *cookie_hash;
1595
1596 MAKE_STD_ZVAL(cookie_hash);
1597 array_init(cookie_hash);
1598 convert_to_string_ex(header);
1599
1600 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
1601 if (!cookie_len) {
1602 add_next_index_zval(return_value, cookie_hash);
1603 } else {
1604 zval **name;
1605
1606 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1607 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1608 add_next_index_zval(return_value, cookie_hash);
1609 } else {
1610 zval_dtor(cookie_hash);
1611 efree(cookie_hash);
1612 }
1613 }
1614 } else {
1615 zval_dtor(cookie_hash);
1616 efree(cookie_hash);
1617 }
1618 }
1619 break;
1620 }
1621 /* reset key */
1622 key = NULL;
1623 }
1624 }
1625 }
1626 }
1627 /* }}} */
1628
1629 /* {{{ proto string HttpRequest::getResponseBody()
1630 *
1631 * Get the response body after the request has been sent.
1632 *
1633 * Returns a string containing the response body.
1634 *
1635 * If redirects were allowed and several responses were received, the data
1636 * references the last received response.
1637 */
1638 PHP_METHOD(HttpRequest, getResponseBody)
1639 {
1640 NO_ARGS;
1641
1642 IF_RETVAL_USED {
1643 zval **body;
1644 getObject(http_request_object, obj);
1645 zval *data = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, responseData));
1646
1647 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1648 convert_to_string_ex(body);
1649 RETURN_ZVAL(*body, 1, 0);
1650 } else {
1651 RETURN_FALSE;
1652 }
1653 }
1654 }
1655 /* }}} */
1656
1657 /* {{{ proto int HttpRequest::getResponseCode()
1658 *
1659 * Get the response code after the request has been sent.
1660 *
1661 * Returns an int representing the response code.
1662 *
1663 * If redirects were allowed and several responses were received, the data
1664 * references the last received response.
1665 */
1666 PHP_METHOD(HttpRequest, getResponseCode)
1667 {
1668 NO_ARGS;
1669
1670 IF_RETVAL_USED {
1671 getObject(http_request_object, obj);
1672 zval *code = convert_to_type_ex(IS_LONG, GET_PROP(obj, responseCode));
1673
1674 RETURN_ZVAL(code, 1, 0);
1675 }
1676 }
1677 /* }}} */
1678
1679 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1680 *
1681 * Get response info after the request has been sent.
1682 * See http_get() for a full list of returned info.
1683 *
1684 * Accepts a string as optional parameter specifying the info to read.
1685 * If the parameter is empty or omitted, an associative array containing
1686 * all available info will be returned.
1687 *
1688 * Returns either a scalar containing the value of the info matching name if
1689 * requested, FALSE on failure, or an associative array containing all
1690 * available info.
1691 *
1692 * If redirects were allowed and several responses were received, the data
1693 * references the last received response.
1694 */
1695 PHP_METHOD(HttpRequest, getResponseInfo)
1696 {
1697 IF_RETVAL_USED {
1698 zval *info, **infop;
1699 char *info_name = NULL;
1700 int info_len = 0;
1701 getObject(http_request_object, obj);
1702
1703 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1704 RETURN_FALSE;
1705 }
1706
1707 info = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, responseInfo));
1708
1709 if (info_len && info_name) {
1710 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1711 RETURN_ZVAL(*infop, 1, 0);
1712 } else {
1713 http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
1714 RETURN_FALSE;
1715 }
1716 } else {
1717 array_init(return_value);
1718 array_copy(info, return_value);
1719 }
1720 }
1721 }
1722 /* }}}*/
1723
1724 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1725 *
1726 * Get the full response as HttpMessage object after the request has been sent.
1727 *
1728 * Returns an HttpMessage object of the response.
1729 *
1730 * If redirects were allowed and several responses were received, the data
1731 * references the last received response. Use HttpMessage::getParentMessage()
1732 * to access the data of previously received responses whithin this request
1733 * cycle.
1734 *
1735 * Throws HttpException.
1736 */
1737 PHP_METHOD(HttpRequest, getResponseMessage)
1738 {
1739 NO_ARGS;
1740
1741 IF_RETVAL_USED {
1742 zval *message;
1743 getObject(http_request_object, obj);
1744
1745 SET_EH_THROW_HTTP();
1746 message = GET_PROP(obj, responseMessage);
1747 if (Z_TYPE_P(message) == IS_OBJECT) {
1748 RETVAL_OBJECT(message);
1749 } else {
1750 RETVAL_NULL();
1751 }
1752 SET_EH_NORMAL();
1753 }
1754 }
1755 /* }}} */
1756
1757 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1758 *
1759 * Get sent HTTP message.
1760 *
1761 * Returns an HttpMessage object representing the sent request.
1762 *
1763 * If redirects were allowed and several responses were received, the data
1764 * references the last received response. Use HttpMessage::getParentMessage()
1765 * to access the data of previously sent requests whithin this request
1766 * cycle.
1767 */
1768 PHP_METHOD(HttpRequest, getRequestMessage)
1769 {
1770 NO_ARGS;
1771
1772 IF_RETVAL_USED {
1773 http_message *msg;
1774 getObject(http_request_object, obj);
1775
1776 SET_EH_THROW_HTTP();
1777 if (msg = http_message_parse(PHPSTR_VAL(&obj->request), PHPSTR_LEN(&obj->request))) {
1778 RETVAL_OBJVAL(http_message_object_from_msg(msg));
1779 }
1780 SET_EH_NORMAL();
1781 }
1782 }
1783 /* }}} */
1784
1785 /* {{{ proto HttpMessage HttpRequest::getHistory()
1786 *
1787 * Get all sent requests and received responses as an HttpMessage object.
1788 *
1789 * If you don't want to record history at all, set the instance variable
1790 * HttpRequest::$recoedHistory to FALSE.
1791 *
1792 * Returns an HttpMessage object representing the complete request/response
1793 * history.
1794 *
1795 * The object references the last received response, use HttpMessage::getParentMessage()
1796 * to access the data of previously sent requests and received responses.
1797 *
1798 * Throws HttpMalformedHeaderException.
1799 */
1800 PHP_METHOD(HttpRequest, getHistory)
1801 {
1802 NO_ARGS;
1803
1804 IF_RETVAL_USED {
1805 http_message *msg;
1806 getObject(http_request_object, obj);
1807
1808 SET_EH_THROW_HTTP();
1809 if (msg = http_message_parse(PHPSTR_VAL(&obj->history), PHPSTR_LEN(&obj->history))) {
1810 RETVAL_OBJVAL(http_message_object_from_msg(msg));
1811 }
1812 SET_EH_NORMAL();
1813 }
1814 }
1815 /* }}} */
1816
1817 /* {{{ proto void HttpRequest::clearHistory()
1818 *
1819 * Clear the history.
1820 */
1821 PHP_METHOD(HttpRequest, clearHistory)
1822 {
1823 NO_ARGS {
1824 getObject(http_request_object, obj);
1825 phpstr_dtor(&obj->history);
1826 }
1827 }
1828 /* }}} */
1829
1830 /* {{{ proto HttpMessage HttpRequest::send()
1831 *
1832 * Send the HTTP request.
1833 *
1834 * Returns the received response as HttpMessage object.
1835 *
1836 * Throws HttpRuntimeException, HttpRequestException,
1837 * HttpMalformedHeaderException, HttpEncodingException.
1838 *
1839 * GET example:
1840 * <pre>
1841 * <?php
1842 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1843 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1844 * $r->addQueryData(array('category' => 3));
1845 * try {
1846 * $r->send();
1847 * if ($r->getResponseCode() == 200) {
1848 * file_put_contents('local.rss', $r->getResponseBody());
1849 * }
1850 * } catch (HttpException $ex) {
1851 * echo $ex;
1852 * }
1853 * ?>
1854 * </pre>
1855 *
1856 * POST example:
1857 * <pre>
1858 * <?php
1859 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1860 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1861 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1862 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1863 * try {
1864 * echo $r->send()->getBody();
1865 * } catch (HttpException $ex) {
1866 * echo $ex;
1867 * }
1868 * ?>
1869 * </pre>
1870 */
1871 PHP_METHOD(HttpRequest, send)
1872 {
1873 http_request_body body = {0, NULL, 0};
1874 getObject(http_request_object, obj);
1875
1876 NO_ARGS;
1877
1878 SET_EH_THROW_HTTP();
1879
1880 if (obj->pool) {
1881 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1882 SET_EH_NORMAL();
1883 RETURN_FALSE;
1884 }
1885
1886 RETVAL_NULL();
1887
1888 if ( (SUCCESS == http_request_object_requesthandler(obj, getThis(), &body)) &&
1889 (SUCCESS == http_request_exec(obj->ch, NULL, &obj->response, &obj->request)) &&
1890 (SUCCESS == http_request_object_responsehandler(obj, getThis()))) {
1891 RETVAL_OBJECT(GET_PROP(obj, responseMessage));
1892 }
1893 http_request_body_dtor(&body);
1894
1895 SET_EH_NORMAL();
1896 }
1897 /* }}} */
1898
1899 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1900
1901 /*
1902 * Local variables:
1903 * tab-width: 4
1904 * c-basic-offset: 4
1905 * End:
1906 * vim600: noet sw=4 ts=4 fdm=marker
1907 * vim<600: noet sw=4 ts=4
1908 */
1909