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