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