Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure content-type properly reflects gateway kernelspec resources #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion jupyter_server/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,9 @@ def update_api_activity(self):
def finish(self, *args, **kwargs):
"""Finish an API response."""
self.update_api_activity()
self.set_header("Content-Type", "application/json")
# Allow caller to indicate content-type...
set_content_type = kwargs.pop("set_content_type", "application/json")
self.set_header("Content-Type", set_content_type)
return super().finish(*args, **kwargs)

def options(self, *args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions jupyter_server/gateway/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import mimetypes
import os
import random
from typing import cast
from typing import Optional, cast

from jupyter_client.session import Session
from tornado import web
Expand Down Expand Up @@ -281,6 +281,7 @@ class GatewayResourceHandler(APIHandler):
@web.authenticated
async def get(self, kernel_name, path, include_body=True):
"""Get a gateway resource by name and path."""
mimetype: Optional[str] = None
ksm = self.kernel_spec_manager
kernel_spec_res = await ksm.get_kernel_spec_resource(kernel_name, path)
if kernel_spec_res is None:
Expand All @@ -290,8 +291,7 @@ async def get(self, kernel_name, path, include_body=True):
)
else:
mimetype = mimetypes.guess_type(path)[0] or "text/plain"
self.set_header("Content-Type", mimetype)
self.finish(kernel_spec_res)
self.finish(kernel_spec_res, set_content_type=mimetype)


from ..services.kernels.handlers import _kernel_id_regex
Expand Down
3 changes: 2 additions & 1 deletion tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,10 @@ async def test_gateway_get_named_kernelspec(init_gateway, jp_fetch):
kspec_foo = json.loads(r.body.decode("utf-8"))
assert kspec_foo.get("name") == "kspec_foo"

r = await jp_fetch("kernelspecs", "kspec_foo", "hi", method="GET")
r = await jp_fetch("kernelspecs", "kspec_foo", "logo-64x64.png", method="GET")
assert r.code == 200
assert r.body == b"foo"
assert r.headers["content-type"] == "image/png"

with pytest.raises(tornado.httpclient.HTTPClientError) as e:
await jp_fetch("api", "kernelspecs", "no_such_spec", method="GET")
Expand Down