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)
122 # define HTTP_REGISTER_CLASS_EX(classname, name, parent, flags) \
124 zend_class_entry ce; \
125 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
126 ce.create_object = name## _new_object; \
127 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
128 name## _ce->ce_flags |= flags; \
129 memcpy(& name## _object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
130 name## _object_handlers.clone_obj = NULL; \
131 name## _declare_default_properties(name## _ce); \
134 # define HTTP_REGISTER_CLASS(classname, name, parent, flags) \
136 zend_class_entry ce; \
137 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
138 ce.create_object = NULL; \
139 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
140 name## _ce->ce_flags |= flags; \
143 # define getObject(t, o) t * o = ((t *) zend_object_store_get_object(getThis() TSRMLS_CC))
144 # define OBJ_PROP(o) o->zo.properties
145 # define DCL_PROP(a, t, n, v) zend_declare_property_ ##t(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC)
146 # define UPD_PROP(o, t, n, v) zend_update_property_ ##t(o->zo.ce, getThis(), (#n), sizeof(#n), (v) TSRMLS_CC)
147 # define SET_PROP(o, n, z) zend_update_property(o->zo.ce, getThis(), (#n), sizeof(#n), (z) TSRMLS_CC)
148 # define GET_PROP(o, n) zend_read_property(o->zo.ce, getThis(), (#n), sizeof(#n), 0 TSRMLS_CC)
152 zend_class_entry
*httpi_ce
;
154 #define HTTPi_ME(me, al, ai) ZEND_FENTRY(me, ZEND_FN(al), ai, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
156 zend_function_entry httpi_class_methods
[] = {
157 HTTPi_ME(date
, http_date
, NULL
)
158 HTTPi_ME(absoluteURI
, http_absolute_uri
, NULL
)
159 HTTPi_ME(negotiateLanguage
, http_negotiate_language
, NULL
)
160 HTTPi_ME(negotiateCharset
, http_negotiate_charset
, NULL
)
161 HTTPi_ME(redirect
, http_redirect
, NULL
)
162 HTTPi_ME(sendStatus
, http_send_status
, NULL
)
163 HTTPi_ME(sendLastModified
, http_send_last_modified
, NULL
)
164 HTTPi_ME(sendContentType
, http_send_content_type
, NULL
)
165 HTTPi_ME(sendContentDisposition
, http_send_content_disposition
, NULL
)
166 HTTPi_ME(matchModified
, http_match_modified
, NULL
)
167 HTTPi_ME(matchEtag
, http_match_etag
, NULL
)
168 HTTPi_ME(cacheLastModified
, http_cache_last_modified
, NULL
)
169 HTTPi_ME(cacheEtag
, http_cache_etag
, NULL
)
170 HTTPi_ME(chunkedDecode
, http_chunked_decode
, NULL
)
171 HTTPi_ME(splitResponse
, http_split_response
, NULL
)
172 HTTPi_ME(parseHeaders
, http_parse_headers
, NULL
)
173 HTTPi_ME(getRequestHeaders
, http_get_request_headers
, NULL
)
174 #ifdef HTTP_HAVE_CURL
175 HTTPi_ME(get
, http_get
, http_request_info_ref_3
)
176 HTTPi_ME(head
, http_head
, http_request_info_ref_3
)
177 HTTPi_ME(postData
, http_post_data
, http_request_info_ref_4
)
178 HTTPi_ME(postArray
, http_post_array
, http_request_info_ref_4
)
180 HTTPi_ME(authBasic
, http_auth_basic
, NULL
)
181 HTTPi_ME(authBasicCallback
, http_auth_basic_cb
, NULL
)
186 /* {{{ HTTPi_Response */
188 zend_class_entry
*httpi_response_ce
;
189 static zend_object_handlers httpi_response_object_handlers
;
193 } httpi_response_object
;
195 #define httpi_response_declare_default_properties(ce) _httpi_response_declare_default_properties(ce TSRMLS_CC)
196 static inline void _httpi_response_declare_default_properties(zend_class_entry
*ce TSRMLS_DC
)
198 DCL_PROP(PROTECTED
, string
, contentType
, "application/x-octetstream");
199 DCL_PROP(PROTECTED
, string
, eTag
, "");
200 DCL_PROP(PROTECTED
, string
, dispoFile
, "");
201 DCL_PROP(PROTECTED
, string
, cacheControl
, "public");
202 DCL_PROP(PROTECTED
, string
, data
, "");
203 DCL_PROP(PROTECTED
, string
, file
, "");
204 DCL_PROP(PROTECTED
, long, stream
, 0);
205 DCL_PROP(PROTECTED
, long, lastModified
, 0);
206 DCL_PROP(PROTECTED
, long, dispoInline
, 0);
207 DCL_PROP(PROTECTED
, long, cache
, 0);
208 DCL_PROP(PROTECTED
, long, gzip
, 0);
210 DCL_PROP(PRIVATE
, long, raw_cache_header
, 0);
211 DCL_PROP(PRIVATE
, long, send_mode
, -1);
214 #define httpi_response_destroy_object _httpi_response_destroy_object
215 void _httpi_response_destroy_object(void *object
, zend_object_handle handle TSRMLS_DC
)
217 httpi_response_object
*o
= object
;
219 zend_hash_destroy(OBJ_PROP(o
));
220 FREE_HASHTABLE(OBJ_PROP(o
));
225 #define httpi_response_new_object _httpi_response_new_object
226 zend_object_value
_httpi_response_new_object(zend_class_entry
*ce TSRMLS_DC
)
228 zend_object_value ov
;
229 httpi_response_object
*o
;
231 o
= ecalloc(sizeof(httpi_response_object
), 1);
234 ALLOC_HASHTABLE(OBJ_PROP(o
));
235 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
236 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
238 ov
.handle
= zend_objects_store_put(o
, httpi_response_destroy_object
, NULL
, NULL TSRMLS_CC
);
239 ov
.handlers
= &httpi_response_object_handlers
;
244 zend_function_entry httpi_response_class_methods
[] = {
245 PHP_ME(HTTPi_Response
, __construct
, NULL
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
246 /* PHP_ME(HTTPi_Response, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
248 PHP_ME(HTTPi_Response
, setETag
, NULL
, ZEND_ACC_PUBLIC
)
249 PHP_ME(HTTPi_Response
, getETag
, NULL
, ZEND_ACC_PUBLIC
)
251 PHP_ME(HTTPi_Response
, setContentDisposition
, NULL
, ZEND_ACC_PUBLIC
)
252 PHP_ME(HTTPi_Response
, getContentDisposition
, NULL
, ZEND_ACC_PUBLIC
)
254 PHP_ME(HTTPi_Response
, setContentType
, NULL
, ZEND_ACC_PUBLIC
)
255 PHP_ME(HTTPi_Response
, getContentType
, NULL
, ZEND_ACC_PUBLIC
)
257 PHP_ME(HTTPi_Response
, setCache
, NULL
, ZEND_ACC_PUBLIC
)
258 PHP_ME(HTTPi_Response
, getCache
, NULL
, ZEND_ACC_PUBLIC
)
260 PHP_ME(HTTPi_Response
, setCacheControl
, NULL
, ZEND_ACC_PUBLIC
)
261 PHP_ME(HTTPi_Response
, getCacheControl
, NULL
, ZEND_ACC_PUBLIC
)
263 PHP_ME(HTTPi_Response
, setGzip
, NULL
, ZEND_ACC_PUBLIC
)
264 PHP_ME(HTTPi_Response
, getGzip
, NULL
, ZEND_ACC_PUBLIC
)
266 PHP_ME(HTTPi_Response
, setData
, NULL
, ZEND_ACC_PUBLIC
)
267 PHP_ME(HTTPi_Response
, getData
, NULL
, ZEND_ACC_PUBLIC
)
269 PHP_ME(HTTPi_Response
, setFile
, NULL
, ZEND_ACC_PUBLIC
)
270 PHP_ME(HTTPi_Response
, getFile
, NULL
, ZEND_ACC_PUBLIC
)
272 PHP_ME(HTTPi_Response
, setStream
, NULL
, ZEND_ACC_PUBLIC
)
273 PHP_ME(HTTPi_Response
, getStream
, NULL
, ZEND_ACC_PUBLIC
)
275 PHP_ME(HTTPi_Response
, send
, NULL
, ZEND_ACC_PUBLIC
)
280 /* {{{ proto void HTTPi_Response::__construct(bool cache, bool gzip)
283 PHP_METHOD(HTTPi_Response
, __construct
)
285 zend_bool do_cache
= 0, do_gzip
= 0;
286 getObject(httpi_response_object
, obj
);
288 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
293 UPD_PROP(obj
, long, cache
, do_cache
);
294 UPD_PROP(obj
, long, gzip
, do_gzip
);
298 /* {{{ proto bool HTTPi_Response::setCache(bool cache)
301 PHP_METHOD(HTTPi_Response
, setCache
)
303 zend_bool do_cache
= 0;
304 getObject(httpi_response_object
, obj
);
306 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
310 UPD_PROP(obj
, long, cache
, do_cache
);
315 /* {{{ proto bool HTTPi_Response::getCache()
318 PHP_METHOD(HTTPi_Response
, getCache
)
320 zval
*do_cache
= NULL
;
321 getObject(httpi_response_object
, obj
);
323 if (ZEND_NUM_ARGS()) {
327 do_cache
= GET_PROP(obj
, cache
);
328 RETURN_BOOL(Z_LVAL_P(do_cache
));
332 /* {{{ proto bool HTTPi_Response::setGzip(bool gzip)
335 PHP_METHOD(HTTPi_Response
, setGzip
)
337 zend_bool do_gzip
= 0;
338 getObject(httpi_response_object
, obj
);
340 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
344 UPD_PROP(obj
, long, gzip
, do_gzip
);
349 /* {{{ proto bool HTTPi_Response::getGzip()
352 PHP_METHOD(HTTPi_Response
, getGzip
)
354 zval
*do_gzip
= NULL
;
355 getObject(httpi_response_object
, obj
);
357 if (ZEND_NUM_ARGS()) {
361 do_gzip
= GET_PROP(obj
, gzip
);
362 RETURN_BOOL(Z_LVAL_P(do_gzip
));
366 /* {{{ proto bool HTTPi_Response::setCacheControl(string control[, bool raw = false])
369 PHP_METHOD(HTTPi_Response
, setCacheControl
)
374 getObject(httpi_response_object
, obj
);
376 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
380 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
381 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
385 UPD_PROP(obj
, long, raw_cache_header
, raw
);
386 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
391 /* {{{ proto string HTTPi_Response::getCacheControl()
394 PHP_METHOD(HTTPi_Response
, getCacheControl
)
397 getObject(httpi_response_object
, obj
);
399 if (ZEND_NUM_ARGS()) {
403 ccontrol
= GET_PROP(obj
, cacheControl
);
404 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
408 /* {{{ proto bool HTTPi::setContentType(string content_type)
411 PHP_METHOD(HTTPi_Response
, setContentType
)
415 getObject(httpi_response_object
, obj
);
417 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
421 if (!strchr(ctype
, '/')) {
422 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
423 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
427 UPD_PROP(obj
, string
, contentType
, ctype
);
433 /* {{{ proto string HTTPi_Response::getContentType()
436 PHP_METHOD(HTTPi_Response
, getContentType
)
439 getObject(httpi_response_object
, obj
);
441 if (ZEND_NUM_ARGS()) {
445 ctype
= GET_PROP(obj
, contentType
);
446 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
450 /* {{{ proto bool HTTPi_Response::setContentDisposition(string filename[, bool inline = false])
453 PHP_METHOD(HTTPi_Response
, setContentDisposition
)
457 zend_bool is_inline
= 0;
458 getObject(httpi_response_object
, obj
);
460 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
464 UPD_PROP(obj
, string
, dispoFile
, file
);
465 UPD_PROP(obj
, long, dispoInline
, is_inline
);
470 /* {{{ proto array HTTPi_Response::getContentDisposition()
473 PHP_METHOD(HTTPi_Response
, getContentDisposition
)
477 getObject(httpi_response_object
, obj
);
479 if (ZEND_NUM_ARGS()) {
483 file
= GET_PROP(obj
, dispoFile
);
484 is_inline
= GET_PROP(obj
, dispoInline
);
486 array_init(return_value
);
487 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
488 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
492 /* {{{ proto bool HTTPi_Response::setETag(string etag)
495 PHP_METHOD(HTTPi_Response
, setETag
)
499 getObject(httpi_response_object
, obj
);
501 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
505 UPD_PROP(obj
, string
, eTag
, etag
);
510 /* {{{ proto string HTTPi_Response::getETag()
513 PHP_METHOD(HTTPi_Response
, getETag
)
516 getObject(httpi_response_object
, obj
);
518 if (ZEND_NUM_ARGS()) {
522 etag
= GET_PROP(obj
, eTag
);
523 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
527 /* {{{ proto bool HTTPi_Response::setData(string data)
530 PHP_METHOD(HTTPi_Response
, setData
)
534 getObject(httpi_response_object
, obj
);
536 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_data
)) {
540 convert_to_string_ex(&the_data
);
541 SET_PROP(obj
, data
, the_data
);
542 UPD_PROP(obj
, long, lastModified
, http_lmod(the_data
, SEND_DATA
));
543 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
548 /* {{{ proto string HTTPi_Response::getData()
551 PHP_METHOD(HTTPi_Response
, getData
)
554 getObject(httpi_response_object
, obj
);
556 if (ZEND_NUM_ARGS()) {
560 the_data
= GET_PROP(obj
, data
);
561 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
565 /* {{{ proto bool HTTPi_Response::setStream(resource stream)
568 PHP_METHOD(HTTPi_Response
, setStream
)
571 php_stream
*the_real_stream
;
573 getObject(httpi_response_object
, obj
);
575 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
579 php_stream_from_zval(the_real_stream
, &the_stream
);
581 SET_PROP(obj
, stream
, the_stream
);
582 UPD_PROP(obj
, long, lastModified
, http_lmod(the_real_stream
, SEND_RSRC
));
583 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
588 /* {{{ proto resource HTTPi_Response::getStream()
591 PHP_METHOD(HTTPi_Response
, getStream
)
594 getObject(httpi_response_object
, obj
);
596 if (ZEND_NUM_ARGS()) {
600 the_stream
= GET_PROP(obj
, stream
);
601 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
605 /* {{{ proto bool HTTPi_Response::setFile(string file)
608 PHP_METHOD(HTTPi_Response
, setFile
)
611 getObject(httpi_response_object
, obj
);
613 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
617 convert_to_string_ex(&the_file
);
619 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
620 UPD_PROP(obj
, long, lastModified
, http_lmod(the_file
, -1));
621 UPD_PROP(obj
, long, send_mode
, -1);
626 /* {{{ proto string HTTPi_Response::getFile()
629 PHP_METHOD(HTTPi_Response
, getFile
)
632 getObject(httpi_response_object
, obj
);
634 if (ZEND_NUM_ARGS()) {
638 the_file
= GET_PROP(obj
, file
);
639 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
643 PHP_METHOD(HTTPi_Response
, send
)
645 zval
*do_cache
, *do_gzip
;
646 getObject(httpi_response_object
, obj
);
648 do_cache
= GET_PROP(obj
, cache
);
649 do_gzip
= GET_PROP(obj
, gzip
);
652 if (Z_LVAL_P(do_cache
)) {
653 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
655 etag
= GET_PROP(obj
, eTag
);
656 lmod
= GET_PROP(obj
, lastModified
);
657 cctrl
= GET_PROP(obj
, cacheControl
);
658 ccraw
= GET_PROP(obj
, raw_cache_header
);
660 if (Z_LVAL_P(ccraw
)) {
661 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
662 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
));
664 char cc_header
[42] = {0};
665 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
666 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
667 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
672 if (Z_LVAL_P(do_gzip
)) {
678 zval
*ctype
= GET_PROP(obj
, contentType
);
679 if (Z_STRLEN_P(ctype
)) {
680 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
682 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
686 /* content disposition */
688 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
689 if (Z_STRLEN_P(dispo_file
)) {
690 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
691 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), Z_LVAL_P(dispo_inline
));
697 zval
*send_mode
= GET_PROP(obj
, send_mode
);
698 switch (Z_LVAL_P(send_mode
))
702 RETURN_SUCCESS(http_send_data(GET_PROP(obj
, data
)));
707 php_stream
*the_real_stream
;
708 zval
*the_stream
= GET_PROP(obj
, stream
);
709 php_stream_from_zval(the_real_stream
, &the_stream
);
710 RETURN_SUCCESS(http_send_stream(the_real_stream
));
715 RETURN_SUCCESS(http_send_file(GET_PROP(obj
, file
)));
721 #endif /* ZEND_ENGINE_2 */
723 /* {{{ http_module_entry */
724 zend_module_entry http_module_entry
= {
725 #if ZEND_MODULE_API_NO >= 20010901
726 STANDARD_MODULE_HEADER
,
735 #if ZEND_MODULE_API_NO >= 20010901
736 PHP_EXT_HTTP_VERSION
,
738 STANDARD_MODULE_PROPERTIES
742 /* {{{ proto string http_date([int timestamp])
744 * This function returns a valid HTTP date regarding RFC 822/1123
745 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
748 PHP_FUNCTION(http_date
)
752 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
757 t
= (long) time(NULL
);
760 RETURN_STRING(http_date(t
), 0);
764 /* {{{ proto string http_absolute_uri(string url[, string proto])
766 * This function returns an absolute URI constructed from url.
767 * If the url is already abolute but a different proto was supplied,
768 * only the proto part of the URI will be updated. If url has no
769 * path specified, the path of the current REQUEST_URI will be taken.
770 * The host will be taken either from the Host HTTP header of the client
771 * the SERVER_NAME or just localhost if prior are not available.
775 * url = "page.php" => http://www.example.com/current/path/page.php
776 * url = "/page.php" => http://www.example.com/page.php
777 * url = "/page.php", proto = "https" => https://www.example.com/page.php
781 PHP_FUNCTION(http_absolute_uri
)
783 char *url
= NULL
, *proto
= NULL
;
784 int url_len
= 0, proto_len
= 0;
786 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s", &url
, &url_len
, &proto
, &proto_len
) != SUCCESS
) {
790 RETURN_STRING(http_absolute_uri(url
, proto
), 0);
794 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
796 * This function negotiates the clients preferred language based on its
797 * Accept-Language HTTP header. It returns the negotiated language or
798 * the default language if none match.
800 * The qualifier is recognized and languages without qualifier are rated highest.
802 * The supported parameter is expected to be an array having
803 * the supported languages as array values.
817 * include './langs/'. http_negotiate_language($langs) .'.php';
822 PHP_FUNCTION(http_negotiate_language
)
828 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
836 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
840 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
842 * This function negotiates the clients preferred charset based on its
843 * Accept-Charset HTTP header. It returns the negotiated charset or
844 * the default charset if none match.
846 * The qualifier is recognized and charset without qualifier are rated highest.
848 * The supported parameter is expected to be an array having
849 * the supported charsets as array values.
855 * 'iso-8859-1', // default
860 * $pref = http_negotiate_charset($charsets);
861 * if (!strcmp($pref, 'iso-8859-1')) {
862 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
863 * iconv_set_encoding('output_encoding', $pref);
864 * ob_start('ob_iconv_handler');
869 PHP_FUNCTION(http_negotiate_charset
)
875 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
883 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
887 /* {{{ proto bool http_send_status(int status)
889 * Send HTTP status code.
892 PHP_FUNCTION(http_send_status
)
896 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
899 if (status
< 100 || status
> 510) {
900 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Invalid HTTP status code (100-510): %d", status
);
904 RETURN_SUCCESS(http_send_status(status
));
908 /* {{{ proto bool http_send_last_modified([int timestamp])
910 * This converts the given timestamp to a valid HTTP date and
911 * sends it as "Last-Modified" HTTP header. If timestamp is
912 * omitted, current time is sent.
915 PHP_FUNCTION(http_send_last_modified
)
919 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
924 t
= (long) time(NULL
);
927 RETURN_SUCCESS(http_send_last_modified(t
));
931 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
933 * Sets the content type.
936 PHP_FUNCTION(http_send_content_type
)
941 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
946 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1));
948 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
952 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
954 * Set the Content Disposition. The Content-Disposition header is very useful
955 * if the data actually sent came from a file or something similar, that should
956 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
959 PHP_FUNCTION(http_send_content_disposition
)
963 zend_bool send_inline
= 0;
965 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
968 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
972 /* {{{ proto bool http_match_modified([int timestamp])
974 * Matches the given timestamp against the clients "If-Modified-Since" resp.
975 * "If-Unmodified-Since" HTTP headers.
978 PHP_FUNCTION(http_match_modified
)
982 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
986 // current time if not supplied (senseless though)
988 t
= (long) time(NULL
);
991 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t
) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t
));
995 /* {{{ proto bool http_match_etag(string etag)
997 * This matches the given ETag against the clients
998 * "If-Match" resp. "If-None-Match" HTTP headers.
1001 PHP_FUNCTION(http_match_etag
)
1006 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
) != SUCCESS
) {
1010 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag
) || http_etag_match("HTTP_IF_MATCH", etag
));
1014 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
1016 * If timestamp_or_exires is greater than 0, it is handled as timestamp
1017 * and will be sent as date of last modification. If it is 0 or omitted,
1018 * the current time will be sent as Last-Modified date. If it's negative,
1019 * it is handled as expiration time in seconds, which means that if the
1020 * requested last modification date is not between the calculated timespan,
1021 * the Last-Modified header is updated and the actual body will be sent.
1024 PHP_FUNCTION(http_cache_last_modified
)
1026 long last_modified
= 0, send_modified
= 0, t
;
1029 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
1033 t
= (long) time(NULL
);
1036 if (!last_modified
) {
1037 /* does the client have? (att: caching "forever") */
1038 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
1039 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
1040 /* send current time */
1044 /* negative value is supposed to be expiration time */
1045 } else if (last_modified
< 0) {
1048 /* send supplied time explicitly */
1050 send_modified
= last_modified
;
1053 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, sizeof(HTTP_DEFAULT_CACHECONTROL
) - 1));
1057 /* {{{ proto bool http_cache_etag([string etag])
1059 * This function attempts to cache the HTTP body based on an ETag,
1060 * either supplied or generated through calculation of the MD5
1061 * checksum of the output (uses output buffering).
1063 * If clients "If-None-Match" header matches the supplied/calculated
1064 * ETag, the body is considered cached on the clients side and
1065 * a "304 Not Modified" status code is issued.
1068 PHP_FUNCTION(http_cache_etag
)
1073 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
1077 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, sizeof(HTTP_DEFAULT_CACHECONTROL
) - 1));
1081 /* {{{ proto string ob_httpetaghandler(string data, int mode)
1083 * For use with ob_start().
1084 * Note that this has to be started as first output buffer.
1085 * WARNING: Don't use with http_send_*().
1087 PHP_FUNCTION(ob_httpetaghandler
)
1093 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1097 if (mode
& PHP_OUTPUT_HANDLER_START
) {
1098 if (HTTP_G(etag_started
)) {
1099 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "ob_httpetaghandler can only be used once");
1100 RETURN_STRINGL(data
, data_len
, 1);
1102 http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL
);
1103 HTTP_G(etag_started
) = 1;
1106 if (OG(ob_nesting_level
) > 1) {
1107 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "ob_httpetaghandler must be started prior to other output buffers");
1108 RETURN_STRINGL(data
, data_len
, 1);
1111 Z_TYPE_P(return_value
) = IS_STRING
;
1112 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), &Z_STRLEN_P(return_value
), mode
);
1116 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
1118 * Redirect to a given url.
1119 * The supplied url will be expanded with http_absolute_uri(), the params array will
1120 * be treated with http_build_query() and the session identification will be appended
1121 * if session is true.
1123 * Depending on permanent the redirection will be issued with a permanent
1124 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
1127 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
1128 * if the client doesn't redirect immediatly.
1130 PHP_FUNCTION(http_redirect
)
1133 zend_bool session
= 0, permanent
= 0;
1134 zval
*params
= NULL
;
1135 smart_str qstr
= {0};
1136 char *url
, *URI
, LOC
[HTTP_URI_MAXLEN
+ 9], RED
[HTTP_URI_MAXLEN
* 2 + 34];
1138 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
1142 /* append session info */
1143 if (session
&& (PS(session_status
) == php_session_active
)) {
1145 MAKE_STD_ZVAL(params
);
1148 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
1149 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not append session information");
1153 /* treat params array with http_build_query() */
1155 if (php_url_encode_hash_ex(Z_ARRVAL_P(params
), &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
1159 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode query parameters");
1165 URI
= http_absolute_uri(url
, NULL
);
1167 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s?%s", URI
, qstr
.c
);
1168 sprintf(RED
, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, qstr
.c
, URI
, qstr
.c
);
1171 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s", URI
);
1172 sprintf(RED
, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
1176 if ((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302)))) {
1177 php_body_write(RED
, strlen(RED
) TSRMLS_CC
);
1184 /* {{{ proto bool http_send_data(string data)
1186 * Sends raw data with support for (multiple) range requests.
1189 PHP_FUNCTION(http_send_data
)
1193 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
1197 convert_to_string_ex(&zdata
);
1198 http_send_header("Accept-Ranges: bytes");
1199 RETURN_SUCCESS(http_send_data(zdata
));
1203 /* {{{ proto bool http_send_file(string file)
1205 * Sends a file with support for (multiple) range requests.
1208 PHP_FUNCTION(http_send_file
)
1212 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zfile
) != SUCCESS
) {
1216 convert_to_string_ex(&zfile
);
1217 http_send_header("Accept-Ranges: bytes");
1218 RETURN_SUCCESS(http_send_file(zfile
));
1222 /* {{{ proto bool http_send_stream(resource stream)
1224 * Sends an already opened stream with support for (multiple) range requests.
1227 PHP_FUNCTION(http_send_stream
)
1232 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
1236 php_stream_from_zval(file
, &zstream
);
1237 http_send_header("Accept-Ranges: bytes");
1238 RETURN_SUCCESS(http_send_stream(file
));
1242 /* {{{ proto string http_chunked_decode(string encoded)
1244 * This function decodes a string that was HTTP-chunked encoded.
1245 * Returns false on failure.
1247 PHP_FUNCTION(http_chunked_decode
)
1249 char *encoded
= NULL
, *decoded
= NULL
;
1250 int encoded_len
= 0, decoded_len
= 0;
1252 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
1256 if (SUCCESS
== http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
1257 RETURN_STRINGL(decoded
, decoded_len
, 0);
1264 /* {{{ proto array http_split_response(string http_response)
1266 * This function splits an HTTP response into an array with headers and the
1267 * content body. The returned array may look simliar to the following example:
1273 * 'Status' => '200 Ok',
1274 * 'Content-Type' => 'text/plain',
1275 * 'Content-Language' => 'en-US'
1277 * 1 => "Hello World!"
1282 PHP_FUNCTION(http_split_response
)
1284 zval
*zresponse
, *zbody
, *zheaders
;
1286 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zresponse
) != SUCCESS
) {
1290 convert_to_string_ex(&zresponse
);
1292 MAKE_STD_ZVAL(zbody
);
1293 MAKE_STD_ZVAL(zheaders
);
1294 array_init(zheaders
);
1296 if (SUCCESS
!= http_split_response(zresponse
, zheaders
, zbody
)) {
1297 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP response");
1301 array_init(return_value
);
1302 add_index_zval(return_value
, 0, zheaders
);
1303 add_index_zval(return_value
, 1, zbody
);
1307 /* {{{ proto array http_parse_headers(string header)
1310 PHP_FUNCTION(http_parse_headers
)
1312 char *header
, *rnrn
;
1315 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
1319 array_init(return_value
);
1321 if (rnrn
= strstr(header
, HTTP_CRLF HTTP_CRLF
)) {
1322 header_len
= rnrn
- header
+ 2;
1324 if (SUCCESS
!= http_parse_headers(header
, header_len
, return_value
)) {
1325 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP header");
1326 zval_dtor(return_value
);
1332 /* {{{ proto array http_get_request_headers(void)
1335 PHP_FUNCTION(http_get_request_headers
)
1337 if (ZEND_NUM_ARGS()) {
1341 array_init(return_value
);
1342 http_get_request_headers(return_value
);
1347 #ifdef HTTP_HAVE_CURL
1349 /* {{{ proto string http_get(string url[, array options[, array &info]])
1351 * Performs an HTTP GET request on the supplied url.
1353 * The second parameter is expected to be an associative
1354 * array where the following keys will be recognized:
1356 * - redirect: int, whether and how many redirects to follow
1357 * - unrestrictedauth: bool, whether to continue sending credentials on
1358 * redirects to a different host
1359 * - proxyhost: string, proxy host in "host[:port]" format
1360 * - proxyport: int, use another proxy port as specified in proxyhost
1361 * - proxyauth: string, proxy credentials in "user:pass" format
1362 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1363 * - httpauth: string, http credentials in "user:pass" format
1364 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1365 * - compress: bool, whether to allow gzip/deflate content encoding
1366 * (defaults to true)
1367 * - port: int, use another port as specified in the url
1368 * - referer: string, the referer to sends
1369 * - useragent: string, the user agent to send
1370 * (defaults to PECL::HTTP/version (PHP/version)))
1371 * - headers: array, list of custom headers as associative array
1372 * like array("header" => "value")
1373 * - cookies: array, list of cookies as associative array
1374 * like array("cookie" => "value")
1375 * - cookiestore: string, path to a file where cookies are/will be stored
1378 * The optional third parameter will be filled with some additional information
1379 * in form af an associative array, if supplied, like the following example:
1383 * 'effective_url' => 'http://localhost',
1384 * 'response_code' => 403,
1385 * 'total_time' => 0.017,
1386 * 'namelookup_time' => 0.013,
1387 * 'connect_time' => 0.014,
1388 * 'pretransfer_time' => 0.014,
1389 * 'size_upload' => 0,
1390 * 'size_download' => 202,
1391 * 'speed_download' => 11882,
1392 * 'speed_upload' => 0,
1393 * 'header_size' => 145,
1394 * 'request_size' => 62,
1395 * 'ssl_verifyresult' => 0,
1397 * 'content_length_download' => 202,
1398 * 'content_length_upload' => 0,
1399 * 'starttransfer_time' => 0.017,
1400 * 'content_type' => 'text/html; charset=iso-8859-1',
1401 * 'redirect_time' => 0,
1402 * 'redirect_count' => 0,
1404 * 'http_connectcode' => 0,
1405 * 'httpauth_avail' => 0,
1406 * 'proxyauth_avail' => 0,
1411 PHP_FUNCTION(http_get
)
1413 char *URL
, *data
= NULL
;
1414 size_t data_len
= 0;
1416 zval
*options
= NULL
, *info
= NULL
;
1418 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1427 if (SUCCESS
== http_get(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
1428 RETURN_STRINGL(data
, data_len
, 0);
1435 /* {{{ proto string http_head(string url[, array options[, array &info]])
1437 * Performs an HTTP HEAD request on the suppied url.
1438 * Returns the HTTP response as string.
1439 * See http_get() for a full list of available options.
1441 PHP_FUNCTION(http_head
)
1443 char *URL
, *data
= NULL
;
1444 size_t data_len
= 0;
1446 zval
*options
= NULL
, *info
= NULL
;
1448 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1457 if (SUCCESS
== http_head(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
1458 RETURN_STRINGL(data
, data_len
, 0);
1465 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
1467 * Performs an HTTP POST request, posting data.
1468 * Returns the HTTP response as string.
1469 * See http_get() for a full list of available options.
1471 PHP_FUNCTION(http_post_data
)
1473 char *URL
, *postdata
, *data
= NULL
;
1474 size_t data_len
= 0;
1475 int postdata_len
, URL_len
;
1476 zval
*options
= NULL
, *info
= NULL
;
1478 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
1487 if (SUCCESS
== http_post_data(URL
, postdata
, (size_t) postdata_len
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
1488 RETURN_STRINGL(data
, data_len
, 0);
1495 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
1497 * Performs an HTTP POST request, posting www-form-urlencoded array data.
1498 * Returns the HTTP response as string.
1499 * See http_get() for a full list of available options.
1501 PHP_FUNCTION(http_post_array
)
1503 char *URL
, *data
= NULL
;
1504 size_t data_len
= 0;
1506 zval
*options
= NULL
, *info
= NULL
, *postdata
;
1508 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|a/!z", &URL
, &URL_len
, &postdata
, &options
, &info
) != SUCCESS
) {
1517 if (SUCCESS
== http_post_array(URL
, Z_ARRVAL_P(postdata
), HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
1518 RETURN_STRINGL(data
, data_len
, 0);
1529 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1534 * if (!http_auth_basic('mike', 's3c|r3t')) {
1535 * die('<h1>Authorization failed!</h1>');
1540 PHP_FUNCTION(http_auth_basic
)
1542 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
1543 int r_len
, u_len
, p_len
;
1545 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
1550 realm
= "Restricted";
1553 if (SUCCESS
!= http_auth_credentials(&suser
, &spass
)) {
1554 http_auth_header("Basic", realm
);
1558 if (strcasecmp(suser
, user
)) {
1559 http_auth_header("Basic", realm
);
1563 if (strcmp(spass
, pass
)) {
1564 http_auth_header("Basic", realm
);
1572 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
1577 * function auth_cb($user, $pass)
1580 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
1581 * if (strlen($realpass = $db->getOne($query)) {
1582 * return $pass === $realpass;
1587 * if (!http_auth_basic_cb('auth_cb')) {
1588 * die('<h1>Authorization failed</h1>');
1593 PHP_FUNCTION(http_auth_basic_cb
)
1596 char *realm
= NULL
, *user
, *pass
;
1599 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
1604 realm
= "Restricted";
1607 if (SUCCESS
!= http_auth_credentials(&user
, &pass
)) {
1608 http_auth_header("Basic", realm
);
1612 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
1615 MAKE_STD_ZVAL(zparams
[0]);
1616 MAKE_STD_ZVAL(zparams
[1]);
1617 ZVAL_STRING(zparams
[0], user
, 0);
1618 ZVAL_STRING(zparams
[1], pass
, 0);
1620 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
1621 &retval
, 2, zparams TSRMLS_CC
)) {
1622 result
= Z_LVAL(retval
);
1631 http_auth_header("Basic", realm
);
1634 RETURN_BOOL(result
);
1640 /* {{{ php_http_init_globals(zend_http_globals *) */
1641 static void php_http_init_globals(zend_http_globals
*http_globals
)
1643 http_globals
->etag_started
= 0;
1644 http_globals
->ctype
= NULL
;
1645 http_globals
->etag
= NULL
;
1646 http_globals
->lmod
= 0;
1647 #ifdef HTTP_HAVE_CURL
1648 http_globals
->curlbuf
.body
.data
= NULL
;
1649 http_globals
->curlbuf
.body
.used
= 0;
1650 http_globals
->curlbuf
.body
.free
= 0;
1651 http_globals
->curlbuf
.hdrs
.data
= NULL
;
1652 http_globals
->curlbuf
.hdrs
.used
= 0;
1653 http_globals
->curlbuf
.hdrs
.free
= 0;
1655 http_globals
->allowed_methods
= NULL
;
1659 /* {{{ static inline STATUS http_check_allowed_methods(char *, int) */
1660 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
1661 static inline void _http_check_allowed_methods(char *methods
, int length TSRMLS_DC
)
1663 if (length
&& SG(request_info
).request_method
&& (!strstr(methods
, SG(request_info
).request_method
))) {
1664 char *allow_header
= emalloc(length
+ sizeof("Allow: "));
1665 sprintf(allow_header
, "Allow: %s", methods
);
1666 http_send_header(allow_header
);
1667 efree(allow_header
);
1668 http_send_status(405);
1675 PHP_INI_MH(update_allowed_methods
)
1677 http_check_allowed_methods(new_value
, new_value_length
);
1678 return OnUpdateString(entry
, new_value
, new_value_length
, mh_arg1
, mh_arg2
, mh_arg3
, stage TSRMLS_CC
);
1682 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
)
1686 /* {{{ PHP_MINIT_FUNCTION */
1687 PHP_MINIT_FUNCTION(http
)
1689 ZEND_INIT_MODULE_GLOBALS(http
, php_http_init_globals
, NULL
);
1690 REGISTER_INI_ENTRIES();
1692 #ifdef HTTP_HAVE_CURL
1693 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
| CONST_PERSISTENT
);
1694 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
| CONST_PERSISTENT
);
1695 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
| CONST_PERSISTENT
);
1698 #ifdef ZEND_ENGINE_2
1700 REGISTER_LONG_CONSTANT("HTTP_GET", HTTP_GET, CONST_CS | CONST_PERSISTENT);
1701 REGISTER_LONG_CONSTANT("HTTP_HEAD", HTTP_HEAD, CONST_CS | CONST_PERSISTENT);
1702 REGISTER_LONG_CONSTANT("HTTP_POST", HTTP_POST, CONST_CS | CONST_PERSISTENT);
1704 HTTP_REGISTER_CLASS(HTTPi
, httpi
, NULL
, ZEND_ACC_FINAL_CLASS
);
1705 HTTP_REGISTER_CLASS_EX(HTTPi_Response
, httpi_response
, NULL
, 0);
1711 /* {{{ PHP_MSHUTDOWN_FUNCTION */
1712 PHP_MSHUTDOWN_FUNCTION(http
)
1714 UNREGISTER_INI_ENTRIES();
1719 /* {{{ PHP_RINIT_FUNCTION */
1720 PHP_RINIT_FUNCTION(http
)
1722 char *allowed_methods
= INI_STR("http.allowed_methods");
1723 http_check_allowed_methods(allowed_methods
, strlen(allowed_methods
));
1728 /* {{{ PHP_RSHUTDOWN_FUNCTION */
1729 PHP_RSHUTDOWN_FUNCTION(http
)
1731 HTTP_G(etag_started
) = 0;
1735 efree(HTTP_G(etag
));
1736 HTTP_G(etag
) = NULL
;
1739 if (HTTP_G(ctype
)) {
1740 efree(HTTP_G(ctype
));
1741 HTTP_G(ctype
) = NULL
;
1743 #ifdef HTTP_HAVE_CURL
1744 if (HTTP_G(curlbuf
).body
.data
) {
1745 efree(HTTP_G(curlbuf
).body
.data
);
1746 HTTP_G(curlbuf
).body
.data
= NULL
;
1748 if (HTTP_G(curlbuf
).hdrs
.data
) {
1749 efree(HTTP_G(curlbuf
).hdrs
.data
);
1750 HTTP_G(curlbuf
).hdrs
.data
= NULL
;
1757 /* {{{ PHP_MINFO_FUNCTION */
1758 PHP_MINFO_FUNCTION(http
)
1760 php_info_print_table_start();
1761 php_info_print_table_header(2, "Extended HTTP support", "enabled");
1762 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION
);
1763 php_info_print_table_row(2, "cURL convenience functions:",
1764 #ifdef HTTP_HAVE_CURL
1770 php_info_print_table_end();
1772 DISPLAY_INI_ENTRIES();
1781 * vim600: noet sw=4 ts=4 fdm=marker
1782 * vim<600: noet sw=4 ts=4