By default, RawHttp
will fix the following common mistakes when parsing a HTTP message:
Line separators
According to RFC-7230, which defines the HTTP/1.1 message format,
line separators must be CRLF
, or \r\n
.
However, by default, RawHttp
allows a simple \n
to be used:
import rawhttp.core.*;
RawHttp http = new RawHttp();
RawHttpRequest request = http.parseRequest(
"GET /hello HTTP/1.1\n" +
"Host: example.com");
HTTP version
For simplicity, the HTTP version can be omitted from HTTP messages, in which case HTTP/1.1
is used:
import rawhttp.core.*;
RawHttp http = new RawHttp();
RawHttpRequest request = http.parseRequest(
"GET /hello\n" +
"Host: example.com");
Host header
The Host
header is mandatory in HTTP requests. But RawHttp
allows specifying a full URL in the method-line,
which makes it automatically add a Host
header to the request in case it’s missing:
import rawhttp.core.*;
RawHttp http = new RawHttp();
RawHttpRequest request = http.parseRequest(
"GET http://example.com/hello");
Leading new-line in message
RFC-7230 recommends that HTTP message receivers ignore a leading new-line for the sake of robustness and historical reasons.
RawHttp
will do that by default:
import rawhttp.core.*;
RawHttp http = new RawHttp();
RawHttpRequest request = http.parseRequest(
// this is ok!
"\nGET http://example.com/hello");
Configuring RawHttp
The RawHttp
class has a constructor which takes a RawHttpOptions
instance, allowing for all of the fix-ups
mentioned above to be turned off:
import rawhttp.core.*;
RawHttp strictHttp = new RawHttp(RawHttpOptions.newBuilder()
.doNotAllowNewLineWithoutReturn()
.doNotInsertHostHeaderIfMissing()
.doNotInsertHttpVersionIfMissing()
.doNotIgnoreLeadingEmptyLine()
.build());
Allowing unescaped URIs
By default, RawHTTP is strict when parsing URIs. However, you can make it lenient so that you don’t need to escape
all forbidden characters (e.g. whitespaces don’t need to be given as %20
).
import rawhttp.core.*;
RawHttp lenientUriOptions = new RawHttp(RawHttpOptions.newBuilder()
.allowIllegalStartLineCharacters()
.build());
With this enabled, you can give URIs more easily:
GET https://www.example.com/name=Joe Doe&age=43 HTTP/1.1
Accept: text/html