# HG changeset patch # User paulb # Date 1076861135 0 # Node ID 7bb5468759983d7cd8b212a48c99ae0b2b9ea389 # Parent 181626bcc49a7fae473dec5b44dbab956907f4bb [project @ 2004-02-15 16:05:35 by paulb] Added improved header retrieval support. diff -r 181626bcc49a -r 7bb546875998 WebStack/BaseHTTPRequestHandler.py --- a/WebStack/BaseHTTPRequestHandler.py Fri Jun 20 22:21:30 2008 +0200 +++ b/WebStack/BaseHTTPRequestHandler.py Sun Feb 15 16:05:35 2004 +0000 @@ -70,12 +70,21 @@ def get_headers(self): """ - A framework-specific method which returns the request headers. - NOTE: Experimental, since framework support varies somewhat. + A framework-specific method which returns all request headers. """ return self.trans.headers + def get_header_values(self, key): + + """ + A framework-specific method which returns a list of all request header + values associated with the given 'key'. Note that according to RFC 2616, + 'key' is treated as a case-insensitive string. + """ + + return self.convert_to_list(self.trans.headers.get(key)) + def get_content_type(self): """ diff -r 181626bcc49a -r 7bb546875998 WebStack/Generic.py --- a/WebStack/Generic.py Fri Jun 20 22:21:30 2008 +0200 +++ b/WebStack/Generic.py Sun Feb 15 16:05:35 2004 +0000 @@ -102,6 +102,24 @@ accept_prefs.append(t[0].strip()) return accept_prefs + def convert_to_list(self, value): + + """ + Returns a single element list containing 'value' if it is not itself a list, a + tuple, or None. If 'value' is a list then it is itself returned; if 'value' is a + tuple then a new list containing the same elements is returned; if 'value' is None + then an empty list is returned. + """ + + if type(value) == type([]): + return value + elif type(value) == type(()): + return list(value) + elif value is None: + return [] + else: + return [value] + # Request-related methods. def get_request_stream(self): @@ -124,12 +142,21 @@ def get_headers(self): """ - A framework-specific method which returns the request headers. - NOTE: Experimental, since framework support varies somewhat. + A framework-specific method which returns all request headers. """ raise NotImplementedError, "get_headers" + def get_header_values(self, key): + + """ + A framework-specific method which returns a list of all request header + values associated with the given 'key'. Note that according to RFC 2616, + 'key' is treated as a case-insensitive string. + """ + + raise NotImplementedError, "get_header_values" + def get_content_type(self): """ diff -r 181626bcc49a -r 7bb546875998 WebStack/ModPython.py --- a/WebStack/ModPython.py Fri Jun 20 22:21:30 2008 +0200 +++ b/WebStack/ModPython.py Sun Feb 15 16:05:35 2004 +0000 @@ -43,12 +43,21 @@ def get_headers(self): """ - A framework-specific method which returns the request headers. - NOTE: Experimental, since framework support varies somewhat. + A framework-specific method which returns all request headers. """ return self.trans.headers_in + def get_header_values(self, key): + + """ + A framework-specific method which returns a list of all request header + values associated with the given 'key'. Note that according to RFC 2616, + 'key' is treated as a case-insensitive string. + """ + + return self.convert_to_list(self.trans.headers_in.get(key)) + def get_content_type(self): """ diff -r 181626bcc49a -r 7bb546875998 WebStack/Twisted.py --- a/WebStack/Twisted.py Fri Jun 20 22:21:30 2008 +0200 +++ b/WebStack/Twisted.py Sun Feb 15 16:05:35 2004 +0000 @@ -41,13 +41,23 @@ def get_headers(self): """ - A framework-specific method which returns the request headers. - NOTE: Experimental, since framework support varies somewhat. + A framework-specific method which returns all request headers. """ - # NOTE: Accessing attribute of transaction object. + return self.trans.received_headers + + def get_header_values(self, key): - return self.trans.received_headers + """ + A framework-specific method which returns a list of all request header + values associated with the given 'key'. Note that according to RFC 2616, + 'key' is treated as a case-insensitive string. + """ + + # Twisted does not convert the header key to lower case (which is the + # stored representation). + + return self.convert_to_list(self.trans.received_headers.get(key.lower())) def get_content_type(self): diff -r 181626bcc49a -r 7bb546875998 WebStack/Webware.py --- a/WebStack/Webware.py Fri Jun 20 22:21:30 2008 +0200 +++ b/WebStack/Webware.py Sun Feb 15 16:05:35 2004 +0000 @@ -44,14 +44,25 @@ def get_headers(self): """ - A framework-specific method which returns the request headers. - NOTE: Experimental, since framework support varies somewhat. + A framework-specific method which returns all request headers. """ # NOTE: Webware doesn't really provide access to headers in the request. return {} + def get_header_values(self, key): + + """ + A framework-specific method which returns a list of all request header + values associated with the given 'key'. Note that according to RFC 2616, + 'key' is treated as a case-insensitive string. + """ + + # NOTE: Webware doesn't really provide access to headers in the request. + + return [] + def get_content_type(self): """