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