http: parser handles IPv6 bracketed IP hostnames
[unicorn.git] / ext / unicorn_http / unicorn_http_common.rl
blobcf93fece921dd115856f122f029f20ebbd2305cf
1 %%{
3   machine unicorn_http_common;
5 #### HTTP PROTOCOL GRAMMAR
6 # line endings
7   CRLF = "\r\n";
9 # character types
10   CTL = (cntrl | 127);
11   safe = ("$" | "-" | "_" | ".");
12   extra = ("!" | "*" | "'" | "(" | ")" | ",");
13   reserved = (";" | "/" | "?" | ":" | "@" | "&" | "=" | "+");
14   sorta_safe = ("\"" | "<" | ">");
15   unsafe = (CTL | " " | "#" | "%" | sorta_safe);
16   national = any -- (alpha | digit | reserved | extra | safe | unsafe);
17   unreserved = (alpha | digit | safe | extra | national);
18   escape = ("%" xdigit xdigit);
19   uchar = (unreserved | escape | sorta_safe);
20   pchar = (uchar | ":" | "@" | "&" | "=" | "+");
21   tspecials = ("(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\\" | "\"" | "/" | "[" | "]" | "?" | "=" | "{" | "}" | " " | "\t");
22   lws = (" " | "\t");
24 # elements
25   token = (ascii -- (CTL | tspecials));
27 # URI schemes and absolute paths
28   scheme = ( "http"i ("s"i)? ) $downcase_char >mark %scheme;
29   hostname = ((alnum | "-" | "." | "_")+ | ("[" (":" | xdigit)+ "]"));
30   host_with_port = (hostname (":" digit*)?) >mark %host;
31   userinfo = ((unreserved | escape | ";" | ":" | "&" | "=" | "+")+ "@")*;
33   path = ( pchar+ ( "/" pchar* )* ) ;
34   query = ( uchar | reserved )* %query_string ;
35   param = ( pchar | "/" )* ;
36   params = ( param ( ";" param )* ) ;
37   rel_path = (path? (";" params)? %request_path) ("?" %start_query query)?;
38   absolute_path = ( "/"+ rel_path );
39   path_uri = absolute_path > mark %request_uri;
40   Absolute_URI = (scheme "://" userinfo host_with_port path_uri);
42   Request_URI = ((absolute_path | "*") >mark %request_uri) | Absolute_URI;
43   Fragment = ( uchar | reserved )* >mark %fragment;
44   Method = (token){1,20} >mark %request_method;
45   GetOnly = "GET" >mark %request_method;
47   http_number = ( digit+ "." digit+ ) ;
48   HTTP_Version = ( "HTTP/" http_number ) >mark %http_version ;
49   Request_Line = ( Method " " Request_URI ("#" Fragment){0,1} " " HTTP_Version CRLF ) ;
51   field_name = ( token -- ":" )+ >start_field $snake_upcase_field %write_field;
53   field_value = any* >start_value %write_value;
55   value_cont = lws+ any* >start_value %write_cont_value;
57   message_header = ((field_name ":" lws* field_value)|value_cont) :> CRLF;
58   chunk_ext_val = token*;
59   chunk_ext_name = token*;
60   chunk_extension = ( ";" " "* chunk_ext_name ("=" chunk_ext_val)? )*;
61   last_chunk = "0"+ chunk_extension CRLF;
62   chunk_size = (xdigit* [1-9a-fA-F] xdigit*) $add_to_chunk_size;
63   chunk_end = CRLF;
64   chunk_body = any >skip_chunk_data;
65   chunk_begin = chunk_size chunk_extension CRLF;
66   chunk = chunk_begin chunk_body chunk_end;
67   ChunkedBody := chunk* last_chunk @end_chunked_body;
68   Trailers := (message_header)* CRLF @end_trailers;
70   FullRequest = Request_Line (message_header)* CRLF @header_done;
71   SimpleRequest = GetOnly " " Request_URI ("#"Fragment){0,1} CRLF @header_done;
73 main := FullRequest | SimpleRequest;
75 }%%