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