Skip to content

Commit

Permalink
support customized http headers and disable tornado Etag computing.
Browse files Browse the repository at this point in the history
assign header with add_header instead of set_header, because header may appear multiple times, eg 'Set-Cookie'
  • Loading branch information
boytm committed Oct 21, 2015
1 parent 40150f8 commit 1675c38
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions tornado_proxy/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import tornado.iostream
import tornado.web
import tornado.httpclient
import tornado.httputil

logger = logging.getLogger('tornado_proxy')

Expand Down Expand Up @@ -65,11 +66,14 @@ def fetch_request(url, callback, **kwargs):

req = tornado.httpclient.HTTPRequest(url, **kwargs)
client = tornado.httpclient.AsyncHTTPClient()
client.fetch(req, callback)
client.fetch(req, callback, raise_error=False)


class ProxyHandler(tornado.web.RequestHandler):
SUPPORTED_METHODS = ['GET', 'POST', 'CONNECT']

def compute_etag(self):
return None # disable tornado Etag

@tornado.web.asynchronous
def get(self):
Expand All @@ -82,23 +86,24 @@ def handle_response(response):
self.set_status(500)
self.write('Internal server error:\n' + str(response.error))
else:
self.set_status(response.code)
for header in ('Date', 'Cache-Control', 'Server','Content-Type', 'Location'):
v = response.headers.get(header)
if v:
self.set_header(header, v)
v = response.headers.get_list('Set-Cookie')
if v:
for i in v:
self.add_header('Set-Cookie', i)
if response.body:
self.set_status(response.code, response.reason)
self._headers = tornado.httputil.HTTPHeaders() # clear tornado default header

for header, v in response.headers.get_all():
if header not in ('Content-Length', 'Transfer-Encoding', 'Content-Encoding', 'Connection'):
self.add_header(header, v) # some header appear multiple times, eg 'Set-Cookie'

if response.body:
self.set_header('Content-Length', len(response.body))
self.write(response.body)
self.finish()

body = self.request.body
if not body:
body = None
try:
if 'Proxy-Connection' in self.request.headers:
del self.request.headers['Proxy-Connection']
fetch_request(
self.request.uri, handle_response,
method=self.request.method, body=body,
Expand Down

0 comments on commit 1675c38

Please sign in to comment.