Commits

Brian Mearns committed fe995bf

Added a little more to the hashing stuff.

Comments (0)

Files changed (1)

         start = None
         end = None
 
-        qs = urlparse.parse_qs(urlparse.urlparse(self.path).query)
-        print self.server.fingerprint(), qs
-        if "fpr" in qs:
-            if qs["fpr"][0] == self.server.fingerprint():
-                #Set an expiration nearly a year inthe future (that's max allowed by HTTP standard).
-                # 31449600 seconds = 364 days.
-                self.send_header("Expires", format_date_time(time.time() + 31449600))
-            
+        if self.server.doCache():
+            qs = urlparse.parse_qs(urlparse.urlparse(self.path).query)
+            print self.server.fingerprint(), qs
+            if "fpr" in qs:
+                if qs["fpr"][0] == self.server.fingerprint():
+                    #Set an expiration nearly a year inthe future (that's max allowed by HTTP standard).
+                    # 31449600 seconds = 364 days.
+                    self.send_header("Expires", format_date_time(time.time() + 31449600))
 
         if "Range" in self.headers:
             rstr = self.headers["Range"]
 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,
+        maxPreBufferSize=10*1024*1024, maxPreHashSize=100*1024*1024, noCache=False,
         handler=GonzoHandler):
         BaseHTTPServer.HTTPServer.__init__(self, address, handler)
 
         self.__contentLength = os.path.getsize(self.__filepath)
         self.__contents = None
         self.__hash = None
+        self.__noCache = noCache
+
         if maxPreBufferSize < 0 or (self.__contentLength <= maxPreBufferSize):
             self.prebuffer()
-        if self.__hash is None and (maxPreHashSize < 0 or (self.__contentLength < maxPreHashSize)):
+        if (not noCache) and (self.__hash is None) and (maxPreHashSize < 0 or (self.__contentLength < maxPreHashSize)):
             self.prehash()
 
-        if self.__hash is None or uniqueFpr:
+        if self.__hash is None:
             self.__fingerprint = (
                 ("%.3f" % time.time())
                 + ("%d" % address[1])
                 self.socket, keyfile=keyfile, certfile=certfile, server_side=True
             )
 
+    def doCache(self):
+        return not self.__noCache
+
     def prehash(self):
         try:
             m = hashlib.sha256()
         default=None,
     )
 
+    argp.add_argument("-B", "--prebuffer",
+        help="""Sets the size limit (in bytes) for which to pre-buffer the file. Pre-buffering
+will allow it to be downloaded a little more quickly, but will consume RAM on the server. A value of
+0 means it will not buffer any file. A value less than 0 means it will (attempt to) prebuffer any
+file, regardless of size.""",
+        metavar="LIMIT",
+        dest="prebuffer",
+        default=10*1024*1024,
+        type=int,
+    )
+    argp.add_argument("-H", "--prehash",
+        help="""Sets the size limit (in bytes) for which to pre-hash the file. Pre-hashing is required
+to generate a file-dependent fingerprint, but requires that the entire file be read before the server
+can start. If the file is prebuffered, the hash is generated regardless of the prehash limit.""",
+        metavar="LIMIT",
+        dest="prehash",
+        default=50*1024*1024,
+        type=int,
+    )
+    argp.add_argument("-U", "--unique", "--no-cache",
+        help="""Suppress the generation of headers that encourage caching or reading from cache.""",
+        dest="unique",
+        action="store_true",
+        default=False,
+    )
+
     argp.add_argument("-v", "--verbose",
         help="""Tells the program to generate verbose output.""",
         action="store_const",
 
     server = GonzoServer((args.HOST, args.PORT), args.FILE, out=out, password=password,
         https=args.https, keyfile=args.keyfile, certfile=args.certfile,
-        message=message, mimeType=mimeType, filename=args.filename
+        message=message, mimeType=mimeType, filename=args.filename,
+        maxPreBufferSize=args.prebuffer, maxPreHashSize=args.prehash,
+        noCache=args.unique,
     )
     if len(args.HOST):
         where = "%s:%d" % (args.HOST, args.PORT)