Updated deps.
[cocanwiki.git] / scripts / cocanwiki_cgi_args.ml
1 (* COCANWIKI - a wiki written in Objective CAML.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: cocanwiki_cgi_args.ml,v 1.1 2004/09/24 10:44:55 rich Exp $
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * This badly needs to become a standard part of the Cgi library.
22  *)
23
24 let split1_re = Pcre.regexp "\\?"
25 let split2_re = Pcre.regexp "&"
26 let split3_re = Pcre.regexp "="
27
28 let parse full_url =
29   let url, qs = match Pcre.split ~rex:split1_re ~max:2 full_url with
30     | [url] | [url;""] -> url, None
31     | [url;qs] -> url, Some qs
32     | _ -> assert false in
33   let args = match qs with
34       None -> []
35     | Some qs ->
36         let args = Pcre.split ~rex:split2_re qs in
37         let f arg =
38           match Pcre.split ~rex:split3_re ~max:2 arg with
39             | [] -> ("", "")
40             | [key] -> (key, "")
41             | [key;value] -> (key, value)
42             | _ -> assert false
43         in
44         List.map f args in
45   url, qs, args