aboutsummaryrefslogtreecommitdiff
path: root/scripts/async-http.py
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2009-04-16 22:16:16 +0000
committerRob Austein <sra@hactrn.net>2009-04-16 22:16:16 +0000
commit34daddbfddac735566edf9c16beaf1f8d7ccbbeb (patch)
treefb6db6170b0115fe2221b924218251d6a0ee7b7a /scripts/async-http.py
parent3d390d54342ba4cb7571bfdf5b01c448aaa6c62c (diff)
Header tweaks
svn path=/scripts/async-http.py; revision=2344
Diffstat (limited to 'scripts/async-http.py')
-rw-r--r--scripts/async-http.py25
1 files changed, 15 insertions, 10 deletions
diff --git a/scripts/async-http.py b/scripts/async-http.py
index 22554bcd..9d7c2ffa 100644
--- a/scripts/async-http.py
+++ b/scripts/async-http.py
@@ -27,6 +27,8 @@ import sys, os, time, socket, fcntl, asyncore, asynchat, getopt, email, tracebac
class http_message(object):
+ software_name = "WombatWare test HTTP code"
+
@classmethod
def parse_from_wire(cls, headers):
self = cls()
@@ -39,7 +41,7 @@ class http_message(object):
self.headers = {}
for h in headers:
k,v = h.split(":", 1)
- k = k.capitalize()
+ k = "-".join(s.capitalize() for s in k.split("-"))
if k in self.headers:
self.headers[k] += ", " + v
else:
@@ -50,7 +52,7 @@ class http_message(object):
s = self.format_first_line()
if self.body is not None:
assert isinstance(self.body, str)
- self.headers["Content-length"] = len(self.body)
+ self.headers["Content-Length"] = len(self.body)
for kv in self.headers.iteritems():
s += "%s: %s\r\n" % kv
s += "\r\n"
@@ -83,6 +85,7 @@ class http_request(http_message):
self.path = path
def format_first_line(self):
+ self.headers.setdefault("User-Agent", self.software_name)
return "%s %s HTTP/%d.%d\r\n" % (self.cmd, self.path, self.version[0], self.version[1])
class http_response(http_message):
@@ -100,6 +103,8 @@ class http_response(http_message):
self.msg = msg
def format_first_line(self):
+ self.headers.setdefault("Date", time.strftime("%a, %d %b %Y %T GMT"))
+ self.headers.setdefault("Server", self.software_name)
return "HTTP/%d.%d %s %s\r\n" % (self.version[0], self.version[1], self.code, self.msg)
class http_stream(asynchat.async_chat):
@@ -137,7 +142,7 @@ class http_stream(asynchat.async_chat):
def handle_body(self):
self.msg.body = self.get_buffer()
- assert len(self.msg.body) == int(self.msg.headers["Content-length"])
+ assert len(self.msg.body) == int(self.msg.headers["Content-Length"])
self.handle_message()
def handle_error(self):
@@ -149,13 +154,13 @@ class http_server(http_stream):
def handle_headers(self):
self.msg = http_request.parse_from_wire(self.get_buffer())
if self.msg.cmd == "POST":
- self.set_terminator(int(self.msg.headers["Content-length"]))
+ self.set_terminator(int(self.msg.headers["Content-Length"]))
else:
self.handle_message()
def handle_message(self):
print self.msg
- self.push(http_response(code = 200, msg = "OK", body = self.msg.format(), headers = { "Content-type" : "text/plain" }).format())
+ self.push(http_response(code = 200, msg = "OK", body = self.msg.format(), headers = { "Content-Type" : "text/plain" }).format())
if False:
self.close_when_done()
else:
@@ -168,7 +173,7 @@ class http_client(http_stream):
def handle_headers(self):
self.msg = http_response.parse_from_wire(self.get_buffer())
- self.set_terminator(int(self.msg.headers["Content-length"]))
+ self.set_terminator(int(self.msg.headers["Content-Length"]))
def handle_message(self):
print self.msg
@@ -221,10 +226,10 @@ else:
# whether the parser can survive multiple mssages.
client = http_client.queue_messages([
- http_request(cmd = sys.argv[1].upper(), path = "/", body = "Hi, Mom!\r\n", headers = { "Content-type" : "text/plain" }),
- http_request(cmd = sys.argv[1].upper(), path = "/", body = "Hi, Dad!\r\n", headers = { "Content-type" : "text/plain" }),
- http_request(cmd = sys.argv[1].upper(), path = "/", body = "Hi, Bro!\r\n", headers = { "Content-type" : "text/plain" }),
- http_request(cmd = sys.argv[1].upper(), path = "/", body = "Hi, Sis!\r\n", headers = { "Content-type" : "text/plain" }),
+ http_request(cmd = sys.argv[1].upper(), path = "/", body = "Hi, Mom!\r\n", headers = { "Content-Type" : "text/plain" }),
+ http_request(cmd = sys.argv[1].upper(), path = "/", body = "Hi, Dad!\r\n", headers = { "Content-Type" : "text/plain" }),
+ http_request(cmd = sys.argv[1].upper(), path = "/", body = "Hi, Bro!\r\n", headers = { "Content-Type" : "text/plain" }),
+ http_request(cmd = sys.argv[1].upper(), path = "/", body = "Hi, Sis!\r\n", headers = { "Content-Type" : "text/plain" }),
])
asyncore.loop()