A parser for the HTTP Structured Fields syntax defined in IETF RFC 9651.
- Parsing and Serialization
- (Still) supports Java 8
- Zero dependencies (except for tests)
- Comprehensive parsing diagnostics
- https://greenbytes.de/tech/specs/rfc9651.html
- https://www.rfc-editor.org/rfc/rfc9651.html
- https://github.com/httpwg/structured-header-tests
- API docs: https://reschke.github.io/structured-fields/apidocs/
{
Parser p = new Parser("a=?0, b, c; foo=bar");
Dictionary d = p.parseDictionary();
for (Map.Entry<String, Item<? extends Object>> e : d.get()) {
String key = e.getKey();
Item<? extends Object> item = e.getValue();
Object value = item.get();
Parameters params = item.getParams();
System.out.println(key + " -> " + value + (params.isEmpty() ? "" : (" (" + params.serialize() + ")")));
}
}gives:
a -> false
b -> true
c -> true (;foo=bar)
Here's a command line tool which will feed all arguments into the parser (as if obtained from multiple field lines), parsed as Item, List, or Dictionary:
$ java -jar target/structured-fields-0.6-SNAPSHOT.jar ':cHJldGVuZCB0aGlzIGlzIGJpbmFyeSBjb250ZW50Lg==:' 'x'
Item: >>:cHJldGVuZCB0aGlzIGlzIGJpbmFyeSBjb250ZW50Lg==:,x<<
----------------------------------------------^ (0x2c) Extra characters in string parsed as Item
List: :cHJldGVuZCB0aGlzIGlzIGJpbmFyeSBjb250ZW50Lg==:, x (OuterList)
Dict: >>:cHJldGVuZCB0aGlzIGlzIGJpbmFyeSBjb250ZW50Lg==:,x<<
^ (0x3a) Key must start with LCALPHA or '*': ':' (\u003a)
This implementation is experimental and makes no promises yet on API stability (feedback on what might be missing is appreciated).
In the mid-term, this code might transition to the Apache HTTP Components project.
<dependency>
<groupId>org.greenbytes.http</groupId>
<artifactId>structured-fields</artifactId>
<version>0.5</version>
</dependency>