changeset 30:e5b85cdd7228

Fixed some bugs.
author Atul Varma <varmaa@toolness.com>
date Mon, 27 Apr 2009 11:13:54 -0700
parents 02b9a564d841
children 041bda62b614
files openwebchat.py
diffstat 1 files changed, 27 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/openwebchat.py	Mon Apr 27 10:53:15 2009 -0700
+++ b/openwebchat.py	Mon Apr 27 11:13:54 2009 -0700
@@ -24,17 +24,17 @@
 
     def _until_http_response_sent(self, msg = '', mimetype = 'text/plain',
                                   length = None, code = 200):
-        if length is None:
-            length = len(msg)
         content = '\r\n'.join(
             ('HTTP/1.1 %d %s' % (code,
                                  httplib.responses[code]),
              'Keep-Alive: timeout=99, max=99',
              'Connection: Keep-Alive',
-             'Content-Length: %d' % length,
-             'Content-Type: %s' % mimetype,
-             '',
-             msg))
+             'Content-Type: %s' % mimetype))
+        if not mimetype.startswith('multipart'):
+            if length is None:
+                length = len(msg)
+            content += '\r\nContent-Length: %d' % length
+        content += '\r\n\r\n%s' % msg
         yield until_sent(content)
 
     def _until_file_sent(self, filename):
@@ -59,6 +59,22 @@
         while 1:
             yield self._until_one_request_processed(addr)
 
+    def _until_multipart_header_sent(self, boundary):
+        yield self._until_http_response_sent(
+            '--%s\r\n' % boundary,
+            mimetype = ('multipart/x-mixed-replace; '
+                        'boundary="%s"' % boundary))
+
+    def _until_multipart_part_sent(self, boundary, msg):
+        yield until_sent('\r\n'.join(
+                ('Content-Length: %d' % len(msg),
+                 'Content-Type: text/plain',
+                 '',
+                 msg,
+                 '',
+                 '--%s' % boundary,
+                 '')))
+
     def _until_one_request_processed(self, addr):
         request = yield until_received(terminator = '\r\n\r\n')
         request = request.splitlines()
@@ -66,24 +82,15 @@
         request_headers = request[1:]
         req_parts = request_line.split()
         if req_parts[1] == '/listen':
-            yield self._until_http_response_sent(
-                '--chunk\r\n',
-                mimetype = ('Content-Type: multipart/x-mixed-replace; ' +
-                            'boundary="chunk"'))
+            yield self._until_multipart_header_sent('chunk')
             while 1:
                 ip, num = yield channels.until_message_received('global')
-                msg = 'Got message %d from %s.' % (num, ip)
-                yield until_sent('\r\n'.join(
-                        ('Content-Length: %d' % len(msg),
-                         'Content-Type: text/plain',
-                         '',
-                         msg,
-                         '',
-                         '--chunk',
-                         '')))
+                yield self._until_multipart_part_sent(
+                    'chunk',
+                    'Got message %d from %s.' % (num, ip))
         elif req_parts[1] == '/send':
             yield channels.until_message_sent('global',
-                                              (addr[0], num_messages))
+                                              (addr[0], 1))
             yield self._until_http_response_sent('sent.')
         elif req_parts[1] in ['/', '/jquery.js', '/openwebchat.js',
                               '/openwebchat.css']: