1.1 --- a/WebStack/BaseHTTPRequestHandler.py Thu Mar 25 22:19:41 2004 +0000
1.2 +++ b/WebStack/BaseHTTPRequestHandler.py Thu Mar 25 22:20:25 2004 +0000
1.3 @@ -71,6 +71,8 @@
1.4
1.5 """
1.6 A framework-specific method which returns all request headers.
1.7 + NOTE: If duplicate header names are permitted, then this interface will
1.8 + NOTE: need to change.
1.9 """
1.10
1.11 return self.trans.headers
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/WebStack/JavaServlet.py Thu Mar 25 22:20:25 2004 +0000
2.3 @@ -0,0 +1,263 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +Java Servlet classes.
2.8 +"""
2.9 +
2.10 +import Generic
2.11 +from StringIO import StringIO
2.12 +
2.13 +class Stream:
2.14 +
2.15 + """
2.16 + Wrapper around java.io.BufferedReader.
2.17 + """
2.18 +
2.19 + def __init__(self, stream):
2.20 +
2.21 + "Initialise the stream with the given underlying 'stream'."
2.22 +
2.23 + self.stream = stream
2.24 +
2.25 + def read(self):
2.26 +
2.27 + "Read the entire message, returning it as a string."
2.28 +
2.29 + characters = StringIO()
2.30 + while 1:
2.31 + c = self.stream.read()
2.32 + if c == -1:
2.33 + return characters.getvalue()
2.34 + else:
2.35 + characters.write(chr(c))
2.36 +
2.37 + def readline(self):
2.38 +
2.39 + "Read a line from the stream, returning it as a string."
2.40 +
2.41 + return self.stream.readLine()
2.42 +
2.43 +class Transaction(Generic.Transaction):
2.44 +
2.45 + """
2.46 + Java Servlet transaction interface.
2.47 + """
2.48 +
2.49 + def __init__(self, request, response):
2.50 +
2.51 + """
2.52 + Initialise the transaction using the Java Servlet HTTP 'request' and
2.53 + 'response'.
2.54 + """
2.55 +
2.56 + self.request = request
2.57 + self.response = response
2.58 + self.status = None
2.59 +
2.60 + # Request-related methods.
2.61 +
2.62 + def get_request_stream(self):
2.63 +
2.64 + """
2.65 + A framework-specific method which returns the request stream for
2.66 + the transaction.
2.67 + """
2.68 +
2.69 + return Stream(self.request.getReader())
2.70 +
2.71 + def get_request_method(self):
2.72 +
2.73 + """
2.74 + A framework-specific method which gets the request method.
2.75 + """
2.76 +
2.77 + return self.request.getMethod()
2.78 +
2.79 + def get_headers(self):
2.80 +
2.81 + """
2.82 + A framework-specific method which returns all request headers.
2.83 + NOTE: If duplicate header names are permitted, then this interface will
2.84 + NOTE: need to change.
2.85 + """
2.86 +
2.87 + headers = {}
2.88 + header_names = self.request.getHeaderNames()
2.89 + if header_names:
2.90 + for header_name in header_names:
2.91 +
2.92 + # NOTE: Retrieve only a single value (not using getHeaders).
2.93 +
2.94 + headers[header_name] = self.request.getHeader(header_name)
2.95 +
2.96 + return headers
2.97 +
2.98 + def get_header_values(self, key):
2.99 +
2.100 + """
2.101 + A framework-specific method which returns a list of all request header
2.102 + values associated with the given 'key'. Note that according to RFC 2616,
2.103 + 'key' is treated as a case-insensitive string.
2.104 + """
2.105 +
2.106 + return self.request.getHeaders(key)
2.107 +
2.108 + def get_content_type(self):
2.109 +
2.110 + """
2.111 + A framework-specific method which gets the content type specified on the
2.112 + request, along with the charset employed.
2.113 + """
2.114 +
2.115 + content_types = self.get_header_values("Content-Type")
2.116 + if len(content_types) >= 1:
2.117 + return self.parse_content_type(content_types[0])
2.118 + else:
2.119 + return None
2.120 +
2.121 + def get_content_charsets(self):
2.122 +
2.123 + """
2.124 + Returns the character set preferences.
2.125 + """
2.126 +
2.127 + accept_charsets = self.get_header_values("Accept-Charset")
2.128 + if len(accept_charsets) >= 1:
2.129 + return self.parse_content_preferences(accept_charsets[0])
2.130 + else:
2.131 + return None
2.132 +
2.133 + def get_content_languages(self):
2.134 +
2.135 + """
2.136 + A framework-specific method which extracts language information from
2.137 + the transaction.
2.138 + """
2.139 +
2.140 + accept_languages = self.get_header_values("Accept-Language")
2.141 + if len(accept_languages) >= 1:
2.142 + return self.parse_content_preferences(accept_languages[0])
2.143 + else:
2.144 + return None
2.145 +
2.146 + def get_path(self):
2.147 +
2.148 + """
2.149 + A framework-specific method which gets the entire path from the request.
2.150 + """
2.151 +
2.152 + return self.request.getServletPath()
2.153 +
2.154 + def get_path_info(self):
2.155 +
2.156 + """
2.157 + A framework-specific method which gets the "path info" (the part of the
2.158 + URL after the resource name handling the current request) from the
2.159 + request.
2.160 + """
2.161 +
2.162 + return self.request.getPathInfo()
2.163 +
2.164 + def get_query_string(self):
2.165 +
2.166 + """
2.167 + A framework-specific method which gets the query string from the path in
2.168 + the request.
2.169 + """
2.170 +
2.171 + return self.request.getQueryString()
2.172 +
2.173 + # Higher level request-related methods.
2.174 +
2.175 + def get_fields_from_path(self):
2.176 +
2.177 + """
2.178 + A framework-specific method which extracts the form fields from the
2.179 + path specified in the transaction. The underlying framework may refuse
2.180 + to supply fields from the path if handling a POST transaction.
2.181 +
2.182 + Returns a dictionary mapping field names to lists of values (even if a
2.183 + single value is associated with any given field name).
2.184 +
2.185 + NOTE: There may not be a reliable means of extracting only the fields
2.186 + NOTE: from the path.
2.187 + """
2.188 +
2.189 + return self.get_fields_from_body()
2.190 +
2.191 + def get_fields_from_body(self):
2.192 +
2.193 + """
2.194 + A framework-specific method which extracts the form fields from the
2.195 + message body in the transaction.
2.196 +
2.197 + Returns a dictionary mapping field names to lists of values (even if a
2.198 + single value is associated with any given field name).
2.199 +
2.200 + NOTE: There may not be a reliable means of extracting only the fields
2.201 + NOTE: from the message body.
2.202 + """
2.203 +
2.204 + parameter_map = self.request.getParameterMap()
2.205 + fields = {}
2.206 + for key in parameter_map.keySet():
2.207 + fields[key] = parameter_map[key]
2.208 + return fields
2.209 +
2.210 + def get_user(self):
2.211 +
2.212 + """
2.213 + A framework-specific method which extracts user information from the
2.214 + transaction.
2.215 + """
2.216 +
2.217 + return self.request.getRemoteUser()
2.218 +
2.219 + # Response-related methods.
2.220 +
2.221 + def get_response_stream(self):
2.222 +
2.223 + """
2.224 + A framework-specific method which returns the response stream for
2.225 + the transaction.
2.226 + """
2.227 +
2.228 + return self.response.getWriter()
2.229 +
2.230 + def get_response_code(self):
2.231 +
2.232 + """
2.233 + Get the response code associated with the transaction. If no response
2.234 + code is defined, None is returned.
2.235 + """
2.236 +
2.237 + return self.status
2.238 +
2.239 + def set_response_code(self, response_code):
2.240 +
2.241 + """
2.242 + Set the 'response_code' using a numeric constant defined in the HTTP
2.243 + specification.
2.244 + """
2.245 +
2.246 + self.status = response_code
2.247 + self.response.setStatus(self.status)
2.248 +
2.249 + def set_header_value(self, header, value):
2.250 +
2.251 + """
2.252 + Set the HTTP 'header' with the given 'value'.
2.253 + """
2.254 +
2.255 + self.response.setHeader(self.format_header_value(header), self.format_header_value(value))
2.256 +
2.257 + def set_content_type(self, content_type):
2.258 +
2.259 + """
2.260 + A framework-specific method which sets the 'content_type' for the
2.261 + response.
2.262 + """
2.263 +
2.264 + return self.response.setHeader("Content-Type", self.format_content_type(content_type))
2.265 +
2.266 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/WebStack/ModPython.py Thu Mar 25 22:19:41 2004 +0000
3.2 +++ b/WebStack/ModPython.py Thu Mar 25 22:20:25 2004 +0000
3.3 @@ -44,6 +44,8 @@
3.4
3.5 """
3.6 A framework-specific method which returns all request headers.
3.7 + NOTE: If duplicate header names are permitted, then this interface will
3.8 + NOTE: need to change.
3.9 """
3.10
3.11 return self.trans.headers_in
4.1 --- a/WebStack/Twisted.py Thu Mar 25 22:19:41 2004 +0000
4.2 +++ b/WebStack/Twisted.py Thu Mar 25 22:20:25 2004 +0000
4.3 @@ -43,6 +43,8 @@
4.4
4.5 """
4.6 A framework-specific method which returns all request headers.
4.7 + NOTE: If duplicate header names are permitted, then this interface will
4.8 + NOTE: need to change.
4.9 """
4.10
4.11 return self.trans.received_headers
5.1 --- a/WebStack/Webware.py Thu Mar 25 22:19:41 2004 +0000
5.2 +++ b/WebStack/Webware.py Thu Mar 25 22:20:25 2004 +0000
5.3 @@ -55,11 +55,21 @@
5.4
5.5 """
5.6 A framework-specific method which returns all request headers.
5.7 + NOTE: If duplicate header names are permitted, then this interface will
5.8 + NOTE: need to change.
5.9 """
5.10
5.11 - # NOTE: Webware doesn't really provide access to headers in the request.
5.12 + # Use the Webware environment and some assumptions about variable names.
5.13 + # NOTE: Using lower case for the header names.
5.14
5.15 - return {}
5.16 + env = self.trans.request().environ()
5.17 + headers = {}
5.18 + for cgi_key, value in env.items():
5.19 + if cgi_key.startswith("HTTP_"):
5.20 + header_name = cgi_key[len("HTTP_"):].replace("_", "-").lower()
5.21 + headers[header_name] = value
5.22 +
5.23 + return headers
5.24
5.25 def get_header_values(self, key):
5.26
5.27 @@ -69,9 +79,14 @@
5.28 'key' is treated as a case-insensitive string.
5.29 """
5.30
5.31 - # NOTE: Webware doesn't really provide access to headers in the request.
5.32 + # Use the Webware environment and some assumptions about variable names.
5.33
5.34 - return []
5.35 + env = self.trans.request().environ()
5.36 + cgi_key = "HTTP_" + key.replace("-", "_").upper()
5.37 + if env.has_key(cgi_key):
5.38 + return [env[cgi_key]]
5.39 + else:
5.40 + return []
5.41
5.42 def get_content_type(self):
5.43
5.44 @@ -117,10 +132,7 @@
5.45 request.
5.46 """
5.47
5.48 - # NOTE: Webware does not seem to prefix the "path info" in a way
5.49 - # NOTE: consistent with the other frameworks.
5.50 -
5.51 - return "/" + self.trans.request().pathInfo()
5.52 + return self.trans.request().extraURLPath()
5.53
5.54 def get_query_string(self):
5.55