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