The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

HTTP::Headers::Fancy - Fancy naming schema of HTTP headers

VERSION

version 1.001

SYNOPSIS

    my %fancy = decode_hash('content-type' => ..., 'x-foo-bar-baf-baz' => ...);
    my $content_type = $fancy{ContentType};
    my $x_foo_bar_baf_baz = $fancy{-FooBarBafBaz};

    my %headers = encode_hash(ContentType => ..., -foo_bar => ...);
    # %headers = ('content-type' => ..., 'x-foo-bar' => ...);

DESCRIPTION

This module provides method for renaming HTTP header keys to a lightier, easier-to-use format.

METHODS

new

Creates a new instance of ourself. No options are supported.

    my $fancy = HTTP::Headers::Fancy->new;

encode

Wrapper for "encode_hash" or "encode_key", depending on what is given.

    $fancy->encode(%hash)    # encode_hash(%hash);
    $fancy->encode($hashref) # encode_hash($hashref);
    $fancy->encode($scalar)  # encode_key($scalar);

decode

Wrapper for "decode_hash" or "decode_key", depending on what is given

    $fancy->decode(%hash)    # decode_hash(%hash);
    $fancy->decode($hashref) # decode_hash($hashref);
    $fancy->decode($scalar)  # decode_key($scalar);

split

Wrapper for "split_field_list" or "split_field_hash", depending on what is given

    $fancy->split(q{"a", "b", "c"}) # split_field_list(...)
    $fancy->split(q{W/"a", ...})    # split_field_list(...)
    $fancy->split(q{no-cache})      # split_field_hash(...)

Or deflate a HashRef directly:

    $headers = $fancy->decode(...);
    $fancy->split($headers, qw(CacheControl Etag));
    $headers->{CacheControl}->{NoCache};
    $headers->{Etag}->[0];

build

Wrapper for "build_field_hash" or "build_field_list"

    $fancy->build(NoCache => undef) # build_field_hash(...)
    $fancy->build({ ... }) # build_field_hash(...)
    $fancy->build([ ... ]) # build_field_list(...)

etags

Wrapper for "build_field_list"

    $fancy->build('a', \'b', 'c') # build_field_list(...)

FUNCTIONS

decode_key

Decode original HTTP header name

    my $new = decode_key($old);

The header field name will be separated by the dash ('-') sign into pieces. Every piece will be lowercased and the first character uppercased. The pieces will be concatenated.

    # Original  ->  Fancy
    # Accept        Accept
    # accept        Accept
    # aCCEPT        Accept
    # Acc-Ept       AccEpt
    # Content-Type  ContentType
    # a-b-c         ABC
    # abc           Abc
    # a-bc          ABc
    # ab-c          AbC

Experimental headers starting with X- will be accessable by a preleading dash sign:

    # Original  ->  Fancy
    # x-abc         -Abc

decode_hash

Decode a hash (or HashRef) of HTTP headers and rename the keys

    my %new_hash = decode_hash(%old_hash);
    my $new_hashref = decode_hash($old_hashref);

encode_key

Encode fancy key name to a valid HTTP header key name

    my $new = encode_key($old);

Any uppercase (if not at beginning) will be prepended with a dash sign. Underscores will be replaced by a dash-sign too. The result will be lowercased.

    # Fancy -> Original
    # FooBar   foo-bar
    # foo_bar  foo-bar
    # FoOoOoF  fo-oo-oo-f

Experimental headers starting with X- are also createable with a preleading dash sign:

    # Fancy -> Original
    # -foo     x-foo

encode_hash

Encode a hash (or HashRef) of HTTP headers and rename the keys

Removes also a keypair if a value in undefined.

    my %new_hash = encode_hash(%old_hash);
    my $new_hashref = encode_hash($old_hashref);

prettify_key

Reformats a key with all lowercase and each part uppercase first.

    my $pretty_key = prettify_key('foo-bar');

Examples:

    # Unpretty  ->  Pretty
    # foo-bar       Foo-Bar
    # x-fooof       X-Fooof
    # ABC-DEF       Abc-Def

Since a HTTP header parser ignores the case, this is just for a nice human-readable output.

split_field_hash

Split a HTTP header field into a hash with decoding of keys

    my %cc = split_field('no-cache, s-maxage=5');
    # %cc = (NoCache => undef, SMaxage => 5);

Or deflate fields in a hashref directly:

    # First, get a fancy hashref of header fields;
    my $headers = decode_hash(...);
    # Second deflate the CacheControl field
    split_field_hash($headers, qw( CacheControl ));
    # Then access the fancy way
    $headers->{CacheControl}->{SMaxage};

The first argument has to be a hashref, all other argument a list of fields to be deflated by split_field_hash

split_field_list

Split a field into pieces

    my @list = split_field('"a", "b", "c"');
    # @list = qw( a b c );

Weak values are stored as ScalarRef

    my @list = split_field('"a", W/"b"', W/"c"');
    # @list = ('a', \'b', \'c');

Or deflate fields in a hashref directly:

    # First, get a fancy hashref of header fields;
    my $headers = decode_hash(...);
    # Second deflate the Etag field
    split_field_list($headers, qw( Etag ));
    # Then access the fancy way
    $headers->{Etag}->[0];

The first argument has to be a hashref, all other argument a list of fields to be deflated by split_field_list

build_field_hash

The opposite method of "split_field_hash" with encoding of keys.

    my $field_value = build_field_hash(NoCache => undef, MaxAge => 3600);
    # $field_value = 'no-cache,maxage=3600'

An HashRef as first argument is interpreted as hash

    build_field_hash({ NoCache => undef })

build_field_list

Build a list from pieces

    my $field_value = build_field_list(qw( a b c ));
    # $field_value = '"a", "b", "c"'

ScalarRefs evaluates to a weak value

    my $field_value = build_field_list('a', \'b', \'c');
    # $field_value = '"a", W/"b", W/"c"';

An ArrayRef as first argument is interpreted as list

    build_field_list([ 'a', 'b', ... ])

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/zurborg/libhttp-headers-fancy-perl/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

David Zurborg <zurborg@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2015 by David Zurborg.

This is free software, licensed under:

  The ISC License