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