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