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