- fix php4 build
[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 #include "php.h"
23
24 #include "SAPI.h"
25 #include "php_ini.h"
26 #include "ext/standard/info.h"
27
28 #include "php_http.h"
29 #include "php_http_std_defs.h"
30 #include "php_http_api.h"
31 #include "php_http_send_api.h"
32 #include "php_http_cache_api.h"
33 #ifdef HTTP_HAVE_CURL
34 # include "php_http_request_api.h"
35 #endif
36
37 #ifdef ZEND_ENGINE_2
38 # include "php_http_util_object.h"
39 # include "php_http_message_object.h"
40 # include "php_http_response_object.h"
41 # ifdef HTTP_HAVE_CURL
42 # include "php_http_request_object.h"
43 # include "php_http_requestpool_object.h"
44 # endif
45 # include "php_http_exception_object.h"
46 #endif
47
48 #include "missing.h"
49 #include "phpstr/phpstr.h"
50
51 #ifdef HTTP_HAVE_CURL
52 # ifdef PHP_WIN32
53 # include <winsock2.h>
54 # endif
55 # include <curl/curl.h>
56 #endif
57
58 #include <ctype.h>
59
60 ZEND_DECLARE_MODULE_GLOBALS(http);
61 HTTP_DECLARE_ARG_PASS_INFO();
62
63 #ifdef COMPILE_DL_HTTP
64 ZEND_GET_MODULE(http)
65 #endif
66
67 /* {{{ http_functions[] */
68 function_entry http_functions[] = {
69 PHP_FE(http_test, NULL)
70 PHP_FE(http_date, NULL)
71 PHP_FE(http_absolute_uri, NULL)
72 PHP_FE(http_negotiate_language, NULL)
73 PHP_FE(http_negotiate_charset, NULL)
74 PHP_FE(http_redirect, NULL)
75 PHP_FE(http_throttle, NULL)
76 PHP_FE(http_send_status, NULL)
77 PHP_FE(http_send_last_modified, NULL)
78 PHP_FE(http_send_content_type, NULL)
79 PHP_FE(http_send_content_disposition, NULL)
80 PHP_FE(http_match_modified, NULL)
81 PHP_FE(http_match_etag, NULL)
82 PHP_FE(http_cache_last_modified, NULL)
83 PHP_FE(http_cache_etag, NULL)
84 PHP_FE(http_send_data, NULL)
85 PHP_FE(http_send_file, NULL)
86 PHP_FE(http_send_stream, NULL)
87 PHP_FE(http_chunked_decode, NULL)
88 PHP_FE(http_parse_message, NULL)
89 PHP_FE(http_parse_headers, 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)
93 #ifdef HTTP_HAVE_CURL
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)
100 PHP_FE(http_request_method_register, NULL)
101 PHP_FE(http_request_method_unregister, NULL)
102 PHP_FE(http_request_method_exists, NULL)
103 PHP_FE(http_request_method_name, NULL)
104 #endif
105 PHP_FE(http_auth_basic, NULL)
106 PHP_FE(http_auth_basic_cb, NULL)
107 #ifndef ZEND_ENGINE_2
108 PHP_FE(http_build_query, NULL)
109 #endif
110 PHP_FE(ob_etaghandler, NULL)
111 {NULL, NULL, NULL}
112 };
113 /* }}} */
114
115 /* {{{ http_module_entry */
116 zend_module_entry http_module_entry = {
117 #if ZEND_MODULE_API_NO >= 20010901
118 STANDARD_MODULE_HEADER,
119 #endif
120 "http",
121 http_functions,
122 PHP_MINIT(http),
123 PHP_MSHUTDOWN(http),
124 PHP_RINIT(http),
125 PHP_RSHUTDOWN(http),
126 PHP_MINFO(http),
127 #if ZEND_MODULE_API_NO >= 20010901
128 HTTP_PEXT_VERSION,
129 #endif
130 STANDARD_MODULE_PROPERTIES
131 };
132 /* }}} */
133
134 int http_module_number;
135
136 /* {{{ http_globals */
137 static void http_globals_init_once(zend_http_globals *G)
138 {
139 memset(G, 0, sizeof(zend_http_globals));
140 }
141
142 static inline void http_globals_init(zend_http_globals *G)
143 {
144 G->send.buffer_size = HTTP_SENDBUF_SIZE;
145 zend_hash_init(&G->request.methods.custom, 0, NULL, ZVAL_PTR_DTOR, 0);
146 #ifdef HTTP_HAVE_CURL
147 zend_llist_init(&G->request.copies.strings, sizeof(char *), http_request_data_free_string, 0);
148 zend_llist_init(&G->request.copies.slists, sizeof(struct curl_slist *), http_request_data_free_slist, 0);
149 zend_llist_init(&G->request.copies.contexts, sizeof(http_curl_callback_ctx *), http_request_data_free_context, 0);
150 zend_llist_init(&G->request.copies.convs, sizeof(http_curl_conv *), http_request_data_free_conv, 0);
151 #endif
152 }
153
154 static inline void http_globals_free(zend_http_globals *G)
155 {
156 STR_SET(G->send.content_type, NULL);
157 STR_SET(G->send.unquoted_etag, NULL);
158 zend_hash_destroy(&G->request.methods.custom);
159 #ifdef HTTP_HAVE_CURL
160 zend_llist_clean(&G->request.copies.strings);
161 zend_llist_clean(&G->request.copies.slists);
162 zend_llist_clean(&G->request.copies.contexts);
163 zend_llist_clean(&G->request.copies.convs);
164 #endif
165 }
166 /* }}} */
167
168 /* {{{ static inline void http_check_allowed_methods(char *, int) */
169 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
170 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
171 {
172 if (length && SG(request_info).request_method) {
173 if (SUCCESS != http_check_method_ex(SG(request_info).request_method, methods)) {
174 char *header = emalloc(length + sizeof("Allow: "));
175 sprintf(header, "Allow: %s", methods);
176 http_exit(405, header);
177 }
178 }
179 }
180 /* }}} */
181
182 /* {{{ PHP_INI */
183 PHP_INI_MH(http_update_allowed_methods)
184 {
185 http_check_allowed_methods(new_value, new_value_length);
186 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
187 }
188
189 PHP_INI_BEGIN()
190 HTTP_PHP_INI_ENTRY("http.allowed_methods", "", PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
191 HTTP_PHP_INI_ENTRY("http.cache_log", "", PHP_INI_ALL, OnUpdateString, log.cache)
192 HTTP_PHP_INI_ENTRY("http.redirect_log", "", PHP_INI_ALL, OnUpdateString, log.redirect)
193 HTTP_PHP_INI_ENTRY("http.allowed_methods_log", "", PHP_INI_ALL, OnUpdateString, log.allowed_methods)
194 HTTP_PHP_INI_ENTRY("http.composite_log", "", PHP_INI_ALL, OnUpdateString, log.composite)
195 #ifdef ZEND_ENGINE_2
196 HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL, OnUpdateBool, only_exceptions)
197 #endif
198 HTTP_PHP_INI_ENTRY("http.etag_mode", "-2", PHP_INI_ALL, OnUpdateLong, etag.mode)
199 PHP_INI_END()
200 /* }}} */
201
202
203 /* {{{ PHP_MINIT_FUNCTION */
204 PHP_MINIT_FUNCTION(http)
205 {
206 http_module_number = module_number;
207
208 ZEND_INIT_MODULE_GLOBALS(http, http_globals_init_once, NULL)
209
210 REGISTER_INI_ENTRIES();
211
212 HTTP_LONG_CONSTANT("HTTP_ETAG_MD5", HTTP_ETAG_MD5);
213 HTTP_LONG_CONSTANT("HTTP_ETAG_SHA1", HTTP_ETAG_SHA1);
214 HTTP_LONG_CONSTANT("HTTP_ETAG_MHASH", HTTP_ETAG_MHASH);
215
216 #ifdef HTTP_HAVE_CURL
217 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
218 return FAILURE;
219 }
220 #endif /* HTTP_HAVE_CURL */
221
222 #ifdef ZEND_ENGINE_2
223 http_util_object_init();
224 http_message_object_init();
225 http_response_object_init();
226 # ifdef HTTP_HAVE_CURL
227 http_request_object_init();
228 http_requestpool_object_init();
229 # endif /* HTTP_HAVE_CURL */
230 http_exception_object_init();
231 #endif /* ZEND_ENGINE_2 */
232
233 #ifdef ZEND_ENGINE_2
234 zend_hash_init_ex(&http_response_statics, 0, NULL, ZVAL_INTERNAL_PTR_DTOR, 1, 0);
235 zend_fix_static_properties(http_response_object_ce, &http_response_statics TSRMLS_CC);
236 #endif
237 return SUCCESS;
238 }
239 /* }}} */
240
241 /* {{{ PHP_MSHUTDOWN_FUNCTION */
242 PHP_MSHUTDOWN_FUNCTION(http)
243 {
244 #ifdef ZEND_ENGINE_2
245 zend_hash_destroy(&http_response_statics);
246 #endif
247 UNREGISTER_INI_ENTRIES();
248 #ifdef HTTP_HAVE_CURL
249 curl_global_cleanup();
250 #endif
251 return SUCCESS;
252 }
253 /* }}} */
254
255 /* {{{ PHP_RINIT_FUNCTION */
256 PHP_RINIT_FUNCTION(http)
257 {
258 char *m;
259
260 if (m = INI_STR("http.allowed_methods")) {
261 http_check_allowed_methods(m, strlen(m));
262 }
263
264 http_globals_init(HTTP_GLOBALS);
265 #ifdef ZEND_ENGINE_2
266 zend_init_static_properties(http_response_object_ce, &http_response_statics TSRMLS_CC);
267 #endif
268 return SUCCESS;
269 }
270 /* }}} */
271
272 /* {{{ PHP_RSHUTDOWN_FUNCTION */
273 PHP_RSHUTDOWN_FUNCTION(http)
274 {
275 #ifdef ZEND_ENGINE_2
276 zend_clean_static_properties(http_response_object_ce TSRMLS_CC);
277 #endif
278 http_globals_free(HTTP_GLOBALS);
279 return SUCCESS;
280 }
281 /* }}} */
282
283 /* {{{ PHP_MINFO_FUNCTION */
284 PHP_MINFO_FUNCTION(http)
285 {
286 #ifdef ZEND_ENGINE_2
287 # define HTTP_FUNC_AVAIL(CLASS) "procedural, object oriented (" CLASS ")"
288 #else
289 # define HTTP_FUNC_AVAIL(CLASS) "procedural"
290 #endif
291
292 #ifdef HTTP_HAVE_CURL
293 # define HTTP_CURL_VERSION curl_version()
294 # ifdef ZEND_ENGINE_2
295 # define HTTP_CURL_AVAIL(CLASS) "procedural, object oriented (" CLASS ")"
296 # else
297 # define HTTP_CURL_AVAIL(CLASS) "procedural"
298 # endif
299 #else
300 # define HTTP_CURL_VERSION "libcurl not available"
301 # define HTTP_CURL_AVAIL(CLASS) "libcurl not available"
302 #endif
303
304 #include "php_http_request_api.h"
305
306 php_info_print_table_start();
307 {
308 char full_version_string[1024] = {0};
309 snprintf(full_version_string, 1023, "%s (%s)", HTTP_PEXT_VERSION, HTTP_CURL_VERSION);
310
311 php_info_print_table_row(2, "Extended HTTP support:", "enabled");
312 php_info_print_table_row(2, "Extension Version:", full_version_string);
313 }
314 php_info_print_table_end();
315
316 #ifdef HTTP_HAVE_CURL
317 php_info_print_table_start();
318 {
319 unsigned i;
320 zval **custom_method;
321 phpstr *known_request_methods = phpstr_new();
322 phpstr *custom_request_methods = phpstr_new();
323
324 for (i = HTTP_NO_REQUEST_METHOD+1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
325 phpstr_appendl(known_request_methods, http_request_method_name(i));
326 phpstr_appends(known_request_methods, ", ");
327 }
328 FOREACH_HASH_VAL(&HTTP_G(request).methods.custom, custom_method) {
329 phpstr_append(custom_request_methods, Z_STRVAL_PP(custom_method), Z_STRLEN_PP(custom_method));
330 phpstr_appends(custom_request_methods, ", ");
331 }
332
333 phpstr_append(known_request_methods, PHPSTR_VAL(custom_request_methods), PHPSTR_LEN(custom_request_methods));
334 phpstr_fix(known_request_methods);
335 phpstr_fix(custom_request_methods);
336
337 php_info_print_table_row(2, "Known Request Methods:", PHPSTR_VAL(known_request_methods));
338 php_info_print_table_row(2, "Custom Request Methods:",
339 PHPSTR_LEN(custom_request_methods) ? PHPSTR_VAL(custom_request_methods) : "none registered");
340
341 phpstr_free(&known_request_methods);
342 phpstr_free(&custom_request_methods);
343 }
344 php_info_print_table_end();
345 #endif
346
347 php_info_print_table_start();
348 {
349 php_info_print_table_header(2, "Functionality", "Availability");
350 php_info_print_table_row(2, "Miscellaneous Utilities:", HTTP_FUNC_AVAIL("HttpUtil, HttpMessage"));
351 php_info_print_table_row(2, "Extended HTTP Responses:", HTTP_FUNC_AVAIL("HttpResponse"));
352 php_info_print_table_row(2, "Extended HTTP Requests:", HTTP_CURL_AVAIL("HttpRequest, HttpRequestPool"));
353 }
354 php_info_print_table_end();
355
356 DISPLAY_INI_ENTRIES();
357 }
358 /* }}} */
359
360 /*
361 * Local variables:
362 * tab-width: 4
363 * c-basic-offset: 4
364 * End:
365 * vim600: noet sw=4 ts=4 fdm=marker
366 * vim<600: noet sw=4 ts=4
367 */
368