Commits

Brian Mearns committed b6a3c76

Added some GonzoServer init params to generate a unique fpr, or just a hash, and to prebuffer or not (not currently used though). No command line options to go along with them yet.

Comments (0)

Files changed (1)

 class GonzoServer(BaseHTTPServer.HTTPServer):
     def __init__(self, address, filepath, out, password=None, https=True, keyfile=None, certfile=None,
         message=None, mimeType=None, filename=None,
+        maxPreBufferSize=10*1024*1024, maxPreHashSize=100*1024*1024,
+        uniqueFpr=False,
         handler=GonzoHandler):
         BaseHTTPServer.HTTPServer.__init__(self, address, handler)
 
         self.__filepath = filepath
         self.__password = password
         self.__activeNonces = []
-        self.__contentLength = None
-        self.__fingerprint = (
-            ("%.3f" % time.time())
-            + ("%d" % address[1])
-            + base64.urlsafe_b64encode(os.urandom(16))
-        )
+        self.__contentLength = os.path.getsize(self.__filepath)
+        self.__contents = None
+        self.__hash = None
+        if maxPreBufferSize < 0 or (self.__contentLength <= maxPreBufferSize):
+            self.prebuffer()
+        if self.__hash is None and (maxPreHashSize < 0 or (self.__contentLength < maxPreHashSize)):
+            self.prehash()
+
+        if self.__hash is None or uniqueFpr:
+            self.__fingerprint = (
+                ("%.3f" % time.time())
+                + ("%d" % address[1])
+                + base64.urlsafe_b64encode(os.urandom(16))
+            )
+        else:
+            self.__fingerprint = base64.urlsafe_b64encode(self.__hash)
 
         if filename is None:
             self.__filename = os.path.basename(filepath)
                 self.socket, keyfile=keyfile, certfile=certfile, server_side=True
             )
 
+    def prehash(self):
+        try:
+            m = hashlib.sha256()
+            fid = open(self.__filepath, "rb")
+            while True:
+                chunk = fid.read(4096)
+                if len(chunk) == 0:
+                    break;
+                m.update(chunk)
+            self.__hash = m.digest()
+        finally:
+            try: fid.close()
+            except: pass
+        
+    def prebuffer(self):
+        try:
+            contents = ""
+            m = hashlib.sha256()
+            fid = open(self.__filepath, "rb")
+            while True:
+                chunk = fid.read(4096)
+                if len(chunk) == 0:
+                    break;
+                contents += chunk
+                m.update(chunk)
+            self.__contents = contents
+            self.__hash = m.digest()
+        finally:
+            try: fid.close()
+            except: pass
+        
     def fingerprint(self):
         return self.__fingerprint
 
     def getContentLength(self, start=None, end=None):
-        if self.__contentLength is None:
-            self.__contentLength = os.path.getsize(self.__filepath)
         if start is None:
             if end is None:
                 #Both are none