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