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