Commits

James Mills committed f01b801

- circuits.web: Allow HTTP status message to be customized so implementing
WebSockets is a little eaiser.
- circuits.web: Don't presume to add a "Content-Type" header.

Comments (0)

Files changed (1)

circuits/web/wrappers.py

 """
 
 
-import os
 from cStringIO import StringIO
 from time import strftime, time
 from Cookie import SimpleCookie
 from circuits.net.sockets import BUFSIZE
 from constants import HTTP_STATUS_CODES, SERVER_PROTOCOL, SERVER_VERSION
 
+
 def file_generator(input, chunkSize=BUFSIZE):
     chunk = input.read(chunkSize)
     while chunk:
         chunk = input.read(chunkSize)
     input.close()
 
+
 class Host(object):
     """An internet address.
 
     def __repr__(self):
         return "Host(%r, %r, %r)" % (self.ip, self.port, self.name)
 
+
 class Request(object):
     """Creates a new Request object to hold information about a request.
-    
+
     :param sock: The socket object of the request.
     :type  sock: socket.socket
 
     def url(self, *args, **kwargs):
         return url(self, *args, **kwargs)
 
+
 class Body(object):
     """Response Body"""
-    
+
     def __get__(self, response, cls=None):
         if response is None:
             return self
         else:
             return response._body
-    
+
     def __set__(self, response, value):
         if response == value:
             return
 
         response._body = value
 
+
 class Response(object):
     """Response(sock, request) -> new Response object
 
     """
 
     code = 200
+    message = "OK"
+
     body = Body()
     done = False
     close = False
 
     protocol = "HTTP/%d.%d" % SERVER_PROTOCOL
 
-    def __init__(self, request, code=None):
+    def __init__(self, request, code=None, message=None):
         "initializes x; see x.__class__.__doc__ for signature"
 
         self.request = request
         if code is not None:
             self.code = code
 
+        if message is not None:
+            self.message = message
+
         self._body = []
         self.time = time()
 
         self.headers = Headers([])
+        self.headers.add_header("Content-Type", "text/html")
         self.headers.add_header("Date", strftime("%a, %d %b %Y %H:%M:%S %Z"))
 
         if self.request.server:
                 self.status,
                 self.headers["Content-Type"],
                 (len(self.body) if type(self.body) == str else 0))
-    
+
     def __str__(self):
         self.prepare()
         protocol = self.protocol
 
     @property
     def status(self):
-        return "%d %s" % (self.code, HTTP_STATUS_CODES[self.code])
+        return "%d %s" % (self.code,
+                self.message or HTTP_STATUS_CODES[self.code])
 
     def prepare(self):
-        # Set a default content-Type if we don't have one.
-        self.headers.setdefault("Content-Type", "text/html")
-
         if self.body and type(self.body) is ListType:
             if unicode in map(type, self.body):
                 cLength = sum(map(lambda s: len(s.encode("utf-8")), self.body))
                 else:
                     self.close = True
 
-        if self.request.server is not None and "Connection" not in self.headers:
+        if (self.request.server is not None
+                and"Connection" not in self.headers):
             if self.protocol == "HTTP/1.1":
                 if self.close:
                     self.headers.add_header("Connection", "close")