Commit e94c8463 by Sheng

Added default handler NotFoundHandler

parent 499f3b6d
...@@ -497,8 +497,13 @@ class TestAppInDebug(OtherTestBase): ...@@ -497,8 +497,13 @@ class TestAppInDebug(OtherTestBase):
self.assertEqual(response.code, 500) self.assertEqual(response.code, 500)
self.assertIn(b'Uncaught exception', response.body) self.assertIn(b'Uncaught exception', response.body)
def test_server_error(self): def test_server_error_for_post_method(self):
response = self.fetch('/?error=generate', method='GET') response = self.fetch(
'/',
method='POST',
body=urlencode(dict(self.body, error='raise')),
headers=self.headers
)
self.assert_response(b'"status": "Internal Server Error"', response) self.assert_response(b'"status": "Internal Server Error"', response)
def test_html(self): def test_html(self):
......
...@@ -54,11 +54,15 @@ class MixinHandler(object): ...@@ -54,11 +54,15 @@ class MixinHandler(object):
lst = context.trusted_downstream lst = context.trusted_downstream
if lst and ip not in lst: if lst and ip not in lst:
logging.info(
'IP {!r} not found in trusted downstream {!r}'.format(ip, lst)
)
return True return True
if context._orig_protocol == 'http': if context._orig_protocol == 'http':
ipaddr = to_ip_address(ip) ipaddr = to_ip_address(ip)
if not ipaddr.is_private: if not ipaddr.is_private:
logging.info('Public non-https request is forbidden.')
return True return True
def set_default_headers(self): def set_default_headers(self):
...@@ -93,6 +97,16 @@ class MixinHandler(object): ...@@ -93,6 +97,16 @@ class MixinHandler(object):
return (ip, port) return (ip, port)
class NotFoundHandler(MixinHandler, tornado.web.ErrorHandler):
def initialize(self):
pass
def prepare(self):
super(NotFoundHandler, self).prepare()
raise tornado.web.HTTPError(404)
class IndexHandler(MixinHandler, tornado.web.RequestHandler): class IndexHandler(MixinHandler, tornado.web.RequestHandler):
def initialize(self, loop, policy, host_keys_settings): def initialize(self, loop, policy, host_keys_settings):
...@@ -101,11 +115,12 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): ...@@ -101,11 +115,12 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
self.host_keys_settings = host_keys_settings self.host_keys_settings = host_keys_settings
self.ssh_client = self.get_ssh_client() self.ssh_client = self.get_ssh_client()
self.privatekey_filename = None self.privatekey_filename = None
self.debug = self.settings.get('debug', False)
self.result = dict(id=None, status=None, encoding=None) self.result = dict(id=None, status=None, encoding=None)
def write_error(self, status_code, **kwargs): def write_error(self, status_code, **kwargs):
if not swallow_http_errors: if self.request.method != 'POST' or not swallow_http_errors:
super(MixinHandler, self).write_error(status_code, **kwargs) super(IndexHandler, self).write_error(status_code, **kwargs)
else: else:
exc_info = kwargs.get('exc_info') exc_info = kwargs.get('exc_info')
if exc_info: if exc_info:
...@@ -269,13 +284,14 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): ...@@ -269,13 +284,14 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
future.set_result(worker) future.set_result(worker)
def get(self): def get(self):
debug = self.settings.get('debug', False) self.render('index.html', debug=self.debug)
if debug and self.get_argument('error', u''):
raise ValueError('Uncaught exception')
self.render('index.html', debug=debug)
@tornado.gen.coroutine @tornado.gen.coroutine
def post(self): def post(self):
if self.debug and self.get_argument('error', u''):
# for testing purpose only
raise ValueError('Uncaught exception')
future = Future() future = Future()
t = threading.Thread(target=self.ssh_connect_wrapped, args=(future,)) t = threading.Thread(target=self.ssh_connect_wrapped, args=(future,))
t.setDaemon(True) t.setDaemon(True)
......
...@@ -3,7 +3,7 @@ import tornado.web ...@@ -3,7 +3,7 @@ import tornado.web
import tornado.ioloop import tornado.ioloop
from tornado.options import options from tornado.options import options
from webssh.handler import IndexHandler, WsockHandler from webssh.handler import IndexHandler, WsockHandler, NotFoundHandler
from webssh.settings import ( from webssh.settings import (
get_app_settings, get_host_keys_settings, get_policy_setting, get_app_settings, get_host_keys_settings, get_policy_setting,
get_ssl_context, get_server_settings get_ssl_context, get_server_settings
...@@ -23,6 +23,7 @@ def make_handlers(loop, options): ...@@ -23,6 +23,7 @@ def make_handlers(loop, options):
def make_app(handlers, settings): def make_app(handlers, settings):
settings.update(default_handler_class=NotFoundHandler)
return tornado.web.Application(handlers, **settings) return tornado.web.Application(handlers, **settings)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment