Skip to content

Commit

Permalink
Merge pull request jupyter#481 from bollwyvl/double-encode-redirect
Browse files Browse the repository at this point in the history
Fixing URL double-encode, dropbox-specifics
  • Loading branch information
rgbkrk committed Jul 28, 2015
2 parents 8aee4d4 + 17aebb6 commit 404771b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
20 changes: 17 additions & 3 deletions nbviewer/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
try:
# py3
from http.client import responses
from urllib.parse import urlparse
from urllib.parse import urlparse, urlunparse
except ImportError:
from httplib import responses
from urlparse import urlparse
from urlparse import urlparse, urlunparse

from tornado import (
gen,
Expand Down Expand Up @@ -66,8 +66,22 @@ def initialize(self, format=None, format_prefix=""):

# Overloaded methods
def redirect(self, url, *args, **kwargs):
purl = urlparse(url)

eurl = urlunparse((
purl.scheme,
purl.netloc,
"/".join([
url_escape(url_unescape(p), plus=False)
for p in purl.path.split("/")
]),
purl.params,
purl.query,
purl.fragment
))

return super(BaseHandler, self).redirect(
"/".join(map(url_escape, url.split("/"))),
eurl,
*args,
**kwargs
)
Expand Down
2 changes: 1 addition & 1 deletion nbviewer/providers/dropbox/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

def uri_rewrites(rewrites=[]):
return rewrites + [
(r'^http(s?)://www.dropbox.com/(sh?)/(.+)$',
(r'^http(s?)://www.dropbox.com/(sh?)/(.+?)(\?dl=.)*$',
u'/url{0}/dl.dropbox.com/{1}/{2}'),
]
9 changes: 6 additions & 3 deletions nbviewer/providers/url/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
web,
)
from tornado.log import app_log
from tornado.escape import url_unescape

from ...utils import (
quote,
Expand All @@ -35,15 +36,17 @@ class URLHandler(RenderingHandler):
"""Renderer for /url or /urls"""
@cached
@gen.coroutine
def get(self, secure, url):
def get(self, secure, netloc, url):
proto = 'http' + secure
netloc = url_unescape(netloc)

if '/?' in url:
url, query = url.rsplit('/?', 1)
else:
query = None

remote_url = u"{}://{}".format(proto, quote(url))
remote_url = u"{}://{}/{}".format(proto, netloc, quote(url))

if query:
remote_url = remote_url + '?' + query
if not url.endswith('.ipynb'):
Expand Down Expand Up @@ -95,7 +98,7 @@ def default_handlers(handlers=[]):
"""Tornado handlers"""

return handlers + [
(r'/url([s]?)/(.*)', URLHandler),
(r'/url([s]?)/([^/]+)/(.*)', URLHandler),
]


Expand Down
2 changes: 1 addition & 1 deletion nbviewer/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_transform_ipynb_uri():
u'/url/dl.dropbox.com/s/bar/baz.qux'),
( u'https://www.dropbox.com/s/zip/baz.qux',
u'/urls/dl.dropbox.com/s/zip/baz.qux'),
( u'https://www.dropbox.com/sh/mhviow274da2wly/CZKwRRcA0k/nested/furthernested/User%2520Interface.ipynb',
( u'https://www.dropbox.com/sh/mhviow274da2wly/CZKwRRcA0k/nested/furthernested/User%2520Interface.ipynb?dl=1',
u'/urls/dl.dropbox.com/sh/mhviow274da2wly/CZKwRRcA0k/nested/furthernested/User%2520Interface.ipynb'),
# URL
('https://example.org/ipynb',
Expand Down
13 changes: 8 additions & 5 deletions nbviewer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ def transform_ipynb_uri(value, rewrite_providers=None):
rewrite_providers = rewrite_providers or default_rewrites
uri_rewrite_dict.update(provider_uri_rewrites(rewrite_providers))

for reg, rewrite in uri_rewrite_dict.items():
matches = re.match(reg, value)
if matches:
value = rewrite.format(*matches.groups())
break

# encode query parameters as last url part
if '?' in value:
value, query = value.split('?', 1)
value = '%s/%s' % (value, quote('?' + query))

for reg, rewrite in uri_rewrite_dict.items():
matches = re.match(reg, value)
if matches:
return rewrite.format(*matches.groups())

return value

# get_encoding_from_headers from requests.utils (1.2.3)
# (c) 2013 Kenneth Reitz
Expand Down

0 comments on commit 404771b

Please sign in to comment.