Changeset 62 in bookmarks


Ignore:
Timestamp:
08/23/13 17:26:14 (11 years ago)
Author:
peter
Message:
  • removed unused code from the feed() method of the BookmarkController
  • added a _check_modified() method that checks the If-Modified-Since header against a given time
  • view() and view_field() support conditional GET requests using Last-Modified/If-Modified-Since headers
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BookmarkController.pm

    r61 r62  
    265265    $feed->title($title); 
    266266 
    267     my $feed_uri = URI->new_abs('feed', $self->base_uri); 
    268     $feed_uri->query_form(tag => \@tags); 
    269     $feed->id($feed_uri->canonical); 
    270  
    271267    for my $link ($self->_get_list_links('application/atom+xml', { q => $query, tag => \@tags })) { 
    272268        my $atom_link = XML::Atom::Link->new; 
     
    308304} 
    309305 
     306#TODO: better method name 
     307# returns 1 if there is an If-Modified-Since header and it is newer than the given $mtime 
     308# returns 0 if there is an If-Modified-Since header but the $mtime is newer 
     309# returns undef if there is no If-Modified-Since header 
     310sub _check_modified { 
     311    my $self = shift; 
     312    my $mtime = shift; 
     313 
     314    # check If-Modified-Since header to return cache response 
     315    if ($self->request->env->{HTTP_IF_MODIFIED_SINCE}) { 
     316        my $cache_time = str2time($self->request->env->{HTTP_IF_MODIFIED_SINCE}); 
     317        return $mtime <= $cache_time ? 1 : 0; 
     318    } else { 
     319        return; 
     320    } 
     321} 
     322 
    310323sub view { 
    311324    my ($self, $id) = @_; 
     
    315328    my $bookmark = $self->get_bookmark({ id => $id }); 
    316329    if ($bookmark) { 
    317         # check If-Modified-Since header to return cache response 
    318         if ($self->request->env->{HTTP_IF_MODIFIED_SINCE}) { 
    319             my $cache_time = str2time($self->request->env->{HTTP_IF_MODIFIED_SINCE}); 
    320             if ($bookmark->mtime <= $cache_time) { 
    321                 return [304, [], []]; 
    322             } 
    323         } 
     330        return [304, [], []] if $self->_check_modified($bookmark->mtime); 
     331 
    324332        my $last_modified = time2str($bookmark->mtime); 
    325333         
     
    348356    my ($self, $id, $field) = @_; 
    349357 
    350     my $bookmark = $self->bookmarks->get_bookmark({ id => $id }); 
     358    my $bookmark = $self->get_bookmark({ id => $id }); 
    351359    if ($bookmark) { 
     360        return [304, [], []] if $self->_check_modified($bookmark->mtime); 
     361 
    352362        # respond with just the requested field as plain text 
    353363        my $value = eval { $bookmark->$field }; 
     
    359369            } 
    360370        } 
    361         return [200, ['Content-Type' => 'text/plain; charset=UTF-8'], [ref $value eq 'ARRAY' ? join(' ', @{ $value }) : $value]]; 
     371        my $last_modified = time2str($bookmark->mtime); 
     372        return [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Last-Modified' => $last_modified], [ref $value eq 'ARRAY' ? join(' ', @{ $value }) : $value]]; 
    362373    } else { 
    363374        return [404, ['Content-Type' => 'text/plain; charset=UTF-8'], ["Boomark $id not found"]]; 
Note: See TracChangeset for help on using the changeset viewer.