Skip to content

Instantly share code, notes, and snippets.

@eliasdorneles
Last active September 10, 2015 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasdorneles/ffd12f05811fee803fa0 to your computer and use it in GitHub Desktop.
Save eliasdorneles/ffd12f05811fee803fa0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, make_response, redirect, request
app = Flask(__name__)
INDEX_TEMPLATE = u"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>redirectests</title>
</head>
<body>{body}</body>
</html>
"""
@app.route('/')
def index():
urls = [
'/path_redir_utf8',
'/path_redir_latin1',
'/query_redirect_utf8',
'/query_redirect_latin1',
]
body = u''.join([
u'<div><a href="{href}">{href}</a></div>'.format(href=href)
for href in urls])
return INDEX_TEMPLATE.format(body=body)
@app.route("/redir")
def redir():
dest = request.args.get('to', u'/ação')
return redirect(dest)
@app.route("/path_redir_utf8")
def path_redir_utf8():
response = make_response('')
response.headers['Location'] = u'/ação'.encode('utf-8')
response.status = '302'
return response
@app.route("/path_redir_latin1")
def path_redir_latin1():
response = make_response('')
response.headers['Location'] = u'/ação'.encode('latin1')
response.status = '302'
return response
@app.route("/query_redirect_utf8")
def query_redirect_utf8():
response = make_response('')
response.headers['Location'] = u'/something?q=ação'.encode('utf-8')
response.status = '302'
return response
@app.route("/query_redirect_latin1")
def query_redirect_latin1():
response = make_response('')
response.headers['Location'] = u'/something?q=ação'.encode('latin1')
response.status = '302'
return response
@app.route("/<path>")
def whatever(path):
return u"Path: {path} ({path!r})<br />Args: {data!r}".format(
path=path, data=request.args.items())
if __name__ == "__main__":
app.debug = True
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment