6e798f5fc5c4263eb8e8f1a67d40586a687d7fef
[m6w6/ext-http] / http.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #define _WINSOCKAPI_
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "php.h"
26 #include "php_ini.h"
27 #include "snprintf.h"
28 #include "ext/standard/info.h"
29 #include "ext/session/php_session.h"
30 #include "ext/standard/php_string.h"
31 #include "ext/standard/php_smart_str.h"
32
33 #include "SAPI.h"
34
35 #include "php_http.h"
36 #include "php_http_api.h"
37
38 #ifdef ZEND_ENGINE_2
39 # include "ext/standard/php_http.h"
40 #endif
41
42 #ifdef HTTP_HAVE_CURL
43
44 # ifdef PHP_WIN32
45 # include <winsock2.h>
46 # include <sys/types.h>
47 # endif
48
49 # include <curl/curl.h>
50
51 /* {{{ ARG_INFO */
52 # ifdef ZEND_BEGIN_ARG_INFO
53 ZEND_BEGIN_ARG_INFO(http_request_info_ref_3, 0)
54 ZEND_ARG_PASS_INFO(0)
55 ZEND_ARG_PASS_INFO(0)
56 ZEND_ARG_PASS_INFO(1)
57 ZEND_END_ARG_INFO();
58
59 ZEND_BEGIN_ARG_INFO(http_request_info_ref_4, 0)
60 ZEND_ARG_PASS_INFO(0)
61 ZEND_ARG_PASS_INFO(0)
62 ZEND_ARG_PASS_INFO(0)
63 ZEND_ARG_PASS_INFO(1)
64 ZEND_END_ARG_INFO();
65 # else
66 static unsigned char http_request_info_ref_3[] = {3, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
67 static unsigned char http_request_info_ref_4[] = {4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
68 # endif
69 /* }}} ARG_INFO */
70
71 #endif /* HTTP_HAVE_CURL */
72
73 ZEND_DECLARE_MODULE_GLOBALS(http)
74
75 #ifdef COMPILE_DL_HTTP
76 ZEND_GET_MODULE(http)
77 #endif
78
79 /* {{{ http_functions[] */
80 function_entry http_functions[] = {
81 PHP_FE(http_date, NULL)
82 PHP_FE(http_absolute_uri, NULL)
83 PHP_FE(http_negotiate_language, NULL)
84 PHP_FE(http_negotiate_charset, NULL)
85 PHP_FE(http_redirect, NULL)
86 PHP_FE(http_send_status, NULL)
87 PHP_FE(http_send_last_modified, NULL)
88 PHP_FE(http_send_content_type, NULL)
89 PHP_FE(http_send_content_disposition, NULL)
90 PHP_FE(http_match_modified, NULL)
91 PHP_FE(http_match_etag, NULL)
92 PHP_FE(http_cache_last_modified, NULL)
93 PHP_FE(http_cache_etag, NULL)
94 PHP_FE(http_send_data, NULL)
95 PHP_FE(http_send_file, NULL)
96 PHP_FE(http_send_stream, NULL)
97 PHP_FE(http_chunked_decode, NULL)
98 PHP_FE(http_split_response, NULL)
99 PHP_FE(http_parse_headers, NULL)
100 PHP_FE(http_get_request_headers, NULL)
101 #ifdef HTTP_HAVE_CURL
102 PHP_FE(http_get, http_request_info_ref_3)
103 PHP_FE(http_head, http_request_info_ref_3)
104 PHP_FE(http_post_data, http_request_info_ref_4)
105 PHP_FE(http_post_array, http_request_info_ref_4)
106 #endif
107 PHP_FE(http_auth_basic, NULL)
108 PHP_FE(http_auth_basic_cb, NULL)
109 #ifndef ZEND_ENGINE_2
110 PHP_FE(http_build_query, NULL)
111 #endif
112 PHP_FE(ob_httpetaghandler, NULL)
113 {NULL, NULL, NULL}
114 };
115 /* }}} */
116
117 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
118 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
119 #define NO_ARGS if (ZEND_NUM_ARGS()) WRONG_PARAM_COUNT
120 #define HTTP_URL_ARGSEP_OVERRIDE zend_alter_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, "&", 1, ZEND_INI_ALL, ZEND_INI_STAGE_RUNTIME)
121 #define HTTP_URL_ARGSEP_RESTORE zend_restore_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, ZEND_INI_STAGE_RUNTIME)
122
123 #define array_copy(src, dst) zend_hash_copy(Z_ARRVAL_P(dst), Z_ARRVAL_P(src), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
124 #define array_merge(src, dst) zend_hash_merge(Z_ARRVAL_P(dst), Z_ARRVAL_P(src), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *), 1)
125
126 #ifdef ZEND_ENGINE_2
127
128 # define HTTP_REGISTER_CLASS_EX(classname, name, parent, flags) \
129 { \
130 zend_class_entry ce; \
131 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
132 ce.create_object = name## _new_object; \
133 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
134 name## _ce->ce_flags |= flags; \
135 memcpy(& name## _object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
136 name## _object_handlers.clone_obj = NULL; \
137 name## _declare_default_properties(name## _ce); \
138 }
139
140 # define HTTP_REGISTER_CLASS(classname, name, parent, flags) \
141 { \
142 zend_class_entry ce; \
143 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
144 ce.create_object = NULL; \
145 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
146 name## _ce->ce_flags |= flags; \
147 }
148
149 # define getObject(t, o) t * o = ((t *) zend_object_store_get_object(getThis() TSRMLS_CC))
150 # define OBJ_PROP(o) o->zo.properties
151 # define DCL_PROP(a, t, n, v) zend_declare_property_ ##t(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC)
152 # define DCL_PROP_Z(a, n, v) zend_declare_property(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC)
153 # define DCL_PROP_N(a, n) zend_declare_property_null(ce, (#n), sizeof(#n), (ZEND_ACC_ ##a) TSRMLS_CC)
154 # define UPD_PROP(o, t, n, v) zend_update_property_ ##t(o->zo.ce, getThis(), (#n), sizeof(#n), (v) TSRMLS_CC)
155 # define SET_PROP(o, n, z) zend_update_property(o->zo.ce, getThis(), (#n), sizeof(#n), (z) TSRMLS_CC)
156 # define GET_PROP(o, n) zend_read_property(o->zo.ce, getThis(), (#n), sizeof(#n), 0 TSRMLS_CC)
157
158 # define INIT_PARR(o, n) \
159 { \
160 zval *__tmp; \
161 MAKE_STD_ZVAL(__tmp); \
162 array_init(__tmp); \
163 SET_PROP(o, n, __tmp); \
164 }
165
166 # define FREE_PARR(o, p) \
167 { \
168 zval *__tmp = NULL; \
169 if (__tmp = GET_PROP(o, p)) { \
170 zval_dtor(__tmp); \
171 FREE_ZVAL(__tmp); \
172 __tmp = NULL; \
173 } \
174 }
175
176 /* {{{ HTTPi */
177
178 zend_class_entry *httpi_ce;
179
180 #define HTTPi_ME(me, al, ai) ZEND_FENTRY(me, ZEND_FN(al), ai, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
181
182 zend_function_entry httpi_class_methods[] = {
183 HTTPi_ME(date, http_date, NULL)
184 HTTPi_ME(absoluteURI, http_absolute_uri, NULL)
185 HTTPi_ME(negotiateLanguage, http_negotiate_language, NULL)
186 HTTPi_ME(negotiateCharset, http_negotiate_charset, NULL)
187 HTTPi_ME(redirect, http_redirect, NULL)
188 HTTPi_ME(sendStatus, http_send_status, NULL)
189 HTTPi_ME(sendLastModified, http_send_last_modified, NULL)
190 HTTPi_ME(sendContentType, http_send_content_type, NULL)
191 HTTPi_ME(sendContentDisposition, http_send_content_disposition, NULL)
192 HTTPi_ME(matchModified, http_match_modified, NULL)
193 HTTPi_ME(matchEtag, http_match_etag, NULL)
194 HTTPi_ME(cacheLastModified, http_cache_last_modified, NULL)
195 HTTPi_ME(cacheEtag, http_cache_etag, NULL)
196 HTTPi_ME(chunkedDecode, http_chunked_decode, NULL)
197 HTTPi_ME(splitResponse, http_split_response, NULL)
198 HTTPi_ME(parseHeaders, http_parse_headers, NULL)
199 HTTPi_ME(getRequestHeaders, http_get_request_headers, NULL)
200 #ifdef HTTP_HAVE_CURL
201 HTTPi_ME(get, http_get, http_request_info_ref_3)
202 HTTPi_ME(head, http_head, http_request_info_ref_3)
203 HTTPi_ME(postData, http_post_data, http_request_info_ref_4)
204 HTTPi_ME(postArray, http_post_array, http_request_info_ref_4)
205 #endif
206 HTTPi_ME(authBasic, http_auth_basic, NULL)
207 HTTPi_ME(authBasicCallback, http_auth_basic_cb, NULL)
208 {NULL, NULL, NULL}
209 };
210 /* }}} HTTPi */
211
212 /* {{{ HTTPi_Response */
213
214 zend_class_entry *httpi_response_ce;
215 static zend_object_handlers httpi_response_object_handlers;
216
217 typedef struct {
218 zend_object zo;
219 } httpi_response_object;
220
221 #define httpi_response_declare_default_properties(ce) _httpi_response_declare_default_properties(ce TSRMLS_CC)
222 static inline void _httpi_response_declare_default_properties(zend_class_entry *ce TSRMLS_DC)
223 {
224 DCL_PROP(PROTECTED, string, contentType, "application/x-octetstream");
225 DCL_PROP(PROTECTED, string, eTag, "");
226 DCL_PROP(PROTECTED, string, dispoFile, "");
227 DCL_PROP(PROTECTED, string, cacheControl, "public");
228 DCL_PROP(PROTECTED, string, data, "");
229 DCL_PROP(PROTECTED, string, file, "");
230 DCL_PROP(PROTECTED, long, stream, 0);
231 DCL_PROP(PROTECTED, long, lastModified, 0);
232 DCL_PROP(PROTECTED, long, dispoInline, 0);
233 DCL_PROP(PROTECTED, long, cache, 0);
234 DCL_PROP(PROTECTED, long, gzip, 0);
235
236 DCL_PROP(PRIVATE, long, raw_cache_header, 0);
237 DCL_PROP(PRIVATE, long, send_mode, -1);
238 }
239
240 #define httpi_response_destroy_object _httpi_response_destroy_object
241 void _httpi_response_destroy_object(void *object, zend_object_handle handle TSRMLS_DC)
242 {
243 httpi_response_object *o = object;
244 if (OBJ_PROP(o)) {
245 zend_hash_destroy(OBJ_PROP(o));
246 FREE_HASHTABLE(OBJ_PROP(o));
247 }
248 efree(o);
249 }
250
251 #define httpi_response_new_object _httpi_response_new_object
252 zend_object_value _httpi_response_new_object(zend_class_entry *ce TSRMLS_DC)
253 {
254 zend_object_value ov;
255 httpi_response_object *o;
256
257 o = ecalloc(sizeof(httpi_response_object), 1);
258 o->zo.ce = ce;
259
260 ALLOC_HASHTABLE(OBJ_PROP(o));
261 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
262 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
263
264 ov.handle = zend_objects_store_put(o, httpi_response_destroy_object, NULL, NULL TSRMLS_CC);
265 ov.handlers = &httpi_response_object_handlers;
266
267 return ov;
268 }
269
270 zend_function_entry httpi_response_class_methods[] = {
271 PHP_ME(HTTPi_Response, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
272 /* PHP_ME(HTTPi_Response, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
273 */
274 PHP_ME(HTTPi_Response, setETag, NULL, ZEND_ACC_PUBLIC)
275 PHP_ME(HTTPi_Response, getETag, NULL, ZEND_ACC_PUBLIC)
276
277 PHP_ME(HTTPi_Response, setContentDisposition, NULL, ZEND_ACC_PUBLIC)
278 PHP_ME(HTTPi_Response, getContentDisposition, NULL, ZEND_ACC_PUBLIC)
279
280 PHP_ME(HTTPi_Response, setContentType, NULL, ZEND_ACC_PUBLIC)
281 PHP_ME(HTTPi_Response, getContentType, NULL, ZEND_ACC_PUBLIC)
282
283 PHP_ME(HTTPi_Response, setCache, NULL, ZEND_ACC_PUBLIC)
284 PHP_ME(HTTPi_Response, getCache, NULL, ZEND_ACC_PUBLIC)
285
286 PHP_ME(HTTPi_Response, setCacheControl, NULL, ZEND_ACC_PUBLIC)
287 PHP_ME(HTTPi_Response, getCacheControl, NULL, ZEND_ACC_PUBLIC)
288
289 PHP_ME(HTTPi_Response, setGzip, NULL, ZEND_ACC_PUBLIC)
290 PHP_ME(HTTPi_Response, getGzip, NULL, ZEND_ACC_PUBLIC)
291
292 PHP_ME(HTTPi_Response, setData, NULL, ZEND_ACC_PUBLIC)
293 PHP_ME(HTTPi_Response, getData, NULL, ZEND_ACC_PUBLIC)
294
295 PHP_ME(HTTPi_Response, setFile, NULL, ZEND_ACC_PUBLIC)
296 PHP_ME(HTTPi_Response, getFile, NULL, ZEND_ACC_PUBLIC)
297
298 PHP_ME(HTTPi_Response, setStream, NULL, ZEND_ACC_PUBLIC)
299 PHP_ME(HTTPi_Response, getStream, NULL, ZEND_ACC_PUBLIC)
300
301 PHP_ME(HTTPi_Response, send, NULL, ZEND_ACC_PUBLIC)
302
303 {NULL, NULL, NULL}
304 };
305
306 /* {{{ proto void HTTPi_Response::__construct(bool cache, bool gzip)
307 *
308 */
309 PHP_METHOD(HTTPi_Response, __construct)
310 {
311 zend_bool do_cache = 0, do_gzip = 0;
312 getObject(httpi_response_object, obj);
313
314 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
315 // throw exception
316 return;
317 }
318
319 UPD_PROP(obj, long, cache, do_cache);
320 UPD_PROP(obj, long, gzip, do_gzip);
321 }
322 /* }}} */
323
324 /* {{{ proto bool HTTPi_Response::setCache(bool cache)
325 *
326 */
327 PHP_METHOD(HTTPi_Response, setCache)
328 {
329 zend_bool do_cache = 0;
330 getObject(httpi_response_object, obj);
331
332 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
333 RETURN_FALSE;
334 }
335
336 UPD_PROP(obj, long, cache, do_cache);
337 RETURN_TRUE;
338 }
339 /* }}} */
340
341 /* {{{ proto bool HTTPi_Response::getCache()
342 *
343 */
344 PHP_METHOD(HTTPi_Response, getCache)
345 {
346 zval *do_cache = NULL;
347 getObject(httpi_response_object, obj);
348
349 NO_ARGS;
350
351 do_cache = GET_PROP(obj, cache);
352 RETURN_BOOL(Z_LVAL_P(do_cache));
353 }
354 /* }}}*/
355
356 /* {{{ proto bool HTTPi_Response::setGzip(bool gzip)
357 *
358 */
359 PHP_METHOD(HTTPi_Response, setGzip)
360 {
361 zend_bool do_gzip = 0;
362 getObject(httpi_response_object, obj);
363
364 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
365 RETURN_FALSE;
366 }
367
368 UPD_PROP(obj, long, gzip, do_gzip);
369 RETURN_TRUE;
370 }
371 /* }}} */
372
373 /* {{{ proto bool HTTPi_Response::getGzip()
374 *
375 */
376 PHP_METHOD(HTTPi_Response, getGzip)
377 {
378 zval *do_gzip = NULL;
379 getObject(httpi_response_object, obj);
380
381 NO_ARGS;
382
383 do_gzip = GET_PROP(obj, gzip);
384 RETURN_BOOL(Z_LVAL_P(do_gzip));
385 }
386 /* }}} */
387
388 /* {{{ proto bool HTTPi_Response::setCacheControl(string control[, bool raw = false])
389 *
390 */
391 PHP_METHOD(HTTPi_Response, setCacheControl)
392 {
393 char *ccontrol;
394 int cc_len;
395 zend_bool raw = 0;
396 getObject(httpi_response_object, obj);
397
398 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
399 RETURN_FALSE;
400 }
401
402 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
403 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
404 RETURN_FALSE;
405 }
406
407 UPD_PROP(obj, long, raw_cache_header, raw);
408 UPD_PROP(obj, string, cacheControl, ccontrol);
409 RETURN_TRUE;
410 }
411 /* }}} */
412
413 /* {{{ proto string HTTPi_Response::getCacheControl()
414 *
415 */
416 PHP_METHOD(HTTPi_Response, getCacheControl)
417 {
418 zval *ccontrol;
419 getObject(httpi_response_object, obj);
420
421 NO_ARGS;
422
423 ccontrol = GET_PROP(obj, cacheControl);
424 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
425 }
426 /* }}} */
427
428 /* {{{ proto bool HTTPi::setContentType(string content_type)
429 *
430 */
431 PHP_METHOD(HTTPi_Response, setContentType)
432 {
433 char *ctype;
434 int ctype_len;
435 getObject(httpi_response_object, obj);
436
437 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
438 RETURN_FALSE;
439 }
440
441 if (!strchr(ctype, '/')) {
442 php_error_docref(NULL TSRMLS_CC, E_WARNING,
443 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
444 RETURN_FALSE;
445 }
446
447 UPD_PROP(obj, string, contentType, ctype);
448
449 RETURN_TRUE;
450 }
451 /* }}} */
452
453 /* {{{ proto string HTTPi_Response::getContentType()
454 *
455 */
456 PHP_METHOD(HTTPi_Response, getContentType)
457 {
458 zval *ctype;
459 getObject(httpi_response_object, obj);
460
461 NO_ARGS;
462
463 ctype = GET_PROP(obj, contentType);
464 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
465 }
466 /* }}} */
467
468 /* {{{ proto bool HTTPi_Response::setContentDisposition(string filename[, bool inline = false])
469 *
470 */
471 PHP_METHOD(HTTPi_Response, setContentDisposition)
472 {
473 char *file;
474 int file_len;
475 zend_bool is_inline = 0;
476 getObject(httpi_response_object, obj);
477
478 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_inline)) {
479 RETURN_FALSE;
480 }
481
482 UPD_PROP(obj, string, dispoFile, file);
483 UPD_PROP(obj, long, dispoInline, is_inline);
484 RETURN_TRUE;
485 }
486 /* }}} */
487
488 /* {{{ proto array HTTPi_Response::getContentDisposition()
489 *
490 */
491 PHP_METHOD(HTTPi_Response, getContentDisposition)
492 {
493 zval *file;
494 zval *is_inline;
495 getObject(httpi_response_object, obj);
496
497 if (ZEND_NUM_ARGS()) {
498 WRONG_PARAM_COUNT;
499 }
500
501 file = GET_PROP(obj, dispoFile);
502 is_inline = GET_PROP(obj, dispoInline);
503
504 array_init(return_value);
505 add_assoc_stringl(return_value, "filename", Z_STRVAL_P(file), Z_STRLEN_P(file), 1);
506 add_assoc_bool(return_value, "inline", Z_LVAL_P(is_inline));
507 }
508 /* }}} */
509
510 /* {{{ proto bool HTTPi_Response::setETag(string etag)
511 *
512 */
513 PHP_METHOD(HTTPi_Response, setETag)
514 {
515 char *etag;
516 int etag_len;
517 getObject(httpi_response_object, obj);
518
519 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
520 RETURN_FALSE;
521 }
522
523 UPD_PROP(obj, string, eTag, etag);
524 RETURN_TRUE;
525 }
526 /* }}} */
527
528 /* {{{ proto string HTTPi_Response::getETag()
529 *
530 */
531 PHP_METHOD(HTTPi_Response, getETag)
532 {
533 zval *etag;
534 getObject(httpi_response_object, obj);
535
536 NO_ARGS;
537
538 etag = GET_PROP(obj, eTag);
539 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
540 }
541 /* }}} */
542
543 /* {{{ proto bool HTTPi_Response::setData(string data)
544 *
545 */
546 PHP_METHOD(HTTPi_Response, setData)
547 {
548 zval *the_data;
549 char *etag;
550 getObject(httpi_response_object, obj);
551
552 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_data)) {
553 RETURN_FALSE;
554 }
555
556 convert_to_string_ex(&the_data);
557 SET_PROP(obj, data, the_data);
558 UPD_PROP(obj, long, lastModified, http_lmod(the_data, SEND_DATA));
559 UPD_PROP(obj, long, send_mode, SEND_DATA);
560 RETURN_TRUE;
561 }
562 /* }}} */
563
564 /* {{{ proto string HTTPi_Response::getData()
565 *
566 */
567 PHP_METHOD(HTTPi_Response, getData)
568 {
569 zval *the_data;
570 getObject(httpi_response_object, obj);
571
572 NO_ARGS;
573
574 the_data = GET_PROP(obj, data);
575 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
576 }
577 /* }}} */
578
579 /* {{{ proto bool HTTPi_Response::setStream(resource stream)
580 *
581 */
582 PHP_METHOD(HTTPi_Response, setStream)
583 {
584 zval *the_stream;
585 php_stream *the_real_stream;
586 char *etag;
587 getObject(httpi_response_object, obj);
588
589 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
590 RETURN_FALSE;
591 }
592
593 php_stream_from_zval(the_real_stream, &the_stream);
594
595 SET_PROP(obj, stream, the_stream);
596 UPD_PROP(obj, long, lastModified, http_lmod(the_real_stream, SEND_RSRC));
597 UPD_PROP(obj, long, send_mode, SEND_RSRC);
598 RETURN_TRUE;
599 }
600 /* }}} */
601
602 /* {{{ proto resource HTTPi_Response::getStream()
603 *
604 */
605 PHP_METHOD(HTTPi_Response, getStream)
606 {
607 zval *the_stream;
608 getObject(httpi_response_object, obj);
609
610 NO_ARGS;
611
612 the_stream = GET_PROP(obj, stream);
613 RETURN_RESOURCE(Z_LVAL_P(the_stream));
614 }
615 /* }}} */
616
617 /* {{{ proto bool HTTPi_Response::setFile(string file)
618 *
619 */
620 PHP_METHOD(HTTPi_Response, setFile)
621 {
622 zval *the_file;
623 getObject(httpi_response_object, obj);
624
625 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
626 RETURN_FALSE;
627 }
628
629 convert_to_string_ex(&the_file);
630
631 UPD_PROP(obj, string, file, Z_STRVAL_P(the_file));
632 UPD_PROP(obj, long, lastModified, http_lmod(the_file, -1));
633 UPD_PROP(obj, long, send_mode, -1);
634 RETURN_TRUE;
635 }
636 /* }}} */
637
638 /* {{{ proto string HTTPi_Response::getFile()
639 *
640 */
641 PHP_METHOD(HTTPi_Response, getFile)
642 {
643 zval *the_file;
644 getObject(httpi_response_object, obj);
645
646 NO_ARGS;
647
648 the_file = GET_PROP(obj, file);
649 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
650 }
651 /* }}} */
652
653 PHP_METHOD(HTTPi_Response, send)
654 {
655 zval *do_cache, *do_gzip;
656 getObject(httpi_response_object, obj);
657
658 do_cache = GET_PROP(obj, cache);
659 do_gzip = GET_PROP(obj, gzip);
660
661 /* caching */
662 if (Z_LVAL_P(do_cache)) {
663 zval *cctrl, *etag, *lmod, *ccraw;
664
665 etag = GET_PROP(obj, eTag);
666 lmod = GET_PROP(obj, lastModified);
667 cctrl = GET_PROP(obj, cacheControl);
668 ccraw = GET_PROP(obj, raw_cache_header);
669
670 if (Z_LVAL_P(ccraw)) {
671 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
672 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
673 } else {
674 char cc_header[42] = {0};
675 sprintf(cc_header, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl));
676 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), cc_header, strlen(cc_header));
677 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), cc_header, strlen(cc_header));
678 }
679 }
680
681 /* gzip */
682 if (Z_LVAL_P(do_gzip)) {
683 /* ... */
684 }
685
686 /* content type */
687 {
688 zval *ctype = GET_PROP(obj, contentType);
689 if (Z_STRLEN_P(ctype)) {
690 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
691 } else {
692 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
693 }
694 }
695
696 /* content disposition */
697 {
698 zval *dispo_file = GET_PROP(obj, dispoFile);
699 if (Z_STRLEN_P(dispo_file)) {
700 zval *dispo_inline = GET_PROP(obj, dispoInline);
701 http_send_content_disposition(Z_STRVAL_P(dispo_file), Z_STRLEN_P(dispo_file), Z_LVAL_P(dispo_inline));
702 }
703 }
704
705 /* send */
706 {
707 zval *send_mode = GET_PROP(obj, send_mode);
708 switch (Z_LVAL_P(send_mode))
709 {
710 case SEND_DATA:
711 {
712 RETURN_SUCCESS(http_send_data(GET_PROP(obj, data)));
713 }
714
715 case SEND_RSRC:
716 {
717 php_stream *the_real_stream;
718 zval *the_stream = GET_PROP(obj, stream);
719 php_stream_from_zval(the_real_stream, &the_stream);
720 RETURN_SUCCESS(http_send_stream(the_real_stream));
721 }
722
723 default:
724 {
725 RETURN_SUCCESS(http_send_file(GET_PROP(obj, file)));
726 }
727 }
728 }
729 }
730 /* }}} */
731
732 /* {{{ HTTPi_Request */
733 #ifdef HTTP_HAVE_CURL
734
735 zend_class_entry *httpi_request_ce;
736 static zend_object_handlers httpi_request_object_handlers;
737
738 typedef struct {
739 zend_object zo;
740 CURL *ch;
741 } httpi_request_object;
742
743 #define httpi_request_declare_default_properties(ce) _httpi_request_declare_default_properties(ce TSRMLS_CC)
744 static inline void _httpi_request_declare_default_properties(zend_class_entry *ce TSRMLS_DC)
745 {
746 DCL_PROP_N(PROTECTED, options);
747 DCL_PROP_N(PROTECTED, responseInfo);
748 DCL_PROP_N(PROTECTED, responseData);
749
750 DCL_PROP(PROTECTED, long, method, HTTP_GET);
751
752 DCL_PROP(PROTECTED, string, url, "");
753 DCL_PROP(PROTECTED, string, contentType, "");
754 DCL_PROP(PROTECTED, string, queryData, "");
755 DCL_PROP(PROTECTED, string, postData, "");
756 }
757
758 #define httpi_request_destroy_object _httpi_request_destroy_object
759 void _httpi_request_destroy_object(void *object, zend_object_handle handle TSRMLS_DC)
760 {
761 zend_objects_destroy_object(object, handle TSRMLS_CC);
762 }
763
764 #define httpi_request_free_object _httpi_request_free_object
765 void _httpi_request_free_object(zend_object /* void */ *object TSRMLS_DC)
766 {
767 httpi_request_object *o = (httpi_request_object *) object;
768
769 if (OBJ_PROP(o)) {
770 zend_hash_destroy(OBJ_PROP(o));
771 FREE_HASHTABLE(OBJ_PROP(o));
772 }
773 if (o->ch) {
774 curl_easy_cleanup(o->ch);
775 o->ch = NULL;
776 }
777 efree(o);
778 }
779
780 #define httpi_request_new_object _httpi_request_new_object
781 zend_object_value _httpi_request_new_object(zend_class_entry *ce TSRMLS_DC)
782 {
783 zend_object_value ov;
784 httpi_request_object *o;
785
786 o = ecalloc(sizeof(httpi_request_object), 1);
787 o->zo.ce = ce;
788 o->ch = curl_easy_init();
789
790 ALLOC_HASHTABLE(OBJ_PROP(o));
791 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
792 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
793
794 ov.handle = zend_objects_store_put(o, httpi_request_destroy_object, httpi_request_free_object, NULL TSRMLS_CC);
795 ov.handlers = &httpi_request_object_handlers;
796
797 return ov;
798 }
799
800 zend_function_entry httpi_request_class_methods[] = {
801 PHP_ME(HTTPi_Request, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
802 PHP_ME(HTTPi_Request, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
803
804 PHP_ME(HTTPi_Request, setOptions, NULL, ZEND_ACC_PUBLIC)
805 PHP_ME(HTTPi_Request, getOptions, NULL, ZEND_ACC_PUBLIC)
806
807 PHP_ME(HTTPi_Request, setMethod, NULL, ZEND_ACC_PUBLIC)
808 PHP_ME(HTTPi_Request, getMethod, NULL, ZEND_ACC_PUBLIC)
809
810 PHP_ME(HTTPi_Request, setURL, NULL, ZEND_ACC_PUBLIC)
811 PHP_ME(HTTPi_Request, getURL, NULL, ZEND_ACC_PUBLIC)
812
813 PHP_ME(HTTPi_Request, setContentType, NULL, ZEND_ACC_PUBLIC)
814 PHP_ME(HTTPi_Request, getContentType, NULL, ZEND_ACC_PUBLIC)
815
816 PHP_ME(HTTPi_Request, setQueryData, NULL, ZEND_ACC_PUBLIC)
817 PHP_ME(HTTPi_Request, getQueryData, NULL, ZEND_ACC_PUBLIC)
818 PHP_ME(HTTPi_Request, addQueryData, NULL, ZEND_ACC_PUBLIC)
819 PHP_ME(HTTPi_Request, unsetQueryData, NULL, ZEND_ACC_PUBLIC)
820 /*
821 PHP_ME(HTTPi_Request, setPostData, NULL, ZEND_ACC_PUBLIC)
822 PHP_ME(HTTPi_Request, addPostData, NULL, ZEND_ACC_PUBLIC)
823 PHP_ME(HTTPi_Request, unsetPostData, NULL, ZEND_ACC_PUBLIC)
824
825 PHP_ME(HTTPi_Request, addPostFile, NULL, ZEND_ACC_PUBLIC)
826 */
827 PHP_ME(HTTPi_Request, send, NULL, ZEND_ACC_PUBLIC)
828
829 PHP_ME(HTTPi_Request, getResponseData, NULL, ZEND_ACC_PUBLIC)
830 PHP_ME(HTTPi_Request, getResponseHeaders, NULL, ZEND_ACC_PUBLIC)
831 PHP_ME(HTTPi_Request, getResponseBody, NULL, ZEND_ACC_PUBLIC)
832 PHP_ME(HTTPi_Request, getResponseInfo, NULL, ZEND_ACC_PUBLIC)
833
834 {NULL, NULL, NULL}
835 };
836
837 /* {{{ proto void HTTPi_Request::__construct([string url[, long request_method = HTTP_GET]])
838 *
839 */
840 PHP_METHOD(HTTPi_Request, __construct)
841 {
842 char *URL = NULL;
843 int URL_len;
844 long meth = -1;
845 zval *info, *opts, *resp;
846 getObject(httpi_request_object, obj);
847
848 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
849 return;
850 }
851
852 INIT_PARR(obj, options);
853 INIT_PARR(obj, responseInfo);
854 INIT_PARR(obj, responseData);
855
856 if (URL) {
857 UPD_PROP(obj, string, url, URL);
858 }
859 if (meth > -1) {
860 UPD_PROP(obj, long, method, meth);
861 }
862 }
863 /* }}} */
864
865 /* {{{ proto void HTTPi_Request::__destruct()
866 *
867 */
868 PHP_METHOD(HTTPi_Request, __destruct)
869 {
870 getObject(httpi_request_object, obj);
871
872 NO_ARGS;
873
874 FREE_PARR(obj, options);
875 FREE_PARR(obj, responseInfo);
876 FREE_PARR(obj, responseData);
877 }
878 /* }}} */
879
880 /* {{{ proto bool HTTPi_Request::setOptions(array options)
881 *
882 */
883 PHP_METHOD(HTTPi_Request, setOptions)
884 {
885 zval *opts, *old_opts, **opt;
886 getObject(httpi_request_object, obj);
887
888 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
889 RETURN_FALSE;
890 }
891
892 old_opts = GET_PROP(obj, options);
893
894 /* headers and cookies need extra attention -- thus cannot use zend_hash_merge() or php_array_merge() directly */
895 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(opts));
896 zend_hash_get_current_data(Z_ARRVAL_P(opts), (void **) &opt) == SUCCESS;
897 zend_hash_move_forward(Z_ARRVAL_P(opts))) {
898 char *key;
899 long idx;
900 if (HASH_KEY_IS_STRING == zend_hash_get_current_key(Z_ARRVAL_P(opts), &key, &idx, 0)) {
901 if (!strcmp(key, "headers")) {
902 zval **headers;
903 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
904 array_merge(*opt, *headers);
905 continue;
906 }
907 } else if (!strcmp(key, "cookies")) {
908 zval **cookies;
909 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
910 array_merge(*opt, *cookies);
911 continue;
912 }
913 }
914 zval_add_ref(opt);
915 add_assoc_zval(old_opts, key, *opt);
916 }
917 }
918 RETURN_TRUE;
919 }
920 /* }}} */
921
922 /* {{{ proto array HTTPi_Request::getOptions()
923 *
924 */
925 PHP_METHOD(HTTPi_Request, getOptions)
926 {
927 zval *opts;
928 getObject(httpi_request_object, obj);
929
930 NO_ARGS;
931
932 opts = GET_PROP(obj, options);
933 array_init(return_value);
934 array_copy(opts, return_value);
935 }
936 /* }}} */
937
938 /* {{{ proto bool HTTPi_Request::setURL(string url)
939 *
940 */
941 PHP_METHOD(HTTPi_Request, setURL)
942 {
943 char *URL = NULL;
944 int URL_len;
945 getObject(httpi_request_object, obj);
946
947 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
948 RETURN_FALSE;
949 }
950
951 UPD_PROP(obj, string, url, URL);
952 RETURN_TRUE;
953 }
954 /* }}} */
955
956 /* {{{ proto string HTTPi_Request::getUrl()
957 *
958 */
959 PHP_METHOD(HTTPi_Request, getURL)
960 {
961 zval *URL;
962 getObject(httpi_request_object, obj);
963
964 NO_ARGS;
965
966 URL = GET_PROP(obj, url);
967 RETURN_STRINGL(Z_STRVAL_P(URL), Z_STRLEN_P(URL), 1);
968 }
969 /* }}} */
970
971 /* {{{ proto bool HTTPi_Request::setMethod(long request_method)
972 *
973 */
974 PHP_METHOD(HTTPi_Request, setMethod)
975 {
976 long meth;
977 getObject(httpi_request_object, obj);
978
979 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
980 RETURN_FALSE;
981 }
982
983 UPD_PROP(obj, long, method, meth);
984 RETURN_TRUE;
985 }
986 /* }}} */
987
988 /* {{{ proto long HTTPi_Request::getMethod()
989 *
990 */
991 PHP_METHOD(HTTPi_Request, getMethod)
992 {
993 zval *meth;
994 getObject(httpi_request_object, obj);
995
996 NO_ARGS;
997
998 meth = GET_PROP(obj, method);
999 RETURN_LONG(Z_LVAL_P(meth));
1000 }
1001 /* }}} */
1002
1003 /* {{{ proto bool HTTPi_Request::setContentType(string content_type)
1004 *
1005 */
1006 PHP_METHOD(HTTPi_Request, setContentType)
1007 {
1008 char *ctype;
1009 int ct_len;
1010 getObject(httpi_request_object, obj);
1011
1012 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1013 RETURN_FALSE;
1014 }
1015
1016 if (!strchr(ctype, '/')) {
1017 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1018 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
1019 ctype);
1020 RETURN_FALSE;
1021 }
1022
1023 UPD_PROP(obj, string, contentType, ctype);
1024 RETURN_TRUE;
1025 }
1026 /* }}} */
1027
1028 /* {{{ proto string HTTPi_Request::getContentType()
1029 *
1030 */
1031 PHP_METHOD(HTTPi_Request, getContentType)
1032 {
1033 zval *ctype;
1034 getObject(httpi_request_object, obj);
1035
1036 NO_ARGS;
1037
1038 ctype = GET_PROP(obj, contentType);
1039 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
1040 }
1041 /* }}} */
1042
1043 /* {{{ proto bool HTTPi_Request::setQueryData(mixed query_data)
1044 *
1045 */
1046 PHP_METHOD(HTTPi_Request, setQueryData)
1047 {
1048 zval *qdata;
1049 getObject(httpi_request_object, obj);
1050
1051 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
1052 RETURN_FALSE;
1053 }
1054
1055 if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1056 smart_str qstr = {0};
1057 HTTP_URL_ARGSEP_OVERRIDE;
1058 if (SUCCESS != php_url_encode_hash_ex(HASH_OF(qdata), &qstr, NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC)) {
1059 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't encode query data");
1060 if (qstr.c) {
1061 efree(qstr.c);
1062 }
1063 HTTP_URL_ARGSEP_RESTORE;
1064 RETURN_FALSE;
1065 }
1066 HTTP_URL_ARGSEP_RESTORE;
1067 smart_str_0(&qstr);
1068 UPD_PROP(obj, string, queryData, qstr.c);
1069 efree(qstr.c);
1070 RETURN_TRUE;
1071 }
1072
1073 convert_to_string(qdata);
1074 UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
1075 RETURN_TRUE;
1076 }
1077 /* }}} */
1078
1079 /* {{{ proto string HTTPi_Request::getQueryData()
1080 *
1081 */
1082 PHP_METHOD(HTTPi_Request, getQueryData)
1083 {
1084 zval *qdata;
1085 getObject(httpi_request_object, obj);
1086
1087 NO_ARGS;
1088
1089 qdata = GET_PROP(obj, queryData);
1090 RETURN_STRINGL(Z_STRVAL_P(qdata), Z_STRLEN_P(qdata), 1);
1091 }
1092 /* }}} */
1093
1094 /* {{{ proto bool HTTPi_Request::addQueryData(array query_params)
1095 *
1096 */
1097 PHP_METHOD(HTTPi_Request, addQueryData)
1098 {
1099 zval *qdata, *old_qdata;
1100 smart_str qstr = {0};
1101 char *separator;
1102 getObject(httpi_request_object, obj);
1103
1104 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1105 RETURN_FALSE;
1106 }
1107
1108 old_qdata = GET_PROP(obj, queryData);
1109 if (Z_STRLEN_P(old_qdata)) {
1110 smart_str_appendl(&qstr, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata));
1111 }
1112
1113 HTTP_URL_ARGSEP_OVERRIDE;
1114 if (SUCCESS != php_url_encode_hash_ex(HASH_OF(qdata), &qstr, NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC)) {
1115 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't encode query data");
1116 if (qstr.c) {
1117 efree(qstr.c);
1118 }
1119 HTTP_URL_ARGSEP_RESTORE;
1120 RETURN_FALSE;
1121 }
1122 HTTP_URL_ARGSEP_RESTORE;
1123
1124 smart_str_0(&qstr);
1125
1126 UPD_PROP(obj, string, queryData, qstr.c);
1127 efree(qstr.c);
1128 RETURN_TRUE;
1129 }
1130 /* }}} */
1131
1132 /* {{{ proto void HTTPi_Request::unsetQueryData()
1133 *
1134 */
1135 PHP_METHOD(HTTPi_Request, unsetQueryData)
1136 {
1137 getObject(httpi_request_object, obj);
1138
1139 NO_ARGS;
1140
1141 UPD_PROP(obj, string, queryData, "");
1142 }
1143 /* }}} */
1144
1145 /* {{{ proto array HTTPi_Request::getResponseData()
1146 *
1147 */
1148 PHP_METHOD(HTTPi_Request, getResponseData)
1149 {
1150 zval *data;
1151 getObject(httpi_request_object, obj);
1152
1153 NO_ARGS;
1154
1155 data = GET_PROP(obj, responseData);
1156 array_init(return_value);
1157 array_copy(data, return_value);
1158 }
1159 /* }}} */
1160
1161 /* {{{ proto array HTTPi_Request::getResponseHeaders()
1162 *
1163 */
1164 PHP_METHOD(HTTPi_Request, getResponseHeaders)
1165 {
1166 zval *data, **headers;
1167 getObject(httpi_request_object, obj);
1168
1169 NO_ARGS;
1170
1171 array_init(return_value);
1172 data = GET_PROP(obj, responseData);
1173 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1174 array_copy(*headers, return_value);
1175 }
1176 }
1177 /* }}} */
1178
1179 /* {{{ proto string HTTPi_Request::getResponseBody()
1180 *
1181 */
1182 PHP_METHOD(HTTPi_Request, getResponseBody)
1183 {
1184 zval *data, **body;
1185 getObject(httpi_request_object, obj);
1186
1187 NO_ARGS;
1188
1189 data = GET_PROP(obj, responseData);
1190 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1191 RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
1192 } else {
1193 Z_TYPE_P(return_value) = IS_NULL;
1194 }
1195 }
1196 /* }}} */
1197
1198 /* {{{ proto array HTTPi_Request::getResponseInfo()
1199 *
1200 */
1201 PHP_METHOD(HTTPi_Request, getResponseInfo)
1202 {
1203 zval *info;
1204 getObject(httpi_request_object, obj);
1205
1206 NO_ARGS;
1207
1208 info = GET_PROP(obj, responseInfo);
1209 array_init(return_value);
1210 array_copy(info, return_value);
1211 }
1212 /* }}}*/
1213
1214 /* {{{ proto bool HTTPi_Request::send()
1215 *
1216 */
1217 PHP_METHOD(HTTPi_Request, send)
1218 {
1219 STATUS status = FAILURE;
1220 zval *meth, *URL, *qdata, *opts, *info, *resp;
1221 char *response_data, *request_uri, *uri;
1222 size_t response_len;
1223 getObject(httpi_request_object, obj);
1224
1225 NO_ARGS;
1226
1227 if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
1228 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initilaize cURL");
1229 RETURN_FALSE;
1230 }
1231
1232 meth = GET_PROP(obj, method);
1233 URL = GET_PROP(obj, url);
1234 qdata = GET_PROP(obj, queryData);
1235 opts = GET_PROP(obj, options);
1236 info = GET_PROP(obj, responseInfo);
1237 resp = GET_PROP(obj, responseData);
1238
1239 uri = http_absolute_uri(Z_STRVAL_P(URL), NULL);
1240 request_uri = ecalloc(HTTP_URI_MAXLEN + 1, 1);
1241 strcpy(request_uri, uri);
1242 efree(uri);
1243
1244 if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
1245 if (!strchr(request_uri, '?')) {
1246 strcat(request_uri, "?");
1247 } else {
1248 strcat(request_uri, "&");
1249 }
1250 strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
1251 }
1252
1253 switch (Z_LVAL_P(meth))
1254 {
1255 case HTTP_GET:
1256 status = http_get_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1257 break;
1258
1259 case HTTP_HEAD:
1260 status = http_head_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1261 break;
1262
1263 case HTTP_POST:
1264 break;
1265
1266 default:
1267 break;
1268 }
1269
1270 efree(request_uri);
1271
1272 /* final data handling */
1273 if (status != SUCCESS) {
1274 RETURN_FALSE;
1275 } else {
1276 zval *zheaders, *zbody;
1277
1278 MAKE_STD_ZVAL(zbody);
1279 MAKE_STD_ZVAL(zheaders)
1280 array_init(zheaders);
1281
1282 if (SUCCESS != http_split_response_ex(response_data, response_len, zheaders, zbody)) {
1283 zval_dtor(zheaders);
1284 efree(zheaders),
1285 efree(zbody);
1286 efree(response_data);
1287 RETURN_FALSE;
1288 }
1289
1290 add_assoc_zval(resp, "headers", zheaders);
1291 add_assoc_zval(resp, "body", zbody);
1292
1293 efree(response_data);
1294
1295 RETURN_TRUE;
1296 }
1297 /* */
1298 }
1299 /* }}} */
1300
1301 #endif /* HTTP_HAVE_CURL */
1302 /* }}} */
1303
1304 #endif /* ZEND_ENGINE_2 */
1305
1306 /* {{{ http_module_entry */
1307 zend_module_entry http_module_entry = {
1308 #if ZEND_MODULE_API_NO >= 20010901
1309 STANDARD_MODULE_HEADER,
1310 #endif
1311 "http",
1312 http_functions,
1313 PHP_MINIT(http),
1314 PHP_MSHUTDOWN(http),
1315 PHP_RINIT(http),
1316 PHP_RSHUTDOWN(http),
1317 PHP_MINFO(http),
1318 #if ZEND_MODULE_API_NO >= 20010901
1319 PHP_EXT_HTTP_VERSION,
1320 #endif
1321 STANDARD_MODULE_PROPERTIES
1322 };
1323 /* }}} */
1324
1325 /* {{{ proto string http_date([int timestamp])
1326 *
1327 * This function returns a valid HTTP date regarding RFC 822/1123
1328 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
1329 *
1330 */
1331 PHP_FUNCTION(http_date)
1332 {
1333 long t = -1;
1334
1335 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
1336 RETURN_FALSE;
1337 }
1338
1339 if (t == -1) {
1340 t = (long) time(NULL);
1341 }
1342
1343 RETURN_STRING(http_date(t), 0);
1344 }
1345 /* }}} */
1346
1347 /* {{{ proto string http_absolute_uri(string url[, string proto])
1348 *
1349 * This function returns an absolute URI constructed from url.
1350 * If the url is already abolute but a different proto was supplied,
1351 * only the proto part of the URI will be updated. If url has no
1352 * path specified, the path of the current REQUEST_URI will be taken.
1353 * The host will be taken either from the Host HTTP header of the client
1354 * the SERVER_NAME or just localhost if prior are not available.
1355 *
1356 * Some examples:
1357 * <pre>
1358 * url = "page.php" => http://www.example.com/current/path/page.php
1359 * url = "/page.php" => http://www.example.com/page.php
1360 * url = "/page.php", proto = "https" => https://www.example.com/page.php
1361 * </pre>
1362 *
1363 */
1364 PHP_FUNCTION(http_absolute_uri)
1365 {
1366 char *url = NULL, *proto = NULL;
1367 int url_len = 0, proto_len = 0;
1368
1369 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &url, &url_len, &proto, &proto_len) != SUCCESS) {
1370 RETURN_FALSE;
1371 }
1372
1373 RETURN_STRING(http_absolute_uri(url, proto), 0);
1374 }
1375 /* }}} */
1376
1377 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
1378 *
1379 * This function negotiates the clients preferred language based on its
1380 * Accept-Language HTTP header. It returns the negotiated language or
1381 * the default language if none match.
1382 *
1383 * The qualifier is recognized and languages without qualifier are rated highest.
1384 *
1385 * The supported parameter is expected to be an array having
1386 * the supported languages as array values.
1387 *
1388 * Example:
1389 * <pre>
1390 * <?php
1391 * $langs = array(
1392 * 'en-US',// default
1393 * 'fr',
1394 * 'fr-FR',
1395 * 'de',
1396 * 'de-DE',
1397 * 'de-AT',
1398 * 'de-CH',
1399 * );
1400 * include './langs/'. http_negotiate_language($langs) .'.php';
1401 * ?>
1402 * </pre>
1403 *
1404 */
1405 PHP_FUNCTION(http_negotiate_language)
1406 {
1407 zval *supported;
1408 char *def = NULL;
1409 int def_len = 0;
1410
1411 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
1412 RETURN_FALSE;
1413 }
1414
1415 if (!def) {
1416 def = "en-US";
1417 }
1418
1419 RETURN_STRING(http_negotiate_language(supported, def), 0);
1420 }
1421 /* }}} */
1422
1423 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
1424 *
1425 * This function negotiates the clients preferred charset based on its
1426 * Accept-Charset HTTP header. It returns the negotiated charset or
1427 * the default charset if none match.
1428 *
1429 * The qualifier is recognized and charset without qualifier are rated highest.
1430 *
1431 * The supported parameter is expected to be an array having
1432 * the supported charsets as array values.
1433 *
1434 * Example:
1435 * <pre>
1436 * <?php
1437 * $charsets = array(
1438 * 'iso-8859-1', // default
1439 * 'iso-8859-2',
1440 * 'iso-8859-15',
1441 * 'utf-8'
1442 * );
1443 * $pref = http_negotiate_charset($charsets);
1444 * if (!strcmp($pref, 'iso-8859-1')) {
1445 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
1446 * iconv_set_encoding('output_encoding', $pref);
1447 * ob_start('ob_iconv_handler');
1448 * }
1449 * ?>
1450 * </pre>
1451 */
1452 PHP_FUNCTION(http_negotiate_charset)
1453 {
1454 zval *supported;
1455 char *def = NULL;
1456 int def_len = 0;
1457
1458 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
1459 RETURN_FALSE;
1460 }
1461
1462 if (!def) {
1463 def = "iso-8859-1";
1464 }
1465
1466 RETURN_STRING(http_negotiate_charset(supported, def), 0);
1467 }
1468 /* }}} */
1469
1470 /* {{{ proto bool http_send_status(int status)
1471 *
1472 * Send HTTP status code.
1473 *
1474 */
1475 PHP_FUNCTION(http_send_status)
1476 {
1477 int status = 0;
1478
1479 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
1480 RETURN_FALSE;
1481 }
1482 if (status < 100 || status > 510) {
1483 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid HTTP status code (100-510): %d", status);
1484 RETURN_FALSE;
1485 }
1486
1487 RETURN_SUCCESS(http_send_status(status));
1488 }
1489 /* }}} */
1490
1491 /* {{{ proto bool http_send_last_modified([int timestamp])
1492 *
1493 * This converts the given timestamp to a valid HTTP date and
1494 * sends it as "Last-Modified" HTTP header. If timestamp is
1495 * omitted, current time is sent.
1496 *
1497 */
1498 PHP_FUNCTION(http_send_last_modified)
1499 {
1500 long t = -1;
1501
1502 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
1503 RETURN_FALSE;
1504 }
1505
1506 if (t == -1) {
1507 t = (long) time(NULL);
1508 }
1509
1510 RETURN_SUCCESS(http_send_last_modified(t));
1511 }
1512 /* }}} */
1513
1514 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
1515 *
1516 * Sets the content type.
1517 *
1518 */
1519 PHP_FUNCTION(http_send_content_type)
1520 {
1521 char *ct;
1522 int ct_len = 0;
1523
1524 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
1525 RETURN_FALSE;
1526 }
1527
1528 if (!ct_len) {
1529 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1));
1530 }
1531 RETURN_SUCCESS(http_send_content_type(ct, ct_len));
1532 }
1533 /* }}} */
1534
1535 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
1536 *
1537 * Set the Content Disposition. The Content-Disposition header is very useful
1538 * if the data actually sent came from a file or something similar, that should
1539 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
1540 *
1541 */
1542 PHP_FUNCTION(http_send_content_disposition)
1543 {
1544 char *filename;
1545 int f_len;
1546 zend_bool send_inline = 0;
1547
1548 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
1549 RETURN_FALSE;
1550 }
1551 RETURN_SUCCESS(http_send_content_disposition(filename, f_len, send_inline));
1552 }
1553 /* }}} */
1554
1555 /* {{{ proto bool http_match_modified([int timestamp])
1556 *
1557 * Matches the given timestamp against the clients "If-Modified-Since" resp.
1558 * "If-Unmodified-Since" HTTP headers.
1559 *
1560 */
1561 PHP_FUNCTION(http_match_modified)
1562 {
1563 long t = -1;
1564
1565 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
1566 RETURN_FALSE;
1567 }
1568
1569 // current time if not supplied (senseless though)
1570 if (t == -1) {
1571 t = (long) time(NULL);
1572 }
1573
1574 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t));
1575 }
1576 /* }}} */
1577
1578 /* {{{ proto bool http_match_etag(string etag)
1579 *
1580 * This matches the given ETag against the clients
1581 * "If-Match" resp. "If-None-Match" HTTP headers.
1582 *
1583 */
1584 PHP_FUNCTION(http_match_etag)
1585 {
1586 int etag_len;
1587 char *etag;
1588
1589 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len) != SUCCESS) {
1590 RETURN_FALSE;
1591 }
1592
1593 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag) || http_etag_match("HTTP_IF_MATCH", etag));
1594 }
1595 /* }}} */
1596
1597 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
1598 *
1599 * If timestamp_or_exires is greater than 0, it is handled as timestamp
1600 * and will be sent as date of last modification. If it is 0 or omitted,
1601 * the current time will be sent as Last-Modified date. If it's negative,
1602 * it is handled as expiration time in seconds, which means that if the
1603 * requested last modification date is not between the calculated timespan,
1604 * the Last-Modified header is updated and the actual body will be sent.
1605 *
1606 */
1607 PHP_FUNCTION(http_cache_last_modified)
1608 {
1609 long last_modified = 0, send_modified = 0, t;
1610 zval *zlm;
1611
1612 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
1613 RETURN_FALSE;
1614 }
1615
1616 t = (long) time(NULL);
1617
1618 /* 0 or omitted */
1619 if (!last_modified) {
1620 /* does the client have? (att: caching "forever") */
1621 if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
1622 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
1623 /* send current time */
1624 } else {
1625 send_modified = t;
1626 }
1627 /* negative value is supposed to be expiration time */
1628 } else if (last_modified < 0) {
1629 last_modified += t;
1630 send_modified = t;
1631 /* send supplied time explicitly */
1632 } else {
1633 send_modified = last_modified;
1634 }
1635
1636 RETURN_SUCCESS(http_cache_last_modified(last_modified, send_modified, HTTP_DEFAULT_CACHECONTROL, sizeof(HTTP_DEFAULT_CACHECONTROL) - 1));
1637 }
1638 /* }}} */
1639
1640 /* {{{ proto bool http_cache_etag([string etag])
1641 *
1642 * This function attempts to cache the HTTP body based on an ETag,
1643 * either supplied or generated through calculation of the MD5
1644 * checksum of the output (uses output buffering).
1645 *
1646 * If clients "If-None-Match" header matches the supplied/calculated
1647 * ETag, the body is considered cached on the clients side and
1648 * a "304 Not Modified" status code is issued.
1649 *
1650 */
1651 PHP_FUNCTION(http_cache_etag)
1652 {
1653 char *etag;
1654 int etag_len = 0;
1655
1656 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
1657 RETURN_FALSE;
1658 }
1659
1660 RETURN_SUCCESS(http_cache_etag(etag, etag_len, HTTP_DEFAULT_CACHECONTROL, sizeof(HTTP_DEFAULT_CACHECONTROL) - 1));
1661 }
1662 /* }}} */
1663
1664 /* {{{ proto string ob_httpetaghandler(string data, int mode)
1665 *
1666 * For use with ob_start().
1667 * Note that this has to be started as first output buffer.
1668 * WARNING: Don't use with http_send_*().
1669 */
1670 PHP_FUNCTION(ob_httpetaghandler)
1671 {
1672 char *data;
1673 int data_len;
1674 long mode;
1675
1676 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1677 RETURN_FALSE;
1678 }
1679
1680 if (mode & PHP_OUTPUT_HANDLER_START) {
1681 if (HTTP_G(etag_started)) {
1682 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler can only be used once");
1683 RETURN_STRINGL(data, data_len, 1);
1684 }
1685 http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL);
1686 HTTP_G(etag_started) = 1;
1687 }
1688
1689 if (OG(ob_nesting_level) > 1) {
1690 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler must be started prior to other output buffers");
1691 RETURN_STRINGL(data, data_len, 1);
1692 }
1693
1694 Z_TYPE_P(return_value) = IS_STRING;
1695 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), mode);
1696 }
1697 /* }}} */
1698
1699 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
1700 *
1701 * Redirect to a given url.
1702 * The supplied url will be expanded with http_absolute_uri(), the params array will
1703 * be treated with http_build_query() and the session identification will be appended
1704 * if session is true.
1705 *
1706 * Depending on permanent the redirection will be issued with a permanent
1707 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
1708 * status code.
1709 *
1710 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
1711 * if the client doesn't redirect immediatly.
1712 */
1713 PHP_FUNCTION(http_redirect)
1714 {
1715 int url_len;
1716 zend_bool session = 0, permanent = 0;
1717 zval *params = NULL;
1718 smart_str qstr = {0};
1719 char *url, *URI, LOC[HTTP_URI_MAXLEN + 9], RED[HTTP_URI_MAXLEN * 2 + 34];
1720
1721 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
1722 RETURN_FALSE;
1723 }
1724
1725 /* append session info */
1726 if (session && (PS(session_status) == php_session_active)) {
1727 if (!params) {
1728 MAKE_STD_ZVAL(params);
1729 array_init(params);
1730 }
1731 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
1732 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not append session information");
1733 }
1734 }
1735
1736 /* treat params array with http_build_query() */
1737 if (params) {
1738 if (php_url_encode_hash_ex(Z_ARRVAL_P(params), &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
1739 if (qstr.c) {
1740 efree(qstr.c);
1741 }
1742 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode query parameters");
1743 RETURN_FALSE;
1744 }
1745 smart_str_0(&qstr);
1746 }
1747
1748 URI = http_absolute_uri(url, NULL);
1749 if (qstr.c) {
1750 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s?%s", URI, qstr.c);
1751 sprintf(RED, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, qstr.c, URI, qstr.c);
1752 efree(qstr.c);
1753 } else {
1754 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s", URI);
1755 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
1756 }
1757 efree(URI);
1758
1759 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
1760 php_body_write(RED, strlen(RED) TSRMLS_CC);
1761 RETURN_TRUE;
1762 }
1763 RETURN_FALSE;
1764 }
1765 /* }}} */
1766
1767 /* {{{ proto bool http_send_data(string data)
1768 *
1769 * Sends raw data with support for (multiple) range requests.
1770 *
1771 */
1772 PHP_FUNCTION(http_send_data)
1773 {
1774 zval *zdata;
1775
1776 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
1777 RETURN_FALSE;
1778 }
1779
1780 convert_to_string_ex(&zdata);
1781 http_send_header("Accept-Ranges: bytes");
1782 RETURN_SUCCESS(http_send_data(zdata));
1783 }
1784 /* }}} */
1785
1786 /* {{{ proto bool http_send_file(string file)
1787 *
1788 * Sends a file with support for (multiple) range requests.
1789 *
1790 */
1791 PHP_FUNCTION(http_send_file)
1792 {
1793 zval *zfile;
1794
1795 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zfile) != SUCCESS) {
1796 RETURN_FALSE;
1797 }
1798
1799 convert_to_string_ex(&zfile);
1800 http_send_header("Accept-Ranges: bytes");
1801 RETURN_SUCCESS(http_send_file(zfile));
1802 }
1803 /* }}} */
1804
1805 /* {{{ proto bool http_send_stream(resource stream)
1806 *
1807 * Sends an already opened stream with support for (multiple) range requests.
1808 *
1809 */
1810 PHP_FUNCTION(http_send_stream)
1811 {
1812 zval *zstream;
1813 php_stream *file;
1814
1815 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
1816 RETURN_FALSE;
1817 }
1818
1819 php_stream_from_zval(file, &zstream);
1820 http_send_header("Accept-Ranges: bytes");
1821 RETURN_SUCCESS(http_send_stream(file));
1822 }
1823 /* }}} */
1824
1825 /* {{{ proto string http_chunked_decode(string encoded)
1826 *
1827 * This function decodes a string that was HTTP-chunked encoded.
1828 * Returns false on failure.
1829 */
1830 PHP_FUNCTION(http_chunked_decode)
1831 {
1832 char *encoded = NULL, *decoded = NULL;
1833 int encoded_len = 0, decoded_len = 0;
1834
1835 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
1836 RETURN_FALSE;
1837 }
1838
1839 if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
1840 RETURN_STRINGL(decoded, decoded_len, 0);
1841 } else {
1842 RETURN_FALSE;
1843 }
1844 }
1845 /* }}} */
1846
1847 /* {{{ proto array http_split_response(string http_response)
1848 *
1849 * This function splits an HTTP response into an array with headers and the
1850 * content body. The returned array may look simliar to the following example:
1851 *
1852 * <pre>
1853 * <?php
1854 * array(
1855 * 0 => array(
1856 * 'Status' => '200 Ok',
1857 * 'Content-Type' => 'text/plain',
1858 * 'Content-Language' => 'en-US'
1859 * ),
1860 * 1 => "Hello World!"
1861 * );
1862 * ?>
1863 * </pre>
1864 */
1865 PHP_FUNCTION(http_split_response)
1866 {
1867 zval *zresponse, *zbody, *zheaders;
1868
1869 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
1870 RETURN_FALSE;
1871 }
1872
1873 convert_to_string_ex(&zresponse);
1874
1875 MAKE_STD_ZVAL(zbody);
1876 MAKE_STD_ZVAL(zheaders);
1877 array_init(zheaders);
1878
1879 if (SUCCESS != http_split_response(zresponse, zheaders, zbody)) {
1880 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP response");
1881 RETURN_FALSE;
1882 }
1883
1884 array_init(return_value);
1885 add_index_zval(return_value, 0, zheaders);
1886 add_index_zval(return_value, 1, zbody);
1887 }
1888 /* }}} */
1889
1890 /* {{{ proto array http_parse_headers(string header)
1891 *
1892 */
1893 PHP_FUNCTION(http_parse_headers)
1894 {
1895 char *header, *rnrn;
1896 int header_len;
1897
1898 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
1899 RETURN_FALSE;
1900 }
1901
1902 array_init(return_value);
1903
1904 if (rnrn = strstr(header, HTTP_CRLF HTTP_CRLF)) {
1905 header_len = rnrn - header + 2;
1906 }
1907 if (SUCCESS != http_parse_headers(header, header_len, return_value)) {
1908 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP header");
1909 zval_dtor(return_value);
1910 RETURN_FALSE;
1911 }
1912 }
1913 /* }}}*/
1914
1915 /* {{{ proto array http_get_request_headers(void)
1916 *
1917 */
1918 PHP_FUNCTION(http_get_request_headers)
1919 {
1920 if (ZEND_NUM_ARGS()) {
1921 WRONG_PARAM_COUNT;
1922 }
1923
1924 array_init(return_value);
1925 http_get_request_headers(return_value);
1926 }
1927 /* }}} */
1928
1929 /* {{{ HAVE_CURL */
1930 #ifdef HTTP_HAVE_CURL
1931
1932 /* {{{ proto string http_get(string url[, array options[, array &info]])
1933 *
1934 * Performs an HTTP GET request on the supplied url.
1935 *
1936 * The second parameter is expected to be an associative
1937 * array where the following keys will be recognized:
1938 * <pre>
1939 * - redirect: int, whether and how many redirects to follow
1940 * - unrestrictedauth: bool, whether to continue sending credentials on
1941 * redirects to a different host
1942 * - proxyhost: string, proxy host in "host[:port]" format
1943 * - proxyport: int, use another proxy port as specified in proxyhost
1944 * - proxyauth: string, proxy credentials in "user:pass" format
1945 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1946 * - httpauth: string, http credentials in "user:pass" format
1947 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1948 * - compress: bool, whether to allow gzip/deflate content encoding
1949 * (defaults to true)
1950 * - port: int, use another port as specified in the url
1951 * - referer: string, the referer to sends
1952 * - useragent: string, the user agent to send
1953 * (defaults to PECL::HTTP/version (PHP/version)))
1954 * - headers: array, list of custom headers as associative array
1955 * like array("header" => "value")
1956 * - cookies: array, list of cookies as associative array
1957 * like array("cookie" => "value")
1958 * - cookiestore: string, path to a file where cookies are/will be stored
1959 * </pre>
1960 *
1961 * The optional third parameter will be filled with some additional information
1962 * in form af an associative array, if supplied, like the following example:
1963 * <pre>
1964 * <?php
1965 * array (
1966 * 'effective_url' => 'http://localhost',
1967 * 'response_code' => 403,
1968 * 'total_time' => 0.017,
1969 * 'namelookup_time' => 0.013,
1970 * 'connect_time' => 0.014,
1971 * 'pretransfer_time' => 0.014,
1972 * 'size_upload' => 0,
1973 * 'size_download' => 202,
1974 * 'speed_download' => 11882,
1975 * 'speed_upload' => 0,
1976 * 'header_size' => 145,
1977 * 'request_size' => 62,
1978 * 'ssl_verifyresult' => 0,
1979 * 'filetime' => -1,
1980 * 'content_length_download' => 202,
1981 * 'content_length_upload' => 0,
1982 * 'starttransfer_time' => 0.017,
1983 * 'content_type' => 'text/html; charset=iso-8859-1',
1984 * 'redirect_time' => 0,
1985 * 'redirect_count' => 0,
1986 * 'private' => '',
1987 * 'http_connectcode' => 0,
1988 * 'httpauth_avail' => 0,
1989 * 'proxyauth_avail' => 0,
1990 * )
1991 * ?>
1992 * </pre>
1993 */
1994 PHP_FUNCTION(http_get)
1995 {
1996 char *URL, *data = NULL;
1997 size_t data_len = 0;
1998 int URL_len;
1999 zval *options = NULL, *info = NULL;
2000
2001 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
2002 RETURN_FALSE;
2003 }
2004
2005 if (info) {
2006 zval_dtor(info);
2007 array_init(info);
2008 }
2009
2010 if (SUCCESS == http_get(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
2011 RETURN_STRINGL(data, data_len, 0);
2012 } else {
2013 RETURN_FALSE;
2014 }
2015 }
2016 /* }}} */
2017
2018 /* {{{ proto string http_head(string url[, array options[, array &info]])
2019 *
2020 * Performs an HTTP HEAD request on the suppied url.
2021 * Returns the HTTP response as string.
2022 * See http_get() for a full list of available options.
2023 */
2024 PHP_FUNCTION(http_head)
2025 {
2026 char *URL, *data = NULL;
2027 size_t data_len = 0;
2028 int URL_len;
2029 zval *options = NULL, *info = NULL;
2030
2031 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
2032 RETURN_FALSE;
2033 }
2034
2035 if (info) {
2036 zval_dtor(info);
2037 array_init(info);
2038 }
2039
2040 if (SUCCESS == http_head(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
2041 RETURN_STRINGL(data, data_len, 0);
2042 } else {
2043 RETURN_FALSE;
2044 }
2045 }
2046 /* }}} */
2047
2048 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
2049 *
2050 * Performs an HTTP POST request, posting data.
2051 * Returns the HTTP response as string.
2052 * See http_get() for a full list of available options.
2053 */
2054 PHP_FUNCTION(http_post_data)
2055 {
2056 char *URL, *postdata, *data = NULL;
2057 size_t data_len = 0;
2058 int postdata_len, URL_len;
2059 zval *options = NULL, *info = NULL;
2060
2061 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
2062 RETURN_FALSE;
2063 }
2064
2065 if (info) {
2066 zval_dtor(info);
2067 array_init(info);
2068 }
2069
2070 if (SUCCESS == http_post_data(URL, postdata, (size_t) postdata_len, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
2071 RETURN_STRINGL(data, data_len, 0);
2072 } else {
2073 RETURN_FALSE;
2074 }
2075 }
2076 /* }}} */
2077
2078 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
2079 *
2080 * Performs an HTTP POST request, posting www-form-urlencoded array data.
2081 * Returns the HTTP response as string.
2082 * See http_get() for a full list of available options.
2083 */
2084 PHP_FUNCTION(http_post_array)
2085 {
2086 char *URL, *data = NULL;
2087 size_t data_len = 0;
2088 int URL_len;
2089 zval *options = NULL, *info = NULL, *postdata;
2090
2091 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
2092 RETURN_FALSE;
2093 }
2094
2095 if (info) {
2096 zval_dtor(info);
2097 array_init(info);
2098 }
2099
2100 if (SUCCESS == http_post_array(URL, Z_ARRVAL_P(postdata), HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
2101 RETURN_STRINGL(data, data_len, 0);
2102 } else {
2103 RETURN_FALSE;
2104 }
2105 }
2106 /* }}} */
2107
2108 #endif
2109 /* }}} HAVE_CURL */
2110
2111
2112 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
2113 *
2114 * Example:
2115 * <pre>
2116 * <?php
2117 * if (!http_auth_basic('mike', 's3c|r3t')) {
2118 * die('<h1>Authorization failed!</h1>');
2119 * }
2120 * ?>
2121 * </pre>
2122 */
2123 PHP_FUNCTION(http_auth_basic)
2124 {
2125 char *realm = NULL, *user, *pass, *suser, *spass;
2126 int r_len, u_len, p_len;
2127
2128 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
2129 RETURN_FALSE;
2130 }
2131
2132 if (!realm) {
2133 realm = "Restricted";
2134 }
2135
2136 if (SUCCESS != http_auth_credentials(&suser, &spass)) {
2137 http_auth_header("Basic", realm);
2138 RETURN_FALSE;
2139 }
2140
2141 if (strcasecmp(suser, user)) {
2142 http_auth_header("Basic", realm);
2143 RETURN_FALSE;
2144 }
2145
2146 if (strcmp(spass, pass)) {
2147 http_auth_header("Basic", realm);
2148 RETURN_FALSE;
2149 }
2150
2151 RETURN_TRUE;
2152 }
2153 /* }}} */
2154
2155 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
2156 *
2157 * Example:
2158 * <pre>
2159 * <?php
2160 * function auth_cb($user, $pass)
2161 * {
2162 * global $db;
2163 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
2164 * if (strlen($realpass = $db->getOne($query)) {
2165 * return $pass === $realpass;
2166 * }
2167 * return false;
2168 * }
2169 *
2170 * if (!http_auth_basic_cb('auth_cb')) {
2171 * die('<h1>Authorization failed</h1>');
2172 * }
2173 * ?>
2174 * </pre>
2175 */
2176 PHP_FUNCTION(http_auth_basic_cb)
2177 {
2178 zval *cb;
2179 char *realm = NULL, *user, *pass;
2180 int r_len;
2181
2182 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
2183 RETURN_FALSE;
2184 }
2185
2186 if (!realm) {
2187 realm = "Restricted";
2188 }
2189
2190 if (SUCCESS != http_auth_credentials(&user, &pass)) {
2191 http_auth_header("Basic", realm);
2192 RETURN_FALSE;
2193 }
2194 {
2195 zval *zparams[2] = {NULL, NULL}, retval;
2196 int result = 0;
2197
2198 MAKE_STD_ZVAL(zparams[0]);
2199 MAKE_STD_ZVAL(zparams[1]);
2200 ZVAL_STRING(zparams[0], user, 0);
2201 ZVAL_STRING(zparams[1], pass, 0);
2202
2203 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
2204 &retval, 2, zparams TSRMLS_CC)) {
2205 result = Z_LVAL(retval);
2206 }
2207
2208 efree(user);
2209 efree(pass);
2210 efree(zparams[0]);
2211 efree(zparams[1]);
2212
2213 if (!result) {
2214 http_auth_header("Basic", realm);
2215 }
2216
2217 RETURN_BOOL(result);
2218 }
2219 }
2220 /* }}}*/
2221
2222
2223 /* {{{ php_http_init_globals(zend_http_globals *) */
2224 static void php_http_init_globals(zend_http_globals *http_globals)
2225 {
2226 http_globals->etag_started = 0;
2227 http_globals->ctype = NULL;
2228 http_globals->etag = NULL;
2229 http_globals->lmod = 0;
2230 #ifdef HTTP_HAVE_CURL
2231 http_globals->curlbuf.body.data = NULL;
2232 http_globals->curlbuf.body.used = 0;
2233 http_globals->curlbuf.body.free = 0;
2234 http_globals->curlbuf.hdrs.data = NULL;
2235 http_globals->curlbuf.hdrs.used = 0;
2236 http_globals->curlbuf.hdrs.free = 0;
2237 #endif
2238 http_globals->allowed_methods = NULL;
2239 }
2240 /* }}} */
2241
2242 /* {{{ static inline STATUS http_check_allowed_methods(char *, int) */
2243 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
2244 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
2245 {
2246 if (length && SG(request_info).request_method && (!strstr(methods, SG(request_info).request_method))) {
2247 char *allow_header = emalloc(length + sizeof("Allow: "));
2248 sprintf(allow_header, "Allow: %s", methods);
2249 http_send_header(allow_header);
2250 efree(allow_header);
2251 http_send_status(405);
2252 zend_bailout();
2253 }
2254 }
2255 /* }}} */
2256
2257 /* {{{ PHP_INI */
2258 PHP_INI_MH(update_allowed_methods)
2259 {
2260 http_check_allowed_methods(new_value, new_value_length);
2261 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
2262 }
2263
2264 PHP_INI_BEGIN()
2265 STD_PHP_INI_ENTRY("http.allowed_methods", "OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT", PHP_INI_ALL, update_allowed_methods, allowed_methods, zend_http_globals, http_globals)
2266 PHP_INI_END()
2267 /* }}} */
2268
2269 /* {{{ PHP_MINIT_FUNCTION */
2270 PHP_MINIT_FUNCTION(http)
2271 {
2272 ZEND_INIT_MODULE_GLOBALS(http, php_http_init_globals, NULL);
2273 REGISTER_INI_ENTRIES();
2274
2275 #if defined(HTTP_HAVE_CURL) && (LIBCURL_VERSION_NUM >= 0x070a05)
2276 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC, CONST_CS | CONST_PERSISTENT);
2277 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST, CONST_CS | CONST_PERSISTENT);
2278 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM, CONST_CS | CONST_PERSISTENT);
2279 #endif
2280
2281 #ifdef ZEND_ENGINE_2
2282 HTTP_REGISTER_CLASS(HTTPi, httpi, NULL, ZEND_ACC_FINAL_CLASS);
2283 HTTP_REGISTER_CLASS_EX(HTTPi_Response, httpi_response, NULL, 0);
2284 # ifdef HTTP_HAVE_CURL
2285 HTTP_REGISTER_CLASS_EX(HTTPi_Request, httpi_request, NULL, 0);
2286 REGISTER_LONG_CONSTANT("HTTP_GET", HTTP_GET, CONST_CS | CONST_PERSISTENT);
2287 REGISTER_LONG_CONSTANT("HTTP_HEAD", HTTP_HEAD, CONST_CS | CONST_PERSISTENT);
2288 REGISTER_LONG_CONSTANT("HTTP_POST", HTTP_POST, CONST_CS | CONST_PERSISTENT);
2289 # endif /* HTTP_HAVE_CURL */
2290 #endif /* ZEND_ENGINE_2 */
2291 return SUCCESS;
2292 }
2293 /* }}} */
2294
2295 /* {{{ PHP_MSHUTDOWN_FUNCTION */
2296 PHP_MSHUTDOWN_FUNCTION(http)
2297 {
2298 UNREGISTER_INI_ENTRIES();
2299 return SUCCESS;
2300 }
2301 /* }}} */
2302
2303 /* {{{ PHP_RINIT_FUNCTION */
2304 PHP_RINIT_FUNCTION(http)
2305 {
2306 char *allowed_methods = INI_STR("http.allowed_methods");
2307 http_check_allowed_methods(allowed_methods, strlen(allowed_methods));
2308 return SUCCESS;
2309 }
2310 /* }}} */
2311
2312 /* {{{ PHP_RSHUTDOWN_FUNCTION */
2313 PHP_RSHUTDOWN_FUNCTION(http)
2314 {
2315 HTTP_G(etag_started) = 0;
2316 HTTP_G(lmod) = 0;
2317
2318 if (HTTP_G(etag)) {
2319 efree(HTTP_G(etag));
2320 HTTP_G(etag) = NULL;
2321 }
2322
2323 if (HTTP_G(ctype)) {
2324 efree(HTTP_G(ctype));
2325 HTTP_G(ctype) = NULL;
2326 }
2327 #ifdef HTTP_HAVE_CURL
2328 if (HTTP_G(curlbuf).body.data) {
2329 efree(HTTP_G(curlbuf).body.data);
2330 HTTP_G(curlbuf).body.data = NULL;
2331 }
2332 if (HTTP_G(curlbuf).hdrs.data) {
2333 efree(HTTP_G(curlbuf).hdrs.data);
2334 HTTP_G(curlbuf).hdrs.data = NULL;
2335 }
2336 #endif
2337 return SUCCESS;
2338 }
2339 /* }}} */
2340
2341 /* {{{ PHP_MINFO_FUNCTION */
2342 PHP_MINFO_FUNCTION(http)
2343 {
2344 php_info_print_table_start();
2345 php_info_print_table_header(2, "Extended HTTP support", "enabled");
2346 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION);
2347 php_info_print_table_row(2, "cURL convenience functions:",
2348 #ifdef HTTP_HAVE_CURL
2349 "enabled"
2350 #else
2351 "disabled"
2352 #endif
2353 );
2354 php_info_print_table_end();
2355
2356 DISPLAY_INI_ENTRIES();
2357 }
2358 /* }}} */
2359
2360 /*
2361 * Local variables:
2362 * tab-width: 4
2363 * c-basic-offset: 4
2364 * End:
2365 * vim600: noet sw=4 ts=4 fdm=marker
2366 * vim<600: noet sw=4 ts=4
2367 */
2368