changeset 56:0bb64cafd063

Added redirects to add extra slash at end of conversation name.
author Atul Varma <varmaa@toolness.com>
date Tue, 28 Apr 2009 15:13:07 -0700
parents 5e08a9f4730e
children 08975d96ace2
files openwebchat.py
diffstat 1 files changed, 16 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/openwebchat.py	Tue Apr 28 15:04:29 2009 -0700
+++ b/openwebchat.py	Tue Apr 28 15:13:07 2009 -0700
@@ -42,6 +42,7 @@
         self.__file.flush()
 
 class OpenWebChatServer(object):
+    REDIRECT_TEMPLATE = re.compile('\/([A-Za-z0-9_]+)')
     URL_TEMPLATE = re.compile('\/([A-Za-z0-9_]+)/(.*)')
 
     BOUNDARY = "'''"
@@ -61,10 +62,13 @@
         self._server.run()
 
     def _until_http_response_sent(self, msg = '', mimetype = 'text/plain',
-                                  length = None, code = 200):
+                                  length = None, code = 200,
+                                  additional_headers = None):
         headers = {'Keep-Alive': 'timeout=99, max=99',
                    'Connection': 'Keep-Alive',
                    'Content-Type': mimetype}
+        if additional_headers:
+            headers.update(additional_headers)
         if not mimetype.startswith('multipart'):
             if length is None:
                 length = len(msg)
@@ -165,8 +169,17 @@
         method = req_parts[0]
         match = self.URL_TEMPLATE.match(req_parts[1])
         if not match:
-            yield self._until_http_response_sent('not found',
-                                                 code = 404)
+            match = self.REDIRECT_TEMPLATE.match(req_parts[1])
+            if match:
+                newpath = req_parts[1] + '/'
+                yield self._until_http_response_sent(
+                    newpath,
+                    code = 301,
+                    additional_headers = {'Location': newpath}
+                    )
+            else:
+                yield self._until_http_response_sent('not found',
+                                                     code = 404)
         else:
             conv_name = match.group(1)
             page = match.group(2)