- unify script termination
[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 # endif
50 # include "php_http_exception_object.h"
51 #endif
52
53 #include "phpstr/phpstr.h"
54
55 #ifdef HTTP_HAVE_CURL
56 #ifdef ZEND_ENGINE_2
57 static
58 ZEND_BEGIN_ARG_INFO(http_request_info_ref_3, 0)
59 ZEND_ARG_PASS_INFO(0)
60 ZEND_ARG_PASS_INFO(0)
61 ZEND_ARG_PASS_INFO(1)
62 ZEND_END_ARG_INFO();
63
64 static
65 ZEND_BEGIN_ARG_INFO(http_request_info_ref_4, 0)
66 ZEND_ARG_PASS_INFO(0)
67 ZEND_ARG_PASS_INFO(0)
68 ZEND_ARG_PASS_INFO(0)
69 ZEND_ARG_PASS_INFO(1)
70 ZEND_END_ARG_INFO();
71 #else
72 static unsigned char http_request_info_ref_3[] = {3, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
73 static unsigned char http_request_info_ref_4[] = {4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
74 #endif /* ZEND_ENGINE_2 */
75 #endif /* HTTP_HAVE_CURL */
76
77 ZEND_DECLARE_MODULE_GLOBALS(http)
78
79 #ifdef COMPILE_DL_HTTP
80 ZEND_GET_MODULE(http)
81 #endif
82
83 /* {{{ http_functions[] */
84 function_entry http_functions[] = {
85 PHP_FE(http_test, NULL)
86 PHP_FE(http_date, NULL)
87 PHP_FE(http_absolute_uri, NULL)
88 PHP_FE(http_negotiate_language, NULL)
89 PHP_FE(http_negotiate_charset, NULL)
90 PHP_FE(http_redirect, NULL)
91 PHP_FE(http_send_status, NULL)
92 PHP_FE(http_send_last_modified, NULL)
93 PHP_FE(http_send_content_type, NULL)
94 PHP_FE(http_send_content_disposition, NULL)
95 PHP_FE(http_match_modified, NULL)
96 PHP_FE(http_match_etag, NULL)
97 PHP_FE(http_cache_last_modified, NULL)
98 PHP_FE(http_cache_etag, NULL)
99 PHP_FE(http_send_data, NULL)
100 PHP_FE(http_send_file, NULL)
101 PHP_FE(http_send_stream, NULL)
102 PHP_FE(http_chunked_decode, NULL)
103 PHP_FE(http_split_response, NULL)
104 PHP_FE(http_parse_headers, NULL)
105 PHP_FE(http_get_request_headers, NULL)
106 #ifdef HTTP_HAVE_CURL
107 PHP_FE(http_get, http_request_info_ref_3)
108 PHP_FE(http_head, http_request_info_ref_3)
109 PHP_FE(http_post_data, http_request_info_ref_4)
110 PHP_FE(http_post_array, http_request_info_ref_4)
111 #endif
112 PHP_FE(http_auth_basic, NULL)
113 PHP_FE(http_auth_basic_cb, NULL)
114 #ifndef ZEND_ENGINE_2
115 PHP_FE(http_build_query, NULL)
116 #endif
117 PHP_FE(ob_etaghandler, NULL)
118 {NULL, NULL, NULL}
119 };
120 /* }}} */
121
122 /* {{{ http_module_entry */
123 zend_module_entry http_module_entry = {
124 #if ZEND_MODULE_API_NO >= 20010901
125 STANDARD_MODULE_HEADER,
126 #endif
127 "http",
128 http_functions,
129 PHP_MINIT(http),
130 PHP_MSHUTDOWN(http),
131 PHP_RINIT(http),
132 PHP_RSHUTDOWN(http),
133 PHP_MINFO(http),
134 #if ZEND_MODULE_API_NO >= 20010901
135 HTTP_PEXT_VERSION,
136 #endif
137 STANDARD_MODULE_PROPERTIES
138 };
139 /* }}} */
140
141
142 static void free_to_free(void *s)
143 {
144 efree(*(char **)s);
145 }
146
147 /* {{{ void http_globals_ctor(zend_http_globals *) */
148 #define http_globals_ctor _http_globals_ctor
149 static inline void _http_globals_ctor(zend_http_globals *http_globals)
150 {
151 http_globals->etag_started = 0;
152 http_globals->ctype = NULL;
153 http_globals->etag = NULL;
154 http_globals->lmod = 0;
155 #ifdef HTTP_HAVE_CURL
156 # if LIBCURL_VERSION_NUM < 0x070c00
157 memset(&http_globals->curlerr, 0, sizeof(http_globals->curlerr));
158 # endif
159 zend_llist_init(&http_globals->to_free, sizeof(char *), free_to_free, 0);
160 #endif
161 http_globals->allowed_methods = NULL;
162 http_globals->cache_log = NULL;
163 }
164 /* }}} */
165
166 /* {{{ void http_globals_dtor() */
167 #define http_globals_dtor() _http_globals_dtor(TSRMLS_C)
168 static inline void _http_globals_dtor(TSRMLS_D)
169 {
170 HTTP_G(etag_started) = 0;
171 HTTP_G(lmod) = 0;
172
173 if (HTTP_G(etag)) {
174 efree(HTTP_G(etag));
175 HTTP_G(etag) = NULL;
176 }
177
178 if (HTTP_G(ctype)) {
179 efree(HTTP_G(ctype));
180 HTTP_G(ctype) = NULL;
181 }
182
183 #ifdef HTTP_HAVE_CURL
184 # if LIBCURL_VERSION_NUM < 0x070c00
185 memset(&HTTP_G(curlerr), 0, sizeof(HTTP_G(curlerr)));
186 # endif
187 #endif
188
189 }
190 /* }}} */
191
192 /* {{{ static inline void http_check_allowed_methods(char *, int) */
193 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
194 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
195 {
196 char *found, *header;
197
198 if (!length || !SG(request_info).request_method) {
199 return;
200 }
201
202 if ( (found = strstr(methods, SG(request_info).request_method)) &&
203 (found == SG(request_info).request_method || !isalpha(found[-1])) &&
204 (!isalpha(found[strlen(SG(request_info).request_method) + 1]))) {
205 return;
206 }
207
208 header = emalloc(length + sizeof("Allow: "));
209 sprintf(header, "Allow: %s", methods);
210 http_exit(405, header);
211 }
212 /* }}} */
213
214 /* {{{ PHP_INI */
215 PHP_INI_MH(http_update_allowed_methods)
216 {
217 http_check_allowed_methods(new_value, new_value_length);
218 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
219 }
220
221 #define http_update_cache_log OnUpdateString
222
223 PHP_INI_BEGIN()
224 HTTP_INI_ENTRY("http.allowed_methods", HTTP_KNOWN_METHODS, PHP_INI_ALL, allowed_methods)
225 HTTP_INI_ENTRY("http.cache_log", NULL, PHP_INI_ALL, cache_log)
226 PHP_INI_END()
227 /* }}} */
228
229 /* {{{ HTTP_CURL_USE_ZEND_MM */
230 #if defined(HTTP_HAVE_CURL) && defined(HTTP_CURL_USE_ZEND_MM)
231 static void http_curl_free(void *p) { efree(p); }
232 static char *http_curl_strdup(const char *p) { return estrdup(p); }
233 static void *http_curl_malloc(size_t s) { return emalloc(s); }
234 static void *http_curl_realloc(void *p, size_t s) { return erealloc(p, s); }
235 static void *http_curl_calloc(size_t n, size_t s) { return ecalloc(n, s); }
236 #endif /* HTTP_HAVE_CURL && HTTP_CURL_USE_ZEND_MM */
237 /* }}} */
238
239 /* {{{ PHP_MINIT_FUNCTION */
240 PHP_MINIT_FUNCTION(http)
241 {
242 ZEND_INIT_MODULE_GLOBALS(http, http_globals_ctor, NULL);
243 REGISTER_INI_ENTRIES();
244
245 #ifdef HTTP_HAVE_CURL
246 # ifdef HTTP_CURL_USE_ZEND_MM
247 if (CURLE_OK != curl_global_init_mem(CURL_GLOBAL_ALL,
248 http_curl_malloc,
249 http_curl_free,
250 http_curl_realloc,
251 http_curl_strdup,
252 http_curl_calloc)) {
253 return FAILURE;
254 }
255 # endif /* HTTP_CURL_USE_ZEND_MM */
256 #endif /* HTTP_HAVE_CURL */
257
258 #ifdef ZEND_ENGINE_2
259 http_util_object_init(INIT_FUNC_ARGS_PASSTHRU);
260 http_message_object_init(INIT_FUNC_ARGS_PASSTHRU);
261 http_response_object_init(INIT_FUNC_ARGS_PASSTHRU);
262 # ifdef HTTP_HAVE_CURL
263 http_request_object_init(INIT_FUNC_ARGS_PASSTHRU);
264 # endif /* HTTP_HAVE_CURL */
265 http_exception_object_init(INIT_FUNC_ARGS_PASSTHRU);
266 #endif /* ZEND_ENGINE_2 */
267
268 return SUCCESS;
269 }
270 /* }}} */
271
272 /* {{{ PHP_MSHUTDOWN_FUNCTION */
273 PHP_MSHUTDOWN_FUNCTION(http)
274 {
275 UNREGISTER_INI_ENTRIES();
276 #ifdef HTTP_HAVE_CURL
277 curl_global_cleanup();
278 #endif
279 return SUCCESS;
280 }
281 /* }}} */
282
283 /* {{{ PHP_RINIT_FUNCTION */
284 PHP_RINIT_FUNCTION(http)
285 {
286 char *allowed_methods = INI_STR("http.allowed_methods");
287 http_check_allowed_methods(allowed_methods, strlen(allowed_methods));
288 return SUCCESS;
289 }
290 /* }}} */
291
292 /* {{{ PHP_RSHUTDOWN_FUNCTION */
293 PHP_RSHUTDOWN_FUNCTION(http)
294 {
295 http_globals_dtor();
296 return SUCCESS;
297 }
298 /* }}} */
299
300 /* {{{ PHP_MINFO_FUNCTION */
301 PHP_MINFO_FUNCTION(http)
302 {
303 #ifdef ZEND_ENGINE_2
304 # define HTTP_FUNC_AVAIL(CLASS) "procedural, object oriented (" CLASS ")"
305 #else
306 # define HTTP_FUNC_AVAIL(CLASS) "procedural"
307 #endif
308
309 #ifdef HTTP_HAVE_CURL
310 # define HTTP_CURL_VERSION curl_version()
311 # ifdef ZEND_ENGINE_2
312 # define HTTP_CURL_AVAIL(CLASS) "procedural, object oriented (" CLASS ")"
313 # else
314 # define HTTP_CURL_AVAIL(CLASS) "procedural"
315 # endif
316 #else
317 # define HTTP_CURL_VERSION "libcurl not available"
318 # define HTTP_CURL_AVAIL(CLASS) "libcurl not available"
319 #endif
320
321 char full_version_string[1024] = {0};
322 snprintf(full_version_string, 1023, "%s (%s)", HTTP_PEXT_VERSION, HTTP_CURL_VERSION);
323
324 php_info_print_table_start();
325 php_info_print_table_row(2, "Extended HTTP support", "enabled");
326 php_info_print_table_row(2, "Extension Version:", full_version_string);
327 php_info_print_table_end();
328
329 php_info_print_table_start();
330 php_info_print_table_header(2, "Functionality", "Availability");
331 php_info_print_table_row(2, "Miscellaneous Utilities:", HTTP_FUNC_AVAIL("HttpUtil, HttpMessage"));
332 php_info_print_table_row(2, "Extended HTTP Responses:", HTTP_FUNC_AVAIL("HttpResponse"));
333 php_info_print_table_row(2, "Extended HTTP Requests:", HTTP_CURL_AVAIL("HttpRequest"));
334 php_info_print_table_end();
335
336 DISPLAY_INI_ENTRIES();
337 }
338 /* }}} */
339
340 /*
341 * Local variables:
342 * tab-width: 4
343 * c-basic-offset: 4
344 * End:
345 * vim600: noet sw=4 ts=4 fdm=marker
346 * vim<600: noet sw=4 ts=4
347 */
348