123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218# 1 "src/lib/eliom_lib.client.ml"(* Ocsigen
* Copyright (C) 2005-2008 Vincent Balat, Stéphane Glondu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*)openJs_of_ocamlincludeOcsigen_lib_baseinclude(Eliom_lib_base:moduletypeofEliom_lib_basewithtype'aInt64_map.t='aEliom_lib_base.Int64_map.twithtype'aString_map.t='aEliom_lib_base.String_map.twithtype'aInt_map.t='aEliom_lib_base.Int_map.t)(*****************************************************************************)moduleUrl=structincludeUrlincludeUrl_baseletdecode=Url.urldecodeletencode?pluss=Url.urlencode?with_plus:plussletmake_encoded_parameters=Url.encode_argumentsletsplit_path=Url.path_of_path_stringletssl_re=Regexp.regexp"^(https?):\\/\\/"letget_ssls=Option.map(funr->Regexp.matched_groupr1=Some"https")(Regexp.string_matchssl_res0)letresolves=leta=Dom_html.createADom_html.documentina##.href:=Js.strings;Js.to_stringa##.hreflethas_get_argsurl=tryignore(String.indexurl'?');truewithNot_found->falseletadd_get_argsurlget_args=ifget_args=[]thenurlelseurl^(ifhas_get_argsurlthen"&"else"?")^encode_argumentsget_argsletstring_of_url_path~encodel=ifencodethenprint_endline"Warning: Eliom_lib.string_of_url_path ignores ~encode";String.concat"/"lletpath_of_url=function|Url.Http{Url.hu_path=path;_}|Url.Https{Url.hu_path=path;_}|Url.File{Url.fu_path=path;_}->pathletpath_of_url_strings=matchUrl.url_of_stringswith|Somepath->path_of_urlpath|_->((* assuming relative URL and improvising *)split_path@@tryString.(subs0(indexs'?'))withNot_found->s)endmoduleLwt_log=structincludeLwt_log_jsletraise_error?inspect?exn?section?location?loggermsg=Lwt.ignore_result(log?inspect?exn?section?location?logger~level:Errormsg);matchexnwithSomeexn->raiseexn|None->failwithmsgletraise_error_f?inspect?exn?section?location?loggerfmt=Printf.ksprintf(raise_error?inspect?exn?section?location?logger)fmtleteliom=Section.make"eliom"endlet_=Lwt_log.default:=Lwt_log.console;Lwt.async_exception_hook:=funexn->Firebug.console##error_3(Js.string"Lwt.async:")(Js.string(Printexc.to_stringexn))exn(* Deprecated ON *)letdebug_exnfmtexn=Lwt_log.ign_info_f~exnfmtletdebugfmt=Lwt_log.ign_info_ffmtleterrorfmt=Lwt_log.raise_error_ffmtleterror_anyanyfmt=Lwt_log.raise_error_f~inspect:anyfmtletjsdebuga=Lwt_log.ign_info~inspect:a"Jsdebug"(* Deprecated OFF *)lettracefmt=ifEliom_config.get_tracing()thenLwt_log.ign_info_f(">> "^^fmt)elsePrintf.ksprintfignorefmtletlwt_ignore?(message="")t=Lwt.on_failuret(funexn->Lwt_log.ign_info_f~exn"%s"message)(* Debbuging *)letjsalerta=Dom_html.window##(alerta)letalertfmt=Printf.ksprintf(funs->jsalert(Js.strings))fmtletconfirm=letfs=lets=Js.stringsinDom_html.window##(confirms)|>Js.to_boolinfunfmt->Printf.ksprintfffmtletdebug_varsv=Js.Unsafe.setDom_html.window(Js.strings)vmoduleString=structincludeString_baseleteol_re=Regexp.regexp"[\r\n]"letremove_eolss=Regexp.global_replaceeol_res""end(*****************************************************************************)(* let () =
(Js.Unsafe.coerce Dom_html.window)##set_tracing <-
Js.wrap_callback (fun v -> set_tracing (Js.to_bool v)) *)(* We do not use the deriving (un)marshaling even if typ is available
because direct jsn (un)marshaling is very fast client side
*)letto_json?typ:_s=Js.to_string(Json.outputs)letof_json?typ:_v=Json.unsafe_input(Js.stringv)(* to marshal data and put it in a form *)letencode_form_valuex=to_jsonx(* Url.urlencode ~with_plus:true (Marshal.to_string x [])
(* I encode the data because it seems that multipart does not
like \0 character ... *)
*)letencode_header_valuex=(* We remove end of lines *)String.remove_eols(to_jsonx)letunmarshal_jsvar=Marshal.from_string(Js.to_bytestringvar)0typefile_info=File.fileJs.tletmake_cryptographic_safe_string?len:_()=failwith"make_cryptographic_safe_string not implemented client-side"moduleDom_reference=structclasstype['a,'b]map=objectmethodset:'a->'b->unitJs.methmethodget:'a->'bJs.Optdef.tJs.methmethoddelete:'a->unitJs.methendletcreate_map():(_,_)mapJs.t=letmap=Js.Unsafe.global##._Mapinnew%jsmapletcreate_weak_map():(_,_)mapJs.t=letweakMap=Js.Unsafe.global##._WeakMapinnew%jsweakMaptypekey=unitrefletretain_map:(Obj.t,(key,Obj.t)mapJs.t)mapJs.t=create_weak_map()letnew_key()=ref()letretain?(key=new_key())node~keep=letnode=Obj.reprnodeinletm=Js.Optdef.get(retain_map##getnode)(fun()->letm=create_map()inretain_map##setnodem;m)inm##setkey(Obj.reprkeep)letretain_generic=retainletrelease~keynode=letnode=Obj.reprnodeinJs.Optdef.iter(retain_map##getnode)(funm->m##deletekey)lettransfer~key~src~dst=letsrc=Obj.reprsrcinJs.Optdef.iter(retain_map##getsrc)(funm->Js.Optdef.iter(m##getkey)(funkeep->retaindst~key~keep);m##deletekey)end