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