Show
Ignore:
Timestamp:
09/29/06 09:29:04 (3 years ago)
Author:
aimbert
Message:

add a path filter t the AtomStream:

- the AtomInject plugin receives a PUT request on a given path ("$put_path") with the content of an entry.
- the AtomStream plugin receives a GET request on another path ("$get_path/atom-stream.xml") (the arg 'since' is still allowed)

$put_path and $get_path can be '/' or '/a' or '/a/b', ....

then the AtomStream plugin produces a result filtered like this:

GET /atom-stream.xml returns everything ('/' or '/a' or '/a/b')
GET /a/atom-stream.xml returns the entries put with '/a' or '/a/b'
GET /a/b/atom-stream.xml returns the entries put with '/a/b'

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Perlbal/Plugin/AtomStream.pm

    r431 r580  
    11package Perlbal::Plugin::AtomStream; 
     2 
     3use URI; 
    24 
    35use Perlbal; 
     
    68 
    79our @subs;    # subscribers 
    8 our @recent;  # recent items in format [$epoch, $atom_ref] 
     10our @recent;  # recent items in format [$epoch, $atom_ref, $path_segments_arrayref] 
    911 
    1012our $last_timestamp = 0; 
     
    1416sub InjectFeed { 
    1517    my $class = shift; 
    16     my $atomref = shift; 
     18    my ($atomref, $path) = @_; 
    1719 
    1820    # maintain queue of last 60 seconds worth of posts 
    1921    my $now = time(); 
    20     push @recent, [ $now, $atomref ]; 
     22    my @put_segments = URI->new($path)->path_segments; 
     23    push @recent, [ $now, $atomref, \@put_segments ]; 
    2124    shift @recent while @recent && $recent[0][0] <= $now - 60; 
    2225 
     
    3033        } 
    3134 
     35        next unless filter(\@put_segments, $s->{scratch}{get_segments}); 
     36         
    3237        my $lag = $s->{write_buf_size}; 
    3338 
     
    5863} 
    5964 
     65sub filter { 
     66    my ($put, $get) = @_; 
     67    return 0 if scalar @$put < scalar @$get; 
     68    for( my $i = 0 ; $i < scalar @$get ; $i++) { 
     69        return 0 if $put->[$i] ne $get->[$i]; 
     70    } 
     71    return 1; 
     72} 
     73 
    6074# called when we're being added to a service 
    6175sub register { 
     
    7286        my Perlbal::HTTPHeaders $hds = $self->{req_headers}; 
    7387        return 0 unless $hds; 
    74         my $uri = $hds->request_uri; 
    75         return 0 unless $uri =~ m!^/atom-stream\.xml(?:\?since=(\d+))?$!; 
    76         my $since = $1 || 0; 
     88        my $uri = URI->new($hds->request_uri); 
     89        my @get_segments = $uri->path_segments; 
     90        $self->{scratch}{get_segments} = \@get_segments; 
     91        return 0 unless pop @get_segments eq 'atom-stream.xml'; 
     92        my %params = $uri->query_form; 
     93        my $since = $params{since} =~ /\d+/ ? $params{since} : 0; 
    7794 
    7895        my $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response(200); 
     
    90107            foreach my $item (@recent) { 
    91108                next if $item->[0] < $since; 
     109                next unless filter($item->[2], \@get_segments); 
    92110                $last_rv = $self->write($item->[1]); 
    93111            }