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