123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135(*********************************************************************************)(* Stog *)(* *)(* Copyright (C) 2012-2024 INRIA All rights reserved. *)(* Author: Maxence Guesdon, INRIA Saclay *)(* *)(* 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 the Free Software Foundation, version 3 of the License. *)(* *)(* 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 General Public License for more details. *)(* *)(* You should have received a copy of the GNU 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 *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)typet=Uri.tmoduleW=Ocf.Wrappertypeurl_config={pub:t;priv:t}letof_strings=tryUri.of_stringswith_->failwith(Printf.sprintf"Malformed URL %S"s);;letto_string=Uri.to_string?pct_encoder:None;;letpathurl=letl=matchStog_base.Misc.split_string~keep_empty:true(Uri.pathurl)['/']with""::q->q|x->xinList.mapUri.pct_decodelletwith_pathurlpath=letpath=List.mapUri.pct_encodepathinUri.with_pathurl("/"^(String.concat"/"path))letconcaturis=matchswith""->uri|_->leturi_path=pathuriinletpath=uri_path@[s]intrywith_pathuripathwithe->prerr_endline(Printf.sprintf"url_concat: uri=%s url_path=%s, s=%s"(to_stringuri)(Uri.pathuri)s);raisee;;letfieldnamefurl=matchfurlwith|None->failwith(Printf.sprintf"No %s in url %s"name(to_stringurl))|Somev->vletscheme=field"scheme"Uri.schemeletportt=tryfield"port"Uri.porttwithe->matchschemetwith|exception_->raisee|"http"|"ws"->80|"https"|"wss"->443|_->raiseelethost=field"host"Uri.hostletwith_fragment=Uri.with_fragmentletappendurip=letp0=letp=pathuriinmatchList.revpwith|""::q->List.revq|_->pinletpath=p0@pinwith_pathuripathletremove_ending_slashurl=matchList.rev(pathurl)with|[""]->url|""::q->with_pathurl(List.revq)|_->urlletwrapper=W.string_to_stringof_stringleturl_config_wrapper=letto_j?with_docc=`Assoc["url",wrapper.W.to_json?with_docc.priv;"public_url",wrapper.W.to_jsonc.pub]inletfrom_j?def=function(`Assocl)asjson->beginmatchtrySome(List.assoc"url"l)withNot_found->NonewithNone->Ocf.invalid_valuejson|Somepriv->letpriv=wrapper.W.from_jsonprivinletpub=trywrapper.W.from_json(List.assoc"public_url"l)withNot_found->privin{pub;priv}end|json->letpriv=wrapper.W.from_jsonjsonin{pub=priv;priv}inW.maketo_jfrom_jletdefault_url_configurl={pub=url;priv=url}letremove_queryt=Uri.with_queryt[]