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