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