Merge pull request #89 from rcarz/master

Added redirect-to endpoint
This commit is contained in:
Kenneth Reitz
2013-05-30 13:14:02 -07:00
4 changed files with 23 additions and 0 deletions
+1
View File
@@ -17,5 +17,6 @@ Patches and Suggestions
- Flavio Percoco
- Radomir Stevanovic (http://github.com/randomir)
- Steven Honson
- Bob Carroll <bob.carroll@alum.rit.edu> @rcarz
- Cory Benfield (Lukasa) <cory@lukasa.co.uk>
- Matt Robenolt (https://github.com/mattrobenolt)
+1
View File
@@ -19,6 +19,7 @@ Freely hosted in [HTTP](http://httpbin.org) &
- [`/status/:code`](http://httpbin.org/status/418) Returns given HTTP Status code.
- [`/response-headers?key=val`](http://httpbin.org/response-headers?Content-Type=text/plain;%20charset=UTF-8&Server=httpbin) Returns given response headers.
- [`/redirect/:n`](http://httpbin.org/redirect/6) 302 Redirects *n* times.
- [`/redirect-to?url=foo`](http://httpbin.org/redirect-to?url=http://example.com/) 302 Redirects to the *foo* URL.
- [`/relative-redirect/:n`](http://httpbin.org/relative-redirect/6) 302 Relative redirects *n* times.
- [`/cookies`](http://httpbin.org/cookies) Returns cookie data.
- [`/cookies/set?name=value`](http://httpbin.org/cookies/set?k1=v1&k2=v2) Sets one or more simple cookies.
+20
View File
@@ -17,6 +17,7 @@ from flask import Flask, Response, request, render_template, redirect, jsonify,
from raven.contrib.flask import Sentry
from werkzeug.datastructures import WWWAuthenticate
from werkzeug.http import http_date
from werkzeug.wrappers import BaseResponse
from . import filters
from .helpers import get_headers, status_code, get_dict, check_basic_auth, check_digest_auth, H, ROBOT_TXT, ANGRY_ASCII
@@ -34,6 +35,9 @@ ENV_COOKIES = (
'__utmb'
)
# Prevent WSGI from correcting the casing of the Location header
BaseResponse.autocorrect_location_header = False
app = Flask(__name__)
# Setup error collection
@@ -160,6 +164,22 @@ def redirect_n_times(n):
return redirect('/redirect/{0}'.format(n - 1))
@app.route('/redirect-to')
def redirect_to():
"""302 Redirects to the given URL."""
args = CaseInsensitiveDict(request.args.items())
# We need to build the response manually and convert to UTF-8 to prevent
# werkzeug from "fixing" the URL. This endpoint should set the Location
# header to the exact string supplied.
response = app.make_response('')
response.status_code = 302
response.headers['Location'] = args['url'].encode('utf-8')
return response
@app.route('/relative-redirect/<int:n>')
def relative_redirect_n_times(n):
"""301 Redirects n times."""
+1
View File
@@ -20,6 +20,7 @@
<li><a href="/status/418"><code>/status/:code</code></a> Returns given HTTP Status code.</li>
<li><a href="/response-headers?Content-Type=text/plain;%20charset=UTF-8&amp;Server=httpbin"><code>/response-headers?key=val</code></a> Returns given response headers.</li>
<li><a href="/redirect/6"><code>/redirect/:n</code></a> 302 Redirects <em>n</em> times.</li>
<li><a href="/redirect-to?url=http://example.com/"><code>/redirect-to?url=foo</code></a> 302 Redirects to the <em>foo</em> URL.</li>
<li><a href="/relative-redirect/6"><code>/relative-redirect/:n</code></a> 302 Relative redirects <em>n</em> times.</li>
<li><a href="/cookies" data-bare-link="true"><code>/cookies</code></a> Returns cookie data.</li>
<li><a href="/cookies/set?k1=v1&amp;k2=v2"><code>/cookies/set?name=value</code></a> Sets one or more simple cookies.</li>