2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #define HTTP_WANT_CURL
20 #define HTTP_WANT_ZLIB
21 #define HTTP_WANT_MAGIC
26 #include "ext/standard/info.h"
27 #include "zend_extensions.h"
29 #include "php_http_api.h"
30 #include "php_http_send_api.h"
31 #include "php_http_cache_api.h"
32 #include "php_http_headers_api.h"
33 #include "php_http_message_api.h"
34 #include "php_http_request_method_api.h"
36 # include "php_http_request_api.h"
37 # ifdef HTTP_HAVE_WRAPPER
38 # include "php_http_wrapper_api.h"
43 # include "php_http_filter_api.h"
44 # include "php_http_util_object.h"
45 # include "php_http_message_object.h"
47 # include "php_http_response_object.h"
49 # ifdef HTTP_HAVE_CURL
50 # include "php_http_request_object.h"
51 # include "php_http_requestpool_object.h"
53 # include "php_http_exception_object.h"
57 ZEND_DECLARE_MODULE_GLOBALS(http
);
58 HTTP_DECLARE_ARG_PASS_INFO();
60 #ifdef COMPILE_DL_HTTP
64 /* {{{ http_functions[] */
65 zend_function_entry http_functions
[] = {
66 PHP_FE(http_test
, NULL
)
67 PHP_FE(http_date
, NULL
)
68 PHP_FE(http_build_uri
, NULL
)
69 PHP_FALIAS(http_absolute_uri
, http_build_uri
, NULL
)
70 PHP_FE(http_negotiate_language
, http_arg_pass_ref_2
)
71 PHP_FE(http_negotiate_charset
, http_arg_pass_ref_2
)
72 PHP_FE(http_negotiate_content_type
, http_arg_pass_ref_2
)
73 PHP_FE(http_redirect
, NULL
)
74 PHP_FE(http_throttle
, NULL
)
75 PHP_FE(http_send_status
, NULL
)
76 PHP_FE(http_send_last_modified
, NULL
)
77 PHP_FE(http_send_content_type
, NULL
)
78 PHP_FE(http_send_content_disposition
, NULL
)
79 PHP_FE(http_match_modified
, NULL
)
80 PHP_FE(http_match_etag
, NULL
)
81 PHP_FE(http_cache_last_modified
, NULL
)
82 PHP_FE(http_cache_etag
, NULL
)
83 PHP_FE(http_send_data
, NULL
)
84 PHP_FE(http_send_file
, NULL
)
85 PHP_FE(http_send_stream
, NULL
)
86 PHP_FE(http_chunked_decode
, NULL
)
87 PHP_FE(http_parse_message
, NULL
)
88 PHP_FE(http_parse_headers
, NULL
)
89 PHP_FE(http_parse_cookie
, NULL
)
90 PHP_FE(http_get_request_headers
, NULL
)
91 PHP_FE(http_get_request_body
, NULL
)
92 PHP_FE(http_match_request_header
, NULL
)
94 PHP_FE(http_get
, http_arg_pass_ref_3
)
95 PHP_FE(http_head
, http_arg_pass_ref_3
)
96 PHP_FE(http_post_data
, http_arg_pass_ref_4
)
97 PHP_FE(http_post_fields
, http_arg_pass_ref_5
)
98 PHP_FE(http_put_file
, http_arg_pass_ref_4
)
99 PHP_FE(http_put_stream
, http_arg_pass_ref_4
)
101 PHP_FE(http_request_method_register
, NULL
)
102 PHP_FE(http_request_method_unregister
, NULL
)
103 PHP_FE(http_request_method_exists
, NULL
)
104 PHP_FE(http_request_method_name
, NULL
)
105 #ifndef ZEND_ENGINE_2
106 PHP_FE(http_build_query
, NULL
)
108 PHP_FE(ob_etaghandler
, NULL
)
109 #ifdef HTTP_HAVE_ZLIB
110 PHP_FE(http_gzencode
, NULL
)
111 PHP_FE(http_gzdecode
, NULL
)
112 PHP_FE(http_deflate
, NULL
)
113 PHP_FE(http_inflate
, NULL
)
115 PHP_FE(http_support
, NULL
)
121 /* {{{ http_module_dep */
122 #if ZEND_EXTENSION_API_NO >= 220050617
123 static zend_module_dep http_module_dep
[] = {
125 ZEND_MOD_REQUIRED("spl")
127 # ifdef HTTP_HAVE_EXT_HASH
128 ZEND_MOD_REQUIRED("hash")
130 {NULL
, NULL
, NULL
, 0}
135 /* {{{ http_module_entry */
136 zend_module_entry http_module_entry
= {
137 #if ZEND_EXTENSION_API_NO >= 220050617
138 STANDARD_MODULE_HEADER_EX
, NULL
,
141 STANDARD_MODULE_HEADER
,
150 PHP_EXT_HTTP_VERSION
,
151 STANDARD_MODULE_PROPERTIES
155 int http_module_number
;
157 /* {{{ http_globals */
158 static void http_globals_init_once(zend_http_globals
*G
)
160 memset(G
, 0, sizeof(zend_http_globals
));
163 static inline void http_globals_init(zend_http_globals
*G
)
165 G
->send
.buffer_size
= HTTP_SENDBUF_SIZE
;
166 zend_hash_init(&G
->request
.methods
.custom
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
169 static inline void http_globals_free(zend_http_globals
*G
)
171 STR_SET(G
->send
.content_type
, NULL
);
172 STR_SET(G
->send
.unquoted_etag
, NULL
);
173 zend_hash_destroy(&G
->request
.methods
.custom
);
177 /* {{{ static inline void http_check_allowed_methods(char *, int) */
178 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
179 static inline void _http_check_allowed_methods(char *methods
, int length TSRMLS_DC
)
181 if (length
&& SG(request_info
).request_method
) {
182 if (SUCCESS
!= http_check_method_ex(SG(request_info
).request_method
, methods
)) {
183 char *header
= emalloc(length
+ sizeof("Allow: "));
184 sprintf(header
, "Allow: %s", methods
);
185 http_exit(405, header
);
192 PHP_INI_MH(http_update_allowed_methods
)
194 http_check_allowed_methods(new_value
, new_value_length
);
195 return OnUpdateString(entry
, new_value
, new_value_length
, mh_arg1
, mh_arg2
, mh_arg3
, stage TSRMLS_CC
);
198 #ifndef ZEND_ENGINE_2
199 # define OnUpdateLong OnUpdateInt
203 HTTP_PHP_INI_ENTRY("http.allowed_methods", "", PHP_INI_ALL
, http_update_allowed_methods
, request
.methods
.allowed
)
204 HTTP_PHP_INI_ENTRY("http.cache_log", "", PHP_INI_ALL
, OnUpdateString
, log
.cache
)
205 HTTP_PHP_INI_ENTRY("http.redirect_log", "", PHP_INI_ALL
, OnUpdateString
, log
.redirect
)
206 HTTP_PHP_INI_ENTRY("http.allowed_methods_log", "", PHP_INI_ALL
, OnUpdateString
, log
.allowed_methods
)
207 HTTP_PHP_INI_ENTRY("http.composite_log", "", PHP_INI_ALL
, OnUpdateString
, log
.composite
)
208 HTTP_PHP_INI_ENTRY("http.etag_mode", "MD5", PHP_INI_ALL
, OnUpdateString
, etag
.mode
)
210 HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL
, OnUpdateBool
, only_exceptions
)
212 HTTP_PHP_INI_ENTRY("http.force_exit", "1", PHP_INI_ALL
, OnUpdateBool
, force_exit
)
216 /* {{{ PHP_MINIT_FUNCTION */
217 PHP_MINIT_FUNCTION(http
)
219 http_module_number
= module_number
;
221 ZEND_INIT_MODULE_GLOBALS(http
, http_globals_init_once
, NULL
)
223 REGISTER_INI_ENTRIES();
225 if ( (SUCCESS
!= PHP_MINIT_CALL(http_support
)) ||
226 (SUCCESS
!= PHP_MINIT_CALL(http_headers
)) ||
227 #ifdef HTTP_HAVE_CURL
228 (SUCCESS
!= PHP_MINIT_CALL(http_request
)) ||
229 # ifdef HTTP_HAVE_WRAPPER
230 (SUCCESS
!= PHP_MINIT_CALL(http_wrapper
)) ||
232 #endif /* HTTP_HAVE_CURL */
233 (SUCCESS
!= PHP_MINIT_CALL(http_request_method
))) {
238 if ( (SUCCESS
!= PHP_MINIT_CALL(http_filter
)) ||
239 (SUCCESS
!= PHP_MINIT_CALL(http_util_object
)) ||
240 (SUCCESS
!= PHP_MINIT_CALL(http_message_object
)) ||
242 (SUCCESS
!= PHP_MINIT_CALL(http_response_object
)) ||
244 # ifdef HTTP_HAVE_CURL
245 (SUCCESS
!= PHP_MINIT_CALL(http_request_object
)) ||
246 (SUCCESS
!= PHP_MINIT_CALL(http_requestpool_object
))||
247 # endif /* HTTP_HAVE_CURL */
248 (SUCCESS
!= PHP_MINIT_CALL(http_exception_object
))) {
251 #endif /* ZEND_ENGINE_2 */
257 /* {{{ PHP_MSHUTDOWN_FUNCTION */
258 PHP_MSHUTDOWN_FUNCTION(http
)
260 UNREGISTER_INI_ENTRIES();
261 #ifdef HTTP_HAVE_CURL
262 return PHP_MSHUTDOWN_CALL(http_request
);
268 /* {{{ PHP_RINIT_FUNCTION */
269 PHP_RINIT_FUNCTION(http
)
271 if (HTTP_G(request
).methods
.allowed
) {
272 http_check_allowed_methods(HTTP_G(request
).methods
.allowed
,
273 strlen(HTTP_G(request
).methods
.allowed
));
276 http_globals_init(HTTP_GLOBALS
);
281 /* {{{ PHP_RSHUTDOWN_FUNCTION */
282 PHP_RSHUTDOWN_FUNCTION(http
)
284 STATUS status
= SUCCESS
;
286 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
287 status
= PHP_RSHUTDOWN_CALL(http_request_method
);
290 http_globals_free(HTTP_GLOBALS
);
295 /* {{{ PHP_MINFO_FUNCTION */
296 PHP_MINFO_FUNCTION(http
)
298 php_info_print_table_start();
300 php_info_print_table_row(2, "Extended HTTP support", "enabled");
301 php_info_print_table_row(2, "Extension Version", PHP_EXT_HTTP_VERSION
);
302 #ifdef HTTP_HAVE_CURL
303 php_info_print_table_row(2, "cURL HTTP Requests", curl_version());
305 php_info_print_table_row(2, "cURL HTTP Requests", "disabled");
307 #ifdef HTTP_HAVE_ZLIB
309 char my_zlib_version
[64] = {0};
311 strlcat(my_zlib_version
, "zlib/", 63);
312 strlcat(my_zlib_version
, zlibVersion(), 63);
313 php_info_print_table_row(2, "zlib GZIP Encodings", my_zlib_version
);
316 php_info_print_table_row(2, "zlib GZIP Encodings", "disabled");
318 #if defined(HTTP_HAVE_MAGIC) && !defined(WONKY)
319 php_info_print_table_row(2, "magic MIME Guessing", "libmagic/unknown");
321 php_info_print_table_row(2, "magic MIME Guessing", "disabled");
323 php_info_print_table_row(2, "Registered Classes",
324 #ifndef ZEND_ENGINE_2
329 # ifdef HTTP_HAVE_CURL
339 php_info_print_table_end();
341 php_info_print_table_start();
342 php_info_print_table_colspan_header(2, "Request Methods");
346 zval
**custom_method
;
347 phpstr
*known_request_methods
= phpstr_new();
348 phpstr
*custom_request_methods
= phpstr_new();
350 for (i
= HTTP_NO_REQUEST_METHOD
+1; i
< HTTP_MAX_REQUEST_METHOD
; ++i
) {
351 phpstr_appendl(known_request_methods
, http_request_method_name(i
));
352 phpstr_appends(known_request_methods
, ", ");
354 FOREACH_HASH_VAL(pos
, &HTTP_G(request
).methods
.custom
, custom_method
) {
355 phpstr_append(custom_request_methods
, Z_STRVAL_PP(custom_method
), Z_STRLEN_PP(custom_method
));
356 phpstr_appends(custom_request_methods
, ", ");
359 phpstr_append(known_request_methods
, PHPSTR_VAL(custom_request_methods
), PHPSTR_LEN(custom_request_methods
));
360 phpstr_fix(known_request_methods
);
361 phpstr_fix(custom_request_methods
);
363 php_info_print_table_row(2, "Known", PHPSTR_VAL(known_request_methods
));
364 php_info_print_table_row(2, "Custom",
365 PHPSTR_LEN(custom_request_methods
) ? PHPSTR_VAL(custom_request_methods
) : "none registered");
366 php_info_print_table_row(2, "Allowed", strlen(HTTP_G(request
).methods
.allowed
) ? HTTP_G(request
).methods
.allowed
: "(ANY)");
368 phpstr_free(&known_request_methods
);
369 phpstr_free(&custom_request_methods
);
371 php_info_print_table_end();
373 DISPLAY_INI_ENTRIES();
382 * vim600: noet sw=4 ts=4 fdm=marker
383 * vim<600: noet sw=4 ts=4