Skip to content
This repository was archived by the owner on Sep 22, 2020. It is now read-only.

Commit 4bb9c2c

Browse files
fix response on status codes unequal to 200
1 parent badcc05 commit 4bb9c2c

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

cs_proxy/proxy.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys, os
22
import argparse
33
import colorama
4-
from bottle import route, view, request, response, run, hook, abort, redirect, error, install, auth_basic, template
4+
from bottle import route, view, request, response, run, hook, abort, redirect, error, install, auth_basic, template, HTTPResponse
55
import simplejson as json
66
import random
77
import logging
@@ -77,7 +77,7 @@ def main():
7777
<div style="background:#99d100;padding:20px;color:#fff">&#10003; Proxy is running fine.</div>""" + default_footer_tpl
7878

7979
error_tpl = default_header_tpl + """
80-
<div style="background:#bf1101;padding:20px;color:#fff">&#10008; {{error.body}}</div>""" + default_footer_tpl
80+
<div style="background:#bf1101;padding:20px;color:#fff">&#10008; {{error}}</div>""" + default_footer_tpl
8181

8282
install_success_tpl = default_header_tpl + """
8383
<div style="background:#99d100;padding:20px;color:#fff">&#10003; Installation done.</div>
@@ -149,13 +149,21 @@ def normalize_proxy_url(url):
149149
def proxy_trough_helper(url):
150150
print ('PROXY-GET: {0}'.format(url))
151151
proxy_response = requests.get(url)
152-
if hasattr(proxy_response.headers, 'Last-Modified'):
153-
response.set_header('Last-Modified', proxy_response.headers['Last-Modified'])
154-
if hasattr(proxy_response.headers, 'Content-Type'):
155-
response.set_header('Content-Type', proxy_response.headers['Content-Type'])
156-
if hasattr(proxy_response.headers, 'Expires'):
157-
response.set_header('Expires', proxy_response.headers['Expires'])
158-
return proxy_response
152+
if proxy_response.status_code == 200:
153+
if proxy_response.headers['Last-Modified']:
154+
response.set_header('Last-Modified', proxy_response.headers['Last-Modified'])
155+
if proxy_response.headers['Content-Type']:
156+
response.set_header('Content-Type', proxy_response.headers['Content-Type'])
157+
if proxy_response.headers['Expires']:
158+
response.set_header('Expires', proxy_response.headers['Expires'])
159+
return proxy_response
160+
else:
161+
return HTTPResponse(status=proxy_response.status_code,
162+
body=template(error_tpl,
163+
headline='Error {0}'.format(proxy_response.status_code),
164+
error='error during proxy call'))
165+
166+
159167

160168

161169
#
@@ -168,11 +176,11 @@ def run_proxy(args):
168176
#
169177
@error(401)
170178
def error404(error):
171-
return template(error_tpl, headline='Error '+error.status, error=error)
179+
return template(error_tpl, headline='Error '+error.status, error=error.body)
172180

173181
@error(500)
174182
def error500(error):
175-
return template(error_tpl, headline='Error '+error.status, error=error)
183+
return template(error_tpl, headline='Error '+error.status, error=error.body)
176184

177185
#
178186
# SPECIAL ENDPOINTS

0 commit comments

Comments
 (0)