From f2b42ae72c36db7e4090771cd272afb3cfd4a126 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Thu, 24 Sep 2015 15:03:24 -0500 Subject: [PATCH 1/4] Allow cross-origin patches for config --- notebook/services/config/handlers.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/notebook/services/config/handlers.py b/notebook/services/config/handlers.py index 017c2c8ec..906602f2e 100644 --- a/notebook/services/config/handlers.py +++ b/notebook/services/config/handlers.py @@ -12,7 +12,7 @@ from ipython_genutils.py3compat import PY3 from ...base.handlers import APIHandler, json_errors class ConfigHandler(APIHandler): - SUPPORTED_METHODS = ('GET', 'PUT', 'PATCH') + SUPPORTED_METHODS = ('GET', 'PUT', 'PATCH', 'OPTIONS') @web.authenticated @json_errors @@ -34,6 +34,13 @@ class ConfigHandler(APIHandler): section = self.config_manager.update(section_name, new_data) self.finish(json.dumps(section)) + @web.authenticated + @json_errors + def options(self, section_name): + self.set_header('Access-Control-Allow-Headers', 'accept, content-type') + self.set_header('Access-Control-Allow-Methods', 'GET, PUT, PATCH') + self.finish() + # URL to handler mappings From 88c067ff2702eb0b36c522e870db229e17ce4fb4 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Thu, 24 Sep 2015 15:55:04 -0500 Subject: [PATCH 2/4] Clean up allowed methods --- notebook/services/api/handlers.py | 7 +++++++ notebook/services/config/handlers.py | 8 -------- notebook/services/contents/handlers.py | 6 ------ notebook/services/kernels/handlers.py | 14 -------------- notebook/services/kernelspecs/handlers.py | 7 ------- notebook/services/nbconvert/handlers.py | 3 +-- notebook/services/sessions/handlers.py | 7 ------- 7 files changed, 8 insertions(+), 44 deletions(-) diff --git a/notebook/services/api/handlers.py b/notebook/services/api/handlers.py index c13b4721b..e89e74d38 100644 --- a/notebook/services/api/handlers.py +++ b/notebook/services/api/handlers.py @@ -18,6 +18,13 @@ class APIHandler(web.StaticFileHandler, IPythonHandler): self.set_header('Content-Type', 'text/x-yaml') return web.StaticFileHandler.get(self, 'api.yaml') + @web.authenticated + def options(self, section_name): + self.set_header('Access-Control-Allow-Headers', 'accept, content-type') + self.set_header('Access-Control-Allow-Methods', + 'GET, PUT, POST, PATCH, DELETE, OPTIONS') + self.finish() + default_handlers = [ (r"/api/spec.yaml", APIHandler) ] diff --git a/notebook/services/config/handlers.py b/notebook/services/config/handlers.py index 906602f2e..765646479 100644 --- a/notebook/services/config/handlers.py +++ b/notebook/services/config/handlers.py @@ -12,7 +12,6 @@ from ipython_genutils.py3compat import PY3 from ...base.handlers import APIHandler, json_errors class ConfigHandler(APIHandler): - SUPPORTED_METHODS = ('GET', 'PUT', 'PATCH', 'OPTIONS') @web.authenticated @json_errors @@ -34,13 +33,6 @@ class ConfigHandler(APIHandler): section = self.config_manager.update(section_name, new_data) self.finish(json.dumps(section)) - @web.authenticated - @json_errors - def options(self, section_name): - self.set_header('Access-Control-Allow-Headers', 'accept, content-type') - self.set_header('Access-Control-Allow-Methods', 'GET, PUT, PATCH') - self.finish() - # URL to handler mappings diff --git a/notebook/services/contents/handlers.py b/notebook/services/contents/handlers.py index 521aae5fd..50c4c85a7 100644 --- a/notebook/services/contents/handlers.py +++ b/notebook/services/contents/handlers.py @@ -77,8 +77,6 @@ def validate_model(model, expect_content): class ContentsHandler(APIHandler): - SUPPORTED_METHODS = (u'GET', u'PUT', u'PATCH', u'POST', u'DELETE') - def location_url(self, path): """Return the full URL location of a file. @@ -259,8 +257,6 @@ class ContentsHandler(APIHandler): class CheckpointsHandler(APIHandler): - SUPPORTED_METHODS = ('GET', 'POST') - @web.authenticated @json_errors @gen.coroutine @@ -288,8 +284,6 @@ class CheckpointsHandler(APIHandler): class ModifyCheckpointsHandler(APIHandler): - SUPPORTED_METHODS = ('POST', 'DELETE') - @web.authenticated @json_errors @gen.coroutine diff --git a/notebook/services/kernels/handlers.py b/notebook/services/kernels/handlers.py index 863aa7fd8..c1fb81231 100644 --- a/notebook/services/kernels/handlers.py +++ b/notebook/services/kernels/handlers.py @@ -51,8 +51,6 @@ class MainKernelHandler(APIHandler): class KernelHandler(APIHandler): - SUPPORTED_METHODS = ('DELETE', 'GET', 'OPTIONS') - @web.authenticated @json_errors def get(self, kernel_id): @@ -69,12 +67,6 @@ class KernelHandler(APIHandler): self.set_status(204) self.finish() - @web.authenticated - @json_errors - def options(self, kernel_id): - self.set_header('Access-Control-Allow-Headers', 'accept, content-type') - self.finish() - class KernelActionHandler(APIHandler): @@ -92,12 +84,6 @@ class KernelActionHandler(APIHandler): self.write(json.dumps(model)) self.finish() - @web.authenticated - @json_errors - def options(self, kernel_id, action): - self.set_header('Access-Control-Allow-Headers', 'accept, content-type') - self.finish() - class ZMQChannelsHandler(AuthenticatedZMQStreamHandler): diff --git a/notebook/services/kernelspecs/handlers.py b/notebook/services/kernelspecs/handlers.py index c033e7ce7..bef4aff15 100644 --- a/notebook/services/kernelspecs/handlers.py +++ b/notebook/services/kernelspecs/handlers.py @@ -44,7 +44,6 @@ def kernelspec_model(handler, name): return d class MainKernelSpecHandler(APIHandler): - SUPPORTED_METHODS = ('GET', 'OPTIONS') @web.authenticated @json_errors @@ -64,14 +63,8 @@ class MainKernelSpecHandler(APIHandler): self.set_header("Content-Type", 'application/json') self.finish(json.dumps(model)) - @web.authenticated - @json_errors - def options(self): - self.finish() - class KernelSpecHandler(APIHandler): - SUPPORTED_METHODS = ('GET',) @web.authenticated @json_errors diff --git a/notebook/services/nbconvert/handlers.py b/notebook/services/nbconvert/handlers.py index 7b55bdc2b..c8da3cfc4 100644 --- a/notebook/services/nbconvert/handlers.py +++ b/notebook/services/nbconvert/handlers.py @@ -5,7 +5,6 @@ from tornado import web from ...base.handlers import APIHandler, json_errors class NbconvertRootHandler(APIHandler): - SUPPORTED_METHODS = ('GET',) @web.authenticated @json_errors @@ -23,4 +22,4 @@ class NbconvertRootHandler(APIHandler): default_handlers = [ (r"/api/nbconvert", NbconvertRootHandler), -] \ No newline at end of file +] diff --git a/notebook/services/sessions/handlers.py b/notebook/services/sessions/handlers.py index 888d518e9..4f8831348 100644 --- a/notebook/services/sessions/handlers.py +++ b/notebook/services/sessions/handlers.py @@ -68,16 +68,9 @@ class SessionRootHandler(APIHandler): self.set_status(201) self.finish(json.dumps(model, default=date_default)) - @web.authenticated - @json_errors - def options(self): - self.set_header('Access-Control-Allow-Headers', 'accept, content-type') - self.finish() class SessionHandler(APIHandler): - SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE') - @web.authenticated @json_errors def get(self, session_id): From 1f6a3700a22cc798ec52a9c28fd87a75ce7819f7 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Thu, 24 Sep 2015 16:04:49 -0500 Subject: [PATCH 3/4] Fix signature --- notebook/services/api/handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebook/services/api/handlers.py b/notebook/services/api/handlers.py index e89e74d38..4600d6d41 100644 --- a/notebook/services/api/handlers.py +++ b/notebook/services/api/handlers.py @@ -19,7 +19,7 @@ class APIHandler(web.StaticFileHandler, IPythonHandler): return web.StaticFileHandler.get(self, 'api.yaml') @web.authenticated - def options(self, section_name): + def options(self, *args, **kwargs): self.set_header('Access-Control-Allow-Headers', 'accept, content-type') self.set_header('Access-Control-Allow-Methods', 'GET, PUT, POST, PATCH, DELETE, OPTIONS') From 65eb248209b5fdc9d1d430c0403fc6bfee1057f4 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Thu, 24 Sep 2015 16:21:52 -0500 Subject: [PATCH 4/4] Add the method to the correct base class --- notebook/base/handlers.py | 7 +++++++ notebook/services/api/handlers.py | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/notebook/base/handlers.py b/notebook/base/handlers.py index 8b2cbc99f..993dd0151 100644 --- a/notebook/base/handlers.py +++ b/notebook/base/handlers.py @@ -386,6 +386,13 @@ class APIHandler(IPythonHandler): self.set_header('Content-Type', 'application/json') return super(APIHandler, self).finish(*args, **kwargs) + @web.authenticated + def options(self, *args, **kwargs): + self.set_header('Access-Control-Allow-Headers', 'accept, content-type') + self.set_header('Access-Control-Allow-Methods', + 'GET, PUT, PATCH, DELETE, OPTIONS') + self.finish() + class Template404(IPythonHandler): """Render our 404 template""" diff --git a/notebook/services/api/handlers.py b/notebook/services/api/handlers.py index 4600d6d41..c13b4721b 100644 --- a/notebook/services/api/handlers.py +++ b/notebook/services/api/handlers.py @@ -18,13 +18,6 @@ class APIHandler(web.StaticFileHandler, IPythonHandler): self.set_header('Content-Type', 'text/x-yaml') return web.StaticFileHandler.get(self, 'api.yaml') - @web.authenticated - def options(self, *args, **kwargs): - self.set_header('Access-Control-Allow-Headers', 'accept, content-type') - self.set_header('Access-Control-Allow-Methods', - 'GET, PUT, POST, PATCH, DELETE, OPTIONS') - self.finish() - default_handlers = [ (r"/api/spec.yaml", APIHandler) ]