Skip to content

Commit

Permalink
Check if nbviewer URL would show an error
Browse files Browse the repository at this point in the history
If the nbviewer widget would show a 404 or other error we don't want to
display it. We check the status before rendering the template by making
a HEAD request to nbviewer, if the status code is >=400 we don't show
the preview.
  • Loading branch information
betatim committed Aug 20, 2019
1 parent eb1fc66 commit ed7fcfd
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions binderhub/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Main handler classes for requests
"""
from tornado.httpclient import AsyncHTTPClient
from tornado.web import HTTPError, authenticated
from tornado.httputil import url_concat
from tornado.log import app_log
Expand All @@ -15,6 +16,7 @@
"zenodo": "Zenodo"
}


class MainHandler(BaseHandler):
"""Main handler for requests"""

Expand All @@ -35,7 +37,7 @@ class ParameterizedMainHandler(BaseHandler):
"""Main handler that allows different parameter settings"""

@authenticated
def get(self, provider_prefix, _unescaped_spec):
async def get(self, provider_prefix, _unescaped_spec):
prefix = '/v2/' + provider_prefix
spec = self.get_spec_from_request(prefix)
spec = spec.rstrip("/")
Expand All @@ -61,14 +63,25 @@ def get(self, provider_prefix, _unescaped_spec):
org, repo_name, ref = spec.split('/', 2)
# NOTE: tornado unquotes query arguments too -> notebooks%2Findex.ipynb becomes notebooks/index.ipynb
filepath = self.get_argument('filepath', '').lstrip('/')

# Check if we have a JupyterLab + file path, if so then use it for the filepath
urlpath = self.get_argument('urlpath', '').lstrip('/')
if urlpath.startswith("lab") and "/tree/" in urlpath:
filepath = urlpath.split('tree/', 1)[-1]

blob_or_tree = 'blob' if filepath else 'tree'
nbviewer_url = f'{nbviewer_url}/{org}/{repo_name}/{blob_or_tree}/{ref}/{filepath}'

# Check if the nbviewer URL is valid and would display something
# useful to the reader, if not we don't show it
client = AsyncHTTPClient()
response = await client.fetch(nbviewer_url,
method="HEAD",
user_agent="BinderHub",
raise_error=False)
if response.code >= 400:
nbviewer_url = None

self.render_template(
"loading.html",
base_url=self.settings['base_url'],
Expand Down

0 comments on commit ed7fcfd

Please sign in to comment.