Skip to content

Commit

Permalink
new netius support for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jan 30, 2015
1 parent ef4e640 commit 0aefb92
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/netius/extra/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ def on_serve(self):
if self.env: self.cors = self.get_env("CORS", self.cors, cast = bool)
if self.env: self.cache = self.get_env("CACHE", self.cache, cast = int)
self.base_path = os.path.abspath(self.base_path)
self.cache_d = datetime.timedelta(seconds = self.cache)
self.info("Defining '%s' as the root of the file server ..." % (self.base_path or "."))
if self.cors: self.info("Cross origin resource sharing is enabled")
if self.cache: self.info("Resource cache set with '%d' seconds" % self.cache)
if self.cache: self.info("Resource cache set with %d seconds" % self.cache)

def on_data_http(self, connection, parser):
netius.servers.HTTPServer.on_data_http(self, connection, parser)
Expand Down Expand Up @@ -301,6 +302,16 @@ def on_normal_file(self, connection, path):
if is_partial: headers["content-range"] = content_range_s
if not is_partial: headers["accept-ranges"] = "bytes"

# in case there's a valid cache defined must populate the proper header
# fields so that cache is applied to the request
if self.cache:
current = datetime.datetime.utcnow()
target = current + self.cache_d
target_s = target.strftime("%a, %d %b %Y %H:%M:%S GMT")
cache_s = "public, max-age=%d" % self.cache
headers["expires"] = target_s
headers["cache-control"] = cache_s

# "calculates" the proper returning code taking into account if the
# current data to be sent is partial or not
code = 206 if is_partial else 200
Expand Down

0 comments on commit 0aefb92

Please sign in to comment.