- reset CURLOPT_CUSTOMREQUEST at the correct places
[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, NULL, 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, NULL, &new_obj);
341 if (old_obj->request->ch) {
342 http_curl_init_ex(curl_easy_duphandle(old_obj->request->ch), new_obj->request);
343 }
344
345 zend_objects_clone_members(&new_obj->zo, new_ov, old_zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
346 phpstr_append(&new_obj->history, old_obj->history.data, old_obj->history.used);
347 phpstr_append(&new_obj->request->conv.request, old_obj->request->conv.request.data, old_obj->request->conv.request.used);
348 phpstr_append(&new_obj->request->conv.response, old_obj->request->conv.response.data, old_obj->request->conv.response.used);
349
350 return new_ov;
351 }
352
353 static inline void _http_request_object_declare_default_properties(TSRMLS_D)
354 {
355 zend_class_entry *ce = http_request_object_ce;
356
357 DCL_PROP_N(PRIVATE, options);
358 DCL_PROP_N(PRIVATE, postFields);
359 DCL_PROP_N(PRIVATE, postFiles);
360 DCL_PROP_N(PRIVATE, responseInfo);
361 DCL_PROP_N(PRIVATE, responseData);
362 DCL_PROP_N(PRIVATE, responseMessage);
363 DCL_PROP(PRIVATE, long, responseCode, 0);
364 DCL_PROP(PRIVATE, long, method, HTTP_GET);
365 DCL_PROP(PRIVATE, string, url, "");
366 DCL_PROP(PRIVATE, string, contentType, "");
367 DCL_PROP(PRIVATE, string, rawPostData, "");
368 DCL_PROP(PRIVATE, string, queryData, "");
369 DCL_PROP(PRIVATE, string, putFile, "");
370
371 DCL_PROP(PUBLIC, bool, recordHistory, 0);
372
373 #ifndef WONKY
374 /*
375 * Request Method Constants
376 */
377 /* HTTP/1.1 */
378 DCL_CONST(long, "METH_GET", HTTP_GET);
379 DCL_CONST(long, "METH_HEAD", HTTP_HEAD);
380 DCL_CONST(long, "METH_POST", HTTP_POST);
381 DCL_CONST(long, "METH_PUT", HTTP_PUT);
382 DCL_CONST(long, "METH_DELETE", HTTP_DELETE);
383 DCL_CONST(long, "METH_OPTIONS", HTTP_OPTIONS);
384 DCL_CONST(long, "METH_TRACE", HTTP_TRACE);
385 DCL_CONST(long, "METH_CONNECT", HTTP_CONNECT);
386 /* WebDAV - RFC 2518 */
387 DCL_CONST(long, "METH_PROPFIND", HTTP_PROPFIND);
388 DCL_CONST(long, "METH_PROPPATCH", HTTP_PROPPATCH);
389 DCL_CONST(long, "METH_MKCOL", HTTP_MKCOL);
390 DCL_CONST(long, "METH_COPY", HTTP_COPY);
391 DCL_CONST(long, "METH_MOVE", HTTP_MOVE);
392 DCL_CONST(long, "METH_LOCK", HTTP_LOCK);
393 DCL_CONST(long, "METH_UNLOCK", HTTP_UNLOCK);
394 /* WebDAV Versioning - RFC 3253 */
395 DCL_CONST(long, "METH_VERSION_CONTROL", HTTP_VERSION_CONTROL);
396 DCL_CONST(long, "METH_REPORT", HTTP_REPORT);
397 DCL_CONST(long, "METH_CHECKOUT", HTTP_CHECKOUT);
398 DCL_CONST(long, "METH_CHECKIN", HTTP_CHECKIN);
399 DCL_CONST(long, "METH_UNCHECKOUT", HTTP_UNCHECKOUT);
400 DCL_CONST(long, "METH_MKWORKSPACE", HTTP_MKWORKSPACE);
401 DCL_CONST(long, "METH_UPDATE", HTTP_UPDATE);
402 DCL_CONST(long, "METH_LABEL", HTTP_LABEL);
403 DCL_CONST(long, "METH_MERGE", HTTP_MERGE);
404 DCL_CONST(long, "METH_BASELINE_CONTROL", HTTP_BASELINE_CONTROL);
405 DCL_CONST(long, "METH_MKACTIVITY", HTTP_MKACTIVITY);
406 /* WebDAV Access Control - RFC 3744 */
407 DCL_CONST(long, "METH_ACL", HTTP_ACL);
408
409 /* cURL HTTP protocol versions */
410 DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0);
411 DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1);
412 DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE);
413
414 /*
415 * Auth Constants
416 */
417 DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC);
418 DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST);
419 DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM);
420 DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY);
421 #endif /* WONKY */
422 }
423
424 void _http_request_object_free(zend_object *object TSRMLS_DC)
425 {
426 http_request_object *o = (http_request_object *) object;
427
428 if (OBJ_PROP(o)) {
429 zend_hash_destroy(OBJ_PROP(o));
430 FREE_HASHTABLE(OBJ_PROP(o));
431 }
432 http_request_free(&o->request);
433 phpstr_dtor(&o->history);
434 efree(o);
435 }
436
437 STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
438 {
439 STATUS status = SUCCESS;
440
441 http_request_reset(obj->request);
442 HTTP_CHECK_CURL_INIT(obj->request->ch, http_curl_init(obj->request), return FAILURE);
443
444 obj->request->url = http_absolute_url(Z_STRVAL_P(GET_PROP(url)));
445
446 switch (obj->request->meth = Z_LVAL_P(GET_PROP(method)))
447 {
448 case HTTP_GET:
449 case HTTP_HEAD:
450 break;
451
452 case HTTP_PUT:
453 {
454 php_stream_statbuf ssb;
455 php_stream *stream = php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
456
457 if (stream && !php_stream_stat(stream, &ssb)) {
458 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1);
459 } else {
460 status = FAILURE;
461 }
462 }
463 break;
464
465 case HTTP_POST:
466 default:
467 {
468 /* check for raw post data */
469 zval *raw_data = GET_PROP(rawPostData);
470
471 if (Z_STRLEN_P(raw_data)) {
472 zval *ctype = GET_PROP(contentType);
473
474 if (Z_STRLEN_P(ctype)) {
475 zval **headers, *opts = GET_PROP(options);
476
477 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
478 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) &&
479 (Z_TYPE_PP(headers) == IS_ARRAY)) {
480 zval **ct_header;
481
482 /* only override if not already set */
483 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void **) &ct_header)) && (Z_TYPE_PP(ct_header) == IS_STRING)) {
484 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
485 }
486 } else {
487 zval *headers;
488
489 MAKE_STD_ZVAL(headers);
490 array_init(headers);
491 add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
492 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers);
493 zval_ptr_dtor(&headers);
494 }
495 }
496
497 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
498 estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data)), Z_STRLEN_P(raw_data), 1);
499
500 } else {
501 zval *zfields = GET_PROP(postFields), *zfiles = GET_PROP(postFiles);
502 HashTable *fields;
503 HashTable *files;
504
505 fields = (Z_TYPE_P(zfields) == IS_ARRAY) ? Z_ARRVAL_P(zfields) : NULL;
506 files = (Z_TYPE_P(zfiles) == IS_ARRAY) ? Z_ARRVAL_P(zfiles) : NULL;
507
508 if ((fields && zend_hash_num_elements(fields)) || (files && zend_hash_num_elements(files))) {
509 if (!(obj->request->body = http_request_body_fill(obj->request->body, fields, files))) {
510 status = FAILURE;
511 }
512 }
513 }
514 }
515 break;
516 }
517
518 if (status == SUCCESS) {
519 zval *qdata = GET_PROP(queryData);
520 zval *options = GET_PROP(options);
521
522 if (Z_STRLEN_P(qdata)) {
523 if (!strchr(obj->request->url, '?')) {
524 strlcat(obj->request->url, "?", HTTP_URL_MAXLEN);
525 } else {
526 strlcat(obj->request->url, "&", HTTP_URL_MAXLEN);
527 }
528 strlcat(obj->request->url, Z_STRVAL_P(qdata), HTTP_URL_MAXLEN);
529 }
530
531 http_request_prepare(obj->request, Z_ARRVAL_P(options));
532
533 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
534 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onprogress", sizeof("onprogress"))) {
535 zval **entry, *pcb;
536
537 if ( (Z_TYPE_P(options) != IS_ARRAY)
538 || (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void **) &entry)
539 || (!zval_is_true(*entry)))) {
540 MAKE_STD_ZVAL(pcb);
541 array_init(pcb);
542 ZVAL_ADDREF(getThis());
543 add_next_index_zval(pcb, getThis());
544 add_next_index_stringl(pcb, "onprogress", lenof("onprogress"), 1);
545 http_request_set_progress_callback(obj->request, pcb);
546 zval_ptr_dtor(&pcb);
547 }
548 }
549 }
550
551 return status;
552 }
553
554 STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
555 {
556 http_message *msg;
557
558 phpstr_fix(&obj->request->conv.request);
559 phpstr_fix(&obj->request->conv.response);
560
561 msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response));
562
563 if (!msg) {
564 return FAILURE;
565 } else {
566 char *body;
567 size_t body_len;
568 zval *headers, *message, *resp, *info;
569
570 if (zval_is_true(GET_PROP(recordHistory))) {
571 /* we need to act like a zipper, as we'll receive
572 * the requests and the responses in separate chains
573 * for redirects
574 */
575 http_message *response = msg, *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
576 http_message *free_msg = request;
577
578 do {
579 char *message;
580 size_t msglen;
581
582 http_message_tostring(response, &message, &msglen);
583 phpstr_append(&obj->history, message, msglen);
584 efree(message);
585
586 http_message_tostring(request, &message, &msglen);
587 phpstr_append(&obj->history, message, msglen);
588 efree(message);
589
590 } while ((response = response->parent) && (request = request->parent));
591
592 http_message_free(&free_msg);
593 phpstr_fix(&obj->history);
594 }
595
596 UPD_PROP(long, responseCode, msg->http.info.response.code);
597
598 MAKE_STD_ZVAL(resp);
599 array_init(resp);
600 MAKE_STD_ZVAL(headers);
601 array_init(headers);
602 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
603 add_assoc_zval(resp, "headers", headers);
604 phpstr_data(PHPSTR(msg), &body, &body_len);
605 add_assoc_stringl(resp, "body", body, body_len, 0);
606 SET_PROP(responseData, resp);
607 zval_ptr_dtor(&resp);
608
609 MAKE_STD_ZVAL(message);
610 ZVAL_OBJVAL(message, http_message_object_new_ex(http_message_object_ce, msg, NULL));
611 SET_PROP(responseMessage, message);
612 zval_ptr_dtor(&message);
613
614 MAKE_STD_ZVAL(info);
615 array_init(info);
616 http_request_info(obj->request, Z_ARRVAL_P(info));
617 SET_PROP(responseInfo, info);
618 zval_ptr_dtor(&info);
619
620 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) {
621 zend_call_method_with_0_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL);
622 }
623
624 return SUCCESS;
625 }
626 }
627
628 #define http_request_object_set_options_subr(key, ow) \
629 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow))
630 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite)
631 {
632 zval *old_opts, *new_opts, *opts = NULL, **entry;
633
634 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
635 RETURN_FALSE;
636 }
637
638 MAKE_STD_ZVAL(new_opts);
639 array_init(new_opts);
640 old_opts = GET_PROP(options);
641 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
642 array_copy(old_opts, new_opts);
643 }
644
645 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void **) &entry)) {
646 if (overwrite) {
647 zend_hash_clean(Z_ARRVAL_PP(entry));
648 }
649 if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
650 if (overwrite) {
651 array_copy(opts, *entry);
652 } else {
653 array_merge(opts, *entry);
654 }
655 }
656 } else if (opts) {
657 ZVAL_ADDREF(opts);
658 add_assoc_zval(new_opts, key, opts);
659 }
660 SET_PROP(options, new_opts);
661 zval_ptr_dtor(&new_opts);
662
663 RETURN_TRUE;
664 }
665
666 #define http_request_object_get_options_subr(key) \
667 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
668 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
669 {
670 NO_ARGS;
671
672 IF_RETVAL_USED {
673 zval *opts, **options;
674
675 opts = GET_PROP(options);
676 array_init(return_value);
677
678 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
679 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options))) {
680 convert_to_array(*options);
681 array_copy(*options, return_value);
682 }
683 }
684 }
685
686
687 /* ### USERLAND ### */
688
689 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
690 *
691 * Instantiate a new HttpRequest object.
692 *
693 * Accepts a string as optional parameter containing the target request url.
694 * Additianally accepts an optional int parameter specifying the request method
695 * to use and an associative array as optional third parameter which will be
696 * passed to HttpRequest::setOptions().
697 *
698 * Throws HttpException.
699 */
700 PHP_METHOD(HttpRequest, __construct)
701 {
702 char *URL = NULL;
703 int URL_len;
704 long meth = -1;
705 zval *options = NULL;
706
707 SET_EH_THROW_HTTP();
708 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla", &URL, &URL_len, &meth, &options)) {
709 if (URL) {
710 UPD_STRL(url, URL, URL_len);
711 }
712 if (meth > -1) {
713 UPD_PROP(long, method, meth);
714 }
715 if (options) {
716 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
717 }
718 }
719 SET_EH_NORMAL();
720 }
721 /* }}} */
722
723 /* {{{ proto bool HttpRequest::setOptions([array options])
724 *
725 * Set the request options to use. See http_get() for a full list of available options.
726 *
727 * Accepts an array as optional parameters, wich values will overwrite the
728 * currently set request options. If the parameter is empty or mitted,
729 * the optoions of the HttpRequest object will be reset.
730 *
731 * Returns TRUE on success, or FALSE on failure.
732 */
733 PHP_METHOD(HttpRequest, setOptions)
734 {
735 char *key = NULL;
736 ulong idx = 0;
737 HashPosition pos;
738 zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt;
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
943 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
944 RETURN_FALSE;
945 }
946
947 UPD_STRL(url, URL, URL_len);
948 RETURN_TRUE;
949 }
950 /* }}} */
951
952 /* {{{ proto string HttpRequest::getUrl()
953 *
954 * Get the previously set request URL.
955 *
956 * Returns the currently set request url as string.
957 */
958 PHP_METHOD(HttpRequest, getUrl)
959 {
960 NO_ARGS;
961
962 IF_RETVAL_USED {
963 RETURN_PROP(url);
964 }
965 }
966 /* }}} */
967
968 /* {{{ proto bool HttpRequest::setMethod(int request_method)
969 *
970 * Set the request method.
971 *
972 * Expects an int as parameter specifying the request method to use.
973 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
974 *
975 * Returns TRUE on success, or FALSE on failure.
976 */
977 PHP_METHOD(HttpRequest, setMethod)
978 {
979 long meth;
980
981 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
982 RETURN_FALSE;
983 }
984
985 UPD_PROP(long, method, meth);
986 RETURN_TRUE;
987 }
988 /* }}} */
989
990 /* {{{ proto int HttpRequest::getMethod()
991 *
992 * Get the previously set request method.
993 *
994 * Returns the currently set request method.
995 */
996 PHP_METHOD(HttpRequest, getMethod)
997 {
998 NO_ARGS;
999
1000 IF_RETVAL_USED {
1001 RETURN_PROP(method);
1002 }
1003 }
1004 /* }}} */
1005
1006 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1007 *
1008 * Set the content type the post request should have.
1009 *
1010 * Expects a string as parameters containing the content type of the request
1011 * (primary/secondary).
1012 *
1013 * Returns TRUE on success, or FALSE if the content type does not seem to
1014 * contain a primary and a secondary part.
1015 */
1016 PHP_METHOD(HttpRequest, setContentType)
1017 {
1018 char *ctype;
1019 int ct_len;
1020 getObject(http_request_object, obj);
1021
1022 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1023 RETURN_FALSE;
1024 }
1025
1026 HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
1027 UPD_STRL(contentType, ctype, ct_len);
1028 RETURN_TRUE;
1029 }
1030 /* }}} */
1031
1032 /* {{{ proto string HttpRequest::getContentType()
1033 *
1034 * Get the previously content type.
1035 *
1036 * Returns the previously set content type as string.
1037 */
1038 PHP_METHOD(HttpRequest, getContentType)
1039 {
1040 NO_ARGS;
1041
1042 IF_RETVAL_USED {
1043 RETURN_PROP(contentType);
1044 }
1045 }
1046 /* }}} */
1047
1048 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1049 *
1050 * Set the URL query parameters to use, overwriting previously set query parameters.
1051 * Affects any request types.
1052 *
1053 * Accepts a string or associative array parameter containing the pre-encoded
1054 * query string or to be encoded query fields. If the parameter is empty or
1055 * omitted, the query data will be unset.
1056 *
1057 * Returns TRUE on success, or FALSE on failure.
1058 */
1059 PHP_METHOD(HttpRequest, setQueryData)
1060 {
1061 zval *qdata = NULL;
1062 getObject(http_request_object, obj);
1063
1064 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
1065 RETURN_FALSE;
1066 }
1067
1068 if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
1069 UPD_STRL(queryData, "", 0);
1070 } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1071 char *query_data = NULL;
1072
1073 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1074 RETURN_FALSE;
1075 }
1076
1077 UPD_PROP(string, queryData, query_data);
1078 efree(query_data);
1079 } else {
1080 zval *orig = qdata;
1081
1082 convert_to_string_ex(&qdata);
1083 UPD_STRL(queryData, Z_STRVAL_P(qdata), Z_STRLEN_P(qdata));
1084 if (orig != qdata) {
1085 zval_ptr_dtor(&qdata);
1086 }
1087 }
1088 RETURN_TRUE;
1089 }
1090 /* }}} */
1091
1092 /* {{{ proto string HttpRequest::getQueryData()
1093 *
1094 * Get the current query data in form of an urlencoded query string.
1095 *
1096 * Returns a string containing the urlencoded query.
1097 */
1098 PHP_METHOD(HttpRequest, getQueryData)
1099 {
1100 NO_ARGS;
1101
1102 IF_RETVAL_USED {
1103 RETURN_PROP(queryData);
1104 }
1105 }
1106 /* }}} */
1107
1108 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1109 *
1110 * Add parameters to the query parameter list, leaving previously set unchanged.
1111 * Affects any request type.
1112 *
1113 * Expects an associative array as parameter containing the query fields to add.
1114 *
1115 * Returns TRUE on success, or FALSE on failure.
1116 */
1117 PHP_METHOD(HttpRequest, addQueryData)
1118 {
1119 zval *qdata, *old_qdata;
1120 char *query_data = NULL;
1121 size_t query_data_len = 0;
1122
1123 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &qdata)) {
1124 RETURN_FALSE;
1125 }
1126
1127 old_qdata = GET_PROP(queryData);
1128
1129 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)) {
1130 RETURN_FALSE;
1131 }
1132
1133 UPD_STRL(queryData, query_data, query_data_len);
1134 efree(query_data);
1135
1136 RETURN_TRUE;
1137 }
1138 /* }}} */
1139
1140 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1141 *
1142 * Adds POST data entries, leaving previously set unchanged, unless a
1143 * post entry with the same name already exists.
1144 * Affects only POST and custom requests.
1145 *
1146 * Expects an associative array as parameter containing the post fields.
1147 *
1148 * Returns TRUE on success, or FALSE on failure.
1149 */
1150 PHP_METHOD(HttpRequest, addPostFields)
1151 {
1152 zval *post_data, *old_post, *new_post;
1153
1154 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &post_data)) {
1155 RETURN_FALSE;
1156 }
1157
1158 if (zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1159 MAKE_STD_ZVAL(new_post);
1160 array_init(new_post);
1161 old_post = GET_PROP(postFields);
1162 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1163 array_copy(old_post, new_post);
1164 }
1165 array_merge(post_data, new_post);
1166 SET_PROP(postFields, new_post);
1167 zval_ptr_dtor(&new_post);
1168 }
1169
1170 RETURN_TRUE;
1171 }
1172 /* }}} */
1173
1174 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1175 *
1176 * Set the POST data entries, overwriting previously set POST data.
1177 * Affects only POST and custom requests.
1178 *
1179 * Accepts an associative array as parameter containing the post fields.
1180 * If the parameter is empty or omitted, the post data will be unset.
1181 *
1182 * Returns TRUE on success, or FALSE on failure.
1183 */
1184 PHP_METHOD(HttpRequest, setPostFields)
1185 {
1186 zval *post, *post_data = NULL;
1187
1188 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &post_data)) {
1189 RETURN_FALSE;
1190 }
1191
1192 MAKE_STD_ZVAL(post);
1193 array_init(post);
1194 if (post_data && zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1195 array_copy(post_data, post);
1196 }
1197 SET_PROP(postFields, post);
1198 zval_ptr_dtor(&post);
1199
1200 RETURN_TRUE;
1201 }
1202 /* }}}*/
1203
1204 /* {{{ proto array HttpRequest::getPostFields()
1205 *
1206 * Get previously set POST data.
1207 *
1208 * Returns the currently set post fields as associative array.
1209 */
1210 PHP_METHOD(HttpRequest, getPostFields)
1211 {
1212 NO_ARGS;
1213
1214 IF_RETVAL_USED {
1215 RETURN_PROP(postFields);
1216 }
1217 }
1218 /* }}} */
1219
1220 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1221 *
1222 * Set raw post data to send, overwriting previously set raw post data. Don't
1223 * forget to specify a content type. Affects only POST and custom requests.
1224 * Only either post fields or raw post data can be used for each request.
1225 * Raw post data has higher precedence and will be used even if post fields
1226 * are set.
1227 *
1228 * Accepts a string as parameter containing the *raw* post data.
1229 *
1230 * Returns TRUE on success, or FALSE on failure.
1231 */
1232 PHP_METHOD(HttpRequest, setRawPostData)
1233 {
1234 char *raw_data = NULL;
1235 int data_len = 0;
1236
1237 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &raw_data, &data_len)) {
1238 RETURN_FALSE;
1239 }
1240
1241 if (!raw_data) {
1242 raw_data = "";
1243 }
1244
1245 UPD_STRL(rawPostData, raw_data, data_len);
1246 RETURN_TRUE;
1247 }
1248 /* }}} */
1249
1250 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1251 *
1252 * Add raw post data, leaving previously set raw post data unchanged.
1253 * Affects only POST and custom requests.
1254 *
1255 * Expects a string as parameter containing the raw post data to concatenate.
1256 *
1257 * Returns TRUE on success, or FALSE on failure.
1258 */
1259 PHP_METHOD(HttpRequest, addRawPostData)
1260 {
1261 char *raw_data;
1262 int data_len;
1263
1264 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw_data, &data_len)) {
1265 RETURN_FALSE;
1266 }
1267
1268 if (data_len) {
1269 zval *data = zval_copy(IS_STRING, GET_PROP(rawPostData));
1270
1271 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1272 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1273 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len);
1274 SET_PROP(rawPostData, data);
1275 zval_free(&data);
1276 }
1277
1278 RETURN_TRUE;
1279 }
1280 /* }}} */
1281
1282 /* {{{ proto string HttpRequest::getRawPostData()
1283 *
1284 * Get previously set raw post data.
1285 *
1286 * Returns a string containing the currently set raw post data.
1287 */
1288 PHP_METHOD(HttpRequest, getRawPostData)
1289 {
1290 NO_ARGS;
1291
1292 IF_RETVAL_USED {
1293 RETURN_PROP(rawPostData);
1294 }
1295 }
1296 /* }}} */
1297
1298 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1299 *
1300 * Add a file to the POST request, leaving prefiously set files unchanged.
1301 * Affects only POST and custom requests. Cannot be used with raw post data.
1302 *
1303 * Expects a string parameter containing the form element name, and a string
1304 * paremeter containing the path to the file which should be uploaded.
1305 * Additionally accepts an optional string parameter which chould contain
1306 * the content type of the file.
1307 *
1308 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1309 * primary and a secondary content type part.
1310 */
1311 PHP_METHOD(HttpRequest, addPostFile)
1312 {
1313 zval *entry, *old_post, *new_post;
1314 char *name, *file, *type = NULL;
1315 int name_len, file_len, type_len = 0;
1316
1317 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1318 RETURN_FALSE;
1319 }
1320
1321 if (type_len) {
1322 HTTP_CHECK_CONTENT_TYPE(type, RETURN_FALSE);
1323 } else {
1324 type = "application/x-octetstream";
1325 type_len = sizeof("application/x-octetstream") - 1;
1326 }
1327
1328 MAKE_STD_ZVAL(entry);
1329 array_init(entry);
1330
1331 add_assoc_stringl(entry, "name", name, name_len, 1);
1332 add_assoc_stringl(entry, "type", type, type_len, 1);
1333 add_assoc_stringl(entry, "file", file, file_len, 1);
1334
1335 MAKE_STD_ZVAL(new_post);
1336 array_init(new_post);
1337 old_post = GET_PROP(postFiles);
1338 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1339 array_copy(old_post, new_post);
1340 }
1341 add_next_index_zval(new_post, entry);
1342 SET_PROP(postFiles, new_post);
1343 zval_ptr_dtor(&new_post);
1344
1345 RETURN_TRUE;
1346 }
1347 /* }}} */
1348
1349 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1350 *
1351 * Set files to post, overwriting previously set post files.
1352 * Affects only POST and requests. Cannot be used with raw post data.
1353 *
1354 * Accepts an array containing the files to post. Each entry should be an
1355 * associative array with "name", "file" and "type" keys. If the parameter
1356 * is empty or omitted the post files will be unset.
1357 *
1358 * Returns TRUE on success, or FALSE on failure.
1359 */
1360 PHP_METHOD(HttpRequest, setPostFiles)
1361 {
1362 zval *files = NULL, *post;
1363
1364 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!/", &files)) {
1365 RETURN_FALSE;
1366 }
1367
1368 MAKE_STD_ZVAL(post);
1369 array_init(post);
1370 if (files && (Z_TYPE_P(files) == IS_ARRAY)) {
1371 array_copy(files, post);
1372 }
1373 SET_PROP(postFiles, post);
1374 zval_ptr_dtor(&post);
1375
1376 RETURN_TRUE;
1377 }
1378 /* }}} */
1379
1380 /* {{{ proto array HttpRequest::getPostFiles()
1381 *
1382 * Get all previously added POST files.
1383 *
1384 * Returns an array containing currently set post files.
1385 */
1386 PHP_METHOD(HttpRequest, getPostFiles)
1387 {
1388 NO_ARGS;
1389
1390 IF_RETVAL_USED {
1391 RETURN_PROP(postFiles);
1392 }
1393 }
1394 /* }}} */
1395
1396 /* {{{ proto bool HttpRequest::setPutFile([string file])
1397 *
1398 * Set file to put. Affects only PUT requests.
1399 *
1400 * Accepts a string as parameter referencing the path to file.
1401 * If the parameter is empty or omitted the put file will be unset.
1402 *
1403 * Returns TRUE on success, or FALSE on failure.
1404 */
1405 PHP_METHOD(HttpRequest, setPutFile)
1406 {
1407 char *file = "";
1408 int file_len = 0;
1409
1410 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &file, &file_len)) {
1411 RETURN_FALSE;
1412 }
1413
1414 UPD_STRL(putFile, file, file_len);
1415 RETURN_TRUE;
1416 }
1417 /* }}} */
1418
1419 /* {{{ proto string HttpRequest::getPutFile()
1420 *
1421 * Get previously set put file.
1422 *
1423 * Returns a string containing the path to the currently set put file.
1424 */
1425 PHP_METHOD(HttpRequest, getPutFile)
1426 {
1427 NO_ARGS;
1428
1429 IF_RETVAL_USED {
1430 RETURN_PROP(putFile);
1431 }
1432 }
1433 /* }}} */
1434
1435 /* {{{ proto array HttpRequest::getResponseData()
1436 *
1437 * Get all response data after the request has been sent.
1438 *
1439 * Returns an associative array with the key "headers" containing an associative
1440 * array holding all response headers, as well as the ley "body" containing a
1441 * string with the response body.
1442 *
1443 * If redirects were allowed and several responses were received, the data
1444 * references the last received response.
1445 */
1446 PHP_METHOD(HttpRequest, getResponseData)
1447 {
1448 NO_ARGS;
1449
1450 IF_RETVAL_USED {
1451 RETURN_PROP(responseData);
1452 }
1453 }
1454 /* }}} */
1455
1456 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1457 *
1458 * Get response header(s) after the request has been sent.
1459 *
1460 * Accepts an string as optional parameter specifying a certain header to read.
1461 * If the parameter is empty or omitted all response headers will be returned.
1462 *
1463 * Returns either a string with the value of the header matching name if requested,
1464 * FALSE on failure, or an associative array containing all reponse headers.
1465 *
1466 * If redirects were allowed and several responses were received, the data
1467 * references the last received response.
1468 */
1469 PHP_METHOD(HttpRequest, getResponseHeader)
1470 {
1471 IF_RETVAL_USED {
1472 zval *data, **headers, **header;
1473 char *header_name = NULL;
1474 int header_len = 0;
1475
1476 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1477 RETURN_FALSE;
1478 }
1479
1480 data = GET_PROP(responseData);
1481 if ( (Z_TYPE_P(data) == IS_ARRAY) &&
1482 (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) &&
1483 (Z_TYPE_PP(headers) == IS_ARRAY)) {
1484 if (!header_len || !header_name) {
1485 RETVAL_ZVAL(*headers, 1, 0);
1486 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1487 RETVAL_ZVAL(*header, 1, 0);
1488 } else {
1489 RETVAL_FALSE;
1490 }
1491 } else {
1492 RETVAL_FALSE;
1493 }
1494 }
1495 }
1496 /* }}} */
1497
1498 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1499 *
1500 * Get response cookie(s) after the request has been sent.
1501 *
1502 * Accepts a string as optional parameter specifying the name of the cookie to read.
1503 * If the parameter is empty or omitted, an associative array with all received
1504 * cookies will be returned.
1505 *
1506 * Returns either an associative array with the cookie's name, value and any
1507 * additional params of the cookie matching name if requested, FALSE on failure,
1508 * or an array containing all received cookies as arrays.
1509 *
1510 * If redirects were allowed and several responses were received, the data
1511 * references the last received response.
1512 */
1513 PHP_METHOD(HttpRequest, getResponseCookie)
1514 {
1515 IF_RETVAL_USED {
1516 zval *data, **headers;
1517 char *cookie_name = NULL;
1518 int cookie_len = 0;
1519
1520 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
1521 RETURN_FALSE;
1522 }
1523
1524 array_init(return_value);
1525
1526 data = GET_PROP(responseData);
1527 if ( (Z_TYPE_P(data) == IS_ARRAY) &&
1528 (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) &&
1529 (Z_TYPE_PP(headers) == IS_ARRAY)) {
1530 ulong idx = 0;
1531 char *key = NULL;
1532 zval **header = NULL;
1533 HashPosition pos1;
1534
1535 convert_to_array(*headers);
1536 FOREACH_HASH_KEYVAL(pos1, Z_ARRVAL_PP(headers), key, idx, header) {
1537 if (key && !strcasecmp(key, "Set-Cookie")) {
1538 /* several cookies? */
1539 if (Z_TYPE_PP(header) == IS_ARRAY) {
1540 zval **cookie;
1541 HashPosition pos2;
1542
1543 FOREACH_HASH_VAL(pos2, Z_ARRVAL_PP(header), cookie) {
1544 zval *cookie_hash;
1545 MAKE_STD_ZVAL(cookie_hash);
1546 array_init(cookie_hash);
1547
1548 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
1549 if (!cookie_len) {
1550 add_next_index_zval(return_value, cookie_hash);
1551 } else {
1552 zval **name;
1553
1554 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1555 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1556 add_next_index_zval(return_value, cookie_hash);
1557 return; /* <<< FOUND >>> */
1558 } else {
1559 zval_dtor(cookie_hash);
1560 efree(cookie_hash);
1561 }
1562 }
1563 } else {
1564 zval_dtor(cookie_hash);
1565 efree(cookie_hash);
1566 }
1567 }
1568 } else {
1569 zval *cookie_hash;
1570
1571 MAKE_STD_ZVAL(cookie_hash);
1572 array_init(cookie_hash);
1573 convert_to_string_ex(header);
1574
1575 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
1576 if (!cookie_len) {
1577 add_next_index_zval(return_value, cookie_hash);
1578 } else {
1579 zval **name;
1580
1581 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1582 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1583 add_next_index_zval(return_value, cookie_hash);
1584 } else {
1585 zval_dtor(cookie_hash);
1586 efree(cookie_hash);
1587 }
1588 }
1589 } else {
1590 zval_dtor(cookie_hash);
1591 efree(cookie_hash);
1592 }
1593 }
1594 break;
1595 }
1596 /* reset key */
1597 key = NULL;
1598 }
1599 }
1600 }
1601 }
1602 /* }}} */
1603
1604 /* {{{ proto string HttpRequest::getResponseBody()
1605 *
1606 * Get the response body after the request has been sent.
1607 *
1608 * Returns a string containing the response body.
1609 *
1610 * If redirects were allowed and several responses were received, the data
1611 * references the last received response.
1612 */
1613 PHP_METHOD(HttpRequest, getResponseBody)
1614 {
1615 NO_ARGS;
1616
1617 IF_RETVAL_USED {
1618 zval **body;
1619 zval *data = GET_PROP(responseData);
1620
1621 if ( (Z_TYPE_P(data) == IS_ARRAY) &&
1622 (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body))) {
1623 RETURN_ZVAL(*body, 1, 0);
1624 } else {
1625 RETURN_FALSE;
1626 }
1627 }
1628 }
1629 /* }}} */
1630
1631 /* {{{ proto int HttpRequest::getResponseCode()
1632 *
1633 * Get the response code after the request has been sent.
1634 *
1635 * Returns an int representing the response code.
1636 *
1637 * If redirects were allowed and several responses were received, the data
1638 * references the last received response.
1639 */
1640 PHP_METHOD(HttpRequest, getResponseCode)
1641 {
1642 NO_ARGS;
1643
1644 IF_RETVAL_USED {
1645 RETURN_PROP(responseCode);
1646 }
1647 }
1648 /* }}} */
1649
1650 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1651 *
1652 * Get response info after the request has been sent.
1653 * See http_get() for a full list of returned info.
1654 *
1655 * Accepts a string as optional parameter specifying the info to read.
1656 * If the parameter is empty or omitted, an associative array containing
1657 * all available info will be returned.
1658 *
1659 * Returns either a scalar containing the value of the info matching name if
1660 * requested, FALSE on failure, or an associative array containing all
1661 * available info.
1662 *
1663 * If redirects were allowed and several responses were received, the data
1664 * references the last received response.
1665 */
1666 PHP_METHOD(HttpRequest, getResponseInfo)
1667 {
1668 IF_RETVAL_USED {
1669 zval *info, **infop;
1670 char *info_name = NULL;
1671 int info_len = 0;
1672
1673 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1674 RETURN_FALSE;
1675 }
1676
1677 info = GET_PROP(responseInfo);
1678
1679 if (Z_TYPE_P(info) != IS_ARRAY) {
1680 RETURN_FALSE;
1681 }
1682
1683 if (info_len && info_name) {
1684 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1685 RETURN_ZVAL(*infop, 1, 0);
1686 } else {
1687 http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
1688 RETURN_FALSE;
1689 }
1690 } else {
1691 RETURN_ZVAL(info, 1, 0);
1692 }
1693 }
1694 }
1695 /* }}}*/
1696
1697 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1698 *
1699 * Get the full response as HttpMessage object after the request has been sent.
1700 *
1701 * Returns an HttpMessage object of the response.
1702 *
1703 * If redirects were allowed and several responses were received, the data
1704 * references the last received response. Use HttpMessage::getParentMessage()
1705 * to access the data of previously received responses whithin this request
1706 * cycle.
1707 *
1708 * Throws HttpException.
1709 */
1710 PHP_METHOD(HttpRequest, getResponseMessage)
1711 {
1712 NO_ARGS;
1713
1714 IF_RETVAL_USED {
1715 zval *message;
1716
1717 SET_EH_THROW_HTTP();
1718 message = GET_PROP(responseMessage);
1719 if (Z_TYPE_P(message) == IS_OBJECT) {
1720 RETVAL_OBJECT(message);
1721 } else {
1722 RETVAL_NULL();
1723 }
1724 SET_EH_NORMAL();
1725 }
1726 }
1727 /* }}} */
1728
1729 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1730 *
1731 * Get sent HTTP message.
1732 *
1733 * Returns an HttpMessage object representing the sent request.
1734 *
1735 * If redirects were allowed and several responses were received, the data
1736 * references the last received response. Use HttpMessage::getParentMessage()
1737 * to access the data of previously sent requests whithin this request
1738 * cycle.
1739 *
1740 * Note that the internal request message is immutable, that means that the
1741 * request message received through HttpRequest::getRequestMessage() will
1742 * always look the same for the same request, regardless of any changes you
1743 * may have made to the returned object.
1744 *
1745 * Throws HttpMalformedHeadersException, HttpEncodingException.
1746 */
1747 PHP_METHOD(HttpRequest, getRequestMessage)
1748 {
1749 NO_ARGS;
1750
1751 IF_RETVAL_USED {
1752 http_message *msg;
1753 getObject(http_request_object, obj);
1754
1755 SET_EH_THROW_HTTP();
1756 if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
1757 ZVAL_OBJVAL(return_value, http_message_object_new_ex(http_message_object_ce, msg, NULL));
1758 }
1759 SET_EH_NORMAL();
1760 }
1761 }
1762 /* }}} */
1763
1764 /* {{{ proto string HttpRequest::getRawRequestMessage()
1765 *
1766 * Get sent HTTP message.
1767 *
1768 * Returns an HttpMessage in a form of a string
1769 *
1770 */
1771 PHP_METHOD(HttpRequest, getRawRequestMessage)
1772 {
1773 NO_ARGS;
1774
1775 IF_RETVAL_USED {
1776 getObject(http_request_object, obj);
1777
1778 RETURN_PHPSTR_DUP(&obj->request->conv.request);
1779 }
1780 }
1781 /* }}} */
1782
1783 /* {{{ proto string HttpRequest::getRawResponseMessage()
1784 *
1785 * Get the entire HTTP response.
1786 *
1787 * Returns the complete web server response, including the headers in a form of a string.
1788 *
1789 */
1790 PHP_METHOD(HttpRequest, getRawResponseMessage)
1791 {
1792 NO_ARGS;
1793
1794 IF_RETVAL_USED {
1795 getObject(http_request_object, obj);
1796
1797 RETURN_PHPSTR_DUP(&obj->request->conv.response);
1798 }
1799 }
1800 /* }}} */
1801
1802 /* {{{ proto HttpMessage HttpRequest::getHistory()
1803 *
1804 * Get all sent requests and received responses as an HttpMessage object.
1805 *
1806 * If you don't want to record history at all, set the instance variable
1807 * HttpRequest::$recoedHistory to FALSE.
1808 *
1809 * Returns an HttpMessage object representing the complete request/response
1810 * history.
1811 *
1812 * The object references the last received response, use HttpMessage::getParentMessage()
1813 * to access the data of previously sent requests and received responses.
1814 *
1815 * Note that the internal history is immutable, that means that any changes
1816 * you make the the message list won't affect a history message list newly
1817 * created by another call to HttpRequest::getHistory().
1818 *
1819 * Throws HttpMalformedHeaderException, HttpEncodingException.
1820 */
1821 PHP_METHOD(HttpRequest, getHistory)
1822 {
1823 NO_ARGS;
1824
1825 IF_RETVAL_USED {
1826 http_message *msg;
1827 getObject(http_request_object, obj);
1828
1829 SET_EH_THROW_HTTP();
1830 if ((msg = http_message_parse(PHPSTR_VAL(&obj->history), PHPSTR_LEN(&obj->history)))) {
1831 ZVAL_OBJVAL(return_value, http_message_object_new_ex(http_message_object_ce, msg, NULL));
1832 }
1833 SET_EH_NORMAL();
1834 }
1835 }
1836 /* }}} */
1837
1838 /* {{{ proto void HttpRequest::clearHistory()
1839 *
1840 * Clear the history.
1841 */
1842 PHP_METHOD(HttpRequest, clearHistory)
1843 {
1844 NO_ARGS {
1845 getObject(http_request_object, obj);
1846 phpstr_dtor(&obj->history);
1847 }
1848 }
1849 /* }}} */
1850
1851 /* {{{ proto HttpMessage HttpRequest::send()
1852 *
1853 * Send the HTTP request.
1854 *
1855 * Returns the received response as HttpMessage object.
1856 *
1857 * NOTE: While an exception may be thrown, the transfer could have succeeded
1858 * at least partially, so you might want to check the return values of various
1859 * HttpRequest::getResponse*() methods.
1860 *
1861 * Throws HttpRuntimeException, HttpRequestException,
1862 * HttpMalformedHeaderException, HttpEncodingException.
1863 *
1864 * GET example:
1865 * <pre>
1866 * <?php
1867 * $r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
1868 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1869 * $r->addQueryData(array('category' => 3));
1870 * try {
1871 * $r->send();
1872 * if ($r->getResponseCode() == 200) {
1873 * file_put_contents('local.rss', $r->getResponseBody());
1874 * }
1875 * } catch (HttpException $ex) {
1876 * echo $ex;
1877 * }
1878 * ?>
1879 * </pre>
1880 *
1881 * POST example:
1882 * <pre>
1883 * <?php
1884 * $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
1885 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1886 * $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
1887 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1888 * try {
1889 * echo $r->send()->getBody();
1890 * } catch (HttpException $ex) {
1891 * echo $ex;
1892 * }
1893 * ?>
1894 * </pre>
1895 */
1896 PHP_METHOD(HttpRequest, send)
1897 {
1898 getObject(http_request_object, obj);
1899
1900 NO_ARGS;
1901
1902 SET_EH_THROW_HTTP();
1903
1904 RETVAL_FALSE;
1905
1906 if (obj->pool) {
1907 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1908 } else if (SUCCESS == http_request_object_requesthandler(obj, getThis())) {
1909 http_request_exec(obj->request);
1910 if (SUCCESS == http_request_object_responsehandler(obj, getThis())) {
1911 RETVAL_OBJECT(GET_PROP(responseMessage));
1912 }
1913 }
1914
1915 SET_EH_NORMAL();
1916 }
1917 /* }}} */
1918
1919 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1920
1921 /*
1922 * Local variables:
1923 * tab-width: 4
1924 * c-basic-offset: 4
1925 * End:
1926 * vim600: noet sw=4 ts=4 fdm=marker
1927 * vim<600: noet sw=4 ts=4
1928 */
1929