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