- fix build (unknown macro)
[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), 0, 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);
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 (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 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param);
697 zval_ptr_dtor(&param);
698 }
699
700 return ret;
701 }
702
703 static int apply_pretty_key(void *pDest, int num_args, va_list args, zend_hash_key *hash_key)
704 {
705 if (hash_key->nKeyLength > 1) {
706 hash_key->h = zend_get_hash_value(pretty_key(hash_key->arKey, hash_key->nKeyLength - 1, 1, 0), hash_key->nKeyLength);
707 }
708 return ZEND_HASH_APPLY_KEEP;
709 }
710
711 #define http_request_object_set_options_subr(key, ow, pk) \
712 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
713 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys)
714 {
715 zval *old_opts, *new_opts, *opts = NULL, **entry = NULL;
716
717 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
718 RETURN_FALSE;
719 }
720
721 MAKE_STD_ZVAL(new_opts);
722 array_init(new_opts);
723 old_opts = GET_PROP(options);
724 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
725 array_copy(old_opts, new_opts);
726 }
727
728 if (prettify_keys && opts) {
729 zend_hash_apply_with_arguments(Z_ARRVAL_P(opts), apply_pretty_key, 0);
730 }
731 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void **) &entry)) {
732 if (overwrite) {
733 zend_hash_clean(Z_ARRVAL_PP(entry));
734 }
735 if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
736 if (overwrite) {
737 array_copy(opts, *entry);
738 } else {
739 array_merge(opts, *entry);
740 }
741 }
742 } else if (opts) {
743 ZVAL_ADDREF(opts);
744 add_assoc_zval(new_opts, key, opts);
745 }
746 SET_PROP(options, new_opts);
747 zval_ptr_dtor(&new_opts);
748
749 RETURN_TRUE;
750 }
751
752 #define http_request_object_get_options_subr(key) \
753 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
754 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
755 {
756 NO_ARGS;
757
758 IF_RETVAL_USED {
759 zval *opts, **options;
760
761 opts = GET_PROP(options);
762 array_init(return_value);
763
764 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
765 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options))) {
766 convert_to_array(*options);
767 array_copy(*options, return_value);
768 }
769 }
770 }
771
772
773 /* ### USERLAND ### */
774
775 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
776 *
777 * Instantiate a new HttpRequest object.
778 *
779 * Accepts a string as optional parameter containing the target request url.
780 * Additionally accepts an optional int parameter specifying the request method
781 * to use and an associative array as optional third parameter which will be
782 * passed to HttpRequest::setOptions().
783 *
784 * Throws HttpException.
785 */
786 PHP_METHOD(HttpRequest, __construct)
787 {
788 char *URL = NULL;
789 int URL_len;
790 long meth = -1;
791 zval *options = NULL;
792
793 SET_EH_THROW_HTTP();
794 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla", &URL, &URL_len, &meth, &options)) {
795 if (URL) {
796 UPD_STRL(url, URL, URL_len);
797 }
798 if (meth > -1) {
799 UPD_PROP(long, method, meth);
800 }
801 if (options) {
802 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
803 }
804 }
805 SET_EH_NORMAL();
806 }
807 /* }}} */
808
809 /* {{{ proto bool HttpRequest::setOptions([array options])
810 *
811 * Set the request options to use. See http_get() for a full list of available options.
812 *
813 * Accepts an array as optional parameters, which values will overwrite the
814 * currently set request options. If the parameter is empty or omitted,
815 * the options of the HttpRequest object will be reset.
816 *
817 * Returns TRUE on success, or FALSE on failure.
818 */
819 PHP_METHOD(HttpRequest, setOptions)
820 {
821 char *key = NULL;
822 ulong idx = 0;
823 HashPosition pos;
824 zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt;
825
826 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
827 RETURN_FALSE;
828 }
829
830 MAKE_STD_ZVAL(new_opts);
831 array_init(new_opts);
832
833 if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
834 SET_PROP(options, new_opts);
835 zval_ptr_dtor(&new_opts);
836 RETURN_TRUE;
837 }
838
839 MAKE_STD_ZVAL(add_opts);
840 array_init(add_opts);
841 /* some options need extra attention -- thus cannot use array_merge() directly */
842 FOREACH_KEYVAL(pos, opts, key, idx, opt) {
843 if (key) {
844 if (!strcmp(key, "headers")) {
845 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, *opt);
846 } else if (!strcmp(key, "cookies")) {
847 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt);
848 } else if (!strcmp(key, "ssl")) {
849 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt);
850 } else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) {
851 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt);
852 } else if (!strcmp(key, "method")) {
853 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt);
854 } else {
855 ZVAL_ADDREF(*opt);
856 add_assoc_zval(add_opts, key, *opt);
857 }
858 /* reset */
859 key = NULL;
860 }
861 }
862
863 old_opts = GET_PROP(options);
864 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
865 array_copy(old_opts, new_opts);
866 }
867 array_merge(add_opts, new_opts);
868 SET_PROP(options, new_opts);
869 zval_ptr_dtor(&new_opts);
870 zval_ptr_dtor(&add_opts);
871
872 RETURN_TRUE;
873 }
874 /* }}} */
875
876 /* {{{ proto array HttpRequest::getOptions()
877 *
878 * Get currently set options.
879 *
880 * Returns an associative array containing currently set options.
881 */
882 PHP_METHOD(HttpRequest, getOptions)
883 {
884 NO_ARGS;
885
886 IF_RETVAL_USED {
887 RETURN_PROP(options);
888 }
889 }
890 /* }}} */
891
892 /* {{{ proto bool HttpRequest::setSslOptions([array options])
893 *
894 * Set SSL options.
895 *
896 * Accepts an associative array as parameter containing any SSL specific options.
897 * If the parameter is empty or omitted, the SSL options will be reset.
898 *
899 * Returns TRUE on success, or FALSE on failure.
900 */
901 PHP_METHOD(HttpRequest, setSslOptions)
902 {
903 http_request_object_set_options_subr("ssl", 1, 0);
904 }
905 /* }}} */
906
907 /* {{{ proto bool HttpRequest::addSslOptions(array options)
908 *
909 * Set additional SSL options.
910 *
911 * Expects an associative array as parameter containing additional SSL specific options.
912 *
913 * Returns TRUE on success, or FALSE on failure.
914 */
915 PHP_METHOD(HttpRequest, addSslOptions)
916 {
917 http_request_object_set_options_subr("ssl", 0, 0);
918 }
919 /* }}} */
920
921 /* {{{ proto array HttpRequest::getSslOtpions()
922 *
923 * Get previously set SSL options.
924 *
925 * Returns an associative array containing any previously set SSL options.
926 */
927 PHP_METHOD(HttpRequest, getSslOptions)
928 {
929 http_request_object_get_options_subr("ssl");
930 }
931 /* }}} */
932
933 /* {{{ proto bool HttpRequest::addHeaders(array headers)
934 *
935 * Add request header name/value pairs.
936 *
937 * Expects an associative array as parameter containing additional header
938 * name/value pairs.
939 *
940 * Returns TRUE on success, or FALSE on failure.
941 */
942 PHP_METHOD(HttpRequest, addHeaders)
943 {
944 http_request_object_set_options_subr("headers", 0, 1);
945 }
946
947 /* {{{ proto bool HttpRequest::setHeaders([array headers])
948 *
949 * Set request header name/value pairs.
950 *
951 * Accepts an associative array as parameter containing header name/value pairs.
952 * If the parameter is empty or omitted, all previously set headers will be unset.
953 *
954 * Returns TRUE on success, or FALSE on failure.
955 */
956 PHP_METHOD(HttpRequest, setHeaders)
957 {
958 http_request_object_set_options_subr("headers", 1, 1);
959 }
960 /* }}} */
961
962 /* {{{ proto array HttpRequest::getHeaders()
963 *
964 * Get previously set request headers.
965 *
966 * Returns an associative array containing all currently set headers.
967 */
968 PHP_METHOD(HttpRequest, getHeaders)
969 {
970 http_request_object_get_options_subr("headers");
971 }
972 /* }}} */
973
974 /* {{{ proto bool HttpRequest::setCookies([array cookies])
975 *
976 * Set cookies.
977 *
978 * Accepts an associative array as parameter containing cookie name/value pairs.
979 * If the parameter is empty or omitted, all previously set cookies will be unset.
980 *
981 * Returns TRUE on success, or FALSE on failure.
982 */
983 PHP_METHOD(HttpRequest, setCookies)
984 {
985 http_request_object_set_options_subr("cookies", 1, 0);
986 }
987 /* }}} */
988
989 /* {{{ proto bool HttpRequest::addCookies(array cookies)
990 *
991 * Add cookies.
992 *
993 * Expects an associative array as parameter containing any cookie name/value
994 * pairs to add.
995 *
996 * Returns TRUE on success, or FALSE on failure.
997 */
998 PHP_METHOD(HttpRequest, addCookies)
999 {
1000 http_request_object_set_options_subr("cookies", 0, 0);
1001 }
1002 /* }}} */
1003
1004 /* {{{ proto array HttpRequest::getCookies()
1005 *
1006 * Get previously set cookies.
1007 *
1008 * Returns an associative array containing any previously set cookies.
1009 */
1010 PHP_METHOD(HttpRequest, getCookies)
1011 {
1012 http_request_object_get_options_subr("cookies");
1013 }
1014 /* }}} */
1015
1016 /* {{{ proto bool HttpRequest::setUrl(string url)
1017 *
1018 * Set the request URL.
1019 *
1020 * Expects a string as parameter specifying the request url.
1021 *
1022 * Returns TRUE on success, or FALSE on failure.
1023 */
1024 PHP_METHOD(HttpRequest, setUrl)
1025 {
1026 char *URL = NULL;
1027 int URL_len;
1028
1029 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
1030 RETURN_FALSE;
1031 }
1032
1033 UPD_STRL(url, URL, URL_len);
1034 RETURN_TRUE;
1035 }
1036 /* }}} */
1037
1038 /* {{{ proto string HttpRequest::getUrl()
1039 *
1040 * Get the previously set request URL.
1041 *
1042 * Returns the currently set request url as string.
1043 */
1044 PHP_METHOD(HttpRequest, getUrl)
1045 {
1046 NO_ARGS;
1047
1048 IF_RETVAL_USED {
1049 RETURN_PROP(url);
1050 }
1051 }
1052 /* }}} */
1053
1054 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1055 *
1056 * Set the request method.
1057 *
1058 * Expects an int as parameter specifying the request method to use.
1059 * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
1060 *
1061 * Returns TRUE on success, or FALSE on failure.
1062 */
1063 PHP_METHOD(HttpRequest, setMethod)
1064 {
1065 long meth;
1066
1067 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
1068 RETURN_FALSE;
1069 }
1070
1071 UPD_PROP(long, method, meth);
1072 RETURN_TRUE;
1073 }
1074 /* }}} */
1075
1076 /* {{{ proto int HttpRequest::getMethod()
1077 *
1078 * Get the previously set request method.
1079 *
1080 * Returns the currently set request method.
1081 */
1082 PHP_METHOD(HttpRequest, getMethod)
1083 {
1084 NO_ARGS;
1085
1086 IF_RETVAL_USED {
1087 RETURN_PROP(method);
1088 }
1089 }
1090 /* }}} */
1091
1092 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1093 *
1094 * Set the content type the post request should have.
1095 *
1096 * Expects a string as parameters containing the content type of the request
1097 * (primary/secondary).
1098 *
1099 * Returns TRUE on success, or FALSE if the content type does not seem to
1100 * contain a primary and a secondary part.
1101 */
1102 PHP_METHOD(HttpRequest, setContentType)
1103 {
1104 char *ctype;
1105 int ct_len;
1106
1107 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1108 RETURN_FALSE;
1109 }
1110
1111 if (ct_len) {
1112 HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
1113 }
1114 UPD_STRL(contentType, ctype, ct_len);
1115 RETURN_TRUE;
1116 }
1117 /* }}} */
1118
1119 /* {{{ proto string HttpRequest::getContentType()
1120 *
1121 * Get the previously content type.
1122 *
1123 * Returns the previously set content type as string.
1124 */
1125 PHP_METHOD(HttpRequest, getContentType)
1126 {
1127 NO_ARGS;
1128
1129 IF_RETVAL_USED {
1130 RETURN_PROP(contentType);
1131 }
1132 }
1133 /* }}} */
1134
1135 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1136 *
1137 * Set the URL query parameters to use, overwriting previously set query parameters.
1138 * Affects any request types.
1139 *
1140 * Accepts a string or associative array parameter containing the pre-encoded
1141 * query string or to be encoded query fields. If the parameter is empty or
1142 * omitted, the query data will be unset.
1143 *
1144 * Returns TRUE on success, or FALSE on failure.
1145 */
1146 PHP_METHOD(HttpRequest, setQueryData)
1147 {
1148 zval *qdata = NULL;
1149
1150 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
1151 RETURN_FALSE;
1152 }
1153
1154 if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
1155 UPD_STRL(queryData, "", 0);
1156 } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1157 char *query_data = NULL;
1158
1159 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1160 RETURN_FALSE;
1161 }
1162
1163 UPD_PROP(string, queryData, query_data);
1164 efree(query_data);
1165 } else {
1166 zval *orig = qdata;
1167
1168 convert_to_string_ex(&qdata);
1169 UPD_STRL(queryData, Z_STRVAL_P(qdata), Z_STRLEN_P(qdata));
1170 if (orig != qdata) {
1171 zval_ptr_dtor(&qdata);
1172 }
1173 }
1174 RETURN_TRUE;
1175 }
1176 /* }}} */
1177
1178 /* {{{ proto string HttpRequest::getQueryData()
1179 *
1180 * Get the current query data in form of an urlencoded query string.
1181 *
1182 * Returns a string containing the urlencoded query.
1183 */
1184 PHP_METHOD(HttpRequest, getQueryData)
1185 {
1186 NO_ARGS;
1187
1188 IF_RETVAL_USED {
1189 RETURN_PROP(queryData);
1190 }
1191 }
1192 /* }}} */
1193
1194 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1195 *
1196 * Add parameters to the query parameter list, leaving previously set unchanged.
1197 * Affects any request type.
1198 *
1199 * Expects an associative array as parameter containing the query fields to add.
1200 *
1201 * Returns TRUE on success, or FALSE on failure.
1202 */
1203 PHP_METHOD(HttpRequest, addQueryData)
1204 {
1205 zval *qdata, *old_qdata;
1206 char *query_data = NULL;
1207 size_t query_data_len = 0;
1208
1209 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &qdata)) {
1210 RETURN_FALSE;
1211 }
1212
1213 old_qdata = GET_PROP(queryData);
1214
1215 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)) {
1216 RETURN_FALSE;
1217 }
1218
1219 UPD_STRL(queryData, query_data, query_data_len);
1220 efree(query_data);
1221
1222 RETURN_TRUE;
1223 }
1224 /* }}} */
1225
1226 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1227 *
1228 * Adds POST data entries, leaving previously set unchanged, unless a
1229 * post entry with the same name already exists.
1230 * Affects only POST and custom requests.
1231 *
1232 * Expects an associative array as parameter containing the post fields.
1233 *
1234 * Returns TRUE on success, or FALSE on failure.
1235 */
1236 PHP_METHOD(HttpRequest, addPostFields)
1237 {
1238 zval *post_data, *old_post, *new_post;
1239
1240 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &post_data)) {
1241 RETURN_FALSE;
1242 }
1243
1244 if (zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1245 MAKE_STD_ZVAL(new_post);
1246 array_init(new_post);
1247 old_post = GET_PROP(postFields);
1248 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1249 array_copy(old_post, new_post);
1250 }
1251 array_merge(post_data, new_post);
1252 SET_PROP(postFields, new_post);
1253 zval_ptr_dtor(&new_post);
1254 }
1255
1256 RETURN_TRUE;
1257 }
1258 /* }}} */
1259
1260 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1261 *
1262 * Set the POST data entries, overwriting previously set POST data.
1263 * Affects only POST and custom requests.
1264 *
1265 * Accepts an associative array as parameter containing the post fields.
1266 * If the parameter is empty or omitted, the post data will be unset.
1267 *
1268 * Returns TRUE on success, or FALSE on failure.
1269 */
1270 PHP_METHOD(HttpRequest, setPostFields)
1271 {
1272 zval *post, *post_data = NULL;
1273
1274 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &post_data)) {
1275 RETURN_FALSE;
1276 }
1277
1278 MAKE_STD_ZVAL(post);
1279 array_init(post);
1280 if (post_data && zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1281 array_copy(post_data, post);
1282 }
1283 SET_PROP(postFields, post);
1284 zval_ptr_dtor(&post);
1285
1286 RETURN_TRUE;
1287 }
1288 /* }}}*/
1289
1290 /* {{{ proto array HttpRequest::getPostFields()
1291 *
1292 * Get previously set POST data.
1293 *
1294 * Returns the currently set post fields as associative array.
1295 */
1296 PHP_METHOD(HttpRequest, getPostFields)
1297 {
1298 NO_ARGS;
1299
1300 IF_RETVAL_USED {
1301 RETURN_PROP(postFields);
1302 }
1303 }
1304 /* }}} */
1305
1306 /* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
1307 *
1308 * Set raw post data to send, overwriting previously set raw post data. Don't
1309 * forget to specify a content type. Affects only POST and custom requests.
1310 * Only either post fields or raw post data can be used for each request.
1311 * Raw post data has higher precedence and will be used even if post fields
1312 * are set.
1313 *
1314 * Accepts a string as parameter containing the *raw* post data.
1315 *
1316 * Returns TRUE on success, or FALSE on failure.
1317 */
1318 PHP_METHOD(HttpRequest, setRawPostData)
1319 {
1320 char *raw_data = NULL;
1321 int data_len = 0;
1322
1323 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &raw_data, &data_len)) {
1324 RETURN_FALSE;
1325 }
1326
1327 if (!raw_data) {
1328 raw_data = "";
1329 }
1330
1331 UPD_STRL(rawPostData, raw_data, data_len);
1332 RETURN_TRUE;
1333 }
1334 /* }}} */
1335
1336 /* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
1337 *
1338 * Add raw post data, leaving previously set raw post data unchanged.
1339 * Affects only POST and custom requests.
1340 *
1341 * Expects a string as parameter containing the raw post data to concatenate.
1342 *
1343 * Returns TRUE on success, or FALSE on failure.
1344 */
1345 PHP_METHOD(HttpRequest, addRawPostData)
1346 {
1347 char *raw_data;
1348 int data_len;
1349
1350 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw_data, &data_len)) {
1351 RETURN_FALSE;
1352 }
1353
1354 if (data_len) {
1355 zval *data = GET_PROP(rawPostData);
1356
1357 if (Z_STRLEN_P(data)) {
1358 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1359 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1360 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len);
1361 } else {
1362 UPD_STRL(putData, raw_data, data_len);
1363 }
1364 }
1365
1366 RETURN_TRUE;
1367 }
1368 /* }}} */
1369
1370 /* {{{ proto string HttpRequest::getRawPostData()
1371 *
1372 * Get previously set raw post data.
1373 *
1374 * Returns a string containing the currently set raw post data.
1375 */
1376 PHP_METHOD(HttpRequest, getRawPostData)
1377 {
1378 NO_ARGS;
1379
1380 IF_RETVAL_USED {
1381 RETURN_PROP(rawPostData);
1382 }
1383 }
1384 /* }}} */
1385
1386 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1387 *
1388 * Add a file to the POST request, leaving previously set files unchanged.
1389 * Affects only POST and custom requests. Cannot be used with raw post data.
1390 *
1391 * Expects a string parameter containing the form element name, and a string
1392 * paremeter containing the path to the file which should be uploaded.
1393 * Additionally accepts an optional string parameter which should contain
1394 * the content type of the file.
1395 *
1396 * Returns TRUE on success, or FALSE if the content type seems not to contain a
1397 * primary and a secondary content type part.
1398 */
1399 PHP_METHOD(HttpRequest, addPostFile)
1400 {
1401 zval *entry, *old_post, *new_post;
1402 char *name, *file, *type = NULL;
1403 int name_len, file_len, type_len = 0;
1404
1405 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1406 RETURN_FALSE;
1407 }
1408
1409 if (type_len) {
1410 HTTP_CHECK_CONTENT_TYPE(type, RETURN_FALSE);
1411 } else {
1412 type = "application/x-octetstream";
1413 type_len = sizeof("application/x-octetstream") - 1;
1414 }
1415
1416 MAKE_STD_ZVAL(entry);
1417 array_init(entry);
1418
1419 add_assoc_stringl(entry, "name", name, name_len, 1);
1420 add_assoc_stringl(entry, "type", type, type_len, 1);
1421 add_assoc_stringl(entry, "file", file, file_len, 1);
1422
1423 MAKE_STD_ZVAL(new_post);
1424 array_init(new_post);
1425 old_post = GET_PROP(postFiles);
1426 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1427 array_copy(old_post, new_post);
1428 }
1429 add_next_index_zval(new_post, entry);
1430 SET_PROP(postFiles, new_post);
1431 zval_ptr_dtor(&new_post);
1432
1433 RETURN_TRUE;
1434 }
1435 /* }}} */
1436
1437 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1438 *
1439 * Set files to post, overwriting previously set post files.
1440 * Affects only POST and requests. Cannot be used with raw post data.
1441 *
1442 * Accepts an array containing the files to post. Each entry should be an
1443 * associative array with "name", "file" and "type" keys. If the parameter
1444 * is empty or omitted the post files will be unset.
1445 *
1446 * Returns TRUE on success, or FALSE on failure.
1447 */
1448 PHP_METHOD(HttpRequest, setPostFiles)
1449 {
1450 zval *files = NULL, *post;
1451
1452 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!/", &files)) {
1453 RETURN_FALSE;
1454 }
1455
1456 MAKE_STD_ZVAL(post);
1457 array_init(post);
1458 if (files && (Z_TYPE_P(files) == IS_ARRAY)) {
1459 array_copy(files, post);
1460 }
1461 SET_PROP(postFiles, post);
1462 zval_ptr_dtor(&post);
1463
1464 RETURN_TRUE;
1465 }
1466 /* }}} */
1467
1468 /* {{{ proto array HttpRequest::getPostFiles()
1469 *
1470 * Get all previously added POST files.
1471 *
1472 * Returns an array containing currently set post files.
1473 */
1474 PHP_METHOD(HttpRequest, getPostFiles)
1475 {
1476 NO_ARGS;
1477
1478 IF_RETVAL_USED {
1479 RETURN_PROP(postFiles);
1480 }
1481 }
1482 /* }}} */
1483
1484 /* {{{ proto bool HttpRequest::setPutFile([string file])
1485 *
1486 * Set file to put. Affects only PUT requests.
1487 *
1488 * Accepts a string as parameter referencing the path to file.
1489 * If the parameter is empty or omitted the put file will be unset.
1490 *
1491 * Returns TRUE on success, or FALSE on failure.
1492 */
1493 PHP_METHOD(HttpRequest, setPutFile)
1494 {
1495 char *file = "";
1496 int file_len = 0;
1497
1498 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &file, &file_len)) {
1499 RETURN_FALSE;
1500 }
1501
1502 UPD_STRL(putFile, file, file_len);
1503 RETURN_TRUE;
1504 }
1505 /* }}} */
1506
1507 /* {{{ proto string HttpRequest::getPutFile()
1508 *
1509 * Get previously set put file.
1510 *
1511 * Returns a string containing the path to the currently set put file.
1512 */
1513 PHP_METHOD(HttpRequest, getPutFile)
1514 {
1515 NO_ARGS;
1516
1517 IF_RETVAL_USED {
1518 RETURN_PROP(putFile);
1519 }
1520 }
1521 /* }}} */
1522
1523 /* {{{ proto bool HttpRequest::setPutData([string put_data])
1524 *
1525 * Set PUT data to send, overwriting previously set PUT data.
1526 * Affects only PUT requests.
1527 * Only either PUT data or PUT file can be used for each request.
1528 * PUT data has higher precedence and will be used even if a PUT
1529 * file is set.
1530 *
1531 * Accepts a string as parameter containing the data to upload.
1532 *
1533 * Returns TRUE on success, or FALSE on failure.
1534 */
1535 PHP_METHOD(HttpRequest, setPutData)
1536 {
1537 char *put_data = NULL;
1538 int data_len = 0;
1539
1540 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &put_data, &data_len)) {
1541 RETURN_FALSE;
1542 }
1543
1544 if (!put_data) {
1545 put_data = "";
1546 }
1547
1548 UPD_STRL(putData, put_data, data_len);
1549 RETURN_TRUE;
1550 }
1551 /* }}} */
1552
1553 /* {{{ proto bool HttpRequest::addPutData(string put_data)
1554 *
1555 * Add PUT data, leaving previously set PUT data unchanged.
1556 * Affects only PUT requests.
1557 *
1558 * Expects a string as parameter containing the data to concatenate.
1559 *
1560 * Returns TRUE on success, or FALSE on failure.
1561 */
1562 PHP_METHOD(HttpRequest, addPutData)
1563 {
1564 char *put_data;
1565 int data_len;
1566
1567 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &put_data, &data_len)) {
1568 RETURN_FALSE;
1569 }
1570
1571 if (data_len) {
1572 zval *data = GET_PROP(putData);
1573
1574 if (Z_STRLEN_P(data)) {
1575 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1576 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1577 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, put_data, data_len);
1578 } else {
1579 UPD_STRL(putData, put_data, data_len);
1580 }
1581 }
1582
1583 RETURN_TRUE;
1584 }
1585 /* }}} */
1586
1587 /* {{{ proto string HttpRequest::getPutData()
1588 *
1589 * Get previously set PUT data.
1590 *
1591 * Returns a string containing the currently set raw post data.
1592 */
1593 PHP_METHOD(HttpRequest, getPutData)
1594 {
1595 NO_ARGS;
1596
1597 IF_RETVAL_USED {
1598 RETURN_PROP(putData);
1599 }
1600 }
1601 /* }}} */
1602
1603 /* {{{ proto array HttpRequest::getResponseData()
1604 *
1605 * Get all response data after the request has been sent.
1606 *
1607 * Returns an associative array with the key "headers" containing an associative
1608 * array holding all response headers, as well as the key "body" containing a
1609 * string with the response body.
1610 *
1611 * If redirects were allowed and several responses were received, the data
1612 * references the last received response.
1613 */
1614 PHP_METHOD(HttpRequest, getResponseData)
1615 {
1616 NO_ARGS;
1617
1618 IF_RETVAL_USED {
1619 RETURN_PROP(responseData);
1620 }
1621 }
1622 /* }}} */
1623
1624 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1625 *
1626 * Get response header(s) after the request has been sent.
1627 *
1628 * Accepts an string as optional parameter specifying a certain header to read.
1629 * If the parameter is empty or omitted all response headers will be returned.
1630 *
1631 * Returns either a string with the value of the header matching name if requested,
1632 * FALSE on failure, or an associative array containing all response headers.
1633 *
1634 * If redirects were allowed and several responses were received, the data
1635 * references the last received response.
1636 */
1637 PHP_METHOD(HttpRequest, getResponseHeader)
1638 {
1639 IF_RETVAL_USED {
1640 zval *data, **headers, **header;
1641 char *header_name = NULL;
1642 int header_len = 0;
1643
1644 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1645 RETURN_FALSE;
1646 }
1647
1648 data = GET_PROP(responseData);
1649 if ( (Z_TYPE_P(data) == IS_ARRAY) &&
1650 (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) &&
1651 (Z_TYPE_PP(headers) == IS_ARRAY)) {
1652 if (!header_len || !header_name) {
1653 RETVAL_ZVAL(*headers, 1, 0);
1654 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1655 RETVAL_ZVAL(*header, 1, 0);
1656 } else {
1657 RETVAL_FALSE;
1658 }
1659 } else {
1660 RETVAL_FALSE;
1661 }
1662 }
1663 }
1664 /* }}} */
1665
1666 /* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]])
1667 *
1668 * Get response cookie(s) after the request has been sent.
1669 *
1670 * Returns an array of stdClass objects like http_parse_cookie would return.
1671 *
1672 * If redirects were allowed and several responses were received, the data
1673 * references the last received response.
1674 */
1675 PHP_METHOD(HttpRequest, getResponseCookies)
1676 {
1677 IF_RETVAL_USED {
1678 long flags = 0;
1679 zval *allowed_extras_array = NULL, *data, **headers;
1680
1681 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la", &flags, &allowed_extras_array)) {
1682 RETURN_FALSE;
1683 }
1684
1685 data = GET_PROP(responseData);
1686 if ( (Z_TYPE_P(data) == IS_ARRAY) &&
1687 (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) &&
1688 (Z_TYPE_PP(headers) == IS_ARRAY)) {
1689 int i = 0;
1690 ulong idx = 0;
1691 char *key = NULL, **allowed_extras = NULL;
1692 zval **header = NULL, **entry = NULL;
1693 HashPosition pos, pos1, pos2;
1694
1695 array_init(return_value);
1696
1697 if (allowed_extras_array) {
1698 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
1699 FOREACH_VAL(pos, allowed_extras_array, entry) {
1700 ZVAL_ADDREF(*entry);
1701 convert_to_string_ex(entry);
1702 allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
1703 zval_ptr_dtor(entry);
1704 }
1705 }
1706
1707 FOREACH_HASH_KEYVAL(pos1, Z_ARRVAL_PP(headers), key, idx, header) {
1708 if (key && !strcasecmp(key, "Set-Cookie")) {
1709 http_cookie_list list;
1710
1711 if (Z_TYPE_PP(header) == IS_ARRAY) {
1712 zval **single_header;
1713
1714 FOREACH_VAL(pos2, *header, single_header) {
1715 ZVAL_ADDREF(*single_header);
1716 convert_to_string_ex(single_header);
1717 if (http_parse_cookie_ex(&list, Z_STRVAL_PP(single_header), flags, allowed_extras)) {
1718 zval *cookie;
1719
1720 MAKE_STD_ZVAL(cookie);
1721 object_init(cookie);
1722 http_cookie_list_tostruct(&list, cookie);
1723 add_next_index_zval(return_value, cookie);
1724 http_cookie_list_dtor(&list);
1725 }
1726 zval_ptr_dtor(single_header);
1727 }
1728 } else {
1729 ZVAL_ADDREF(*header);
1730 convert_to_string_ex(header);
1731 if (http_parse_cookie_ex(&list, Z_STRVAL_PP(header), flags, allowed_extras)) {
1732 zval *cookie;
1733
1734 MAKE_STD_ZVAL(cookie);
1735 object_init(cookie);
1736 http_cookie_list_tostruct(&list, cookie);
1737 add_next_index_zval(return_value, cookie);
1738 http_cookie_list_dtor(&list);
1739 }
1740 zval_ptr_dtor(header);
1741 }
1742 }
1743 /* reset key */
1744 key = NULL;
1745 }
1746
1747 if (allowed_extras) {
1748 for (i = 0; allowed_extras[i]; ++i) {
1749 efree(allowed_extras[i]);
1750 }
1751 efree(allowed_extras);
1752 }
1753 } else {
1754 RETURN_FALSE;
1755 }
1756 }
1757 }
1758 /* }}} */
1759
1760 /* {{{ proto string HttpRequest::getResponseBody()
1761 *
1762 * Get the response body after the request has been sent.
1763 *
1764 * Returns a string containing the response body.
1765 *
1766 * If redirects were allowed and several responses were received, the data
1767 * references the last received response.
1768 */
1769 PHP_METHOD(HttpRequest, getResponseBody)
1770 {
1771 NO_ARGS;
1772
1773 IF_RETVAL_USED {
1774 zval **body;
1775 zval *data = GET_PROP(responseData);
1776
1777 if ( (Z_TYPE_P(data) == IS_ARRAY) &&
1778 (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body))) {
1779 RETURN_ZVAL(*body, 1, 0);
1780 } else {
1781 RETURN_FALSE;
1782 }
1783 }
1784 }
1785 /* }}} */
1786
1787 /* {{{ proto int HttpRequest::getResponseCode()
1788 *
1789 * Get the response code after the request has been sent.
1790 *
1791 * Returns an int representing the response code.
1792 *
1793 * If redirects were allowed and several responses were received, the data
1794 * references the last received response.
1795 */
1796 PHP_METHOD(HttpRequest, getResponseCode)
1797 {
1798 NO_ARGS;
1799
1800 IF_RETVAL_USED {
1801 RETURN_PROP(responseCode);
1802 }
1803 }
1804 /* }}} */
1805
1806 /* {{{ proto string HttpRequest::getResponseStatus()
1807 *
1808 * Get the response status (i.e. the string after the response code) after the message has been sent.
1809 *
1810 * Returns a string containing the response status text.
1811 */
1812 PHP_METHOD(HttpRequest, getResponseStatus)
1813 {
1814 NO_ARGS;
1815
1816 IF_RETVAL_USED {
1817 RETURN_PROP(responseStatus);
1818 }
1819 }
1820 /* }}} */
1821
1822 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1823 *
1824 * Get response info after the request has been sent.
1825 * See http_get() for a full list of returned info.
1826 *
1827 * Accepts a string as optional parameter specifying the info to read.
1828 * If the parameter is empty or omitted, an associative array containing
1829 * all available info will be returned.
1830 *
1831 * Returns either a scalar containing the value of the info matching name if
1832 * requested, FALSE on failure, or an associative array containing all
1833 * available info.
1834 *
1835 * If redirects were allowed and several responses were received, the data
1836 * references the last received response.
1837 */
1838 PHP_METHOD(HttpRequest, getResponseInfo)
1839 {
1840 IF_RETVAL_USED {
1841 zval *info, **infop;
1842 char *info_name = NULL;
1843 int info_len = 0;
1844
1845 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1846 RETURN_FALSE;
1847 }
1848
1849 info = GET_PROP(responseInfo);
1850
1851 if (Z_TYPE_P(info) != IS_ARRAY) {
1852 RETURN_FALSE;
1853 }
1854
1855 if (info_len && info_name) {
1856 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1857 RETURN_ZVAL(*infop, 1, 0);
1858 } else {
1859 http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
1860 RETURN_FALSE;
1861 }
1862 } else {
1863 RETURN_ZVAL(info, 1, 0);
1864 }
1865 }
1866 }
1867 /* }}}*/
1868
1869 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1870 *
1871 * Get the full response as HttpMessage object after the request has been sent.
1872 *
1873 * Returns an HttpMessage object of the response.
1874 *
1875 * If redirects were allowed and several responses were received, the data
1876 * references the last received response. Use HttpMessage::getParentMessage()
1877 * to access the data of previously received responses within this request
1878 * cycle.
1879 *
1880 * Throws HttpException, HttpRuntimeException.
1881 */
1882 PHP_METHOD(HttpRequest, getResponseMessage)
1883 {
1884 NO_ARGS {
1885 zval *message;
1886
1887 SET_EH_THROW_HTTP();
1888 message = GET_PROP(responseMessage);
1889 if (Z_TYPE_P(message) == IS_OBJECT) {
1890 RETVAL_OBJECT(message, 1);
1891 } else {
1892 http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpRequest does not contain a response message");
1893 }
1894 SET_EH_NORMAL();
1895 }
1896 }
1897 /* }}} */
1898
1899 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1900 *
1901 * Get sent HTTP message.
1902 *
1903 * Returns an HttpMessage object representing the sent request.
1904 *
1905 * If redirects were allowed and several responses were received, the data
1906 * references the last received response. Use HttpMessage::getParentMessage()
1907 * to access the data of previously sent requests within this request
1908 * cycle.
1909 *
1910 * Note that the internal request message is immutable, that means that the
1911 * request message received through HttpRequest::getRequestMessage() will
1912 * always look the same for the same request, regardless of any changes you
1913 * may have made to the returned object.
1914 *
1915 * Throws HttpMalformedHeadersException, HttpEncodingException.
1916 */
1917 PHP_METHOD(HttpRequest, getRequestMessage)
1918 {
1919 NO_ARGS;
1920
1921 IF_RETVAL_USED {
1922 http_message *msg;
1923 getObject(http_request_object, obj);
1924
1925 SET_EH_THROW_HTTP();
1926 if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
1927 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
1928 }
1929 SET_EH_NORMAL();
1930 }
1931 }
1932 /* }}} */
1933
1934 /* {{{ proto string HttpRequest::getRawRequestMessage()
1935 *
1936 * Get sent HTTP message.
1937 *
1938 * Returns an HttpMessage in a form of a string
1939 */
1940 PHP_METHOD(HttpRequest, getRawRequestMessage)
1941 {
1942 NO_ARGS;
1943
1944 IF_RETVAL_USED {
1945 getObject(http_request_object, obj);
1946
1947 RETURN_PHPSTR_DUP(&obj->request->conv.request);
1948 }
1949 }
1950 /* }}} */
1951
1952 /* {{{ proto string HttpRequest::getRawResponseMessage()
1953 *
1954 * Get the entire HTTP response.
1955 *
1956 * Returns the complete web server response, including the headers in a form of a string.
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