- ZTS needs ZEND_INIT_MODULE_GLOBALS
[m6w6/ext-http] / http.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #ifdef HTTP_HAVE_CURL
24 # ifdef PHP_WIN32
25 # include <winsock2.h>
26 # endif
27 # include <curl/curl.h>
28 #endif
29
30 #include <ctype.h>
31
32 #include "php.h"
33 #include "php_ini.h"
34 #include "ext/standard/info.h"
35
36 #include "SAPI.h"
37
38 #include "php_http.h"
39 #include "php_http_std_defs.h"
40 #include "php_http_api.h"
41 #include "php_http_send_api.h"
42
43 #ifdef ZEND_ENGINE_2
44 # include "php_http_util_object.h"
45 # include "php_http_message_object.h"
46 # include "php_http_response_object.h"
47 # ifdef HTTP_HAVE_CURL
48 # include "php_http_request_object.h"
49 # include "php_http_requestpool_object.h"
50 # endif
51 # include "php_http_exception_object.h"
52 #endif
53
54 #include "phpstr/phpstr.h"
55
56 ZEND_DECLARE_MODULE_GLOBALS(http);
57 HTTP_DECLARE_ARG_PASS_INFO();
58
59 #ifdef COMPILE_DL_HTTP
60 ZEND_GET_MODULE(http)
61 #endif
62
63 /* {{{ http_functions[] */
64 function_entry http_functions[] = {
65 PHP_FE(http_test, NULL)
66 PHP_FE(http_date, NULL)
67 PHP_FE(http_absolute_uri, NULL)
68 PHP_FE(http_negotiate_language, NULL)
69 PHP_FE(http_negotiate_charset, NULL)
70 PHP_FE(http_redirect, NULL)
71 PHP_FE(http_throttle, NULL)
72 PHP_FE(http_send_status, NULL)
73 PHP_FE(http_send_last_modified, NULL)
74 PHP_FE(http_send_content_type, NULL)
75 PHP_FE(http_send_content_disposition, NULL)
76 PHP_FE(http_match_modified, NULL)
77 PHP_FE(http_match_etag, NULL)
78 PHP_FE(http_cache_last_modified, NULL)
79 PHP_FE(http_cache_etag, NULL)
80 PHP_FE(http_send_data, NULL)
81 PHP_FE(http_send_file, NULL)
82 PHP_FE(http_send_stream, NULL)
83 PHP_FE(http_chunked_decode, NULL)
84 PHP_FE(http_split_response, NULL)
85 PHP_FE(http_parse_headers, NULL)
86 PHP_FE(http_get_request_headers, NULL)
87 PHP_FE(http_match_request_header, NULL)
88 #ifdef HTTP_HAVE_CURL
89 PHP_FE(http_get, http_arg_pass_ref_3)
90 PHP_FE(http_head, http_arg_pass_ref_3)
91 PHP_FE(http_post_data, http_arg_pass_ref_4)
92 PHP_FE(http_post_fields, http_arg_pass_ref_5)
93 PHP_FE(http_put_file, http_arg_pass_ref_4)
94 PHP_FE(http_put_stream, http_arg_pass_ref_4)
95 PHP_FE(http_request_method_register, NULL)
96 PHP_FE(http_request_method_unregister, NULL)
97 PHP_FE(http_request_method_exists, NULL)
98 PHP_FE(http_request_method_name, NULL)
99 #endif
100 PHP_FE(http_auth_basic, NULL)
101 PHP_FE(http_auth_basic_cb, NULL)
102 #ifndef ZEND_ENGINE_2
103 PHP_FE(http_build_query, NULL)
104 #endif
105 PHP_FE(ob_etaghandler, NULL)
106 {NULL, NULL, NULL}
107 };
108 /* }}} */
109
110 /* {{{ http_module_entry */
111 zend_module_entry http_module_entry = {
112 #if ZEND_MODULE_API_NO >= 20010901
113 STANDARD_MODULE_HEADER,
114 #endif
115 "http",
116 http_functions,
117 PHP_MINIT(http),
118 PHP_MSHUTDOWN(http),
119 PHP_RINIT(http),
120 PHP_RSHUTDOWN(http),
121 PHP_MINFO(http),
122 #if ZEND_MODULE_API_NO >= 20010901
123 HTTP_PEXT_VERSION,
124 #endif
125 STANDARD_MODULE_PROPERTIES
126 };
127 /* }}} */
128
129 int http_module_number;
130
131 /* {{{ http_globals */
132 static inline void http_globals_init(zend_http_globals *G)
133 {
134 memset(G, 0, sizeof(zend_http_globals));
135 G->send.buffer_size = HTTP_SENDBUF_SIZE;
136 zend_hash_init(&G->request.methods.custom, 0, NULL, ZVAL_PTR_DTOR, 0);
137 #ifdef HTTP_HAVE_CURL
138 zend_llist_init(&G->request.copies.strings, sizeof(char *), http_request_data_free_string, 0);
139 zend_llist_init(&G->request.copies.slists, sizeof(struct curl_slist *), http_request_data_free_slist, 0);
140 zend_llist_init(&G->request.copies.contexts, sizeof(http_curl_callback_ctx *), http_request_data_free_context, 0);
141 #endif
142 }
143
144 static inline void http_globals_free(zend_http_globals *G)
145 {
146 STR_FREE(G->send.content_type);
147 STR_FREE(G->send.unquoted_etag);
148 zend_hash_destroy(&G->request.methods.custom);
149 zend_llist_clean(&G->request.copies.strings);
150 zend_llist_clean(&G->request.copies.slists);
151 zend_llist_clean(&G->request.copies.contexts);
152 }
153 /* }}} */
154
155 /* {{{ static inline void http_check_allowed_methods(char *, int) */
156 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
157 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
158 {
159 if (length && SG(request_info).request_method) {
160 if (SUCCESS != http_check_method_ex(SG(request_info).request_method, methods)) {
161 char *header = emalloc(length + sizeof("Allow: "));
162 sprintf(header, "Allow: %s", methods);
163 http_exit(405, header);
164 }
165 }
166 }
167 /* }}} */
168
169 /* {{{ PHP_INI */
170 PHP_INI_MH(http_update_allowed_methods)
171 {
172 http_check_allowed_methods(new_value, new_value_length);
173 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
174 }
175
176 PHP_INI_BEGIN()
177 HTTP_PHP_INI_ENTRY("http.allowed_methods", NULL, PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
178 HTTP_PHP_INI_ENTRY("http.cache_log", NULL, PHP_INI_ALL, OnUpdateString, log.cache)
179 PHP_INI_END()
180 /* }}} */
181
182
183 /* {{{ PHP_MINIT_FUNCTION */
184 PHP_MINIT_FUNCTION(http)
185 {
186 http_module_number = module_number;
187
188 #ifdef ZTS
189 ZEND_INIT_MODULE_GLOBALS(http, NULL, NULL)
190 #endif
191
192 REGISTER_INI_ENTRIES();
193
194 #ifdef HTTP_HAVE_CURL
195 if (SUCCESS != http_request_global_init()) {
196 return FAILURE;
197 }
198 #endif /* HTTP_HAVE_CURL */
199
200 #ifdef ZEND_ENGINE_2
201 http_util_object_init();
202 http_message_object_init();
203 http_response_object_init();
204 # ifdef HTTP_HAVE_CURL
205 http_request_object_init();
206 http_requestpool_object_init();
207 # endif /* HTTP_HAVE_CURL */
208 http_exception_object_init();
209 #endif /* ZEND_ENGINE_2 */
210
211 return SUCCESS;
212 }
213 /* }}} */
214
215 /* {{{ PHP_MSHUTDOWN_FUNCTION */
216 PHP_MSHUTDOWN_FUNCTION(http)
217 {
218 UNREGISTER_INI_ENTRIES();
219 #ifdef HTTP_HAVE_CURL
220 curl_global_cleanup();
221 #endif
222 return SUCCESS;
223 }
224 /* }}} */
225
226 /* {{{ PHP_RINIT_FUNCTION */
227 PHP_RINIT_FUNCTION(http)
228 {
229 char *m;
230
231 if (m = INI_STR("http.allowed_methods")) {
232 http_check_allowed_methods(m, strlen(m));
233 }
234
235 http_globals_init(HTTP_GLOBALS);
236 return SUCCESS;
237 }
238 /* }}} */
239
240 /* {{{ PHP_RSHUTDOWN_FUNCTION */
241 PHP_RSHUTDOWN_FUNCTION(http)
242 {
243 http_globals_free(HTTP_GLOBALS);
244 return SUCCESS;
245 }
246 /* }}} */
247
248 /* {{{ PHP_MINFO_FUNCTION */
249 PHP_MINFO_FUNCTION(http)
250 {
251 #ifdef ZEND_ENGINE_2
252 # define HTTP_FUNC_AVAIL(CLASS) "procedural, object oriented (" CLASS ")"
253 #else
254 # define HTTP_FUNC_AVAIL(CLASS) "procedural"
255 #endif
256
257 #ifdef HTTP_HAVE_CURL
258 # define HTTP_CURL_VERSION curl_version()
259 # ifdef ZEND_ENGINE_2
260 # define HTTP_CURL_AVAIL(CLASS) "procedural, object oriented (" CLASS ")"
261 # else
262 # define HTTP_CURL_AVAIL(CLASS) "procedural"
263 # endif
264 #else
265 # define HTTP_CURL_VERSION "libcurl not available"
266 # define HTTP_CURL_AVAIL(CLASS) "libcurl not available"
267 #endif
268
269 #include "php_http_request_api.h"
270
271 php_info_print_table_start();
272 {
273 char full_version_string[1024] = {0};
274 snprintf(full_version_string, 1023, "%s (%s)", HTTP_PEXT_VERSION, HTTP_CURL_VERSION);
275
276 php_info_print_table_row(2, "Extended HTTP support:", "enabled");
277 php_info_print_table_row(2, "Extension Version:", full_version_string);
278 }
279 php_info_print_table_end();
280
281 php_info_print_table_start();
282 {
283 unsigned i;
284 zval **custom_method;
285 phpstr *known_request_methods = phpstr_new();
286 phpstr *custom_request_methods = phpstr_new();
287
288 for (i = HTTP_NO_REQUEST_METHOD+1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
289 phpstr_appendl(known_request_methods, http_request_method_name(i));
290 phpstr_appends(known_request_methods, ", ");
291 }
292 FOREACH_HASH_VAL(&HTTP_G(request).methods.custom, custom_method) {
293 phpstr_append(custom_request_methods, Z_STRVAL_PP(custom_method), Z_STRLEN_PP(custom_method));
294 phpstr_appends(custom_request_methods, ", ");
295 }
296
297 phpstr_append(known_request_methods, PHPSTR_VAL(custom_request_methods), PHPSTR_LEN(custom_request_methods));
298 phpstr_fix(known_request_methods);
299 phpstr_fix(custom_request_methods);
300
301 php_info_print_table_row(2, "Known Request Methods:", PHPSTR_VAL(known_request_methods));
302 php_info_print_table_row(2, "Custom Request Methods:",
303 PHPSTR_LEN(custom_request_methods) ? PHPSTR_VAL(custom_request_methods) : "none registered");
304
305 phpstr_free(known_request_methods);
306 phpstr_free(custom_request_methods);
307 }
308 php_info_print_table_end();
309
310 php_info_print_table_start();
311 {
312 php_info_print_table_header(2, "Functionality", "Availability");
313 php_info_print_table_row(2, "Miscellaneous Utilities:", HTTP_FUNC_AVAIL("HttpUtil, HttpMessage"));
314 php_info_print_table_row(2, "Extended HTTP Responses:", HTTP_FUNC_AVAIL("HttpResponse"));
315 php_info_print_table_row(2, "Extended HTTP Requests:", HTTP_CURL_AVAIL("HttpRequest, HttpRequestPool"));
316 }
317 php_info_print_table_end();
318
319 DISPLAY_INI_ENTRIES();
320 }
321 /* }}} */
322
323 /*
324 * Local variables:
325 * tab-width: 4
326 * c-basic-offset: 4
327 * End:
328 * vim600: noet sw=4 ts=4 fdm=marker
329 * vim<600: noet sw=4 ts=4
330 */
331