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