Skip to content

Instantly share code, notes, and snippets.

@FROGGS
Created June 17, 2014 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FROGGS/2bcba957bd4d0abd0249 to your computer and use it in GitHub Desktop.
Save FROGGS/2bcba957bd4d0abd0249 to your computer and use it in GitHub Desktop.
my $CRLF = "\r\n";
my grammar HTTP-message {
token TOP {
:my $*transfer-encoding;
[
| <!http-version> <request>
| <?http-version> <response>
]
}
token request {
<request-line>
[ <message-header> $CRLF ]*
$CRLF
<message-body>
}
token response {
<response-line>
[ <message-header> $CRLF ]*
$CRLF
<message-body>
}
token request-line {
<method> ' ' $<request-uri>=[ .+? ] ' ' <http-version> $CRLF
}
token response-line {
<http-version> ' ' $<status-code>=[ \d ** 3 ] ' ' $<reason-phrase>=[ \V+ ] $CRLF
}
token http-version {
| 'HTTP/1.0'
| 'HTTP/1.1'
}
token message-header {
$<field-name>=[ <-[:]>+ ] ':' <field-value>
{
if $<field-name>.Str.lc eq 'transfer-encoding' {
$*transfer-encoding = $<field-value>.trim;
}
}
}
token field-value {
[ <!before \h> $<field-content>=[ <-[\r\n]>+ ] | \h+ ]*
}
token method {
OPTIONS | GET | HEAD | POST | PUT | DELETE | TRACE | CONNECT | $<extension-method>=[ \w+ ]
}
token message-body {
.*
}
}
my $msg =
"HTTP/1.1 200 OK\r\n"
~ "Server: Apache/2.2.3 (CentOS)\r\n"
~ "Last-Modified: Sat, 31 May 2014 16:39:02 GMT\r\n"
~ "ETag: \"16d3e2-20416-4fab4ccb03580\"\r\n"
~ "Vary: Accept-Encoding\r\n"
~ "Content-Type: text/plain; charset=UTF-8\r\n"
~ "Date: Mon, 02 Jun 2014 17:07:52 GMT\r\n"
~ "X-Varnish: 1992382947 1992382859\r\n"
~ "Age: 40\r\n"
~ "Via: 1.1 varnish\r\n"
~ "Connection: close\r\n"
~ "X-Cache: HIT\r\n"
~ "X-Cache-Hits: 2\r\n"
~ "\r\n"
~ "008000\r\n"
~ "# Last updated Sat May 31 16:39:01 2014 (UTC)\n"
~ "# \n"
~ "# Explanation of the syntax:\n";
say HTTP-message.parse($msg);
#~ say HTTP-message.parse("Server: Apache/2.2.3 (CentOS)\r\n", :rule<message-header>);
#~ say HTTP-message.parse($msg, :rule<response>);
#~ say HTTP-message.parse("HTTP/1.1 200 OK\r\n", :rule<response-line>);
#~ say HTTP-message.parse("008000\r\n# Last updated Sat May 31 16:39:01 2014 (UTC)\n# \n# Explanation of the syntax:\n", :rule<message-body>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment