NAME

HTTP::libsfparse - parse RFC 9651 HTTP Structured Fields using libsfparse

VERSION

version v0.0.1

SYNOPSIS

use HTTP::libsfparse qw(
    parse_item parse_list parse_dictionary :types :index
);

# Every item is [ $type, $value, $params ], and the positions have
# names, so none of this is counted out by hand.

# Single item
my $item = parse_item('text/html;q=0.8');
$item->[SFP_TYPE] == SFPARSE_TYPE_TOKEN;     # true
$item->[SFP_VALUE];                          # 'text/html'
$item->[SFP_PARAMS][0][SFP_KEY];             # 'q'
$item->[SFP_PARAMS][0][SFP_ITEM][SFP_VALUE]; # 0.8

# Dictionary: RFC 9218 priority. A dictionary is a list of pairs.
my $pri = parse_dictionary('u=5, i');
$pri->[0][SFP_KEY];                          # 'u'
$pri->[0][SFP_ITEM][SFP_VALUE];              # 5
$pri->[1][SFP_ITEM][SFP_TYPE] == SFPARSE_TYPE_BOOLEAN;   # true

# Pairs are easy to index when you do not mind losing duplicate keys
my %pri = map { $_->[SFP_KEY] => $_->[SFP_ITEM] } @$pri;
$pri{u}[SFP_VALUE];                          # 5

# List, with an inner list and parameters
my $list = parse_list('(1 2 3;b="foo"), bar, baz;f=:aGVsbG8=:');
my $inner = $list->[0][SFP_VALUE];

$list->[0][SFP_TYPE] == SFPARSE_TYPE_INNER_LIST;         # true
$inner->[1][SFP_VALUE];                                  # 2
$inner->[2][SFP_PARAMS][0][SFP_ITEM][SFP_VALUE];         # 'foo'
$list->[2][SFP_PARAMS][0][SFP_ITEM][SFP_VALUE];          # 'hello'

# Malformed input throws
my $ok = eval { parse_list('a, b, "unterminated'); 1 };

DESCRIPTION

This module is an XS binding to libsfparse, the C parser for HTTP Structured Field Values (RFC 9651, which obsoletes RFC 8941) written for the ngtcp2 and nghttp3 QUIC/HTTP3 stacks.

It uses a bundled copy of libsfparse, and not a version that may already be installed on your system.

Format of the Parsed Data

Every parsed item is an array reference of three elements:

[ $type, $value, $params ]

  0   SFP_TYPE     one of the type constants below
  1   SFP_VALUE    the value itself
  2   SFP_PARAMS   an array reference of [ $key, $item ] pairs

The names in that second column are constants for the positions.

Types

$type is one of the following constants, which are libsfparse's own sfparse_type enumerators and carry its values. Import them individually or as :types.

SFPARSE_TYPE_BOOLEAN

$value is 1 or 0.

SFPARSE_TYPE_INTEGER

$value is the integer. RFC 9651 limits these to 15 digits, so they are always exact, including on a Perl built with 32 bit integers.

SFPARSE_TYPE_DECIMAL

$value is the number as a Perl floating point number. RFC 9651 allows at most twelve digits before the decimal point and three after it, so every legal decimal multiplied by a thousand is a whole number below 2**53: the conversion is exact, and 1.001 compares equal to the literal 1.001.

SFPARSE_TYPE_STRING

$value is the string with its \\ escapes removed. RFC 9651 strings only hold printable ASCII.

SFPARSE_TYPE_TOKEN

$value is the token, for example text/html or gzip.

SFPARSE_TYPE_BYTESEQ

$value is the decoded byte string, not the Base64 text.

SFPARSE_TYPE_DATE

$value is the number of seconds since the Unix epoch, and may be negative.

SFPARSE_TYPE_DISPSTRING

$value is the percent-decoded display string, as a character string with Perl's UTF-8 flag set. libsfparse has already validated the UTF-8.

SFPARSE_TYPE_INNER_LIST

