Small change: Add message to status and put etags in double quotes per
spec.

diff --git a/web/httpserver.py b/web/httpserver.py
index f2c91cc..f9c0e8d 100644
--- a/web/httpserver.py
+++ b/web/httpserver.py
@@ -183,6 +183,18 @@ class StaticApp(SimpleHTTPRequestHandler):
         from cStringIO import StringIO
         self.wfile = StringIO() # for capturing error

+        try:
+            path = self.translate_path(self.path)
+            etag = '"%s"' % os.path.getmtime(path)
+            client_etag = environ.get('HTTP_IF_NONE_MATCH')
+            self.send_header('ETag', etag)
+            if etag == client_etag:
+                self.send_response(304, "Not Modified")
+                self.start_response(self.status, self.headers)
+                raise StopIteration
+        except OSError:
+            pass # Probably a 404
+
         f = self.send_head()
         self.start_response(self.status, self.headers)



On Apr 28, 2:08 am, Justin Davis <[email protected]> wrote:
> Hi guys,
>
> I've been using Chrome as my browser recently (from FF) and noticed
> something I had forgotten: Firefox caches in content in weird ways
> that breaks the http spec.  Mostly, this refers to static content --
> FF chooses to cache most things even if the web server doesn't
> explicitly tell it to so long as there aren't url arguments and
> possibly some other voodoo (and if Mozilla would fix this, that would
> be fantastic).
>
> That said, I noticed that static content was taking a while to load on
> Chrome because they (correctly) request the file each time.  This was
> annoying for some larger javascript files I was working with, so I
> added support for etags to the basic server.  Nothing big, but it
> means I don't need to resend big files.  Here's the diff:
>
> diff --git a/web/httpserver.py b/web/httpserver.py
> index f2c91cc..f46db97 100644
> --- a/web/httpserver.py
> +++ b/web/httpserver.py
> @@ -183,6 +183,18 @@ class StaticApp(SimpleHTTPRequestHandler):
>          from cStringIO import StringIO
>          self.wfile = StringIO() # for capturing error
>
> +        try:
> +            path = self.translate_path(self.path)
> +            etag = str(os.path.getmtime(path))
> +            client_etag = environ.get('HTTP_IF_NONE_MATCH')
> +            self.send_header('ETag', etag)
> +            if etag == client_etag:
> +                self.send_response(304)
> +                self.start_response(self.status, self.headers)
> +                raise StopIteration
> +        except OSError:
> +            pass # Probably a 404
> +
>          f = self.send_head()
>          self.start_response(self.status, self.headers)
>
> I haven't tested this on windows -- I think the getmtime function
> should work correctly but only assuming.
>
> Anand, if this works for other people, think this could get added to
> the tree?
>
> Cheers,
> Justin
>
> --
> You received this message because you are subscribed to the Google Groups 
> "web.py" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/webpy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/webpy?hl=en.

Reply via email to