Create a gist now

Instantly share code, notes, and snippets.

@FROGGS /test_grammar.pl Secret
Created Jun 17, 2014

What would you like to do?
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