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