Apache2-API

 view release on metacpan or  search on metacpan

lib/Apache2/API/Headers/Accept.pm  view on Meta::CPAN

            type        => $type,
            subtype     => $subtype,
            quality     => $q,
            # We save it for later use
            params      => $params,
        });

        if( !exists( $best_q_for{ $media } ) || $q > $best_q_for{ $media } )
        {
            $best_q_for{ $media } = $q;
        }
    }

    # Keep only the records that have the best 'q' for their exact tag.
    @$elements = grep
    {
        my $keep = 0;
        if( exists( $best_q_for{ $_->{token} } ) )
        {
            $keep = ( $best_q_for{ $_->{token} } == $_->{quality} ) ? 1 : 0;
            delete( $best_q_for{ $_->{token} } ) if( $keep );
        }
        $keep;
    } @$elements;
    return( $elements );
}

sub _partial_match
{
    my( $self, $their, $our ) = @_;
    # Here, nothing beyond the type/* already handled in _full_match.
    return(0);
}

sub _specificity
{
    my( $self, $their, $our ) = @_;
    # The more there are wildcard parts on the preferences side, the more it is specific.
    # */* => 0, type/* => 1, type/subtype => 2
    return(2) if( $their->{type} ne '*' && $their->{subtype} ne '*' && $their->{type} eq $our->{type} && $their->{subtype} eq $our->{subtype} );
    return(1) if( $their->{type} eq $our->{type} && $their->{subtype} eq '*' );
    return(0);
}

1;
# NOTE: POD
__END__

=encoding utf-8

=head1 NAME

Apache2::API::Headers::Accept - Parser and matcher for HTTP Accept header

=head1 SYNOPSIS

    use Apache2::API::Headers::Accept;
    my $accept = Apache2::API::Headers::Accept->new('text/html;q=0.9,application/json');
    my $mime = $accept->match(['text/html', 'application/json']); # => 'text/html'
    # inspect client preferences (by q, desc):
    my $prefs = $a->preferences; # [ 'application/json', 'text/html' ]

=head1 DESCRIPTION

Parses the C<Accept> header and selects the best media type among the types you can serve. It supports exact matches (C<type/subtype>), type wildcards (C<type/*>), and full wildcards (C<*/*>), with quality values (C<q>) per RFC 7231 and RFC 9110.

It inherits from L<Apache2::API::Headers::AcceptCommon>.

The algorithm is as follows:

=over 4

=item * Specificity order (highest first): C<type/subtype> E<gt> C<type/*> E<gt> C<*/*>

=item * C<q> value primary sort; on tie, prefer more specific matches.

=item * C<*/*> at same C<q> never outranks a specific match; at strictly higher C<q> it picks the first supported item.

=item * Non-C<q> parameters (e.g. C<charset>, C<version>) do not influence matching; they are parsed but ignored for selection.

=back

=head1 CONSTRUCTOR

=head2 new( $header )

Creates a new instance with the given C<Accept> header string, and returns it.

If an error occurred, it sets an error that can be retrieved with the L<error method|Module::Generic/error>, and it returns C<undef> in scalar context, or an empty list in list context.

=head1 METHODS

=head2 match( \@supported_media_types )

Returns the best matching media type, as a string, from the provided list.

If no suitable media type could be found, it returns an empty string, so you need to check the return value if it is defined or not to differentiate from errors.

If an error occurred, it sets an error that can be retrieved with the L<error method|Module::Generic/error>, and it returns C<undef> in scalar context, or an empty list in list context.

=head2 media_types

This is an alias to L</preferences>

=head2 preferences

Read-only.

Returns an array reference of media types, submitted by the user in his HTTP request, sorted by decreasing quality (C<q>), with duplicates removed (highest C<q> kept).

If an error occurred, it sets an error that can be retrieved with the L<error method|Module::Generic/error>, and it returns C<undef> in scalar context, or an empty list in list context.

=head1 EXAMPLES

=head2 1. More specific beats wildcard at same q

    my $a = Apache2::API::Headers::Accept->new( 'text/html;q=0.9, text/*;q=0.9' );
    $a->match( [ 'text/plain', 'text/html' ] );
    # "text/html"

=head2 2. */* is a fallback only



( run in 1.655 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )