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