From: Stefano Zacchiroli Date: Mon, 29 Jan 2007 10:43:50 +0000 (+0000) Subject: preliminary cookie support X-Git-Tag: 0.4.95@7852~645 X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=commitdiff_plain;h=c55e18b2a3910bbb30f5138079dd1c68d6d0908e;p=helm.git preliminary cookie support --- diff --git a/DEVEL/ocaml-http/.depend b/DEVEL/ocaml-http/.depend index 7c4762fd1..b514ab3f2 100644 --- a/DEVEL/ocaml-http/.depend +++ b/DEVEL/ocaml-http/.depend @@ -1,3 +1,5 @@ +cookie_lexer.cmo: cookie_lexer.cmi +cookie_lexer.cmx: cookie_lexer.cmi http_common.cmo: http_types.cmi http_constants.cmi http_common.cmi http_common.cmx: http_types.cmx http_constants.cmx http_common.cmi http_constants.cmo: http_constants.cmi @@ -15,9 +17,9 @@ http_message.cmx: http_types.cmx http_parser_sanity.cmx http_misc.cmx \ http_misc.cmo: http_types.cmi http_misc.cmi http_misc.cmx: http_types.cmx http_misc.cmi http_parser.cmo: http_types.cmi http_parser_sanity.cmi http_constants.cmi \ - http_common.cmi http_parser.cmi + http_common.cmi cookie_lexer.cmi http_parser.cmi http_parser.cmx: http_types.cmx http_parser_sanity.cmx http_constants.cmx \ - http_common.cmx http_parser.cmi + http_common.cmx cookie_lexer.cmx http_parser.cmi http_parser_sanity.cmo: http_types.cmi http_constants.cmi \ http_parser_sanity.cmi http_parser_sanity.cmx: http_types.cmx http_constants.cmx \ diff --git a/DEVEL/ocaml-http/Makefile b/DEVEL/ocaml-http/Makefile index 540464f29..b3d74b10b 100644 --- a/DEVEL/ocaml-http/Makefile +++ b/DEVEL/ocaml-http/Makefile @@ -2,9 +2,20 @@ include Makefile.defs export SHELL=/bin/bash MODULES = \ - http_constants http_types http_parser_sanity http_misc http_common \ - http_tcp_server http_parser http_message http_request http_daemon \ - http_response http_user_agent + http_constants \ + http_types \ + http_parser_sanity \ + http_misc \ + http_common \ + http_tcp_server \ + cookie_lexer \ + http_parser \ + http_message \ + http_request \ + http_daemon \ + http_response \ + http_user_agent \ + $(NULL) THREADED_SRV = http_threaded_tcp_server MODULES_MT = $(patsubst http_tcp_server, mt/$(THREADED_SRV) http_tcp_server, $(MODULES)) @@ -55,6 +66,8 @@ include .depend depend: $(OCAMLDEP) *.ml *.mli > .depend +%.ml: %.mll + $(OCAMLLEX) $< %.cmi: %.mli $(OCAMLC) -c $< %.cmo: %.ml %.cmi diff --git a/DEVEL/ocaml-http/Makefile.defs b/DEVEL/ocaml-http/Makefile.defs index 4d9f7c5ca..f174b02f8 100644 --- a/DEVEL/ocaml-http/Makefile.defs +++ b/DEVEL/ocaml-http/Makefile.defs @@ -9,6 +9,7 @@ OCAMLFIND = ocamlfind OCAMLC = $(OCAMLFIND) ocamlc $(COMMON_FLAGS) OCAMLOPT = $(OCAMLFIND) ocamlopt $(COMMON_FLAGS) OCAMLDEP = $(OCAMLFIND) ocamldep $(COMMON_FLAGS) +OCAMLLEX = ocamllex OCAMLDOC := \ ocamldoc -stars \ $(shell $(OCAMLFIND) query -i-format unix) \ diff --git a/DEVEL/ocaml-http/cookie_lexer.mli b/DEVEL/ocaml-http/cookie_lexer.mli new file mode 100644 index 000000000..4458d36c7 --- /dev/null +++ b/DEVEL/ocaml-http/cookie_lexer.mli @@ -0,0 +1,29 @@ +(* + OCaml HTTP - do it yourself (fully OCaml) HTTP daemon + + Copyright (C) <2002-2007> Stefano Zacchiroli + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Library General Public License as + published by the Free Software Foundation, version 2. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA +*) + +type cookie_token = + [ `QSTRING of string + | `SEP + | `TOKEN of string + | `ASSIGN + | `EOF ] + +val token : Lexing.lexbuf -> cookie_token + diff --git a/DEVEL/ocaml-http/cookie_lexer.mll b/DEVEL/ocaml-http/cookie_lexer.mll new file mode 100644 index 000000000..e665e26ab Binary files /dev/null and b/DEVEL/ocaml-http/cookie_lexer.mll differ diff --git a/DEVEL/ocaml-http/debian/changelog b/DEVEL/ocaml-http/debian/changelog index 9b39aa5c4..ac30af0d6 100644 --- a/DEVEL/ocaml-http/debian/changelog +++ b/DEVEL/ocaml-http/debian/changelog @@ -2,8 +2,11 @@ ocaml-http (0.1.4-1) UNRELEASED; urgency=low * send internally generated headers as lowercase strings, for consistency with headers generated via setXXX methods + * added preliminary support for cookies (new "cookies" method added to an + http_request, cookies are parsed upon request creation if a "Cookie:" + header has been received) - -- Stefano Zacchiroli Wed, 24 Jan 2007 10:09:12 +0100 + -- Stefano Zacchiroli Mon, 29 Jan 2007 11:43:40 +0100 ocaml-http (0.1.3-3) UNRELEASED; urgency=low diff --git a/DEVEL/ocaml-http/examples/dump_args.ml b/DEVEL/ocaml-http/examples/dump_args.ml index ab082112a..e8a66a57f 100644 --- a/DEVEL/ocaml-http/examples/dump_args.ml +++ b/DEVEL/ocaml-http/examples/dump_args.ml @@ -1,8 +1,7 @@ - (* OCaml HTTP - do it yourself (fully OCaml) HTTP daemon - Copyright (C) <2002-2004> Stefano Zacchiroli + Copyright (C) <2002-2007> Stefano Zacchiroli This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -34,6 +33,16 @@ let callback req outchan = (sprintf "request ALL params = %s\n" (String.concat ";" (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^ + (sprintf "cookies = %s\n" + (match req#cookies with + | None -> + "NO COOKIES " + ^ (if req#hasHeader ~name:"cookie" + then "('Cookie:' header was '" ^ req#header ~name:"cookie" ^ "')" + else "(No 'Cookie:' header received)") + | Some cookies -> + (String.concat ";" + (List.map (fun (n,v) -> String.concat "=" [n;v]) cookies)))) ^ (sprintf "request BODY = '%s'\n\n" req#body) in Http_daemon.respond ~code:(`Code 200) ~body: str outchan diff --git a/DEVEL/ocaml-http/http_parser.ml b/DEVEL/ocaml-http/http_parser.ml index af371bb1e..b92a844e3 100644 --- a/DEVEL/ocaml-http/http_parser.ml +++ b/DEVEL/ocaml-http/http_parser.ml @@ -151,6 +151,28 @@ let parse_headers ic = in parse_headers' [] +let parse_cookies raw_cookies = + prerr_endline ("raw cookies: '" ^ raw_cookies ^ "'"); + let tokens = + let lexbuf = Lexing.from_string raw_cookies in + let rec aux acc = + match Cookie_lexer.token lexbuf with + | `EOF -> acc + | token -> aux (token :: acc) + in + List.rev (aux []) + in + let rec aux = function + | [ `TOKEN n ; `ASSIGN ; (`TOKEN v | `QSTRING v) ] -> + prerr_endline ("found cookie " ^ n ^ " " ^ v); + [ (n,v) ] + | `TOKEN n :: `ASSIGN :: (`TOKEN v | `QSTRING v) :: `SEP :: tl -> + prerr_endline ("found cookie " ^ n ^ " " ^ v); + (n,v) :: aux tl + | _ -> raise (Malformed_cookies raw_cookies) + in + aux tokens + let parse_request ic = let (meth, uri, version) = parse_request_fst_line ic in let path = parse_path uri in diff --git a/DEVEL/ocaml-http/http_parser.mli b/DEVEL/ocaml-http/http_parser.mli index 58e616bd7..1b24d9f09 100644 --- a/DEVEL/ocaml-http/http_parser.mli +++ b/DEVEL/ocaml-http/http_parser.mli @@ -60,6 +60,13 @@ val parse_path: Neturl.url -> string @raise Invalid_header if a not well formed header is encountered *) val parse_headers: in_channel -> (string * string) list + (** parse a Cookie header, extracting an associative list . See RFC 2965 + * @param raw_cookies: value of a "Cookies:" header + * @return a list of pairs cookie_name * cookie_value + * @raise Malformed_cookies *) +val parse_cookies: string -> (string * string) list + (** given an input channel, reads from it a GET HTTP request and @return a pair where path is a string representing the requested path and query_params is a list of pairs (the GET diff --git a/DEVEL/ocaml-http/http_request.ml b/DEVEL/ocaml-http/http_request.ml index cd2dcd165..93e6d8811 100644 --- a/DEVEL/ocaml-http/http_request.ml +++ b/DEVEL/ocaml-http/http_request.ml @@ -1,8 +1,7 @@ - (* OCaml HTTP - do it yourself (fully OCaml) HTTP daemon - Copyright (C) <2002-2005> Stefano Zacchiroli + Copyright (C) <2002-2007> Stefano Zacchiroli This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as @@ -75,6 +74,18 @@ class request ic = in (headers, body)) in + let cookies = + try + let _hdr, raw_cookies = + List.find + (fun (hdr, _cookie) -> String.lowercase hdr = "cookie") + headers + in + Some (Http_parser.parse_cookies raw_cookies) + with + | Not_found -> None + | Malformed_cookies _ -> None + in let query_post_params = match meth with | `POST -> @@ -122,6 +133,8 @@ class request ic = method params_GET = query_get_params method params_POST = query_post_params + method cookies = cookies + method private fstLineToString = let method_string = string_of_method self#meth in match self#version with diff --git a/DEVEL/ocaml-http/http_request.mli b/DEVEL/ocaml-http/http_request.mli index 23da0cc9b..5c9c17583 100644 --- a/DEVEL/ocaml-http/http_request.mli +++ b/DEVEL/ocaml-http/http_request.mli @@ -1,8 +1,7 @@ - (* OCaml HTTP - do it yourself (fully OCaml) HTTP daemon - Copyright (C) <2002-2005> Stefano Zacchiroli + Copyright (C) <2002-2007> Stefano Zacchiroli This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as diff --git a/DEVEL/ocaml-http/http_types.ml b/DEVEL/ocaml-http/http_types.ml index f31f81679..216b9e02f 100644 --- a/DEVEL/ocaml-http/http_types.ml +++ b/DEVEL/ocaml-http/http_types.ml @@ -1,8 +1,7 @@ - (* OCaml HTTP - do it yourself (fully OCaml) HTTP daemon - Copyright (C) <2002-2005> Stefano Zacchiroli + Copyright (C) <2002-2007> Stefano Zacchiroli This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as @@ -117,6 +116,7 @@ exception Malformed_URL of string exception Malformed_query of string exception Malformed_query_part of string * string exception Malformed_request_URI of string +exception Malformed_cookies of string exception Malformed_request of string exception Malformed_response of string exception Param_not_found of string @@ -162,6 +162,7 @@ class type request = object method params: (string * string) list method params_GET: (string * string) list method params_POST: (string * string) list + method cookies: (string * string) list option method authorization: auth_info option end diff --git a/DEVEL/ocaml-http/http_types.mli b/DEVEL/ocaml-http/http_types.mli index 7206d18dc..82967c5e4 100644 --- a/DEVEL/ocaml-http/http_types.mli +++ b/DEVEL/ocaml-http/http_types.mli @@ -1,8 +1,7 @@ - (* OCaml HTTP - do it yourself (fully OCaml) HTTP daemon - Copyright (C) <2002-2005> Stefano Zacchiroli + Copyright (C) <2002-2007> Stefano Zacchiroli This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as @@ -180,6 +179,9 @@ exception Malformed_query_part of string * string (** invalid request URI encountered *) exception Malformed_request_URI of string + (** malformed cookies *) +exception Malformed_cookies of string + (** malformed request received *) exception Malformed_request of string @@ -285,6 +287,8 @@ class type request = object (** @return the list of all parameter received via POST *) method params_POST: (string * string) list + method cookies: (string * string) list option + (** @return authorization information, if given by the client *) method authorization: auth_info option