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