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