8d65cd155ea50d3a7f0803eb5f8acedbd08a502c
[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 #include "php_http_request_method_api.h"
34 #ifdef HTTP_HAVE_CURL
35 # include "php_http_request_api.h"
36 #endif
37
38 #ifdef ZEND_ENGINE_2
39 # include "php_http_util_object.h"
40 # include "php_http_message_object.h"
41 # ifndef WONKY
42 # include "php_http_response_object.h"
43 # endif
44 # ifdef HTTP_HAVE_CURL
45 # include "php_http_request_object.h"
46 # include "php_http_requestpool_object.h"
47 # endif
48 # include "php_http_exception_object.h"
49 #endif
50
51 #include "missing.h"
52 #include "phpstr/phpstr.h"
53
54 #ifdef HTTP_HAVE_CURL
55 # ifdef PHP_WIN32
56 # include <winsock2.h>
57 # endif
58 # include <curl/curl.h>
59 #endif
60 #ifdef HTTP_HAVE_MHASH
61 # include <mhash.h>
62 #endif
63
64 #include <ctype.h>
65
66 ZEND_DECLARE_MODULE_GLOBALS(http);
67 HTTP_DECLARE_ARG_PASS_INFO();
68
69 #ifdef COMPILE_DL_HTTP
70 ZEND_GET_MODULE(http)
71 #endif
72
73 /* {{{ http_functions[] */
74 function_entry http_functions[] = {
75 PHP_FE(http_test, NULL)
76 PHP_FE(http_date, NULL)
77 PHP_FE(http_absolute_uri, NULL)
78 PHP_FE(http_negotiate_language, NULL)
79 PHP_FE(http_negotiate_charset, NULL)
80 PHP_FE(http_redirect, NULL)
81 PHP_FE(http_throttle, NULL)
82 PHP_FE(http_send_status, NULL)
83 PHP_FE(http_send_last_modified, NULL)
84 PHP_FE(http_send_content_type, NULL)
85 PHP_FE(http_send_content_disposition, NULL)
86 PHP_FE(http_match_modified, NULL)
87 PHP_FE(http_match_etag, NULL)
88 PHP_FE(http_cache_last_modified, NULL)
89 PHP_FE(http_cache_etag, NULL)
90 PHP_FE(http_send_data, NULL)
91 PHP_FE(http_send_file, NULL)
92 PHP_FE(http_send_stream, NULL)
93 PHP_FE(http_chunked_decode, NULL)
94 PHP_FE(http_parse_message, NULL)
95 PHP_FE(http_parse_headers, NULL)
96 PHP_FE(http_get_request_headers, NULL)
97 PHP_FE(http_get_request_body, NULL)
98 PHP_FE(http_match_request_header, NULL)
99 #ifdef HTTP_HAVE_CURL
100 PHP_FE(http_get, http_arg_pass_ref_3)
101 PHP_FE(http_head, http_arg_pass_ref_3)
102 PHP_FE(http_post_data, http_arg_pass_ref_4)
103 PHP_FE(http_post_fields, http_arg_pass_ref_5)
104 PHP_FE(http_put_file, http_arg_pass_ref_4)
105 PHP_FE(http_put_stream, http_arg_pass_ref_4)
106 #endif
107 PHP_FE(http_request_method_register, NULL)
108 PHP_FE(http_request_method_unregister, NULL)
109 PHP_FE(http_request_method_exists, NULL)
110 PHP_FE(http_request_method_name, NULL)
111 #ifndef ZEND_ENGINE_2
112 PHP_FE(http_build_query, NULL)
113 #endif
114 PHP_FE(ob_etaghandler, NULL)
115 {NULL, NULL, NULL}
116 };
117 /* }}} */
118
119 /* {{{ http_module_entry */
120 zend_module_entry http_module_entry = {
121 #if ZEND_MODULE_API_NO >= 20010901
122 STANDARD_MODULE_HEADER,
123 #endif
124 "http",
125 http_functions,
126 PHP_MINIT(http),
127 PHP_MSHUTDOWN(http),
128 PHP_RINIT(http),
129 PHP_RSHUTDOWN(http),
130 PHP_MINFO(http),
131 #if ZEND_MODULE_API_NO >= 20010901
132 HTTP_PEXT_VERSION,
133 #endif
134 STANDARD_MODULE_PROPERTIES
135 };
136 /* }}} */
137
138 int http_module_number;
139
140 /* {{{ http_globals */
141 static void http_globals_init_once(zend_http_globals *G)
142 {
143 memset(G, 0, sizeof(zend_http_globals));
144 }
145
146 static inline void http_globals_init(zend_http_globals *G)
147 {
148 G->send.buffer_size = HTTP_SENDBUF_SIZE;
149 zend_hash_init(&G->request.methods.custom, 0, NULL, ZVAL_PTR_DTOR, 0);
150 #ifdef HTTP_HAVE_CURL
151 zend_llist_init(&G->request.copies.strings, sizeof(char *), http_request_data_free_string, 0);
152 zend_llist_init(&G->request.copies.slists, sizeof(struct curl_slist *), http_request_data_free_slist, 0);
153 zend_llist_init(&G->request.copies.contexts, sizeof(http_curl_callback_ctx *), http_request_data_free_context, 0);
154 zend_llist_init(&G->request.copies.convs, sizeof(http_curl_conv *), http_request_data_free_conv, 0);
155 #endif
156 }
157
158 static inline void http_globals_free(zend_http_globals *G)
159 {
160 STR_SET(G->send.content_type, NULL);
161 STR_SET(G->send.unquoted_etag, NULL);
162 zend_hash_destroy(&G->request.methods.custom);
163 #ifdef HTTP_HAVE_CURL
164 zend_llist_clean(&G->request.copies.strings);
165 zend_llist_clean(&G->request.copies.slists);
166 zend_llist_clean(&G->request.copies.contexts);
167 zend_llist_clean(&G->request.copies.convs);
168 #endif
169 }
170 /* }}} */
171
172 /* {{{ static inline void http_check_allowed_methods(char *, int) */
173 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
174 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
175 {
176 if (length && SG(request_info).request_method) {
177 if (SUCCESS != http_check_method_ex(SG(request_info).request_method, methods)) {
178 char *header = emalloc(length + sizeof("Allow: "));
179 sprintf(header, "Allow: %s", methods);
180 http_exit(405, header);
181 }
182 }
183 }
184 /* }}} */
185
186 /* {{{ PHP_INI */
187 PHP_INI_MH(http_update_allowed_methods)
188 {
189 http_check_allowed_methods(new_value, new_value_length);
190 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
191 }
192
193 PHP_INI_DISP(http_etag_mode_displayer)
194 {
195 long value;
196
197 if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) {
198 value = (ini_entry->orig_value) ? atoi(ini_entry->orig_value) : HTTP_ETAG_MD5;
199 } else if (ini_entry->value) {
200 value = (ini_entry->value[0]) ? atoi(ini_entry->value) : HTTP_ETAG_MD5;
201 } else {
202 value = HTTP_ETAG_MD5;
203 }
204
205 switch (value)
206 {
207 case HTTP_ETAG_SHA1:
208 ZEND_WRITE("HTTP_ETAG_SHA1", lenof("HTTP_ETAG_SHA1"));
209 break;
210
211 case HTTP_ETAG_MD5:
212 #ifndef HTTP_HAVE_MHASH
213 default:
214 #endif
215 ZEND_WRITE("HTTP_ETAG_MD5", lenof("HTTP_ETAG_MD5"));
216 break;
217
218 #ifdef HTTP_HAVE_MHASH
219 default:
220 {
221 const char *hash_name = mhash_get_hash_name_static(value);
222
223 if (!hash_name) {
224 ZEND_WRITE("HTTP_ETAG_MD5", lenof("HTTP_ETAG_MD5"));
225 } else {
226 ZEND_WRITE("HTTP_ETAG_MHASH|MHASH_", lenof("HTTP_ETAG_MHASH|MHASH_"));
227 ZEND_WRITE(hash_name, strlen(hash_name));
228 }
229 }
230 break;
231 #endif
232 }
233 }
234
235 #ifndef ZEND_ENGINE_2
236 # define OnUpdateLong OnUpdateInt
237 #endif
238
239 PHP_INI_BEGIN()
240 HTTP_PHP_INI_ENTRY("http.allowed_methods", "", PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
241 HTTP_PHP_INI_ENTRY("http.cache_log", "", PHP_INI_ALL, OnUpdateString, log.cache)
242 HTTP_PHP_INI_ENTRY("http.redirect_log", "", PHP_INI_ALL, OnUpdateString, log.redirect)
243 HTTP_PHP_INI_ENTRY("http.allowed_methods_log", "", PHP_INI_ALL, OnUpdateString, log.allowed_methods)
244 HTTP_PHP_INI_ENTRY("http.composite_log", "", PHP_INI_ALL, OnUpdateString, log.composite)
245 #ifdef ZEND_ENGINE_2
246 HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL, OnUpdateBool, only_exceptions)
247 #endif
248 HTTP_PHP_INI_ENTRY_EX("http.etag_mode", "-2", PHP_INI_ALL, OnUpdateLong, http_etag_mode_displayer, etag.mode)
249 PHP_INI_END()
250 /* }}} */
251
252
253 /* {{{ PHP_MINIT_FUNCTION */
254 PHP_MINIT_FUNCTION(http)
255 {
256 http_module_number = module_number;
257
258 ZEND_INIT_MODULE_GLOBALS(http, http_globals_init_once, NULL)
259
260 REGISTER_INI_ENTRIES();
261
262 HTTP_LONG_CONSTANT("HTTP_ETAG_MD5", HTTP_ETAG_MD5);
263 HTTP_LONG_CONSTANT("HTTP_ETAG_SHA1", HTTP_ETAG_SHA1);
264 HTTP_LONG_CONSTANT("HTTP_ETAG_MHASH", HTTP_ETAG_MHASH);
265
266 #ifdef HTTP_HAVE_CURL
267 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
268 return FAILURE;
269 }
270 #endif /* HTTP_HAVE_CURL */
271
272 #ifdef ZEND_ENGINE_2
273 http_util_object_init();
274 http_message_object_init();
275 # ifndef WONKY
276 http_response_object_init();
277 # endif
278 # ifdef HTTP_HAVE_CURL
279 http_request_object_init();
280 http_requestpool_object_init();
281 # endif /* HTTP_HAVE_CURL */
282 http_exception_object_init();
283 #endif /* ZEND_ENGINE_2 */
284
285 return SUCCESS;
286 }
287 /* }}} */
288
289 /* {{{ PHP_MSHUTDOWN_FUNCTION */
290 PHP_MSHUTDOWN_FUNCTION(http)
291 {
292 UNREGISTER_INI_ENTRIES();
293 #ifdef HTTP_HAVE_CURL
294 curl_global_cleanup();
295 #endif
296 return SUCCESS;
297 }
298 /* }}} */
299
300 /* {{{ PHP_RINIT_FUNCTION */
301 PHP_RINIT_FUNCTION(http)
302 {
303 char *m;
304
305 if (m = INI_STR("http.allowed_methods")) {
306 http_check_allowed_methods(m, strlen(m));
307 }
308
309 http_globals_init(HTTP_GLOBALS);
310 return SUCCESS;
311 }
312 /* }}} */
313
314 /* {{{ PHP_RSHUTDOWN_FUNCTION */
315 PHP_RSHUTDOWN_FUNCTION(http)
316 {
317 http_globals_free(HTTP_GLOBALS);
318 return SUCCESS;
319 }
320 /* }}} */
321
322 /* {{{ PHP_MINFO_FUNCTION */
323 PHP_MINFO_FUNCTION(http)
324 {
325 php_info_print_table_start();
326 {
327 php_info_print_table_row(2, "Extended HTTP support:", "enabled");
328 php_info_print_table_row(2, "Extension Version:", HTTP_PEXT_VERSION);
329 #ifdef HTTP_HAVE_CURL
330 php_info_print_table_row(2, "cURL HTTP Requests:", curl_version());
331 #else
332 php_info_print_table_row(2, "cURL HTTP Requests:", "disabled");
333 #endif
334 #ifdef HTTP_HAVE_MHASH
335 {
336 char mhash_info[32];
337
338 snprintf(mhash_info, 32, "libmhash/%d", MHASH_API_VERSION);
339 php_info_print_table_row(2, "mhash ETag Generator:", mhash_info);
340 }
341 #else
342 php_info_print_table_row(2, "mhash ETag Generator:", "disabled");
343 #endif
344 #if defined(HTTP_HAVE_MAGIC) && !defined(WONKY)
345 php_info_print_table_row(2, "magic MIME Guessing:", "libmagic/unknown");
346 #else
347 php_info_print_table_row(2, "magic MIME Guessing:", "disabled");
348 #endif
349 php_info_print_table_row(2, "Registered Classes:",
350 #ifndef ZEND_ENGINE_2
351 "none"
352 #else
353 "HttpUtil, "
354 "HttpMessage, "
355 # ifdef HTTP_HAVE_CURL
356 "HttpRequest, "
357 "HttpRequestPool, "
358 # endif
359 # ifndef WONKY
360 "HttpResponse"
361 # endif
362 #endif
363 );
364 }
365 php_info_print_table_end();
366
367 php_info_print_table_start();
368 {
369 unsigned i;
370 zval **custom_method;
371 phpstr *known_request_methods = phpstr_new();
372 phpstr *custom_request_methods = phpstr_new();
373
374 for (i = HTTP_NO_REQUEST_METHOD+1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
375 phpstr_appendl(known_request_methods, http_request_method_name(i));
376 phpstr_appends(known_request_methods, ", ");
377 }
378 FOREACH_HASH_VAL(&HTTP_G(request).methods.custom, custom_method) {
379 phpstr_append(custom_request_methods, Z_STRVAL_PP(custom_method), Z_STRLEN_PP(custom_method));
380 phpstr_appends(custom_request_methods, ", ");
381 }
382
383 phpstr_append(known_request_methods, PHPSTR_VAL(custom_request_methods), PHPSTR_LEN(custom_request_methods));
384 phpstr_fix(known_request_methods);
385 phpstr_fix(custom_request_methods);
386
387 php_info_print_table_row(2, "Known Request Methods:", PHPSTR_VAL(known_request_methods));
388 php_info_print_table_row(2, "Custom Request Methods:",
389 PHPSTR_LEN(custom_request_methods) ? PHPSTR_VAL(custom_request_methods) : "none registered");
390
391 phpstr_free(&known_request_methods);
392 phpstr_free(&custom_request_methods);
393 }
394 php_info_print_table_end();
395
396 DISPLAY_INI_ENTRIES();
397 }
398 /* }}} */
399
400 /*
401 * Local variables:
402 * tab-width: 4
403 * c-basic-offset: 4
404 * End:
405 * vim600: noet sw=4 ts=4 fdm=marker
406 * vim<600: noet sw=4 ts=4
407 */
408