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