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