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