# HG changeset patch # User paulb # Date 1076276506 0 # Node ID 3ea524db35d486534f6ac168f14efe6323e7eb17 # Parent 44ceb6a2068445bf5ea80e2861480f1d2ea6fb84 [project @ 2004-02-08 21:41:46 by paulb] Added header setting and header value formatting methods. Introduced username acquisition to transactions using either framework mechanisms or the Helpers.Auth.UserInfo class. diff -r 44ceb6a20684 -r 3ea524db35d4 WebStack/BaseHTTPRequestHandler.py --- a/WebStack/BaseHTTPRequestHandler.py Sun Feb 08 18:16:37 2004 +0000 +++ b/WebStack/BaseHTTPRequestHandler.py Sun Feb 08 21:41:46 2004 +0000 @@ -6,6 +6,7 @@ import Generic from Helpers.Request import MessageBodyStream +from Helpers.Auth import UserInfo from cgi import FieldStorage from StringIO import StringIO @@ -29,6 +30,7 @@ self.content_type = None self.response_code = 200 self.content = StringIO() + self.headers = {} def commit(self): @@ -40,6 +42,8 @@ self.trans.send_response(self.response_code) if self.content_type is not None: self.trans.send_header("Content-Type", self.format_content_type(self.content_type)) + for header, value in self.headers.items(): + self.trans.send_header(self.format_header_value(header), self.format_header_value(value)) self.trans.end_headers() self.content.seek(0) self.trans.wfile.write(self.content.read()) @@ -137,9 +141,11 @@ transaction. """ - # NOTE: Not implemented yet, but just pretend that there are no users. - - return None + auth_header = self.get_headers().get("Authorization") + if auth_header: + return UserInfo(auth_header).username + else: + return None # Response-related methods. @@ -172,6 +178,16 @@ self.response_code = response_code + def set_header(self, header, value): + + """ + Set the HTTP 'header' with the given 'value'. + """ + + # The header is not written out immediately due to the buffering in use. + + self.headers[header] = value + def set_content_type(self, content_type): """ diff -r 44ceb6a20684 -r 3ea524db35d4 WebStack/Generic.py --- a/WebStack/Generic.py Sun Feb 08 18:16:37 2004 +0000 +++ b/WebStack/Generic.py Sun Feb 08 21:41:46 2004 +0000 @@ -73,6 +73,15 @@ return field.encode("US-ASCII") + def format_header_value(self, value): + + """ + Format the given header 'value'. Typically, this just ensures the usage + of US-ASCII. + """ + + return value.encode("US-ASCII") + def parse_content_preferences(self, accept_preference): """ @@ -212,6 +221,14 @@ raise NotImplementedError, "set_response_code" + def set_header(self, header, value): + + """ + Set the HTTP 'header' with the given 'value'. + """ + + raise NotImplementedError, "set_header" + def set_content_type(self, content_type): """ diff -r 44ceb6a20684 -r 3ea524db35d4 WebStack/ModPython.py --- a/WebStack/ModPython.py Sun Feb 08 18:16:37 2004 +0000 +++ b/WebStack/ModPython.py Sun Feb 08 21:41:46 2004 +0000 @@ -142,6 +142,14 @@ self.response_code = response_code + def set_header(self, header, value): + + """ + Set the HTTP 'header' with the given 'value'. + """ + + self.trans.headers_out[self.format_header_value(header)] = self.format_header_value(value) + def set_content_type(self, content_type): """ diff -r 44ceb6a20684 -r 3ea524db35d4 WebStack/Twisted.py --- a/WebStack/Twisted.py Sun Feb 08 18:16:37 2004 +0000 +++ b/WebStack/Twisted.py Sun Feb 08 21:41:46 2004 +0000 @@ -5,6 +5,7 @@ """ import Generic +from Helpers.Auth import UserInfo class Transaction(Generic.Transaction): @@ -114,9 +115,13 @@ transaction. """ - # NOTE: Not implemented yet, but just pretend that there are no users. + # NOTE: Twisted makes headers lower case, for some reason. - return None + auth_header = self.get_headers().get("authorization") + if auth_header: + return UserInfo(auth_header).username + else: + return None # Response-related methods. @@ -149,6 +154,14 @@ self.trans.setResponseCode(response_code) + def set_header(self, header, value): + + """ + Set the HTTP 'header' with the given 'value'. + """ + + self.trans.setHeader(self.format_header_value(header), self.format_header_value(value)) + def set_content_type(self, content_type): """ diff -r 44ceb6a20684 -r 3ea524db35d4 WebStack/Webware.py --- a/WebStack/Webware.py Sun Feb 08 18:16:37 2004 +0000 +++ b/WebStack/Webware.py Sun Feb 08 21:41:46 2004 +0000 @@ -119,7 +119,14 @@ transaction. """ - return self.trans.request().remoteUser() + # NOTE: Webware relies entirely on a CGI-style environment where the + # NOTE: actual headers are not available. Therefore, the Web server must + # NOTE: itself be set up to provide user support. + + try: + return self.trans.request().remoteUser() + except KeyError, exc: + return None # Response-related methods. @@ -159,6 +166,14 @@ self.trans.response().setStatus(response_code) + def set_header(self, header, value): + + """ + Set the HTTP 'header' with the given 'value'. + """ + + self.trans.response().setHeader(self.format_header_value(header), self.format_header_value(value)) + def set_content_type(self, content_type): """