991e84e8fc031ff4c30fb487cf0f8bee8f051739
[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 "php.h"
31 #include "php_ini.h"
32 #include "ext/standard/info.h"
33
34 #include "SAPI.h"
35
36 #include "php_http.h"
37 #include "php_http_std_defs.h"
38
39 #include "php_http_send_api.h"
40
41 #include "php_http_util_object.h"
42 #include "php_http_message_object.h"
43 #include "php_http_response_object.h"
44 #include "php_http_request_object.h"
45
46 #include "phpstr/phpstr.h"
47
48 #ifdef HTTP_HAVE_CURL
49 #ifdef ZEND_ENGINE_2
50 static
51 ZEND_BEGIN_ARG_INFO(http_request_info_ref_3, 0)
52 ZEND_ARG_PASS_INFO(0)
53 ZEND_ARG_PASS_INFO(0)
54 ZEND_ARG_PASS_INFO(1)
55 ZEND_END_ARG_INFO();
56
57 static
58 ZEND_BEGIN_ARG_INFO(http_request_info_ref_4, 0)
59 ZEND_ARG_PASS_INFO(0)
60 ZEND_ARG_PASS_INFO(0)
61 ZEND_ARG_PASS_INFO(0)
62 ZEND_ARG_PASS_INFO(1)
63 ZEND_END_ARG_INFO();
64 #else
65 static unsigned char http_request_info_ref_3[] = {3, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
66 static unsigned char http_request_info_ref_4[] = {4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
67 #endif /* ZEND_ENGINE_2 */
68 #endif /* HTTP_HAVE_CURL */
69
70 ZEND_DECLARE_MODULE_GLOBALS(http)
71
72 #ifdef COMPILE_DL_HTTP
73 ZEND_GET_MODULE(http)
74 #endif
75
76 /* {{{ http_functions[] */
77 function_entry http_functions[] = {
78 PHP_FE(http_test, NULL)
79 PHP_FE(http_date, NULL)
80 PHP_FE(http_absolute_uri, NULL)
81 PHP_FE(http_negotiate_language, NULL)
82 PHP_FE(http_negotiate_charset, NULL)
83 PHP_FE(http_redirect, NULL)
84 PHP_FE(http_send_status, NULL)
85 PHP_FE(http_send_last_modified, NULL)
86 PHP_FE(http_send_content_type, NULL)
87 PHP_FE(http_send_content_disposition, NULL)
88 PHP_FE(http_match_modified, NULL)
89 PHP_FE(http_match_etag, NULL)
90 PHP_FE(http_cache_last_modified, NULL)
91 PHP_FE(http_cache_etag, NULL)
92 PHP_FE(http_send_data, NULL)
93 PHP_FE(http_send_file, NULL)
94 PHP_FE(http_send_stream, NULL)
95 PHP_FE(http_chunked_decode, NULL)
96 PHP_FE(http_split_response, NULL)
97 PHP_FE(http_parse_headers, NULL)
98 PHP_FE(http_get_request_headers, NULL)
99 #ifdef HTTP_HAVE_CURL
100 PHP_FE(http_get, http_request_info_ref_3)
101 PHP_FE(http_head, http_request_info_ref_3)
102 PHP_FE(http_post_data, http_request_info_ref_4)
103 PHP_FE(http_post_array, http_request_info_ref_4)
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_httpetaghandler, 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
135 static void free_to_free(void *s)
136 {
137 efree(*(char **)s);
138 }
139
140 /* {{{ php_http_init_globals(zend_http_globals *) */
141 static void php_http_init_globals(zend_http_globals *http_globals)
142 {
143 http_globals->etag_started = 0;
144 http_globals->ctype = NULL;
145 http_globals->etag = NULL;
146 http_globals->lmod = 0;
147 #ifdef HTTP_HAVE_CURL
148 phpstr_init_ex(&http_globals->curlbuf, HTTP_CURLBUF_SIZE, 0);
149 # if LIBCURL_VERSION_NUM < 0x070c00
150 memset(&http_globals->curlerr, 0, sizeof(http_globals->curlerr));
151 # endif
152 zend_llist_init(&http_globals->to_free, sizeof(char *), free_to_free, 0);
153 #endif
154 http_globals->allowed_methods = NULL;
155 }
156 /* }}} */
157
158 /* {{{ static inline STATUS http_check_allowed_methods(char *, int) */
159 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
160 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
161 {
162 if (length && SG(request_info).request_method && (!strstr(methods, SG(request_info).request_method))) {
163 char *allow_header = emalloc(length + sizeof("Allow: "));
164 sprintf(allow_header, "Allow: %s", methods);
165 http_send_header(allow_header);
166 efree(allow_header);
167 http_send_status(405);
168 zend_bailout();
169 }
170 }
171 /* }}} */
172
173 /* {{{ PHP_INI */
174 PHP_INI_MH(update_allowed_methods)
175 {
176 http_check_allowed_methods(new_value, new_value_length);
177 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
178 }
179
180 PHP_INI_BEGIN()
181 STD_PHP_INI_ENTRY("http.allowed_methods",
182 /* HTTP 1.1 */
183 "GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, CONNECT, "
184 /* WebDAV - RFC 2518 * /
185 "PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, "
186 /* WebDAV Versioning - RFC 3253 * /
187 "VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, "
188 "MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, "
189 /* WebDAV Access Control - RFC 3744 * /
190 "ACL, "
191 /* END */
192 ,
193 PHP_INI_ALL, update_allowed_methods, allowed_methods, zend_http_globals, http_globals)
194 PHP_INI_END()
195 /* }}} */
196
197 /* {{{ HTTP_CURL_USE_ZEND_MM */
198 #if defined(HTTP_HAVE_CURL) && defined(HTTP_CURL_USE_ZEND_MM)
199 static void http_curl_free(void *p) { efree(p); }
200 static char *http_curl_strdup(const char *p) { return estrdup(p); }
201 static void *http_curl_malloc(size_t s) { return emalloc(s); }
202 static void *http_curl_realloc(void *p, size_t s) { return erealloc(p, s); }
203 static void *http_curl_calloc(size_t n, size_t s) { return ecalloc(n, s); }
204 #endif /* HTTP_HAVE_CURL && HTTP_CURL_USE_ZEND_MM */
205 /* }}} */
206
207 /* {{{ PHP_MINIT_FUNCTION */
208 PHP_MINIT_FUNCTION(http)
209 {
210 ZEND_INIT_MODULE_GLOBALS(http, php_http_init_globals, NULL);
211 REGISTER_INI_ENTRIES();
212
213 #ifdef HTTP_HAVE_CURL
214 # ifdef HTTP_CURL_USE_ZEND_MM
215 if (CURLE_OK != curl_global_init_mem(CURL_GLOBAL_ALL,
216 http_curl_malloc,
217 http_curl_free,
218 http_curl_realloc,
219 http_curl_strdup,
220 http_curl_calloc)) {
221 return FAILURE;
222 }
223 # endif /* HTTP_CURL_USE_ZEND_MM */
224 #endif /* HTTP_HAVE_CURL */
225
226 #ifdef ZEND_ENGINE_2
227 http_util_object_init(INIT_FUNC_ARGS_PASSTHRU);
228 http_message_object_init(INIT_FUNC_ARGS_PASSTHRU);
229 http_response_object_init(INIT_FUNC_ARGS_PASSTHRU);
230 # ifdef HTTP_HAVE_CURL
231 http_request_object_init(INIT_FUNC_ARGS_PASSTHRU);
232 # endif /* HTTP_HAVE_CURL */
233 #endif /* ZEND_ENGINE_2 */
234
235 return SUCCESS;
236 }
237 /* }}} */
238
239 /* {{{ PHP_MSHUTDOWN_FUNCTION */
240 PHP_MSHUTDOWN_FUNCTION(http)
241 {
242 UNREGISTER_INI_ENTRIES();
243 #ifdef HTTP_HAVE_CURL
244 phpstr_dtor(&HTTP_G(curlbuf));
245 curl_global_cleanup();
246 #endif
247 return SUCCESS;
248 }
249 /* }}} */
250
251 /* {{{ PHP_RINIT_FUNCTION */
252 PHP_RINIT_FUNCTION(http)
253 {
254 char *allowed_methods = INI_STR("http.allowed_methods");
255 http_check_allowed_methods(allowed_methods, strlen(allowed_methods));
256 return SUCCESS;
257 }
258 /* }}} */
259
260 /* {{{ PHP_RSHUTDOWN_FUNCTION */
261 PHP_RSHUTDOWN_FUNCTION(http)
262 {
263 HTTP_G(etag_started) = 0;
264 HTTP_G(lmod) = 0;
265
266 if (HTTP_G(etag)) {
267 efree(HTTP_G(etag));
268 HTTP_G(etag) = NULL;
269 }
270
271 if (HTTP_G(ctype)) {
272 efree(HTTP_G(ctype));
273 HTTP_G(ctype) = NULL;
274 }
275
276 #ifdef HTTP_HAVE_CURL
277 # if LIBCURL_VERSION_NUM < 0x070c00
278 memset(&HTTP_G(curlerr), 0, sizeof(HTTP_G(curlerr)));
279 # endif
280 phpstr_dtor(&HTTP_G(curlbuf));
281 #endif
282
283 return SUCCESS;
284 }
285 /* }}} */
286
287 /* {{{ PHP_MINFO_FUNCTION */
288 PHP_MINFO_FUNCTION(http)
289 {
290 #ifdef ZEND_ENGINE_2
291 # define HTTP_FUNC_AVAIL(CLASS) "procedural, object oriented (class " CLASS ")"
292 #else
293 # define HTTP_FUNC_AVAIL(CLASS) "procedural"
294 #endif
295
296 #ifdef HTTP_HAVE_CURL
297 # define HTTP_CURL_VERSION curl_version()
298 # ifdef ZEND_ENGINE_2
299 # define HTTP_CURL_AVAIL(CLASS) "procedural, object oriented (class " CLASS ")"
300 # else
301 # define HTTP_CURL_AVAIL(CLASS) "procedural"
302 # endif
303 #else
304 # define HTTP_CURL_VERSION "libcurl not available"
305 # define HTTP_CURL_AVAIL(CLASS) "libcurl not available"
306 #endif
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_start();
312 php_info_print_table_row(2, "Extended HTTP support", "enabled");
313 php_info_print_table_row(2, "Extension Version:", full_version_string);
314 php_info_print_table_end();
315
316 php_info_print_table_start();
317 php_info_print_table_header(2, "Functionality", "Availability");
318 php_info_print_table_row(2, "Miscellaneous Utilities:", HTTP_FUNC_AVAIL("HttpUtil"));
319 php_info_print_table_row(2, "Extended HTTP Responses:", HTTP_FUNC_AVAIL("HttpResponse"));
320 php_info_print_table_row(2, "Extended HTTP Requests:", HTTP_CURL_AVAIL("HttpRequest"));
321 php_info_print_table_end();
322
323 DISPLAY_INI_ENTRIES();
324 }
325 /* }}} */
326
327 /*
328 * Local variables:
329 * tab-width: 4
330 * c-basic-offset: 4
331 * End:
332 * vim600: noet sw=4 ts=4 fdm=marker
333 * vim<600: noet sw=4 ts=4
334 */
335