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