2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
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"
36 #include "php_http_api.h"
39 # include "ext/standard/php_http.h"
45 # include <winsock2.h>
46 # include <sys/types.h>
49 # include <curl/curl.h>
52 # ifdef ZEND_BEGIN_ARG_INFO
53 ZEND_BEGIN_ARG_INFO(http_request_info_ref_3
, 0)
59 ZEND_BEGIN_ARG_INFO(http_request_info_ref_4
, 0)
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
};
71 #endif /* HTTP_HAVE_CURL */
73 ZEND_DECLARE_MODULE_GLOBALS(http
)
75 #ifdef COMPILE_DL_HTTP
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
)
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
)
112 PHP_FE(ob_httpetaghandler
, NULL
)
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)
125 # define HTTP_REGISTER_CLASS_EX(classname, name, parent, flags) \
127 zend_class_entry ce; \
128 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
129 ce.create_object = name## _new_object; \
130 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
131 name## _ce->ce_flags |= flags; \
132 memcpy(& name## _object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
133 name## _object_handlers.clone_obj = NULL; \
134 name## _declare_default_properties(name## _ce); \
137 # define HTTP_REGISTER_CLASS(classname, name, parent, flags) \
139 zend_class_entry ce; \
140 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
141 ce.create_object = NULL; \
142 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
143 name## _ce->ce_flags |= flags; \
146 # define getObject(t, o) t * o = ((t *) zend_object_store_get_object(getThis() TSRMLS_CC))
147 # define OBJ_PROP(o) o->zo.properties
148 # define DCL_PROP(a, t, n, v) zend_declare_property_ ##t(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC)
149 # define DCL_PROP_Z(a, n, v) zend_declare_property(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC)
150 # define DCL_PROP_N(a, n) zend_declare_property_null(ce, (#n), sizeof(#n), (ZEND_ACC_ ##a) TSRMLS_CC)
151 # define UPD_PROP(o, t, n, v) zend_update_property_ ##t(o->zo.ce, getThis(), (#n), sizeof(#n), (v) TSRMLS_CC)
152 # define SET_PROP(o, n, z) zend_update_property(o->zo.ce, getThis(), (#n), sizeof(#n), (z) TSRMLS_CC)
153 # define GET_PROP(o, n) zend_read_property(o->zo.ce, getThis(), (#n), sizeof(#n), 0 TSRMLS_CC)
157 zend_class_entry
*httpi_ce
;
159 #define HTTPi_ME(me, al, ai) ZEND_FENTRY(me, ZEND_FN(al), ai, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
161 zend_function_entry httpi_class_methods
[] = {
162 HTTPi_ME(date
, http_date
, NULL
)
163 HTTPi_ME(absoluteURI
, http_absolute_uri
, NULL
)
164 HTTPi_ME(negotiateLanguage
, http_negotiate_language
, NULL
)
165 HTTPi_ME(negotiateCharset
, http_negotiate_charset
, NULL
)
166 HTTPi_ME(redirect
, http_redirect
, NULL
)
167 HTTPi_ME(sendStatus
, http_send_status
, NULL
)
168 HTTPi_ME(sendLastModified
, http_send_last_modified
, NULL
)
169 HTTPi_ME(sendContentType
, http_send_content_type
, NULL
)
170 HTTPi_ME(sendContentDisposition
, http_send_content_disposition
, NULL
)
171 HTTPi_ME(matchModified
, http_match_modified
, NULL
)
172 HTTPi_ME(matchEtag
, http_match_etag
, NULL
)
173 HTTPi_ME(cacheLastModified
, http_cache_last_modified
, NULL
)
174 HTTPi_ME(cacheEtag
, http_cache_etag
, NULL
)
175 HTTPi_ME(chunkedDecode
, http_chunked_decode
, NULL
)
176 HTTPi_ME(splitResponse
, http_split_response
, NULL
)
177 HTTPi_ME(parseHeaders
, http_parse_headers
, NULL
)
178 HTTPi_ME(getRequestHeaders
, http_get_request_headers
, NULL
)
179 #ifdef HTTP_HAVE_CURL
180 HTTPi_ME(get
, http_get
, http_request_info_ref_3
)
181 HTTPi_ME(head
, http_head
, http_request_info_ref_3
)
182 HTTPi_ME(postData
, http_post_data
, http_request_info_ref_4
)
183 HTTPi_ME(postArray
, http_post_array
, http_request_info_ref_4
)
185 HTTPi_ME(authBasic
, http_auth_basic
, NULL
)
186 HTTPi_ME(authBasicCallback
, http_auth_basic_cb
, NULL
)
191 /* {{{ HTTPi_Response */
193 zend_class_entry
*httpi_response_ce
;
194 static zend_object_handlers httpi_response_object_handlers
;
198 } httpi_response_object
;
200 #define httpi_response_declare_default_properties(ce) _httpi_response_declare_default_properties(ce TSRMLS_CC)
201 static inline void _httpi_response_declare_default_properties(zend_class_entry
*ce TSRMLS_DC
)
203 DCL_PROP(PROTECTED
, string
, contentType
, "application/x-octetstream");
204 DCL_PROP(PROTECTED
, string
, eTag
, "");
205 DCL_PROP(PROTECTED
, string
, dispoFile
, "");
206 DCL_PROP(PROTECTED
, string
, cacheControl
, "public");
207 DCL_PROP(PROTECTED
, string
, data
, "");
208 DCL_PROP(PROTECTED
, string
, file
, "");
209 DCL_PROP(PROTECTED
, long, stream
, 0);
210 DCL_PROP(PROTECTED
, long, lastModified
, 0);
211 DCL_PROP(PROTECTED
, long, dispoInline
, 0);
212 DCL_PROP(PROTECTED
, long, cache
, 0);
213 DCL_PROP(PROTECTED
, long, gzip
, 0);
215 DCL_PROP(PRIVATE
, long, raw_cache_header
, 0);
216 DCL_PROP(PRIVATE
, long, send_mode
, -1);
219 #define httpi_response_destroy_object _httpi_response_destroy_object
220 void _httpi_response_destroy_object(void *object
, zend_object_handle handle TSRMLS_DC
)
222 httpi_response_object
*o
= object
;
224 zend_hash_destroy(OBJ_PROP(o
));
225 FREE_HASHTABLE(OBJ_PROP(o
));
230 #define httpi_response_new_object _httpi_response_new_object
231 zend_object_value
_httpi_response_new_object(zend_class_entry
*ce TSRMLS_DC
)
233 zend_object_value ov
;
234 httpi_response_object
*o
;
236 o
= ecalloc(sizeof(httpi_response_object
), 1);
239 ALLOC_HASHTABLE(OBJ_PROP(o
));
240 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
241 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
243 ov
.handle
= zend_objects_store_put(o
, httpi_response_destroy_object
, NULL
, NULL TSRMLS_CC
);
244 ov
.handlers
= &httpi_response_object_handlers
;
249 zend_function_entry httpi_response_class_methods
[] = {
250 PHP_ME(HTTPi_Response
, __construct
, NULL
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
251 /* PHP_ME(HTTPi_Response, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
253 PHP_ME(HTTPi_Response
, setETag
, NULL
, ZEND_ACC_PUBLIC
)
254 PHP_ME(HTTPi_Response
, getETag
, NULL
, ZEND_ACC_PUBLIC
)
256 PHP_ME(HTTPi_Response
, setContentDisposition
, NULL
, ZEND_ACC_PUBLIC
)
257 PHP_ME(HTTPi_Response
, getContentDisposition
, NULL
, ZEND_ACC_PUBLIC
)
259 PHP_ME(HTTPi_Response
, setContentType
, NULL
, ZEND_ACC_PUBLIC
)
260 PHP_ME(HTTPi_Response
, getContentType
, NULL
, ZEND_ACC_PUBLIC
)
262 PHP_ME(HTTPi_Response
, setCache
, NULL
, ZEND_ACC_PUBLIC
)
263 PHP_ME(HTTPi_Response
, getCache
, NULL
, ZEND_ACC_PUBLIC
)
265 PHP_ME(HTTPi_Response
, setCacheControl
, NULL
, ZEND_ACC_PUBLIC
)
266 PHP_ME(HTTPi_Response
, getCacheControl
, NULL
, ZEND_ACC_PUBLIC
)
268 PHP_ME(HTTPi_Response
, setGzip
, NULL
, ZEND_ACC_PUBLIC
)
269 PHP_ME(HTTPi_Response
, getGzip
, NULL
, ZEND_ACC_PUBLIC
)
271 PHP_ME(HTTPi_Response
, setData
, NULL
, ZEND_ACC_PUBLIC
)
272 PHP_ME(HTTPi_Response
, getData
, NULL
, ZEND_ACC_PUBLIC
)
274 PHP_ME(HTTPi_Response
, setFile
, NULL
, ZEND_ACC_PUBLIC
)
275 PHP_ME(HTTPi_Response
, getFile
, NULL
, ZEND_ACC_PUBLIC
)
277 PHP_ME(HTTPi_Response
, setStream
, NULL
, ZEND_ACC_PUBLIC
)
278 PHP_ME(HTTPi_Response
, getStream
, NULL
, ZEND_ACC_PUBLIC
)
280 PHP_ME(HTTPi_Response
, send
, NULL
, ZEND_ACC_PUBLIC
)
285 /* {{{ proto void HTTPi_Response::__construct(bool cache, bool gzip)
288 PHP_METHOD(HTTPi_Response
, __construct
)
290 zend_bool do_cache
= 0, do_gzip
= 0;
291 getObject(httpi_response_object
, obj
);
293 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
298 UPD_PROP(obj
, long, cache
, do_cache
);
299 UPD_PROP(obj
, long, gzip
, do_gzip
);
303 /* {{{ proto bool HTTPi_Response::setCache(bool cache)
306 PHP_METHOD(HTTPi_Response
, setCache
)
308 zend_bool do_cache
= 0;
309 getObject(httpi_response_object
, obj
);
311 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
315 UPD_PROP(obj
, long, cache
, do_cache
);
320 /* {{{ proto bool HTTPi_Response::getCache()
323 PHP_METHOD(HTTPi_Response
, getCache
)
325 zval
*do_cache
= NULL
;
326 getObject(httpi_response_object
, obj
);
330 do_cache
= GET_PROP(obj
, cache
);
331 RETURN_BOOL(Z_LVAL_P(do_cache
));
335 /* {{{ proto bool HTTPi_Response::setGzip(bool gzip)
338 PHP_METHOD(HTTPi_Response
, setGzip
)
340 zend_bool do_gzip
= 0;
341 getObject(httpi_response_object
, obj
);
343 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
347 UPD_PROP(obj
, long, gzip
, do_gzip
);
352 /* {{{ proto bool HTTPi_Response::getGzip()
355 PHP_METHOD(HTTPi_Response
, getGzip
)
357 zval
*do_gzip
= NULL
;
358 getObject(httpi_response_object
, obj
);
362 do_gzip
= GET_PROP(obj
, gzip
);
363 RETURN_BOOL(Z_LVAL_P(do_gzip
));
367 /* {{{ proto bool HTTPi_Response::setCacheControl(string control[, bool raw = false])
370 PHP_METHOD(HTTPi_Response
, setCacheControl
)
375 getObject(httpi_response_object
, obj
);
377 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
381 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
382 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
386 UPD_PROP(obj
, long, raw_cache_header
, raw
);
387 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
392 /* {{{ proto string HTTPi_Response::getCacheControl()
395 PHP_METHOD(HTTPi_Response
, getCacheControl
)
398 getObject(httpi_response_object
, obj
);
402 ccontrol
= GET_PROP(obj
, cacheControl
);
403 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
407 /* {{{ proto bool HTTPi::setContentType(string content_type)
410 PHP_METHOD(HTTPi_Response
, setContentType
)
414 getObject(httpi_response_object
, obj
);
416 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
420 if (!strchr(ctype
, '/')) {
421 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
422 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
426 UPD_PROP(obj
, string
, contentType
, ctype
);
432 /* {{{ proto string HTTPi_Response::getContentType()
435 PHP_METHOD(HTTPi_Response
, getContentType
)
438 getObject(httpi_response_object
, obj
);
442 ctype
= GET_PROP(obj
, contentType
);
443 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
447 /* {{{ proto bool HTTPi_Response::setContentDisposition(string filename[, bool inline = false])
450 PHP_METHOD(HTTPi_Response
, setContentDisposition
)
454 zend_bool is_inline
= 0;
455 getObject(httpi_response_object
, obj
);
457 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
461 UPD_PROP(obj
, string
, dispoFile
, file
);
462 UPD_PROP(obj
, long, dispoInline
, is_inline
);
467 /* {{{ proto array HTTPi_Response::getContentDisposition()
470 PHP_METHOD(HTTPi_Response
, getContentDisposition
)
474 getObject(httpi_response_object
, obj
);
476 if (ZEND_NUM_ARGS()) {
480 file
= GET_PROP(obj
, dispoFile
);
481 is_inline
= GET_PROP(obj
, dispoInline
);
483 array_init(return_value
);
484 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
485 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
489 /* {{{ proto bool HTTPi_Response::setETag(string etag)
492 PHP_METHOD(HTTPi_Response
, setETag
)
496 getObject(httpi_response_object
, obj
);
498 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
502 UPD_PROP(obj
, string
, eTag
, etag
);
507 /* {{{ proto string HTTPi_Response::getETag()
510 PHP_METHOD(HTTPi_Response
, getETag
)
513 getObject(httpi_response_object
, obj
);
517 etag
= GET_PROP(obj
, eTag
);
518 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
522 /* {{{ proto bool HTTPi_Response::setData(string data)
525 PHP_METHOD(HTTPi_Response
, setData
)
529 getObject(httpi_response_object
, obj
);
531 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_data
)) {
535 convert_to_string_ex(&the_data
);
536 SET_PROP(obj
, data
, the_data
);
537 UPD_PROP(obj
, long, lastModified
, http_lmod(the_data
, SEND_DATA
));
538 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
543 /* {{{ proto string HTTPi_Response::getData()
546 PHP_METHOD(HTTPi_Response
, getData
)
549 getObject(httpi_response_object
, obj
);
553 the_data
= GET_PROP(obj
, data
);
554 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
558 /* {{{ proto bool HTTPi_Response::setStream(resource stream)
561 PHP_METHOD(HTTPi_Response
, setStream
)
564 php_stream
*the_real_stream
;
566 getObject(httpi_response_object
, obj
);
568 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
572 php_stream_from_zval(the_real_stream
, &the_stream
);
574 SET_PROP(obj
, stream
, the_stream
);
575 UPD_PROP(obj
, long, lastModified
, http_lmod(the_real_stream
, SEND_RSRC
));
576 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
581 /* {{{ proto resource HTTPi_Response::getStream()
584 PHP_METHOD(HTTPi_Response
, getStream
)
587 getObject(httpi_response_object
, obj
);
591 the_stream
= GET_PROP(obj
, stream
);
592 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
596 /* {{{ proto bool HTTPi_Response::setFile(string file)
599 PHP_METHOD(HTTPi_Response
, setFile
)
602 getObject(httpi_response_object
, obj
);
604 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
608 convert_to_string_ex(&the_file
);
610 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
611 UPD_PROP(obj
, long, lastModified
, http_lmod(the_file
, -1));
612 UPD_PROP(obj
, long, send_mode
, -1);
617 /* {{{ proto string HTTPi_Response::getFile()
620 PHP_METHOD(HTTPi_Response
, getFile
)
623 getObject(httpi_response_object
, obj
);
627 the_file
= GET_PROP(obj
, file
);
628 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
632 PHP_METHOD(HTTPi_Response
, send
)
634 zval
*do_cache
, *do_gzip
;
635 getObject(httpi_response_object
, obj
);
637 do_cache
= GET_PROP(obj
, cache
);
638 do_gzip
= GET_PROP(obj
, gzip
);
641 if (Z_LVAL_P(do_cache
)) {
642 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
644 etag
= GET_PROP(obj
, eTag
);
645 lmod
= GET_PROP(obj
, lastModified
);
646 cctrl
= GET_PROP(obj
, cacheControl
);
647 ccraw
= GET_PROP(obj
, raw_cache_header
);
649 if (Z_LVAL_P(ccraw
)) {
650 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
651 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
));
653 char cc_header
[42] = {0};
654 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
655 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
656 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
661 if (Z_LVAL_P(do_gzip
)) {
667 zval
*ctype
= GET_PROP(obj
, contentType
);
668 if (Z_STRLEN_P(ctype
)) {
669 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
671 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
675 /* content disposition */
677 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
678 if (Z_STRLEN_P(dispo_file
)) {
679 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
680 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), Z_LVAL_P(dispo_inline
));
686 zval
*send_mode
= GET_PROP(obj
, send_mode
);
687 switch (Z_LVAL_P(send_mode
))
691 RETURN_SUCCESS(http_send_data(GET_PROP(obj
, data
)));
696 php_stream
*the_real_stream
;
697 zval
*the_stream
= GET_PROP(obj
, stream
);
698 php_stream_from_zval(the_real_stream
, &the_stream
);
699 RETURN_SUCCESS(http_send_stream(the_real_stream
));
704 RETURN_SUCCESS(http_send_file(GET_PROP(obj
, file
)));
711 /* {{{ HTTPi_Request */
712 #ifdef HTTP_HAVE_CURL
714 zend_class_entry
*httpi_request_ce
;
715 static zend_object_handlers httpi_request_object_handlers
;
720 } httpi_request_object
;
722 #define httpi_request_declare_default_properties(ce) _httpi_request_declare_default_properties(ce TSRMLS_CC)
723 static inline void _httpi_request_declare_default_properties(zend_class_entry
*ce TSRMLS_DC
)
725 DCL_PROP_N(PROTECTED
, options
);
726 DCL_PROP_N(PROTECTED
, responseInfo
);
727 DCL_PROP_N(PROTECTED
, responseData
);
729 DCL_PROP(PROTECTED
, long, method
, HTTP_GET
);
731 DCL_PROP(PROTECTED
, string
, url
, "");
732 DCL_PROP(PROTECTED
, string
, contentType
, "");
733 DCL_PROP(PROTECTED
, string
, queryData
, "");
736 #define httpi_request_destroy_object _httpi_request_destroy_object
737 void _httpi_request_destroy_object(void *object
, zend_object_handle handle TSRMLS_DC
)
739 httpi_request_object
*o
= object
;
741 zend_hash_destroy(OBJ_PROP(o
));
742 FREE_HASHTABLE(OBJ_PROP(o
));
745 curl_easy_cleanup(o
->ch
);
750 #define httpi_request_new_object _httpi_request_new_object
751 zend_object_value
_httpi_request_new_object(zend_class_entry
*ce TSRMLS_DC
)
753 zend_object_value ov
;
754 httpi_request_object
*o
;
756 o
= ecalloc(sizeof(httpi_request_object
), 1);
758 o
->ch
= curl_easy_init();
760 ALLOC_HASHTABLE(OBJ_PROP(o
));
761 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
762 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
764 ov
.handle
= zend_objects_store_put(o
, httpi_request_destroy_object
, NULL
, NULL TSRMLS_CC
);
765 ov
.handlers
= &httpi_request_object_handlers
;
770 zend_function_entry httpi_request_class_methods
[] = {
771 PHP_ME(HTTPi_Request
, __construct
, NULL
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
772 /* PHP_ME(HTTPi_Request, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
774 PHP_ME(HTTPi_Request, setOptions, NULL, ZEND_ACC_PUBLIC)
775 PHP_ME(HTTPi_Request, getOptions, NULL, ZEND_ACC_PUBLIC)
777 PHP_ME(HTTPi_Request
, setMethod
, NULL
, ZEND_ACC_PUBLIC
)
778 PHP_ME(HTTPi_Request
, getMethod
, NULL
, ZEND_ACC_PUBLIC
)
780 PHP_ME(HTTPi_Request
, setURL
, NULL
, ZEND_ACC_PUBLIC
)
781 PHP_ME(HTTPi_Request
, getURL
, NULL
, ZEND_ACC_PUBLIC
)
783 PHP_ME(HTTPi_Request
, setContentType
, NULL
, ZEND_ACC_PUBLIC
)
784 PHP_ME(HTTPi_Request
, getContentType
, NULL
, ZEND_ACC_PUBLIC
)
786 PHP_ME(HTTPi_Request
, setQueryData
, NULL
, ZEND_ACC_PUBLIC
)
787 PHP_ME(HTTPi_Request
, getQueryData
, NULL
, ZEND_ACC_PUBLIC
)
788 PHP_ME(HTTPi_Request
, addQueryData
, NULL
, ZEND_ACC_PUBLIC
)
789 PHP_ME(HTTPi_Request
, unsetQueryData
, NULL
, ZEND_ACC_PUBLIC
)
791 PHP_ME(HTTPi_Request, setPostData, NULL, ZEND_ACC_PUBLIC)
792 PHP_ME(HTTPi_Request, addPostData, NULL, ZEND_ACC_PUBLIC)
793 PHP_ME(HTTPi_Request, unsetPostData, NULL, ZEND_ACC_PUBLIC)
795 PHP_ME(HTTPi_Request, addPostFile, NULL, ZEND_ACC_PUBLIC)
797 PHP_ME(HTTPi_Request
, send
, NULL
, ZEND_ACC_PUBLIC
)
799 PHP_ME(HTTPi_Request
, getResponseData
, NULL
, ZEND_ACC_PUBLIC
)
800 PHP_ME(HTTPi_Request
, getResponseHeaders
, NULL
, ZEND_ACC_PUBLIC
)
801 PHP_ME(HTTPi_Request
, getResponseBody
, NULL
, ZEND_ACC_PUBLIC
)
806 /* {{{ proto void HTTPi_request::__construct([string url[, long request_method = HTTP_GET]])
809 PHP_METHOD(HTTPi_Request
, __construct
)
814 zval
*info
, *opts
, *resp
;
815 getObject(httpi_request_object
, obj
);
817 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
821 MAKE_STD_ZVAL(opts
); array_init(opts
); SET_PROP(obj
, options
, opts
);
822 MAKE_STD_ZVAL(info
); array_init(info
); SET_PROP(obj
, responseInfo
, info
);
823 MAKE_STD_ZVAL(resp
); array_init(resp
); SET_PROP(obj
, responseData
, resp
);
826 UPD_PROP(obj
, string
, url
, URL
);
829 UPD_PROP(obj
, long, method
, meth
);
834 /* {{{ proto bool HTTPi_request::setURL(string url)
837 PHP_METHOD(HTTPi_Request
, setURL
)
841 getObject(httpi_request_object
, obj
);
843 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
847 UPD_PROP(obj
, string
, url
, URL
);
852 /* {{{ proto string HTTPi_request::getUrl()
855 PHP_METHOD(HTTPi_Request
, getURL
)
858 getObject(httpi_request_object
, obj
);
862 URL
= GET_PROP(obj
, url
);
863 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
867 /* {{{ proto bool HTTPi_request::setMethod(long request_method)
870 PHP_METHOD(HTTPi_Request
, setMethod
)
873 getObject(httpi_request_object
, obj
);
875 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
879 UPD_PROP(obj
, long, method
, meth
);
884 /* {{{ proto long HTTPi_request::getMethod()
887 PHP_METHOD(HTTPi_Request
, getMethod
)
890 getObject(httpi_request_object
, obj
);
894 meth
= GET_PROP(obj
, method
);
895 RETURN_LONG(Z_LVAL_P(meth
));
899 /* {{{ proto bool HTTPi_request::setContentType(string content_type)
902 PHP_METHOD(HTTPi_Request
, setContentType
)
906 getObject(httpi_request_object
, obj
);
908 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
912 if (!strchr(ctype
, '/')) {
913 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
914 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
919 UPD_PROP(obj
, string
, contentType
, ctype
);
924 /* {{{ proto string HTTPi_request::getContentType()
927 PHP_METHOD(HTTPi_Request
, getContentType
)
930 getObject(httpi_request_object
, obj
);
934 ctype
= GET_PROP(obj
, contentType
);
935 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
939 /* {{{ proto bool HTTPi_request::setQueryData(mixed query_data)
942 PHP_METHOD(HTTPi_Request
, setQueryData
)
945 getObject(httpi_request_object
, obj
);
947 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
951 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
952 smart_str qstr
= {0};
953 HTTP_URL_ARGSEP_OVERRIDE
;
954 if (SUCCESS
!= php_url_encode_hash_ex(HASH_OF(qdata
), &qstr
, NULL
, 0, NULL
, 0, NULL
, 0, NULL TSRMLS_CC
)) {
955 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
959 HTTP_URL_ARGSEP_RESTORE
;
962 HTTP_URL_ARGSEP_RESTORE
;
964 UPD_PROP(obj
, string
, queryData
, qstr
.c
);
969 convert_to_string(qdata
);
970 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
975 /* {{{ proto string HTTPi_request::getQueryData()
978 PHP_METHOD(HTTPi_Request
, getQueryData
)
981 getObject(httpi_request_object
, obj
);
985 qdata
= GET_PROP(obj
, queryData
);
986 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
990 /* {{{ proto bool HTTPi_request::addQueryData(array query_params)
993 PHP_METHOD(HTTPi_Request
, addQueryData
)
995 zval
*qdata
, *old_qdata
;
996 smart_str qstr
= {0};
998 getObject(httpi_request_object
, obj
);
1000 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1004 old_qdata
= GET_PROP(obj
, queryData
);
1005 if (Z_STRLEN_P(old_qdata
)) {
1006 smart_str_appendl(&qstr
, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
));
1009 HTTP_URL_ARGSEP_OVERRIDE
;
1010 if (SUCCESS
!= php_url_encode_hash_ex(HASH_OF(qdata
), &qstr
, NULL
, 0, NULL
, 0, NULL
, 0, NULL TSRMLS_CC
)) {
1011 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
1015 HTTP_URL_ARGSEP_RESTORE
;
1018 HTTP_URL_ARGSEP_RESTORE
;
1022 UPD_PROP(obj
, string
, queryData
, qstr
.c
);
1028 /* {{{ proto void HTTPi_request::unsetQueryData()
1031 PHP_METHOD(HTTPi_Request
, unsetQueryData
)
1033 getObject(httpi_request_object
, obj
);
1037 UPD_PROP(obj
, string
, queryData
, "");
1041 /* {{{ proto array HTTPi_request::getResponseData()
1044 PHP_METHOD(HTTPi_Request
, getResponseData
)
1047 getObject(httpi_request_object
, obj
);
1051 data
= GET_PROP(obj
, responseData
);
1052 array_init(return_value
);
1053 zend_hash_copy(Z_ARRVAL_P(return_value
), Z_ARRVAL_P(data
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1057 /* {{{ proto array HTTPi_request::getResponseHeaders()
1060 PHP_METHOD(HTTPi_Request
, getResponseHeaders
)
1062 zval
*data
, **headers
;
1063 getObject(httpi_request_object
, obj
);
1067 array_init(return_value
);
1068 data
= GET_PROP(obj
, responseData
);
1069 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1070 zend_hash_copy(Z_ARRVAL_P(return_value
), Z_ARRVAL_PP(headers
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
1075 /* {{{ proto string HTTPi_request::getResponseBody()
1078 PHP_METHOD(HTTPi_Request
, getResponseBody
)
1081 getObject(httpi_request_object
, obj
);
1085 data
= GET_PROP(obj
, responseData
);
1086 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1087 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
1089 Z_TYPE_P(return_value
) = IS_NULL
;
1094 /* {{{ proto bool HTTPi_request::send()
1097 PHP_METHOD(HTTPi_Request
, send
)
1099 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
1100 char *response_data
, *request_uri
;
1101 size_t response_len
;
1102 getObject(httpi_request_object
, obj
);
1106 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
1107 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initilaize cURL");
1111 meth
= GET_PROP(obj
, method
);
1112 URL
= GET_PROP(obj
, url
);
1113 qdata
= GET_PROP(obj
, queryData
);
1114 opts
= GET_PROP(obj
, options
);
1115 info
= GET_PROP(obj
, responseInfo
);
1116 resp
= GET_PROP(obj
, responseData
);
1118 request_uri
= http_absolute_uri(Z_STRVAL_P(URL
), NULL
);
1119 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
1120 if (!strchr(request_uri
, '?')) {
1121 strcat(request_uri
, "?");
1123 strcat(request_uri
, "&");
1125 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1128 switch (Z_LVAL_P(meth
))
1131 if (SUCCESS
!= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
)) {
1137 if (SUCCESS
!= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
)) {
1149 /* final data handling */
1151 zval
*zheaders
, *zbody
;
1153 MAKE_STD_ZVAL(zbody
);
1154 MAKE_STD_ZVAL(zheaders
)
1155 array_init(zheaders
);
1157 if (SUCCESS
!= http_split_response_ex(response_data
, response_len
, zheaders
, zbody
)) {
1158 zval_dtor(zheaders
);
1164 add_assoc_zval(resp
, "headers", zheaders
);
1165 add_assoc_zval(resp
, "body", zbody
);
1173 #endif /* HTTP_HAVE_CURL */
1176 #endif /* ZEND_ENGINE_2 */
1178 /* {{{ http_module_entry */
1179 zend_module_entry http_module_entry
= {
1180 #if ZEND_MODULE_API_NO >= 20010901
1181 STANDARD_MODULE_HEADER
,
1186 PHP_MSHUTDOWN(http
),
1188 PHP_RSHUTDOWN(http
),
1190 #if ZEND_MODULE_API_NO >= 20010901
1191 PHP_EXT_HTTP_VERSION
,
1193 STANDARD_MODULE_PROPERTIES
1197 /* {{{ proto string http_date([int timestamp])
1199 * This function returns a valid HTTP date regarding RFC 822/1123
1200 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
1203 PHP_FUNCTION(http_date
)
1207 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
1212 t
= (long) time(NULL
);
1215 RETURN_STRING(http_date(t
), 0);
1219 /* {{{ proto string http_absolute_uri(string url[, string proto])
1221 * This function returns an absolute URI constructed from url.
1222 * If the url is already abolute but a different proto was supplied,
1223 * only the proto part of the URI will be updated. If url has no
1224 * path specified, the path of the current REQUEST_URI will be taken.
1225 * The host will be taken either from the Host HTTP header of the client
1226 * the SERVER_NAME or just localhost if prior are not available.
1230 * url = "page.php" => http://www.example.com/current/path/page.php
1231 * url = "/page.php" => http://www.example.com/page.php
1232 * url = "/page.php", proto = "https" => https://www.example.com/page.php
1236 PHP_FUNCTION(http_absolute_uri
)
1238 char *url
= NULL
, *proto
= NULL
;
1239 int url_len
= 0, proto_len
= 0;
1241 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s", &url
, &url_len
, &proto
, &proto_len
) != SUCCESS
) {
1245 RETURN_STRING(http_absolute_uri(url
, proto
), 0);
1249 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
1251 * This function negotiates the clients preferred language based on its
1252 * Accept-Language HTTP header. It returns the negotiated language or
1253 * the default language if none match.
1255 * The qualifier is recognized and languages without qualifier are rated highest.
1257 * The supported parameter is expected to be an array having
1258 * the supported languages as array values.
1264 * 'en-US',// default
1272 * include './langs/'. http_negotiate_language($langs) .'.php';
1277 PHP_FUNCTION(http_negotiate_language
)
1283 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
1291 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
1295 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
1297 * This function negotiates the clients preferred charset based on its
1298 * Accept-Charset HTTP header. It returns the negotiated charset or
1299 * the default charset if none match.
1301 * The qualifier is recognized and charset without qualifier are rated highest.
1303 * The supported parameter is expected to be an array having
1304 * the supported charsets as array values.
1309 * $charsets = array(
1310 * 'iso-8859-1', // default
1315 * $pref = http_negotiate_charset($charsets);
1316 * if (!strcmp($pref, 'iso-8859-1')) {
1317 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
1318 * iconv_set_encoding('output_encoding', $pref);
1319 * ob_start('ob_iconv_handler');
1324 PHP_FUNCTION(http_negotiate_charset
)
1330 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
1338 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
1342 /* {{{ proto bool http_send_status(int status)
1344 * Send HTTP status code.
1347 PHP_FUNCTION(http_send_status
)
1351 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
1354 if (status
< 100 || status
> 510) {
1355 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Invalid HTTP status code (100-510): %d", status
);
1359 RETURN_SUCCESS(http_send_status(status
));
1363 /* {{{ proto bool http_send_last_modified([int timestamp])
1365 * This converts the given timestamp to a valid HTTP date and
1366 * sends it as "Last-Modified" HTTP header. If timestamp is
1367 * omitted, current time is sent.
1370 PHP_FUNCTION(http_send_last_modified
)
1374 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
1379 t
= (long) time(NULL
);
1382 RETURN_SUCCESS(http_send_last_modified(t
));
1386 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
1388 * Sets the content type.
1391 PHP_FUNCTION(http_send_content_type
)
1396 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
1401 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1));
1403 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
1407 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
1409 * Set the Content Disposition. The Content-Disposition header is very useful
1410 * if the data actually sent came from a file or something similar, that should
1411 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
1414 PHP_FUNCTION(http_send_content_disposition
)
1418 zend_bool send_inline
= 0;
1420 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
1423 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
1427 /* {{{ proto bool http_match_modified([int timestamp])
1429 * Matches the given timestamp against the clients "If-Modified-Since" resp.
1430 * "If-Unmodified-Since" HTTP headers.
1433 PHP_FUNCTION(http_match_modified
)
1437 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
1441 // current time if not supplied (senseless though)
1443 t
= (long) time(NULL
);
1446 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t
) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t
));
1450 /* {{{ proto bool http_match_etag(string etag)
1452 * This matches the given ETag against the clients
1453 * "If-Match" resp. "If-None-Match" HTTP headers.
1456 PHP_FUNCTION(http_match_etag
)
1461 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
) != SUCCESS
) {
1465 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag
) || http_etag_match("HTTP_IF_MATCH", etag
));
1469 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
1471 * If timestamp_or_exires is greater than 0, it is handled as timestamp
1472 * and will be sent as date of last modification. If it is 0 or omitted,
1473 * the current time will be sent as Last-Modified date. If it's negative,
1474 * it is handled as expiration time in seconds, which means that if the
1475 * requested last modification date is not between the calculated timespan,
1476 * the Last-Modified header is updated and the actual body will be sent.
1479 PHP_FUNCTION(http_cache_last_modified
)
1481 long last_modified
= 0, send_modified
= 0, t
;
1484 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
1488 t
= (long) time(NULL
);
1491 if (!last_modified
) {
1492 /* does the client have? (att: caching "forever") */
1493 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
1494 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
1495 /* send current time */
1499 /* negative value is supposed to be expiration time */
1500 } else if (last_modified
< 0) {
1503 /* send supplied time explicitly */
1505 send_modified
= last_modified
;
1508 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, sizeof(HTTP_DEFAULT_CACHECONTROL
) - 1));
1512 /* {{{ proto bool http_cache_etag([string etag])
1514 * This function attempts to cache the HTTP body based on an ETag,
1515 * either supplied or generated through calculation of the MD5
1516 * checksum of the output (uses output buffering).
1518 * If clients "If-None-Match" header matches the supplied/calculated
1519 * ETag, the body is considered cached on the clients side and
1520 * a "304 Not Modified" status code is issued.
1523 PHP_FUNCTION(http_cache_etag
)
1528 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
1532 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, sizeof(HTTP_DEFAULT_CACHECONTROL
) - 1));
1536 /* {{{ proto string ob_httpetaghandler(string data, int mode)
1538 * For use with ob_start().
1539 * Note that this has to be started as first output buffer.
1540 * WARNING: Don't use with http_send_*().
1542 PHP_FUNCTION(ob_httpetaghandler
)
1548 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1552 if (mode
& PHP_OUTPUT_HANDLER_START
) {
1553 if (HTTP_G(etag_started
)) {
1554 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "ob_httpetaghandler can only be used once");
1555 RETURN_STRINGL(data
, data_len
, 1);
1557 http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL
);
1558 HTTP_G(etag_started
) = 1;
1561 if (OG(ob_nesting_level
) > 1) {
1562 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "ob_httpetaghandler must be started prior to other output buffers");
1563 RETURN_STRINGL(data
, data_len
, 1);
1566 Z_TYPE_P(return_value
) = IS_STRING
;
1567 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), &Z_STRLEN_P(return_value
), mode
);
1571 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
1573 * Redirect to a given url.
1574 * The supplied url will be expanded with http_absolute_uri(), the params array will
1575 * be treated with http_build_query() and the session identification will be appended
1576 * if session is true.
1578 * Depending on permanent the redirection will be issued with a permanent
1579 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
1582 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
1583 * if the client doesn't redirect immediatly.
1585 PHP_FUNCTION(http_redirect
)
1588 zend_bool session
= 0, permanent
= 0;
1589 zval
*params
= NULL
;
1590 smart_str qstr
= {0};
1591 char *url
, *URI
, LOC
[HTTP_URI_MAXLEN
+ 9], RED
[HTTP_URI_MAXLEN
* 2 + 34];
1593 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
1597 /* append session info */
1598 if (session
&& (PS(session_status
) == php_session_active
)) {
1600 MAKE_STD_ZVAL(params
);
1603 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
1604 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not append session information");
1608 /* treat params array with http_build_query() */
1610 if (php_url_encode_hash_ex(Z_ARRVAL_P(params
), &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
1614 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode query parameters");
1620 URI
= http_absolute_uri(url
, NULL
);
1622 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s?%s", URI
, qstr
.c
);
1623 sprintf(RED
, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, qstr
.c
, URI
, qstr
.c
);
1626 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s", URI
);
1627 sprintf(RED
, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
1631 if ((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302)))) {
1632 php_body_write(RED
, strlen(RED
) TSRMLS_CC
);
1639 /* {{{ proto bool http_send_data(string data)
1641 * Sends raw data with support for (multiple) range requests.
1644 PHP_FUNCTION(http_send_data
)
1648 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
1652 convert_to_string_ex(&zdata
);
1653 http_send_header("Accept-Ranges: bytes");
1654 RETURN_SUCCESS(http_send_data(zdata
));
1658 /* {{{ proto bool http_send_file(string file)
1660 * Sends a file with support for (multiple) range requests.
1663 PHP_FUNCTION(http_send_file
)
1667 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zfile
) != SUCCESS
) {
1671 convert_to_string_ex(&zfile
);
1672 http_send_header("Accept-Ranges: bytes");
1673 RETURN_SUCCESS(http_send_file(zfile
));
1677 /* {{{ proto bool http_send_stream(resource stream)
1679 * Sends an already opened stream with support for (multiple) range requests.
1682 PHP_FUNCTION(http_send_stream
)
1687 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
1691 php_stream_from_zval(file
, &zstream
);
1692 http_send_header("Accept-Ranges: bytes");
1693 RETURN_SUCCESS(http_send_stream(file
));
1697 /* {{{ proto string http_chunked_decode(string encoded)
1699 * This function decodes a string that was HTTP-chunked encoded.
1700 * Returns false on failure.
1702 PHP_FUNCTION(http_chunked_decode
)
1704 char *encoded
= NULL
, *decoded
= NULL
;
1705 int encoded_len
= 0, decoded_len
= 0;
1707 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
1711 if (SUCCESS
== http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
1712 RETURN_STRINGL(decoded
, decoded_len
, 0);
1719 /* {{{ proto array http_split_response(string http_response)
1721 * This function splits an HTTP response into an array with headers and the
1722 * content body. The returned array may look simliar to the following example:
1728 * 'Status' => '200 Ok',
1729 * 'Content-Type' => 'text/plain',
1730 * 'Content-Language' => 'en-US'
1732 * 1 => "Hello World!"
1737 PHP_FUNCTION(http_split_response
)
1739 zval
*zresponse
, *zbody
, *zheaders
;
1741 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zresponse
) != SUCCESS
) {
1745 convert_to_string_ex(&zresponse
);
1747 MAKE_STD_ZVAL(zbody
);
1748 MAKE_STD_ZVAL(zheaders
);
1749 array_init(zheaders
);
1751 if (SUCCESS
!= http_split_response(zresponse
, zheaders
, zbody
)) {
1752 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP response");
1756 array_init(return_value
);
1757 add_index_zval(return_value
, 0, zheaders
);
1758 add_index_zval(return_value
, 1, zbody
);
1762 /* {{{ proto array http_parse_headers(string header)
1765 PHP_FUNCTION(http_parse_headers
)
1767 char *header
, *rnrn
;
1770 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
1774 array_init(return_value
);
1776 if (rnrn
= strstr(header
, HTTP_CRLF HTTP_CRLF
)) {
1777 header_len
= rnrn
- header
+ 2;
1779 if (SUCCESS
!= http_parse_headers(header
, header_len
, return_value
)) {
1780 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP header");
1781 zval_dtor(return_value
);
1787 /* {{{ proto array http_get_request_headers(void)
1790 PHP_FUNCTION(http_get_request_headers
)
1792 if (ZEND_NUM_ARGS()) {
1796 array_init(return_value
);
1797 http_get_request_headers(return_value
);
1802 #ifdef HTTP_HAVE_CURL
1804 /* {{{ proto string http_get(string url[, array options[, array &info]])
1806 * Performs an HTTP GET request on the supplied url.
1808 * The second parameter is expected to be an associative
1809 * array where the following keys will be recognized:
1811 * - redirect: int, whether and how many redirects to follow
1812 * - unrestrictedauth: bool, whether to continue sending credentials on
1813 * redirects to a different host
1814 * - proxyhost: string, proxy host in "host[:port]" format
1815 * - proxyport: int, use another proxy port as specified in proxyhost
1816 * - proxyauth: string, proxy credentials in "user:pass" format
1817 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1818 * - httpauth: string, http credentials in "user:pass" format
1819 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1820 * - compress: bool, whether to allow gzip/deflate content encoding
1821 * (defaults to true)
1822 * - port: int, use another port as specified in the url
1823 * - referer: string, the referer to sends
1824 * - useragent: string, the user agent to send
1825 * (defaults to PECL::HTTP/version (PHP/version)))
1826 * - headers: array, list of custom headers as associative array
1827 * like array("header" => "value")
1828 * - cookies: array, list of cookies as associative array
1829 * like array("cookie" => "value")
1830 * - cookiestore: string, path to a file where cookies are/will be stored
1833 * The optional third parameter will be filled with some additional information
1834 * in form af an associative array, if supplied, like the following example:
1838 * 'effective_url' => 'http://localhost',
1839 * 'response_code' => 403,
1840 * 'total_time' => 0.017,
1841 * 'namelookup_time' => 0.013,
1842 * 'connect_time' => 0.014,
1843 * 'pretransfer_time' => 0.014,
1844 * 'size_upload' => 0,
1845 * 'size_download' => 202,
1846 * 'speed_download' => 11882,
1847 * 'speed_upload' => 0,
1848 * 'header_size' => 145,
1849 * 'request_size' => 62,
1850 * 'ssl_verifyresult' => 0,
1852 * 'content_length_download' => 202,
1853 * 'content_length_upload' => 0,
1854 * 'starttransfer_time' => 0.017,
1855 * 'content_type' => 'text/html; charset=iso-8859-1',
1856 * 'redirect_time' => 0,
1857 * 'redirect_count' => 0,
1859 * 'http_connectcode' => 0,
1860 * 'httpauth_avail' => 0,
1861 * 'proxyauth_avail' => 0,
1866 PHP_FUNCTION(http_get
)
1868 char *URL
, *data
= NULL
;
1869 size_t data_len
= 0;
1871 zval
*options
= NULL
, *info
= NULL
;
1873 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1882 if (SUCCESS
== http_get(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
1883 RETURN_STRINGL(data
, data_len
, 0);
1890 /* {{{ proto string http_head(string url[, array options[, array &info]])
1892 * Performs an HTTP HEAD request on the suppied url.
1893 * Returns the HTTP response as string.
1894 * See http_get() for a full list of available options.
1896 PHP_FUNCTION(http_head
)
1898 char *URL
, *data
= NULL
;
1899 size_t data_len
= 0;
1901 zval
*options
= NULL
, *info
= NULL
;
1903 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1912 if (SUCCESS
== http_head(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
1913 RETURN_STRINGL(data
, data_len
, 0);
1920 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
1922 * Performs an HTTP POST request, posting data.
1923 * Returns the HTTP response as string.
1924 * See http_get() for a full list of available options.
1926 PHP_FUNCTION(http_post_data
)
1928 char *URL
, *postdata
, *data
= NULL
;
1929 size_t data_len
= 0;
1930 int postdata_len
, URL_len
;
1931 zval
*options
= NULL
, *info
= NULL
;
1933 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
1942 if (SUCCESS
== http_post_data(URL
, postdata
, (size_t) postdata_len
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
1943 RETURN_STRINGL(data
, data_len
, 0);
1950 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
1952 * Performs an HTTP POST request, posting www-form-urlencoded array data.
1953 * Returns the HTTP response as string.
1954 * See http_get() for a full list of available options.
1956 PHP_FUNCTION(http_post_array
)
1958 char *URL
, *data
= NULL
;
1959 size_t data_len
= 0;
1961 zval
*options
= NULL
, *info
= NULL
, *postdata
;
1963 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|a/!z", &URL
, &URL_len
, &postdata
, &options
, &info
) != SUCCESS
) {
1972 if (SUCCESS
== http_post_array(URL
, Z_ARRVAL_P(postdata
), HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
1973 RETURN_STRINGL(data
, data_len
, 0);
1984 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1989 * if (!http_auth_basic('mike', 's3c|r3t')) {
1990 * die('<h1>Authorization failed!</h1>');
1995 PHP_FUNCTION(http_auth_basic
)
1997 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
1998 int r_len
, u_len
, p_len
;
2000 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
2005 realm
= "Restricted";
2008 if (SUCCESS
!= http_auth_credentials(&suser
, &spass
)) {
2009 http_auth_header("Basic", realm
);
2013 if (strcasecmp(suser
, user
)) {
2014 http_auth_header("Basic", realm
);
2018 if (strcmp(spass
, pass
)) {
2019 http_auth_header("Basic", realm
);
2027 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
2032 * function auth_cb($user, $pass)
2035 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
2036 * if (strlen($realpass = $db->getOne($query)) {
2037 * return $pass === $realpass;
2042 * if (!http_auth_basic_cb('auth_cb')) {
2043 * die('<h1>Authorization failed</h1>');
2048 PHP_FUNCTION(http_auth_basic_cb
)
2051 char *realm
= NULL
, *user
, *pass
;
2054 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
2059 realm
= "Restricted";
2062 if (SUCCESS
!= http_auth_credentials(&user
, &pass
)) {
2063 http_auth_header("Basic", realm
);
2067 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
2070 MAKE_STD_ZVAL(zparams
[0]);
2071 MAKE_STD_ZVAL(zparams
[1]);
2072 ZVAL_STRING(zparams
[0], user
, 0);
2073 ZVAL_STRING(zparams
[1], pass
, 0);
2075 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
2076 &retval
, 2, zparams TSRMLS_CC
)) {
2077 result
= Z_LVAL(retval
);
2086 http_auth_header("Basic", realm
);
2089 RETURN_BOOL(result
);
2095 /* {{{ php_http_init_globals(zend_http_globals *) */
2096 static void php_http_init_globals(zend_http_globals
*http_globals
)
2098 http_globals
->etag_started
= 0;
2099 http_globals
->ctype
= NULL
;
2100 http_globals
->etag
= NULL
;
2101 http_globals
->lmod
= 0;
2102 #ifdef HTTP_HAVE_CURL
2103 http_globals
->curlbuf
.body
.data
= NULL
;
2104 http_globals
->curlbuf
.body
.used
= 0;
2105 http_globals
->curlbuf
.body
.free
= 0;
2106 http_globals
->curlbuf
.hdrs
.data
= NULL
;
2107 http_globals
->curlbuf
.hdrs
.used
= 0;
2108 http_globals
->curlbuf
.hdrs
.free
= 0;
2110 http_globals
->allowed_methods
= NULL
;
2114 /* {{{ static inline STATUS http_check_allowed_methods(char *, int) */
2115 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
2116 static inline void _http_check_allowed_methods(char *methods
, int length TSRMLS_DC
)
2118 if (length
&& SG(request_info
).request_method
&& (!strstr(methods
, SG(request_info
).request_method
))) {
2119 char *allow_header
= emalloc(length
+ sizeof("Allow: "));
2120 sprintf(allow_header
, "Allow: %s", methods
);
2121 http_send_header(allow_header
);
2122 efree(allow_header
);
2123 http_send_status(405);
2130 PHP_INI_MH(update_allowed_methods
)
2132 http_check_allowed_methods(new_value
, new_value_length
);
2133 return OnUpdateString(entry
, new_value
, new_value_length
, mh_arg1
, mh_arg2
, mh_arg3
, stage TSRMLS_CC
);
2137 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
)
2141 /* {{{ PHP_MINIT_FUNCTION */
2142 PHP_MINIT_FUNCTION(http
)
2144 ZEND_INIT_MODULE_GLOBALS(http
, php_http_init_globals
, NULL
);
2145 REGISTER_INI_ENTRIES();
2147 #ifdef HTTP_HAVE_CURL
2148 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
| CONST_PERSISTENT
);
2149 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
| CONST_PERSISTENT
);
2150 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
| CONST_PERSISTENT
);
2153 #ifdef ZEND_ENGINE_2
2154 HTTP_REGISTER_CLASS(HTTPi
, httpi
, NULL
, ZEND_ACC_FINAL_CLASS
);
2155 HTTP_REGISTER_CLASS_EX(HTTPi_Response
, httpi_response
, NULL
, 0);
2156 # ifdef HTTP_HAVE_CURL
2157 HTTP_REGISTER_CLASS_EX(HTTPi_Request
, httpi_request
, NULL
, 0);
2158 REGISTER_LONG_CONSTANT("HTTP_GET", HTTP_GET
, CONST_CS
| CONST_PERSISTENT
);
2159 REGISTER_LONG_CONSTANT("HTTP_HEAD", HTTP_HEAD
, CONST_CS
| CONST_PERSISTENT
);
2160 REGISTER_LONG_CONSTANT("HTTP_POST", HTTP_POST
, CONST_CS
| CONST_PERSISTENT
);
2161 # endif /* HTTP_HAVE_CURL */
2162 #endif /* ZEND_ENGINE_2 */
2167 /* {{{ PHP_MSHUTDOWN_FUNCTION */
2168 PHP_MSHUTDOWN_FUNCTION(http
)
2170 UNREGISTER_INI_ENTRIES();
2175 /* {{{ PHP_RINIT_FUNCTION */
2176 PHP_RINIT_FUNCTION(http
)
2178 char *allowed_methods
= INI_STR("http.allowed_methods");
2179 http_check_allowed_methods(allowed_methods
, strlen(allowed_methods
));
2184 /* {{{ PHP_RSHUTDOWN_FUNCTION */
2185 PHP_RSHUTDOWN_FUNCTION(http
)
2187 HTTP_G(etag_started
) = 0;
2191 efree(HTTP_G(etag
));
2192 HTTP_G(etag
) = NULL
;
2195 if (HTTP_G(ctype
)) {
2196 efree(HTTP_G(ctype
));
2197 HTTP_G(ctype
) = NULL
;
2199 #ifdef HTTP_HAVE_CURL
2200 if (HTTP_G(curlbuf
).body
.data
) {
2201 efree(HTTP_G(curlbuf
).body
.data
);
2202 HTTP_G(curlbuf
).body
.data
= NULL
;
2204 if (HTTP_G(curlbuf
).hdrs
.data
) {
2205 efree(HTTP_G(curlbuf
).hdrs
.data
);
2206 HTTP_G(curlbuf
).hdrs
.data
= NULL
;
2213 /* {{{ PHP_MINFO_FUNCTION */
2214 PHP_MINFO_FUNCTION(http
)
2216 php_info_print_table_start();
2217 php_info_print_table_header(2, "Extended HTTP support", "enabled");
2218 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION
);
2219 php_info_print_table_row(2, "cURL convenience functions:",
2220 #ifdef HTTP_HAVE_CURL
2226 php_info_print_table_end();
2228 DISPLAY_INI_ENTRIES();
2237 * vim600: noet sw=4 ts=4 fdm=marker
2238 * vim<600: noet sw=4 ts=4