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