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