Skip to content

Commit

Permalink
Merge pull request #5 from michael-lazar/url_redirect_quuery_params
Browse files Browse the repository at this point in the history
Fix HTTP redirects when the URL has query params
  • Loading branch information
michael-lazar authored Apr 10, 2019
2 parents d4f56f7 + 20eade7 commit 46a3691
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### UNRELEASED

- Add a helper method to generate a gopher menu from a directory.
- Fixed bug when redirecting to HTTP pages that was causing query parameters
to get lost.

### v2.1.0 (2019-01-08)

- Added support for establishing TLS connections with gopher clients. See the
Expand Down
8 changes: 5 additions & 3 deletions flask_gopher/flask_gopher.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
from flask.helpers import safe_join, send_file
from flask.sessions import SecureCookieSessionInterface, SecureCookieSession
from werkzeug.local import LocalProxy
from werkzeug.urls import url_quote
from werkzeug.serving import WSGIRequestHandler, can_fork
from werkzeug.serving import BaseWSGIServer, ThreadingMixIn, ForkingMixIn
from werkzeug.serving import generate_adhoc_ssl_context, load_ssl_context

from werkzeug.exceptions import HTTPException, BadRequest
from jinja2.filters import escape
from itsdangerous import URLSafeSerializer, BadSignature

from .__version__ import __version__
Expand Down Expand Up @@ -433,7 +432,10 @@ def _add_gopher_url_redirect(self, app):
"""
@app.route('/URL:<path:url>')
def gopher_url_redirect(url):
url = url_quote(url)
# Use the full_path because it keeps any query params intact
url = request.full_path.split(':', 1)[1] # Drop the "/URL:"
url = url.rstrip('?') # Flask adds an ? even if there are no params
url = escape(url)
return self.URL_REDIRECT_TEMPLATE.format(url=url).strip()

def _add_gopher_error_handler(self, app):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_flask_gopher.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,19 @@ def test_url_redirect(self):
href = b'<A HREF="https://gopher.floodgap.com">https://gopher.floodgap.com</A>'
self.assertIn(href, resp)

def test_url_redirect_with_query_params(self):
"""
Selectors starting with URL:<path> should preserve the URL query params.
"""
url = 'https://gopher.floodgap.com?foo=b"r&foz=baz'
escaped_url = 'https://gopher.floodgap.com?foo=b&#34;r&amp;foz=baz'

resp = self.send_data('/URL:{}\r\n'.format(url).encode())
self.assertTrue(resp.startswith(b'<HTML>'))
self.assertTrue(resp.endswith(b'</HTML>'))

self.assertIn('<A HREF="{0}">{0}</A>'.format(escaped_url).encode(), resp)

def test_http_get(self):
"""
Regular HTTP requests should still work and headers should be passed
Expand Down

0 comments on commit 46a3691

Please sign in to comment.