* Patch - For caching with 304 Not Modified
@ 2010-07-21 15:23 Simon Smith
2010-07-21 18:16 ` Eric Wong
0 siblings, 1 reply; 3+ messages in thread
From: Simon Smith @ 2010-07-21 15:23 UTC (permalink / raw)
To: Rack Development
Hi,
I am currently using Rails 2.3.2 with Rack 1.0.0 (I looked at the
source for 1.2.0 and the issue is the same). When a browser requests a
page it is downloading all images and JavaScripts each time, where
what should be happening is a '304 Not Modified' response should be
returned if the browser already has the asset in cache. The caching is
working fine for html pages themselves served by Rails.
I looked through the source code and found that JavaScript and images
are being handled with Rack::File. I made a patch to the Rack::File
class to handle the If-Modified-Since directive so a 304 is sent in
the case the supplied modified time matches that of the file itself.
Below is that patch to Rack::File; it adds the 'notmodified' function
and modifies the '_call' function.
def _call(env)
@path_info = Utils.unescape(env["PATH_INFO"])
return forbidden if @path_info.include? ".."
@path = F.join(@root, @path_info)
begin
if F.file?(@path) && F.readable?(@path)
#check the If-Modified-Since header and send 304 if the
client has the most up to date version already
if env['HTTP_IF_MODIFIED_SINCE']
http_time =
Time.httpdate(env['HTTP_IF_MODIFIED_SINCE'])
mtime = Time.httpdate(F.mtime(@path).httpdate)
if http_time && http_time <= Time.now && mtime = http_time
return notmodified
end
end
serving
else
raise Errno::EPERM
end
rescue SystemCallError
not_found
end
end
def notmodified
body = "Not Modified\n"
[304, {"Content-Type" => "text/plain",
"Content-Length" => body.size.to_s},
[body]]
end
This patch seems to work but it seems strange that it is missing in
the first place as there are so many users of Rack and Rails and I
cannot find any information pertaining to other users with this
problem. I am wondering if there is an alternate way to handle this
caching without modifying the rack code itself and I am just missing a
configuration option in Rails, do you know of one?
Please feel free to use/modify this patch in your next release of
Rack.
Regards,
Simon
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Patch - For caching with 304 Not Modified
2010-07-21 15:23 Patch - For caching with 304 Not Modified Simon Smith
@ 2010-07-21 18:16 ` Eric Wong
2010-07-21 18:59 ` Simon Smith
0 siblings, 1 reply; 3+ messages in thread
From: Eric Wong @ 2010-07-21 18:16 UTC (permalink / raw)
To: rack-devel
Simon Smith <amazingdancingbear@gmail.com> wrote:
> This patch seems to work but it seems strange that it is missing in
> the first place as there are so many users of Rack and Rails and I
> cannot find any information pertaining to other users with this
> problem. I am wondering if there is an alternate way to handle this
> caching without modifying the rack code itself and I am just missing a
> configuration option in Rails, do you know of one?
Rack already includes a Rack::ConditionalGet middleware which users can
load if needed.
However, I suspect most Rails users use a non-Rack server such as nginx
or Apache to serve static files, so having such middleware (or even
Rack::File) would be redundant.
--
Eric Wong
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Patch - For caching with 304 Not Modified
2010-07-21 18:16 ` Eric Wong
@ 2010-07-21 18:59 ` Simon Smith
0 siblings, 0 replies; 3+ messages in thread
From: Simon Smith @ 2010-07-21 18:59 UTC (permalink / raw)
To: Rack Development
I'll take a look into the configuration of the Apache server to serve
these files. I didn't know you could set it up like that.
Thanks,
Simon
On Jul 21, 1:16 pm, Eric Wong <normalper...@yhbt.net> wrote:
> Simon Smith <amazingdancingb...@gmail.com> wrote:
> > This patch seems to work but it seems strange that it is missing in
> > the first place as there are so many users of Rack and Rails and I
> > cannot find any information pertaining to other users with this
> > problem. I am wondering if there is an alternate way to handle this
> > caching without modifying the rack code itself and I am just missing a
> > configuration option in Rails, do you know of one?
>
> Rack already includes a Rack::ConditionalGet middleware which users can
> load if needed.
>
> However, I suspect most Rails users use a non-Rack server such as nginx
> or Apache to serve static files, so having such middleware (or even
> Rack::File) would be redundant.
>
> --
> Eric Wong
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2010-07-21 18:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-21 15:23 Patch - For caching with 304 Not Modified Simon Smith
2010-07-21 18:16 ` Eric Wong
2010-07-21 18:59 ` Simon Smith
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).