Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

support 304 Not Modified responses

  • Loading branch information...
commit aa33a68d510a24a1a709587a529812aa4a143a95 1 parent 5ed24aa
@sekimura authored
Showing with 59 additions and 6 deletions.
  1. +14 −1 lib/LWP/UserAgent/WithCache.pm
  2. +45 −5 t/10_request.t
View
15 lib/LWP/UserAgent/WithCache.pm
@@ -65,7 +65,20 @@ sub request {
}
my $res = $self->SUPER::request(@args);
- $self->set_cache($uri, $res) if $res->code eq HTTP::Status::RC_OK;
+
+ ## return cached data if it is "Not Modified"
+ if ($res->code eq HTTP::Status::RC_NOT_MODIFIED) {
+ my $not_modified_res = HTTP::Response->parse($obj->{as_string});
+ # hrm.. should we use '200 OK' here?
+ $not_modified_res->code(HTTP::Status::RC_NOT_MODIFIED);
+ $not_modified_res->message(HTTP::Status::status_message(HTTP::Status::RC_NOT_MODIFIED));
@ctcherry
ctcherry added a note

I see how this would make sense, because from the real site you are requesting, the real response code is 304, but...

Since the idea is to return the original response body content from the cache, I preferred keeping the original status code (in this case its mostly going to be 200, maybe consider caching other ones as well?). Then any code that uses the library down stream can be ignorant to the caching, in theory.

@sekimura Owner
sekimura added a note

ok. I'll make a change to keep original response code and message. thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return $not_modified_res;
+ }
+
+ ## cache only "200 OK" content
+ if ($res->code eq HTTP::Status::RC_OK) {
+ $self->set_cache($uri, $res);
+ }
return $res;
}
View
50 t/10_request.t
@@ -1,4 +1,4 @@
-use Test::More tests => 2;
+use Test::More tests => 4;
use File::Temp qw/ tempfile tempdir /;
use LWP::UserAgent::WithCache;
@@ -32,9 +32,9 @@ EOF
my $uri = 'http://www.example.com/styles.css';
$ua->set_cache($uri, $res);
-$res = $ua->get('http://www.example.com/styles.css');
+my $cached_res = $ua->get('http://www.example.com/styles.css');
-is ($res->code, 200);
+is ($cached_res->code, 200);
}
# haven't expired yet
@@ -59,7 +59,47 @@ EOF
my $uri = 'http://www.example.com/styles.css';
$ua->set_cache($uri, $res);
-$res = $ua->get('http://www.example.com/styles.css');
+my $cached_res = $ua->get('http://www.example.com/styles.css');
-is ($res->code, 200);
+is ($cached_res->code, 200);
+}
+
+# handle 304 Not Modified response
+{
+$res = HTTP::Response->parse(<<'EOF');
+HTTP/1.1 200 OK
+Connection: close
+Server: nginx/1.0.4
+Content-Length: 237
+Content-Type: text/css
+Date:Fri, 07 Oct 2011 22:43:34 GMT
+Last-Modified: Thr, 01 Jan 1970 00:00:00 GMT
+
+/* This is the StyleCatcher theme addition. Do not remove this block. */
+/* Selected Layout: */
+@import url(base_theme.css);
+@import url(http://mt.qootas.org/mt/mt-static/themes/minimalist-red/screen.css);
+/* end StyleCatcher imports */
+EOF
+
+my $not_modified_res = HTTP::Response->parse(<<'EOF');
+HTTP/1.1 304 Not Modified
+Connection: close
+Server: nginx/1.0.4
+Date:Fri, 07 Oct 2011 22:43:34 GMT
+Last-Modified: Thr, 01 Jan 1970 00:00:00 GMT
+
+EOF
+
+my $uri = 'http://www.example.com/styles.css';
+$ua->set_cache($uri, $res);
+
+## override request method to get not_modified_res
+no warnings 'redefine';
+local *LWP::UserAgent::request = sub {return $not_modified_res};
+
+my $cached_res = $ua->get('http://www.example.com/styles.css');
+
+is ($cached_res->code, 304);
+is ($cached_res->content, $res->content);
}
Please sign in to comment.
Something went wrong with that request. Please try again.