httpserve
  • Overview
  • Repository
  • Tickets
  • Statistics

Repository

Fix GETs without Content-Length header

Parent commits : c3f93bf15a6c7de736f9151faa52d5798408319b,
Children commits : b6c0843a8d45e4553bc680153db52b39ee8b544e,

By Laurent Defert on 2013-05-29 21:50:29
Fix GETs without Content-Length header

Browse content
Difference with parent commit c3f93bf15a6c7de736f9151faa52d5798408319b
Files modified:
httpget.py
--- 
+++ 
@@ -64,24 +64,27 @@
 
     def SetMax(self, max):
         self.lock.acquire()
-        self.max = int(max)
+        self.max = max
         self.lock.release()
 
     def SetVal(self, x, x_len):
         self.lock.acquire()
-        i0 = int(x / float(self.max) * self.BAR_SIZE)
-        i1 = int(((x + x_len) / float(self.max) * self.BAR_SIZE))
-        if i0 >= self.BAR_SIZE:
-            self.lock.release()
-            return
-        if i1 >= self.BAR_SIZE:
-            i1 = self.BAR_SIZE - 1
-        assert len(self.bar) == self.BAR_SIZE
-        self.bar = self.bar[:i0] + ['*'] * (i1 - i0) + self.bar[i1:]
-        assert len(self.bar) == self.BAR_SIZE
+
+        if self.max is not None:
+            i0 = int(x / float(self.max) * self.BAR_SIZE)
+            i1 = int(((x + x_len) / float(self.max) * self.BAR_SIZE))
+            if i0 >= self.BAR_SIZE:
+                self.lock.release()
+                return
+            if i1 >= self.BAR_SIZE:
+                i1 = self.BAR_SIZE - 1
+            assert len(self.bar) == self.BAR_SIZE
+            self.bar = self.bar[:i0] + ['*'] * (i1 - i0) + self.bar[i1:]
+            assert len(self.bar) == self.BAR_SIZE
 
         self.bw_count += x_len
         self.rx_count += x_len
+
         self.lock.release()
 
     def Redraw(self, l):
@@ -127,7 +130,7 @@
             self.bw_t = now
 
         s += ' %s/s' % human_size(self.bw)
-        if self.bw != 0:
+        if self.bw != 0 and self.max is not None:
             time_remaining = (self.max - self.rx_count) / self.bw
             s += ' %02i:%02i' % (time_remaining / 60, time_remaining % 60)
 
@@ -143,19 +146,28 @@
 class HDDFile(object):
     def __init__(self, filename, filesize):
         self.fp = open(filename, 'wb', 0)
-        self.fp.truncate(filesize)
-        self.fp.seek(0)
+
+        if filesize is not None:
+            self.fp.truncate(filesize)
+            self.fp.seek(0, 0)
         self.lock = threading.Lock()
 
     def write(self, content, offset):
         self.lock.acquire()
-        self.fp.seek(offset, 0)
-        self.fp.write(content)
+        try:
+            if offset is not None:
+                self.fp.seek(offset, 0)
+            self.fp.write(content)
+        except Exception, e:
+            print e
         self.lock.release()
 
     def close(self):
         self.lock.acquire()
-        self.fp.close()
+        try:
+            self.fp.close()
+        except Exception, e:
+            print e
         self.lock.release()
 
 class HTTPGet(threading.Thread):
@@ -208,13 +220,15 @@
             if not get.status in (200, 206):
                 raise GetException('HTTP %s %s' % (get.status, httplib.responses[get.status]))
 
-            offset = 0
+            offset = None
+            to_read = IO_SIZE
+            chunk_no = 0
             if self.chunk_no is not None:
                 offset = self.chunk_no * IO_SIZE
+                to_read = end - start + 1
+                chunk_no = self.chunk_no
+
             content = None
-            #to_read = IO_SIZE
-            to_read = end - start + 1
-            chunk_no = self.chunk_no
             while to_read > 0 and content != '':
                 read_len = IO_SIZE
                 if to_read < IO_SIZE:
@@ -225,7 +239,8 @@
                 to_read -= len(content)
                 chunk_no += 1
                 self.hdd_file.write(content, offset)
-                offset += len(content)
+                if offset is not None:
+                    offset += len(content)
             if to_read == 0:
                 self.http_status = get.status
             return
@@ -238,7 +253,7 @@
         except Exception, e:
             from traceback import print_exc
             self.progress_bar.EraseTermLine()
-            print "%s://%s:%s%s %s" % (self.url.scheme, self.host, self.port, self.url.path, str(e))
+            print "[%s] %s://%s:%s%s %s" % (e.__class__.__name__, self.url.scheme, self.host, self.port, self.url.path, str(e))
             print_exc()
         self.http_status = 0
 
@@ -250,7 +265,7 @@
         self.filename = 'index.html'
         if '/' in self.url.path:
             self.filename = unquote(self.url.path).rsplit('/', 1)[1]
-    
+
     def get_connection(self):
         if self.url.scheme == 'http':
             return httplib.HTTPConnection(self.url.hostname, self.url.port)
@@ -277,7 +292,10 @@
         if head.status != 200:  # Ok
             print 'Invalid status code %s %s' % (self.raw_url, head.status)
             return
-        length = int(head.getheader('Content-Length'))
+
+        length = head.getheader('Content-Length', None)
+        if length is not None:
+            length = int(length)
 
         filename = 'index.html'
         if '/' in self.url.path and not self.url.path.endswith('/'):