(************************************************************ url.ml Created : Sat Feb 8 19:44:23 2003 Last modified: Sat Feb 08 22:01:11 2003 Compile: ocamlc.opt str.cma url.ml -o url # FTP Directory: sources/ocaml # ************************************************************) (** @author Takashi Masuyama *) exception IrreguralURL exception Unsupported of string let url_regexp = Str.regexp "^\\([a-z]+\\)://\\([^/]+\\)\\([^\\?]*\\)\\(?\\(.*\\)\\)?$" (*let host_of url =*) (* Str.*) type hostname = string type path = string type protocol = HTTP type t = { protocol : protocol; hostname : string; path : string; param : string option } (* FTP *) let protocol_of_string s = if s = "http" then HTTP else raise (Unsupported(s)) let of_string url = if Str.string_match url_regexp url 0 then let proto = let tmp = Str.matched_group 1 url in protocol_of_string tmp in let host = Str.matched_group 2 url in let path = let tmp = Str.matched_group 3 url in if tmp = "" then "/" else tmp in let param = try let tmp = Str.matched_group 5 url in Some tmp with Not_found -> None in { protocol = proto; hostname = host; path = path; param = param } else raise IrreguralURL (*let protocol_of (URL(proto, _, _, _)) = proto*) (*let host_of (URL(_, host, _, _)) = host*) (*let path_of (URL(_, _, path, _)) = path*) (*let param_of (URL(_, _, _, param)) = param*) let test_data = "http://www.okuiaki.com/gheahegoiahdo?mamewo=0" (*let _ =*) (* let u = of_string test_data in*) (* print_endline (host_of u);*) (* print_endline (path_of u);*) (* match param_of u with*) (* Some(s) -> print_endline s*) (* | None -> print_endline "NONE"*)