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