1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/WebStack/BaseHTTPRequestHandler.py Thu Feb 05 00:10:59 2004 +0000
1.3 @@ -0,0 +1,166 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +BaseHTTPRequestHandler classes.
1.8 +"""
1.9 +
1.10 +import Generic
1.11 +from cgi import FieldStorage
1.12 +from StringIO import StringIO
1.13 +
1.14 +class Transaction(Generic.Transaction):
1.15 +
1.16 + """
1.17 + BaseHTTPRequestHandler transaction interface.
1.18 + """
1.19 +
1.20 + def __init__(self, trans):
1.21 +
1.22 + """
1.23 + Initialise the transaction using the BaseHTTPRequestHandler instance
1.24 + 'trans'.
1.25 + """
1.26 +
1.27 + self.trans = trans
1.28 +
1.29 + # Other attributes of interest in instances of this class.
1.30 +
1.31 + self.content_type = None
1.32 + self.response_code = 100
1.33 + self.content = StringIO()
1.34 +
1.35 + def commit(self):
1.36 +
1.37 + """
1.38 + A special method, synchronising the transaction with framework-specific
1.39 + objects.
1.40 + """
1.41 +
1.42 + self.trans.send_response(self.response_code)
1.43 + self.trans.send_header("Content-Type", self.format_content_type(self.content_type))
1.44 + self.trans.end_headers()
1.45 + self.content.seek(0)
1.46 + self.trans.wfile.write(self.content.read())
1.47 +
1.48 + # Request-related methods.
1.49 +
1.50 + def get_request_stream(self):
1.51 +
1.52 + """
1.53 + A framework-specific method which returns the request stream for
1.54 + the transaction.
1.55 + """
1.56 +
1.57 + return self.trans.rfile
1.58 +
1.59 + def get_request_method(self):
1.60 +
1.61 + """
1.62 + A framework-specific method which gets the request method.
1.63 + """
1.64 +
1.65 + return self.trans.command
1.66 +
1.67 + def get_headers(self):
1.68 +
1.69 + """
1.70 + A framework-specific method which returns the request headers.
1.71 + NOTE: Experimental, since framework support varies somewhat.
1.72 + """
1.73 +
1.74 + return self.trans.headers
1.75 +
1.76 + def get_content_type(self):
1.77 +
1.78 + """
1.79 + A framework-specific method which gets the content type specified on the
1.80 + request, along with the charset employed.
1.81 + """
1.82 +
1.83 + return self.parse_content_type(self.trans.headers.get("Content-type") or
1.84 + self.trans.headers.get("Content-Type"))
1.85 +
1.86 + def get_content_charsets(self):
1.87 +
1.88 + """
1.89 + Returns the character set preferences.
1.90 + """
1.91 +
1.92 + return self.parse_content_preferences(self.trans.headers["Accept-Charset"])
1.93 +
1.94 + def get_content_languages(self):
1.95 +
1.96 + """
1.97 + A framework-specific method which extracts language information from
1.98 + the transaction.
1.99 + """
1.100 +
1.101 + return self.parse_content_preferences(self.trans.headers["Accept-Language"])
1.102 +
1.103 + def get_path(self):
1.104 +
1.105 + """
1.106 + A framework-specific method which gets the entire path from the request.
1.107 + """
1.108 +
1.109 + return self.trans.path
1.110 +
1.111 + def get_path_info(self):
1.112 +
1.113 + """
1.114 + A framework-specific method which gets the "path info" (the part of the
1.115 + URL after the resource name handling the current request) from the
1.116 + request.
1.117 + """
1.118 +
1.119 + # NOTE: No attempt is made to deduce the "path info".
1.120 +
1.121 + return self.trans.path
1.122 +
1.123 + # Higher level request-related methods.
1.124 +
1.125 + def get_fields(self):
1.126 +
1.127 + """
1.128 + A framework-specific method which extracts the form fields from the
1.129 + transaction.
1.130 + """
1.131 +
1.132 + return FieldStorage(self.trans.rfile, keep_blank_values=1)
1.133 +
1.134 + def get_agent_information(self):
1.135 +
1.136 + """
1.137 + A framework-specific method which extracts agent information from
1.138 + the transaction.
1.139 + """
1.140 +
1.141 + return None
1.142 +
1.143 + # Response-related methods.
1.144 +
1.145 + def get_response_stream(self):
1.146 +
1.147 + """
1.148 + A framework-specific method which returns the response stream for
1.149 + the transaction.
1.150 + """
1.151 +
1.152 + # Return a stream which is later emptied into the real stream.
1.153 +
1.154 + return self.content
1.155 +
1.156 + def set_content_type(self, content_type):
1.157 +
1.158 + """
1.159 + A framework-specific method which sets the 'content_type' for the
1.160 + response.
1.161 + """
1.162 +
1.163 + # The content type has to be written as a header, before actual content,
1.164 + # but after the response line. This means that some kind of buffering is
1.165 + # required. Hence, we don't write the header out immediately.
1.166 +
1.167 + self.content_type = content_type
1.168 +
1.169 +# vim: tabstop=4 expandtab shiftwidth=4