Add a Date header to server responses, unless raw is set.

This commit is contained in:
Aldo Cortesi 2012-07-24 12:46:14 +12:00
parent dbed251fb9
commit a9e60fa3e6
2 changed files with 34 additions and 14 deletions

View File

@ -1,6 +1,8 @@
import operator, string, random, mmap, os, time import operator, string, random, mmap, os, time
from email.utils import formatdate
import contrib.pyparsing as pp import contrib.pyparsing as pp
from netlib import http_status, tcp from netlib import http_status, tcp
import utils import utils
BLOCKSIZE = 1024 BLOCKSIZE = 1024
@ -539,7 +541,7 @@ class Message:
l += len(i[2]) l += len(i[2])
return l return l
def serve(self, fp, check): def serve(self, fp, check, is_request):
""" """
fp: The file pointer to write to. fp: The file pointer to write to.
@ -547,15 +549,30 @@ class Message:
values have been calculated). If it returns False service proceeds, values have been calculated). If it returns False service proceeds,
otherwise the return is treated as an error message to be sent to otherwise the return is treated as an error message to be sent to
the client, and service stops. 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() started = time.time()
if self.body and not self.raw and not utils.get_header("Content-Length", self.headers): if not self.raw:
if self.body and not utils.get_header("Content-Length", self.headers):
self.headers.append( self.headers.append(
( (
LiteralGenerator("Content-Length"), LiteralGenerator("Content-Length"),
LiteralGenerator(str(len(self.body))), 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 = [] hdrs = []
for k, v in self.headers: for k, v in self.headers:
@ -690,7 +707,7 @@ class CraftedRequest(Request):
i.accept(settings, self) i.accept(settings, self)
def serve(self, fp, check=None): def serve(self, fp, check=None):
d = Request.serve(self, fp, check) d = Request.serve(self, fp, check, True)
d["spec"] = self.spec d["spec"] = self.spec
return d return d
@ -703,7 +720,7 @@ class CraftedResponse(Response):
i.accept(settings, self) i.accept(settings, self)
def serve(self, fp, check=None): def serve(self, fp, check=None):
d = Response.serve(self, fp, check) d = Response.serve(self, fp, check, False)
d["spec"] = self.spec d["spec"] = self.spec
return d return d
@ -716,13 +733,12 @@ class PathodErrorResponse(Response):
self.body = LiteralGenerator("pathod error: " + (body or msg)) self.body = LiteralGenerator("pathod error: " + (body or msg))
self.headers = [ self.headers = [
( (
LiteralGenerator("Content-Type"), LiteralGenerator("Content-Type"), LiteralGenerator("text/plain")
LiteralGenerator("text/plain")
), ),
] ]
def serve(self, fp, check=None): def serve(self, fp, check=None):
d = Response.serve(self, fp, check) d = Response.serve(self, fp, check, False)
d["internal"] = True d["internal"] = True
return d return d

View File

@ -444,12 +444,16 @@ class TestResponse:
s = cStringIO.StringIO() s = cStringIO.StringIO()
r = rparse.parse_response({}, "400:b'foo'") r = rparse.parse_response({}, "400:b'foo'")
r.serve(s) r.serve(s)
assert "Content-Length" in s.getvalue() v = s.getvalue()
assert "Content-Length" in v
assert "Date" in v
s = cStringIO.StringIO() s = cStringIO.StringIO()
r = rparse.parse_response({}, "400:b'foo':r") r = rparse.parse_response({}, "400:b'foo':r")
r.serve(s) 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 test_length(self):
def testlen(x): def testlen(x):