I'm using this class to run a HTTP-server in Python:
class Server:
def __init__(self):
port = 81
httpd = SocketServer.ThreadingTCPServer(('', port), self.Proxy)
httpd.serve_forever()
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
# just for demonstration:
url = "http://ovh.net/files/10Mio.dat"
opener = urllib2.build_opener()
handle = opener.open(url)
if hasattr(handle.info(), "headers"):
headers = handle.info().headers
for h in headers:
self.send_header(h[0], h[1])
self.end_headers()
self.copyfile(handle, self.wfile)
But unfortunately that doesn't work as expected. If I send a request to the server, I'm getting a file that's a bit bigger than the one I would expect. When I look into it, I see that the response headers are stored at the beginning of that file. The response itself contains no headers, so I think SimpleHTTPServer ended the headers before calling do_GET().
I've seen this answer and edited my code accordingly:
class Server:
def __init__(self):
port = 81
httpd = SocketServer.ThreadingTCPServer(('', port), self.Proxy)
httpd.serve_forever()
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
# get handle again
self.copyfile(handle, self.wfile)
def end_headers(self):
self.send_my_headers()
SimpleHTTPServer.SimpleHTTPRequestHandler.end_headers(self)
def send_my_headers(self):
# Do something smart to get a file-like variable named handle
self.send_header("Access-Control-Allow-Origin", "*")
if hasattr(handle.info(), "headers"):
headers = handle.info().headers
for h in headers:
self.send_header(h[0], h[1])
But that doesn't make any difference. The response headers are still stuffed in the file. What's going on here?
Edit: I updated my first example to clarify what I mean. What I want is a bit like an HTTP-proxy: My server should download a file from another server and just send the data back to the client. For some reasons (would be too much to explain) I can't use a regular proxy.
Now the first code example does that quite efficiently. My problem there is that all the headers get lost in the process. That means the client is unaware of the download size (thus showing an indefinite progress bar) or the file name (Content-disposition).
In my second code example, I tried to send the headers that I got from the upstream server but the headers seem to be finished before do_GET is called.