AnyEvent-HTTPD-Router

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

    - `routes`

        You can add the routes at the constructor. This is an ArrayRef.

    - `known_methods`

        Whenever you register a new route this modules checks if the method is either
        customer method prefixed with ':' or a $known\_method. You would need to change
        this, if you would like to implement WebDAV, for example. This is an ArrayRef.

    - `auto_respond_404`

        If the value for this parameter is set to true a a simple `404` responder will
        be installed that responds if not route matches. You can implement your own
        handler see [EVENTS](https://metacpan.org/pod/EVENTS).

- `reg_routes( [$method, $path, $callback]* )`

    You can add further routes with this method. Multiple routes can be added at
    once. To add a route you need do add 3 parameters: <method>, <path>, <callback>.

- `*`

README.md  view on Meta::CPAN

# EVENTS

- no\_route\_found => $request

    When the dispatcher can not find a route that matches on your request, the
    event `no_route_found` will be emitted.

    In the case that routes and callbacks (`reg_cb()`) for paths as used with
    `AnyEvent::HTTPD` are mixed, keep in mind that that `no_route_found` will
    happen before the other path callbacks are executed. So for a
    `404 not found` handler you could do

        $httpd->reg_cb('' => sub {
            my ( $httpd, $req ) = @_;
            $req->respond( [ 404, 'not found', {}, '' ] );
        });

    If you just use `reg_routes()` and don't mix with `reg_cb()` for paths you
    could implement the `404 not found` handler like this:

        $httpd->reg_cb('no_route_found' => sub {
            my ( $httpd, $req ) = @_;
            $req->respond( [ 404, 'not found', {}, '' ] );
        });

    This is exactly what you get if you specify `auto_respond_404` at the
    constructor.

- See ["EVENTS" in AnyEvent::HTTPD](https://metacpan.org/pod/AnyEvent::HTTPD#EVENTS)

# WRITING YOUR OWN ROUTE DISPATCHER

If you want to change the implementation of the dispatching you specify the
`dispatcher` or `dispatcher_class`. You need to implement the `match()`
method.

lib/AnyEvent/HTTPD/Router.pm  view on Meta::CPAN

our $VERSION = '1.0.1';

sub new {
    my $this  = shift;
    my $class = ref($this) || $this;
    my %args  = @_;

    # todo documentation how to overwrite your dispathing
    my $dispatcher       = delete $args{dispatcher};
    my $routes           = delete $args{routes};
    my $auto_respond_404 = delete $args{auto_respond_404};
    my $dispatcher_class = delete $args{dispatcher_class}
        || 'AnyEvent::HTTPD::Router::DefaultDispatcher';
    my $known_methods    = delete $args{known_methods}
        || [ qw/GET HEAD POST PUT PATCH DELETE TRACE OPTIONS CONNECT/ ];

    my $self = $class->SUPER::new(%args);

    $self->{known_methods} = $known_methods;
    $self->{dispatcher}    = defined $dispatcher
        ? $dispatcher

lib/AnyEvent/HTTPD/Router.pm  view on Meta::CPAN

            my $req  = shift;
            my $matched = $self->dispatcher->match( $self, $req );
            unless ($matched) {
                $self->event( 'no_route_found' => $req );
            }
        },
    );

    $self->reg_cb('no_route_found' => sub {
        my ( $httpd, $req ) = @_;
        $req->respond( [ 404, 'not found', {}, '' ] );
    }) if $auto_respond_404;

    if ($routes) {
        $self->reg_routes( @$routes );
    }

    return $self;
}

sub dispatcher { shift->{dispatcher} }

lib/AnyEvent/HTTPD/Router.pm  view on Meta::CPAN

=item * C<routes>

You can add the routes at the constructor. This is an ArrayRef.

=item * C<known_methods>

Whenever you register a new route this modules checks if the method is either
customer method prefixed with ':' or a $known_method. You would need to change
this, if you would like to implement WebDAV, for example. This is an ArrayRef.

=item * C<auto_respond_404>

If the value for this parameter is set to true a a simple C<404> responder will
be installed that responds if not route matches. You can implement your own
handler see L<EVENTS>.

=back

=item * C<reg_routes( [$method, $path, $callback]* )>

You can add further routes with this method. Multiple routes can be added at
once. To add a route you need do add 3 parameters: <method>, <path>, <callback>.

lib/AnyEvent/HTTPD/Router.pm  view on Meta::CPAN

=over

=item * no_route_found => $request

When the dispatcher can not find a route that matches on your request, the
event C<no_route_found> will be emitted.

In the case that routes and callbacks (C<reg_cb()>) for paths as used with
C<AnyEvent::HTTPD> are mixed, keep in mind that that C<no_route_found> will
happen before the other path callbacks are executed. So for a
C<404 not found> handler you could do

    $httpd->reg_cb('' => sub {
        my ( $httpd, $req ) = @_;
        $req->respond( [ 404, 'not found', {}, '' ] );
    });

If you just use C<reg_routes()> and don't mix with C<reg_cb()> for paths you
could implement the C<404 not found> handler like this:

    $httpd->reg_cb('no_route_found' => sub {
        my ( $httpd, $req ) = @_;
        $req->respond( [ 404, 'not found', {}, '' ] );
    });

This is exactly what you get if you specify C<auto_respond_404> at the
constructor.

=item * See L<AnyEvent::HTTPD/EVENTS>

=back

=head1 WRITING YOUR OWN ROUTE DISPATCHER

If you want to change the implementation of the dispatching you specify the
C<dispatcher> or C<dispatcher_class>. You need to implement the C<match()>

t/04_not_found.t  view on Meta::CPAN

$h->reg_routes(
    GET => '/foo' => sub {
        my ( $httpd, $req ) = @_;
        $req->respond( [ 200, 'ok', {}, 'GET OK' ] );
        $h->stop_request;
    },
);
$h->reg_cb(
    'no_route_found' => sub {
        my ( $httpd, $req ) = @_;
        $req->respond( [ 404, 'not found', {}, '' ] );
        $h->stop_request;
    }
);

my $c = AnyEvent->condvar;
http_request(
    GET => sprintf( "http://%s:%d/foo", '127.0.0.1', $h->port ),
    sub {
        my ( $body, $hdr ) = @_;
        ok( $hdr->{'Status'} == 200, "resp GET 200 Not Found" )

t/04_not_found.t  view on Meta::CPAN

        $c->send;
    }
);
$c->recv;

$c = AnyEvent->condvar();
http_request(
    GET => sprintf( "http://%s:%d/bar", '127.0.0.1', $h->port ),
    sub {
        my ( $body, $hdr ) = @_;
        ok( $hdr->{'Status'} == 404, "resp GET 404 Not Found" )
            or diag explain $hdr;
        $c->send;
    }
);
$c->recv;

done_testing();



( run in 0.903 second using v1.01-cache-2.11-cpan-39bf76dae61 )