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