From a9e60fa3e66d9de28233aab89b426ac74d1f8ad6 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 24 Jul 2012 12:46:14 +1200 Subject: [PATCH] Add a Date header to server responses, unless raw is set. --- libpathod/rparse.py | 40 ++++++++++++++++++++++++++++------------ test/test_rparse.py | 8 ++++++-- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/libpathod/rparse.py b/libpathod/rparse.py index 38b382325..767cb861b 100644 --- a/libpathod/rparse.py +++ b/libpathod/rparse.py @@ -1,6 +1,8 @@ import operator, string, random, mmap, os, time +from email.utils import formatdate import contrib.pyparsing as pp from netlib import http_status, tcp + import utils BLOCKSIZE = 1024 @@ -539,7 +541,7 @@ class Message: l += len(i[2]) return l - def serve(self, fp, check): + def serve(self, fp, check, is_request): """ fp: The file pointer to write to. @@ -547,15 +549,30 @@ class Message: values have been calculated). If it returns False service proceeds, otherwise the return is treated as an error message to be sent to the client, and service stops. + + is_request: Is this a request? If False, we assume it's a response. + Used to decide what standard modifications to make if raw is not + set. """ started = time.time() - if self.body and not self.raw and not utils.get_header("Content-Length", self.headers): - self.headers.append( - ( - LiteralGenerator("Content-Length"), - LiteralGenerator(str(len(self.body))), + if not self.raw: + if self.body and not utils.get_header("Content-Length", self.headers): + self.headers.append( + ( + LiteralGenerator("Content-Length"), + LiteralGenerator(str(len(self.body))), + ) ) - ) + if is_request: + pass + else: + if not utils.get_header("Date", self.headers): + self.headers.append( + ( + LiteralGenerator("Date"), + LiteralGenerator(formatdate(timeval=None, localtime=False, usegmt=True)) + ) + ) hdrs = [] for k, v in self.headers: @@ -690,7 +707,7 @@ class CraftedRequest(Request): i.accept(settings, self) def serve(self, fp, check=None): - d = Request.serve(self, fp, check) + d = Request.serve(self, fp, check, True) d["spec"] = self.spec return d @@ -703,7 +720,7 @@ class CraftedResponse(Response): i.accept(settings, self) def serve(self, fp, check=None): - d = Response.serve(self, fp, check) + d = Response.serve(self, fp, check, False) d["spec"] = self.spec return d @@ -716,13 +733,12 @@ class PathodErrorResponse(Response): self.body = LiteralGenerator("pathod error: " + (body or msg)) self.headers = [ ( - LiteralGenerator("Content-Type"), - LiteralGenerator("text/plain") + LiteralGenerator("Content-Type"), LiteralGenerator("text/plain") ), ] def serve(self, fp, check=None): - d = Response.serve(self, fp, check) + d = Response.serve(self, fp, check, False) d["internal"] = True return d diff --git a/test/test_rparse.py b/test/test_rparse.py index 24ca36538..ef724d68a 100644 --- a/test/test_rparse.py +++ b/test/test_rparse.py @@ -444,12 +444,16 @@ class TestResponse: s = cStringIO.StringIO() r = rparse.parse_response({}, "400:b'foo'") r.serve(s) - assert "Content-Length" in s.getvalue() + v = s.getvalue() + assert "Content-Length" in v + assert "Date" in v s = cStringIO.StringIO() r = rparse.parse_response({}, "400:b'foo':r") r.serve(s) - assert not "Content-Length" in s.getvalue() + v = s.getvalue() + assert not "Content-Length" in v + assert not "Date" in v def test_length(self): def testlen(x):