3 * http://www.ocsigen.org
4 * http_parser.mly Copyright (C) 2005 Denis Berthod
5 * Laboratoire PPS - CNRS Université Paris Diderot
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, with linking exception;
10 * either version 2.1 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 open Ocsigen_http_frame
25 let mode = ref Nofirstline
26 let proto = ref HTTP11
27 let headers = ref Http_headers.empty
29 let reset_header () = headers := Http_headers.empty
31 let add_header (n, v) = headers := Http_headers.add n v !headers
46 | "OPTIONS" -> OPTIONS
47 | "CONNECT" -> CONNECT
55 | "HTTP/1.1" -> HTTP11
56 | "HTTP/1.0" -> HTTP10
57 | s -> raise (Http_error.Http_exception (505, None, None))
62 let ind = String.index s ':' in
63 (String.lowercase (String.sub s 0 ind )),
64 String.lowercase (String.sub s (ind+1) ((String.length s) - ind-1) )
66 raise (Http_error.Http_exception (Some 400,["bad header format"]))
76 %start header nofirstline
77 %type <Ocsigen_http_frame.Http_header.http_header>header
78 %type <Ocsigen_http_frame.Http_header.http_header>nofirstline
82 | firstline EOL {make_header()}
83 | firstline lines EOL {make_header()}
86 | METHOD STRING end_of_firstline {reset_header ();
88 mode := Query (meth_of_string($1), $2^a);
90 | PROTO CODE strings EOL {reset_header ();
91 mode := Answer (int_of_string $2);
92 proto:=(proto_of_string $1)
96 | EOL {mode := Nofirstline;
99 | lines EOL {mode := Nofirstline;
104 | line {add_header $1}
105 | line lines {add_header $1;$2}
107 | strings EOL lines {$3}
110 | STRING COLON strings EOL {(Http_headers.name $1,$3)}
111 | CODE COLON strings EOL {(Http_headers.name $1,$3) (*XXX Check*)}
112 | STRING COLON EOL {(Http_headers.name $1,"")}
113 | CODE COLON EOL {(Http_headers.name $1,"") (*XXX Check*)}
114 /* EOL {split_string $1}*/
119 | STRING COLON strings {$1^":"^$3}
120 | STRING strings {$1^" "^$2}
122 | PROTO COLON strings {$1^":"^$3}
123 | PROTO strings {$1^" "^$2}
125 | METHOD COLON strings {$1^":"^$3}
126 | METHOD strings {$1^" "^$2}
128 | CODE strings {$1^" "^$2}
129 | CODE COLON strings {$1^":"^$3}
132 | PROTO EOL {("", proto_of_string $1)}
133 | COLON end_of_firstline {let (a, b) = $2 in (":"^a, b)}
134 | STRING end_of_firstline {let (a, b) = $2 in ($1^a, b)}