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