Commits

Christian Krebs committed 2d59bd6 Draft

Fixed unzipping.

Comments (0)

Files changed (2)

http/httpconnection.py

 from mimetypes import types_map
 from common import *
 
-log = open('log', 'wb')
+#log = open('log', 'wb')
 
 
 class HTTPConnection(asyncore.dispatcher):
             
             self.flush()
             headers_raw, method, path, protocol, self.headers, self.in_buffer = headers
-            log.write(headers_raw)
+            # log.write(headers_raw)
             path = path.lstrip("/")
             if "?" in path:
                 path, self.query = path.split('?', 1)

http/rawsocketrequest.py

     def __init__(self):
         self.arguments_dict = None
         self.raw = ""
-        
+
 class RawSocketRequest(asyncore.dispatcher):
 
     def __init__(self):
             command = command.replace('-', '_')
             if hasattr(self, command):
                 getattr(self, command)()
-                
+
     def command_unzip(self):
         self.html.unzipped = ""
         self.unzip()
             self.html.response = self.raw_headers + self.html.unzipped.replace('<', '&lt;')
             self.command_done(self.html)
             self.html = None
-    
+
     def command_get(self):
         self.html.raw = self.web_interface.arguments_dict['http-header']
         self.html.response_info = ""
         self.chunks = []
         headers = parse_headers(self.web_interface.arguments_dict['http-header'])
         if headers:
-          (self.web_interface.raw, 
-              method, 
-              path, 
-              protocol, 
-              self.http_headers, 
+          (self.web_interface.raw,
+              method,
+              path,
+              protocol,
+              self.http_headers,
               buffer) = headers
           if 'Host' in self.http_headers:
             port = 80
               self.info.append("<span class='error'>error: no ip address for %s</span>" % self.http_headers['Host'])
             except:
               raise
-       
+
             if ip_address:
               self.info.append('ip address: %s, port: %s' % (ip_address, int(port)))
               self.times.append(int(time()*1000))
               self.in_buffer = ""
               self.first = True
-              self.out_buffer = ""        
+              self.out_buffer = ""
               self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
               self.connect((ip_address, int(port)))
               self.out_buffer += self.web_interface.raw
-                
+
     def check_response_headers(self):
         headers = parse_headers(self.in_buffer)
         if headers:
-            (self.raw_headers, 
-            protocol, 
-            code, 
-            code_text, 
-            self.http_response_headers, 
+            (self.raw_headers,
+            protocol,
+            code,
+            code_text,
+            self.http_response_headers,
             buffer) = headers
             if "Content-Length" in self.http_response_headers:
-                self.total_content_length = ( len(self.raw_headers) + 
+                self.total_content_length = ( len(self.raw_headers) +
                     int(self.http_response_headers["Content-Length"].strip()) )
                 self.check_response = self.check_response_content
                 return self.check_response()
                 self.check_response_chunk_trailer
             return self.check_response()
         return False
-                
+
     def check_response_chunk_data(self):
         pos_crlf = self.in_buffer.find(CRLF, self.total_content_length)
         if pos_crlf != -1:
                 continue
             break
         return pos_crlf == self.total_content_length
-        
+
     def check_response_wait_for_timeout(self):
         return False
 
         if "Content-Encoding" in self.http_response_headers and \
             "gzip" in self.http_response_headers["Content-Encoding"]:
             import gzip, os, tempfile
-            fd, path = tempfile.mkstemp('.gzip')
-            f = os.fdopen(fd, 'wb')
-            f.write(self.chunks and "".join(self.chunks) or self.in_buffer[len(self.raw_headers):])
-            f.close()
+            temp = tempfile.TemporaryFile()
+            temp.write(self.chunks and "".join(self.chunks) or self.in_buffer[len(self.raw_headers):])
+            temp.seek(0)
             try:
-                f = gzip.open(path, 'rb')
+                f = gzip.GzipFile(filename="temp", mode="rb", fileobj=temp) # [, mode[, compresslevel[, fileobj[, mtime]]]]])
                 self.html.unzipped = f.read().replace('<', '&lt;')
                 f.close()
             except Exception, msg:
               self.info.append("<span class='error'>error: %s" % msg)
               self.html.response_info = "<pre class='info'>%s</pre>" % "\n".join(self.info)
-            os.remove(path) 
-        
+
     # ============================================================
     # Implementations of the asyncore.dispatcher class methods
     # ============================================================
 
     def handle_connect(self):
         pass
-    
+
     def writable(self):
         return bool(self.out_buffer)
 
 
     def handle_close(self):
         self.close()
-        
+