Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a basic HTTP memory cache #4117

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Track requests made to the test HTTP server.

  • Loading branch information
jdm committed Nov 27, 2014
commit 195d5f1f2d6aed81898c492a97516cdcd4b01010
@@ -1,15 +1,90 @@
import SimpleHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
import SocketServer
import os
from collections import defaultdict

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
requests = defaultdict(int)

class CountingRequestHandler(SimpleHTTPRequestHandler):
def __init__(self, req, client_addr, server):
SimpleHTTPRequestHandler.__init__(self, req, client_addr, server)

def do_GET(self):
global requests
parts = self.path.split('/')
if parts[1] == 'stats':
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
if parts[2]:
body = str(requests['/' + parts[2]])
else:
body = ''
for key, value in requests.iteritems():
body += key + ': ' + str(value) + '\n'
self.send_header('Content-Length', len(body))
self.end_headers()
self.wfile.write(body)
return

if parts[1] == 'reset':
requests = defaultdict(int)
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', 0)
return

requests[self.path] += 1
path = self.translate_path(self.path)
headers = path + '^headers'
if os.path.isfile(headers):
ctype = self.guess_type(path)
try:
# Always read in binary mode. Opening files in text mode may cause
# newline translations, making the actual size of the content
# transmitted *less* than the content-length!
f = open(path, 'rb')
except IOError:
self.send_error(404, "File not found")
return

try:
try:
h = open(headers, 'rb')
except IOError:
self.send_error(404, "Header file not found")
return

header_lines = h.readlines()

self.send_response(int(header_lines[0]))
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))

for header in header_lines[1:]:
parts = map(lambda x: x.strip(), header.split(':'))
self.send_header(parts[0], parts[1])

self.end_headers()

try:
self.copyfile(f, self.wfile)
finally:
f.close()
except:
f.close()
raise
else:
SimpleHTTPRequestHandler.do_GET(self)

class MyTCPServer(SocketServer.TCPServer):
request_queue_size = 2000
allow_reuse_address = True

httpd = MyTCPServer(("", PORT), Handler)
httpd = MyTCPServer(("", PORT), CountingRequestHandler)

print "serving at port", PORT
httpd.serve_forever()
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.