| 1 |
--- lib/MT/App/Search.pm.bak Sat Jan 6 08:46:47 2007 |
|---|
| 2 |
+++ lib/MT/App/Search.pm Sun Feb 18 16:45:49 2007 |
|---|
| 3 |
@@ -198,6 +198,25 @@ |
|---|
| 4 |
my $app = shift; |
|---|
| 5 |
return $app->error($app->errstr) if $app->errstr; |
|---|
| 6 |
|
|---|
| 7 |
+ ## Calculate Last-Modified and ETag values |
|---|
| 8 |
+ my $latest_epoch = 0; |
|---|
| 9 |
+ for my $blog_id (keys %{ $app->{searchparam}{IncludeBlogs} }) { |
|---|
| 10 |
+ my $blog = MT::Blog->load($blog_id, {cached_ok => 1}); |
|---|
| 11 |
+ my $epoch = MT::Util::ts2epoch($blog, $blog->children_modified_on); |
|---|
| 12 |
+ $latest_epoch = $epoch if $epoch > $latest_epoch; |
|---|
| 13 |
+ } |
|---|
| 14 |
+ my $ts = MT::Util::epoch2ts(undef, $latest_epoch); |
|---|
| 15 |
+ my $last_modified = MT::Util::format_ts('%a, %d %b %Y %H:%M:%S GMT', $ts, undef, 'en'); |
|---|
| 16 |
+ my $etag = Digest::MD5::md5_hex($last_modified) if eval { require Digest::MD5; 1 }; |
|---|
| 17 |
+ |
|---|
| 18 |
+ ## Check whether 'Modified' or 'Not Modified' |
|---|
| 19 |
+ #return if $app->_handle_conditional_get($last_modified, $etag); |
|---|
| 20 |
+ |
|---|
| 21 |
+ # Generate Last-Modified, ETag, and Expires headers |
|---|
| 22 |
+ $app->set_header('Last-Modified' => $last_modified); |
|---|
| 23 |
+ $app->set_header('ETag' => $etag) if $etag; |
|---|
| 24 |
+ $app->set_header('Expires' => '+1h'); |
|---|
| 25 |
+ |
|---|
| 26 |
my @results; |
|---|
| 27 |
if ($app->{searchparam}{RegexSearch}) { |
|---|
| 28 |
eval { m/$app->{search_string}/ }; |
|---|
| 29 |
@@ -345,6 +364,23 @@ |
|---|
| 30 |
$app->print($res); |
|---|
| 31 |
} |
|---|
| 32 |
$res; |
|---|
| 33 |
+} |
|---|
| 34 |
+ |
|---|
| 35 |
+sub _handle_conditional_get { |
|---|
| 36 |
+ my $app = shift; |
|---|
| 37 |
+ my ($last_modified, $etag) = @_; |
|---|
| 38 |
+ my $if_modified_since = $ENV{HTTP_IF_MODIFIED_SINCE} || ''; |
|---|
| 39 |
+ my $if_none_match = $ENV{HTTP_IF_NONE_MATCH} || ''; |
|---|
| 40 |
+ if (( $if_modified_since || $if_none_match) && |
|---|
| 41 |
+ (!$if_none_match || $if_none_match eq $etag) && |
|---|
| 42 |
+ (!$if_modified_since || $if_modified_since eq $last_modified)) { |
|---|
| 43 |
+ $app->response_code(304); |
|---|
| 44 |
+ $app->response_message('Not Modified'); |
|---|
| 45 |
+ $app->send_http_header("text/plain"); |
|---|
| 46 |
+ $app->{no_print_body} = 1; |
|---|
| 47 |
+ return 1; |
|---|
| 48 |
+ } |
|---|
| 49 |
+ 0; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
sub _tag_search { |
|---|