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