WebStack

Changeset

65:7bb546875998
2004-02-15 paulb raw files shortlog changelog graph [project @ 2004-02-15 16:05:35 by paulb] Added improved header retrieval support.
WebStack/BaseHTTPRequestHandler.py (file) WebStack/Generic.py (file) WebStack/ModPython.py (file) WebStack/Twisted.py (file) WebStack/Webware.py (file)
     1.1 --- a/WebStack/BaseHTTPRequestHandler.py	Fri Jun 20 22:21:30 2008 +0200
     1.2 +++ b/WebStack/BaseHTTPRequestHandler.py	Sun Feb 15 16:05:35 2004 +0000
     1.3 @@ -70,12 +70,21 @@
     1.4      def get_headers(self):
     1.5  
     1.6          """
     1.7 -        A framework-specific method which returns the request headers.
     1.8 -        NOTE: Experimental, since framework support varies somewhat.
     1.9 +        A framework-specific method which returns all request headers.
    1.10          """
    1.11  
    1.12          return self.trans.headers
    1.13  
    1.14 +    def get_header_values(self, key):
    1.15 +
    1.16 +        """
    1.17 +        A framework-specific method which returns a list of all request header
    1.18 +        values associated with the given 'key'. Note that according to RFC 2616,
    1.19 +        'key' is treated as a case-insensitive string.
    1.20 +        """
    1.21 +
    1.22 +        return self.convert_to_list(self.trans.headers.get(key))
    1.23 +
    1.24      def get_content_type(self):
    1.25  
    1.26          """
     2.1 --- a/WebStack/Generic.py	Fri Jun 20 22:21:30 2008 +0200
     2.2 +++ b/WebStack/Generic.py	Sun Feb 15 16:05:35 2004 +0000
     2.3 @@ -102,6 +102,24 @@
     2.4                  accept_prefs.append(t[0].strip())
     2.5          return accept_prefs
     2.6  
     2.7 +    def convert_to_list(self, value):
     2.8 +
     2.9 +        """
    2.10 +        Returns a single element list containing 'value' if it is not itself a list, a
    2.11 +        tuple, or None. If 'value' is a list then it is itself returned; if 'value' is a
    2.12 +        tuple then a new list containing the same elements is returned; if 'value' is None
    2.13 +        then an empty list is returned.
    2.14 +        """
    2.15 +
    2.16 +        if type(value) == type([]):
    2.17 +            return value
    2.18 +        elif type(value) == type(()):
    2.19 +            return list(value)
    2.20 +        elif value is None:
    2.21 +            return []
    2.22 +        else:
    2.23 +            return [value]
    2.24 +
    2.25      # Request-related methods.
    2.26  
    2.27      def get_request_stream(self):
    2.28 @@ -124,12 +142,21 @@
    2.29      def get_headers(self):
    2.30  
    2.31          """
    2.32 -        A framework-specific method which returns the request headers.
    2.33 -        NOTE: Experimental, since framework support varies somewhat.
    2.34 +        A framework-specific method which returns all request headers.
    2.35          """
    2.36  
    2.37          raise NotImplementedError, "get_headers"
    2.38  
    2.39 +    def get_header_values(self, key):
    2.40 +
    2.41 +        """
    2.42 +        A framework-specific method which returns a list of all request header
    2.43 +        values associated with the given 'key'. Note that according to RFC 2616,
    2.44 +        'key' is treated as a case-insensitive string.
    2.45 +        """
    2.46 +
    2.47 +        raise NotImplementedError, "get_header_values"
    2.48 +
    2.49      def get_content_type(self):
    2.50  
    2.51          """
     3.1 --- a/WebStack/ModPython.py	Fri Jun 20 22:21:30 2008 +0200
     3.2 +++ b/WebStack/ModPython.py	Sun Feb 15 16:05:35 2004 +0000
     3.3 @@ -43,12 +43,21 @@
     3.4      def get_headers(self):
     3.5  
     3.6          """
     3.7 -        A framework-specific method which returns the request headers.
     3.8 -        NOTE: Experimental, since framework support varies somewhat.
     3.9 +        A framework-specific method which returns all request headers.
    3.10          """
    3.11  
    3.12          return self.trans.headers_in
    3.13  
    3.14 +    def get_header_values(self, key):
    3.15 +
    3.16 +        """
    3.17 +        A framework-specific method which returns a list of all request header
    3.18 +        values associated with the given 'key'. Note that according to RFC 2616,
    3.19 +        'key' is treated as a case-insensitive string.
    3.20 +        """
    3.21 +
    3.22 +        return self.convert_to_list(self.trans.headers_in.get(key))
    3.23 +
    3.24      def get_content_type(self):
    3.25  
    3.26          """
     4.1 --- a/WebStack/Twisted.py	Fri Jun 20 22:21:30 2008 +0200
     4.2 +++ b/WebStack/Twisted.py	Sun Feb 15 16:05:35 2004 +0000
     4.3 @@ -41,13 +41,23 @@
     4.4      def get_headers(self):
     4.5  
     4.6          """
     4.7 -        A framework-specific method which returns the request headers.
     4.8 -        NOTE: Experimental, since framework support varies somewhat.
     4.9 +        A framework-specific method which returns all request headers.
    4.10          """
    4.11  
    4.12 -        # NOTE: Accessing attribute of transaction object.
    4.13 +        return self.trans.received_headers
    4.14 +
    4.15 +    def get_header_values(self, key):
    4.16  
    4.17 -        return self.trans.received_headers
    4.18 +        """
    4.19 +        A framework-specific method which returns a list of all request header
    4.20 +        values associated with the given 'key'. Note that according to RFC 2616,
    4.21 +        'key' is treated as a case-insensitive string.
    4.22 +        """
    4.23 +
    4.24 +        # Twisted does not convert the header key to lower case (which is the
    4.25 +        # stored representation).
    4.26 +
    4.27 +        return self.convert_to_list(self.trans.received_headers.get(key.lower()))
    4.28  
    4.29      def get_content_type(self):
    4.30  
     5.1 --- a/WebStack/Webware.py	Fri Jun 20 22:21:30 2008 +0200
     5.2 +++ b/WebStack/Webware.py	Sun Feb 15 16:05:35 2004 +0000
     5.3 @@ -44,14 +44,25 @@
     5.4      def get_headers(self):
     5.5  
     5.6          """
     5.7 -        A framework-specific method which returns the request headers.
     5.8 -        NOTE: Experimental, since framework support varies somewhat.
     5.9 +        A framework-specific method which returns all request headers.
    5.10          """
    5.11  
    5.12          # NOTE: Webware doesn't really provide access to headers in the request.
    5.13  
    5.14          return {}
    5.15  
    5.16 +    def get_header_values(self, key):
    5.17 +
    5.18 +        """
    5.19 +        A framework-specific method which returns a list of all request header
    5.20 +        values associated with the given 'key'. Note that according to RFC 2616,
    5.21 +        'key' is treated as a case-insensitive string.
    5.22 +        """
    5.23 +
    5.24 +        # NOTE: Webware doesn't really provide access to headers in the request.
    5.25 +
    5.26 +        return []
    5.27 +
    5.28      def get_content_type(self):
    5.29  
    5.30          """