NAME

Dancer2::Plugin::HTTP::ContentNegotiation - Server-driven negotiation

VERSION

Version 0.01

SYNOPSIS

HTTP specifies two types of content negotiation. These are server-driven negotiation and agent-driven negotiation. Server-driven negotiation uses request headers to select a variant, and agent-driven negotiation uses a distinct URI for each variant.

This plugin handles server-driven negotiation.

use Dancer2;

use Dancer2::Plugin::HTTP::ContentNegotiation;

get '/greetings' => sub {
    http_choose_language (
        'en'    => sub { 'Hello World' },
        'en-GB' => sub { 'Hello London' },
        'en-US' => sub { 'Hello Washington' },
        'nl'    => sub { 'Hallo Amsterdam' },
        'de'    => sub { 'Hallo Berlin' },
        # default is first in the list
    );
};

get '/choose/:id' => sub {
    my $data = SomeResource->find(param('id'));
    http_choose_media_type (
        'application/json'  => sub { to_json $data },
        'application/xml '  => sub { to_xml $data },
        { default => undef }, # default is 406: Not Acceptable
    );
};

get '/thumbnail/:id' => sub {
    http_choose_media_type (
        [ 'image/png', 'image/gif', 'image/jpeg' ]
            => sub { Thumbnail->new(param('id'))->to(http_chosen->minor) },
        { default => 'image/png' }, # must be one listed above
    );
};

dance;

HTTP ContentNegotiation

Clients that make an HTTP request can specify what kind of response they prefer. This can be a specific MIME-type, a different language, the text-encoding (if it applies to text documents) and wether it should be compressed or not. For this, the HTTP specifications in RFC 7231 (HTTP/1.1 Semantics and Content) Section 5.3 explains how to use resp. Accept, Accept-Language, Accept-Charset and Accept-Encoding header fields.

The server can try to send a response that the client would accept, but if there is no respresentation avaialbe in that format or language, it has three options. Either give a response in a different way, or respond with a status message 406, Not Accaptable. Another option would provide a list of available formats.

DANCER2 KEYWORDS

Each of the 'http_choose_...' keywords take the following arguments:

a paired list with 'selectors' and coderefs.

those selectors, be it a single one or a anonymous array ref, numerate the available choices, the coderef following will be executed if there would be a match.

an optional hashref with options.

The only option there is at this moment is 'default'. If not present and there is no match, it will use the first mentioned selector. If spcified, it will take that selector. Set to undef will return a status code of 406, Not Acceptable.

http_choose_selector (
    selection_1
        => sub { ... },
    [ selction_2, selection_3, selection_4 ]
        => sub { ... },
    { default => selection_3 }
);

http_choose_media_type

This keyword is used to make a selection between different MIME-types. Please use this explicit version, as there is also http_choose (there is no Accept-MediaType, it's simply Accept)

http_choose_language

This keyword works in conjunction with the Accept-Language.

http_choose_charset

This keyword should only be used with non-binary media-types, like XML or JSON. It is used to select in what 'encoding' the representation should be delivered.

NOTE: not sure yet how this word with the default UTF-8 Encoding of Dancer2.

http_choose_encoding

Mainly used for specifying compressed or uncompressed content. It has nothing to do whith character encoding though!

NOTE: not sure if this is the right place to compress files or not - maybe it would be better of to do this in Middleware.

http_choose

Naming compatability with the HTTP Headers, please use te explicit 'http_choose_media_type'

http_chosen_media_type

returns a MediaType object that has been chosen.

This feature is experimental, but provides methods like type, major and minor

http_chosen_language

returns the LanguageTag being chosen from the selectors.

Experimental too and should privde methods like language, primary, extlang, script, region and variant

http_chosen_charset

returns the chosen Charset.

http_chose_encoding

returns wether or not the resouce should be compressed and how.

http_chosen

Naming compatability with the HTTP Headers, please use te explicit 'http_chosen_media_type'

CAVEATS

the underlying HTTP::ActionPack has it's own bugs - for the time being this module uses those modules and will suffer from many of the shortcommings that come from using ActionPack.

AUTHOR

Theo van Hoesel, <Th.J.v.Hoesel at THEMA-MEDIA.nl>

BUGS

Please report any bugs or feature requests to bug-dancer2-plugin-http-contentnegotiation at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer2-Plugin-HTTP-ContentNegotiation. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Dancer2::Plugin::HTTP::ContentNegotiation

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2015 Theo van Hoesel.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.