From c55e18b2a3910bbb30f5138079dd1c68d6d0908e Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Mon, 29 Jan 2007 10:43:50 +0000 Subject: [PATCH] preliminary cookie support --- DEVEL/ocaml-http/.depend | 6 +++-- DEVEL/ocaml-http/Makefile | 19 +++++++++++++--- DEVEL/ocaml-http/Makefile.defs | 1 + DEVEL/ocaml-http/cookie_lexer.mli | 29 +++++++++++++++++++++++++ DEVEL/ocaml-http/cookie_lexer.mll | Bin 0 -> 1567 bytes DEVEL/ocaml-http/debian/changelog | 5 ++++- DEVEL/ocaml-http/examples/dump_args.ml | 13 +++++++++-- DEVEL/ocaml-http/http_parser.ml | 22 +++++++++++++++++++ DEVEL/ocaml-http/http_parser.mli | 7 ++++++ DEVEL/ocaml-http/http_request.ml | 17 +++++++++++++-- DEVEL/ocaml-http/http_request.mli | 3 +-- DEVEL/ocaml-http/http_types.ml | 5 +++-- DEVEL/ocaml-http/http_types.mli | 8 +++++-- 13 files changed, 119 insertions(+), 16 deletions(-) create mode 100644 DEVEL/ocaml-http/cookie_lexer.mli create mode 100644 DEVEL/ocaml-http/cookie_lexer.mll 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 0000000000000000000000000000000000000000..e665e26aba8125fb25f90d92cd6949e283d731d0 GIT binary patch literal 1567 zcma)6VQ(Tg5bbCFL`QsCNPz>Q>eP};Z!dxbT@EO_=#&Q9m|ZVxWwANio0R6F{`EfZ zZW5waZH>m*!_4!Wc{Yus8qxHOZ8N$E!x^&Hds)<^G_AL5RFO50xDHIuu7SAJnu!8(VA0kIC|T{(I|>OkHd@UA|!uuMW6lo z+@FM31H{@?;a7ggOC+(~Wda=09nGZOqe=jL8qUuykl}wAUyj2oLT5(fa54-68cpZq z)6AcT77jTP8A7lzs(u3{=o`xY>Zz^@%OYqbZg?H-~Br&ZfC@P>cw^Sx_>%a`Ierj~&nE}R zxM2Pz$D=S}BM%r2r=!C#tX-sx-ARc+TPx62?XgzVIUH4!2yBhJRbT1aBYc+DP0~%; zThT)mnDO7&c|wmxq*wQ-as4addxgaMa=EP6n@4Vs=Shg=TZid}6Q>**xA+Od;Qz+S z(7|S7(<&UV?^WcJ0?*MVUq6NRsNsOx)NdZ9Ju6-1R(KWXfrnxW=_<#&!cq0O^tqb; z)&%nYg!3D(?S1RI;$b?0A>BKLwP3*ZeuVu5d*eOT;We;r!dz!pum{*9>^*F`^j6>f YOSf*yoARocB}ooV4+e^I>O 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 -- 2.39.2