$value is an array reference of items, each in this same form, each with its own parameters. The inner list's own parameters are in $params of the inner list itself.

Parameters

$params is always present on items returned by the functions in this module, and is an empty array reference when the item has no parameters. It holds one [ $key, $item ] pair per parameter, in field order, and each $item is itself in the form above (its own $params is undef, since parameters cannot nest).

A parameter with no explicit value is boolean true, as the RFC requires.

Keys are reported as they appear and are not deduplicated. which is what lets a caller reject a field value that repeats one. RFC 9651 says the last occurrence of a repeated key wins; collapsing them that way is one line:

my %params = map { $_->[0] => $_->[1] } @$params;

Conformance

The distribution bundles the HTTP Working Group's structured-field-tests conformance suite and runs it as t/13-httpwg-suite.t: 1607 cases, 873 of them field values that must be rejected.

All of them pass. One passes by exemption rather than agreement: libsfparse rejects a byte sequence carrying extra Base64 padding, which the suite marks can_fail, so either behaviour conforms.

Bytes, not Characters

HTTP field values are ASCII byte strings, and that is what these functions expect. A string is accepted as long as it contains nothing above U+007E.

Display strings (%"...") are the one place where RFC 9651 carries real Unicode, and this module hands those back decoded, with the UTF-8 flag set.

This module has a hard size limit of 32KiB for byte strings. Note that most web servers will default to much lower limits, usually 8KiB.

NAME

HTTP::libsfparse - parse RFC 9651 HTTP Structured Fields using libsfparse

VERSION

version v0.0.1

EXPORTS

parse_item

my $item = parse_item($string);

Parses a field value consisting of a single item and returns one item. Trailing content after the item is a parse error, as is an empty field value.

parse_list

my $list = parse_list($string);

Parses a List and returns an array reference of items. An empty field value yields an empty array reference, which is what RFC 9651 calls an empty list.

parse_dictionary

my $dict = parse_dictionary($string);

Parses a Dictionary and returns an array reference of [ $key, $item ] pairs in field order. An empty field value yields an empty array reference.

As with parameters, keys are not deduplicated, so a field value that repeats one reports it twice; see "Parameters" for why, and for the one-liner that collapses them the way RFC 9651 says to.

before:EXPORTS

Nothing is exported by default

KNOWN ISSUES

libsfparse also provides sfparse_unescape, sfparse_base64decode and sfparse_pctdecode. Those functions assume their input has already been validated by the parser and that the output buffer is large enough; handed arbitrary Perl strings they would read and write out of bounds. They are used internally on parser-produced values only, and are not exported.

SEE ALSO

HTTP::StructuredField provides an object-oriented interface.

RFC 9651, Structured Field Values for HTTP.

RFC 9218, Extensible Prioritization Scheme for HTTP, whose Priority field is a dictionary.

libsfparse and its documentation.

SOURCE

The development version is on github at https://github.com/robrwo/perl-HTTP-StructuredField and may be cloned from https://github.com/robrwo/perl-HTTP-StructuredField.git

SUPPORT

Only the latest release of this module will be supported.

This module requires Perl v5.20 or later. Only Perl versions released in the past ten years will be supported.

Reporting Bugs and Submitting Feature Requests

Please report any bugs or feature requests on the bugtracker website https://github.com/robrwo/perl-HTTP-StructuredField/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.

If the bug you are reporting has security implications which make it inappropriate to send to a public issue tracker, then see SECURITY.md for instructions how to report security vulnerabilities.

AUTHOR

Robert Rothenberg <perl@rhizomnic.com>

This wrapper was written with the assistance of Claude Opus.

CONTRIBUTOR

Paul Johnson <paul@pjcj.net>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2026 by Robert Rothenberg.

This is free software, licensed under:

The MIT (X11) License

The bundled copy of libsfparse in src/ is Copyright (c) 2023 Tatsuhiro Tsujikawa and other sfparse contributors. It is distributed under the MIT license; see src/LICENSE.sfparse. It is included unmodified.