Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file lwt_xmlHttpRequest.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320(* Js_of_ocaml library
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2010 Jérôme Vouillon
* Laboratoire PPS - CNRS Université Paris Diderot
*
* 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_ocamlopenJsopenXmlHttpRequestopen!Importletencode_urll=String.concat"&"(List.map(function|name,`Strings->Url.urlencodename^"="^Url.urlencode(to_strings)|name,`Files->Url.urlencodename^"="^Url.urlencode(to_strings##.name))l)(* Higher level interface: *)type'responsegeneric_http_frame={url:string;code:int;headers:string->stringoption;content:'response;content_xml:unit->Dom.elementDom.documenttoption}(** type of the http headers *)typehttp_frame=stringgeneric_http_frameexceptionWrong_headersof(int*(string->stringoption))letdefault_responseurlcodeheadersreq={url;code;content=Js.Opt.casereq##.responseText(fun()->"")(funx->Js.to_stringx);content_xml=(fun()->matchJs.Opt.to_optionreq##.responseXMLwith|None->None|Somedoc->ifJs.somedoc##.documentElement==Js.nullthenNoneelseSomedoc);headers}lettext_responseurlcodeheadersreq={url;code;content=Js.Opt.casereq##.responseText(fun()->Js.string"")(funx->x);content_xml=(fun()->assertfalse);headers}letdocument_responseurlcodeheadersreq={url;code;content=File.CoerceTo.documentreq##.response;content_xml=(fun()->assertfalse);headers}letjson_responseurlcodeheadersreq={url;code;content=File.CoerceTo.jsonreq##.response;content_xml=(fun()->assertfalse);headers}letblob_responseurlcodeheadersreq={url;code;content=File.CoerceTo.blobreq##.response;content_xml=(fun()->assertfalse);headers}letarraybuffer_responseurlcodeheadersreq={url;code;content=File.CoerceTo.arrayBufferreq##.response;content_xml=(fun()->assertfalse);headers}lethas_get_argsurl=tryignore(String.indexurl'?');truewithNot_found->falseletperform_raw?(headers=[])?content_type?(get_args=[])?(check_headers=fun__->true)?progress?upload_progress?contents?override_mime_type?override_method?with_credentials(typeresptype)~(response_type:resptyperesponse)url=letcontents_normalization=function|`POST_formargs->letonly_strings=List.for_all(funx->matchxwith|_,`String_->true|_->false)argsinletform_contents=ifonly_stringsthen`Fields(ref[])elseForm.empty_form_contents()inList.iter(fun(name,value)->Form.appendform_contents(name,value))args;`Form_contentsform_contents|(`String_|`Form_contents_)asx->x|`Blobb->`Blob(b:#File.blobJs.t:>File.blobJs.t)inletcontents=matchcontentswith|None->None|Somec->Some(contents_normalizationc)inletmethod_to_stringm=matchmwith|`GET->"GET"|`POST->"POST"|`HEAD->"HEAD"|`PUT->"PUT"|`DELETE->"DELETE"|`OPTIONS->"OPTIONS"|`PATCH->"PATCH"inletmethod_,content_type=letoverride_methodm=matchoverride_methodwith|None->m|Somev->method_to_stringvinletoverride_content_typec=matchcontent_typewith|None->Somec|Some_->content_typeinmatchcontentswith|None->override_method"GET",content_type|Some(`Form_contentsform)->(matchformwith|`Fields_strings->(override_method"POST",override_content_type"application/x-www-form-urlencoded")|`FormData_->override_method"POST",content_type)|Some(`String_|`Blob_)->override_method"POST",content_typeinleturl=ifPoly.(get_args=[])thenurlelseurl^(ifhas_get_argsurlthen"&"else"?")^Url.encode_argumentsget_argsinlet(res:resptypegeneric_http_frameLwt.t),w=Lwt.task()inletreq=create()inreq##_open(Js.stringmethod_)(Js.stringurl)Js._true;(matchoverride_mime_typewith|None->()|Somemime_type->req##overrideMimeType(Js.stringmime_type));(matchresponse_typewith|ArrayBuffer->req##.responseType:=Js.string"arraybuffer"|Blob->req##.responseType:=Js.string"blob"|Document->req##.responseType:=Js.string"document"|JSON->req##.responseType:=Js.string"json"|Text->req##.responseType:=Js.string"text"|Default->req##.responseType:=Js.string"");(matchwith_credentialswith|Somec->req##.withCredentials:=Js.boolc|None->());(matchcontent_typewith|Somecontent_type->req##setRequestHeader(Js.string"Content-type")(Js.stringcontent_type)|_->());List.iter(fun(n,v)->req##setRequestHeader(Js.stringn)(Js.stringv))headers;letheaderss=Opt.case(req##getResponseHeader(Js.bytestrings))(fun()->None)(funv->Some(Js.to_stringv))inletdo_check_headers=letst=ref`Not_yetinfun()->ifPoly.(!st=`Not_yet)thenifcheck_headersreq##.statusheadersthenst:=`Passedelse(Lwt.wakeup_exnw(Wrong_headers(req##.status,headers));st:=`Failed;req##abort);Poly.(!st<>`Failed)inreq##.onreadystatechange:=Js.wrap_callback(fun_->matchreq##.readyStatewith(* IE doesn't have the same semantics for HEADERS_RECEIVED.
so we wait til LOADING to check headers. See:
http://msdn.microsoft.com/en-us/library/ms534361(v=vs.85).aspx *)|HEADERS_RECEIVEDwhennotDom_html.onIE->ignore(do_check_headers())|LOADINGwhenDom_html.onIE->ignore(do_check_headers())|DONE->(* If we didn't catch a previous event, we check the header. *)ifdo_check_headers()thenletresponse:resptypegeneric_http_frame=matchresponse_typewith|ArrayBuffer->arraybuffer_responseurlreq##.statusheadersreq|Blob->blob_responseurlreq##.statusheadersreq|Document->document_responseurlreq##.statusheadersreq|JSON->json_responseurlreq##.statusheadersreq|Text->text_responseurlreq##.statusheadersreq|Default->default_responseurlreq##.statusheadersreqinLwt.wakeupwresponse|_->());(matchprogresswith|Someprogress->req##.onprogress:=Dom.handler(fune->progresse##.loadede##.total;Js._true)|None->());Optdef.iterreq##.upload(funupload->matchupload_progresswith|Someupload_progress->upload##.onprogress:=Dom.handler(fune->upload_progresse##.loadede##.total;Js._true)|None->());(matchcontentswith|None->req##sendJs.null|Some(`Form_contents(`Fieldsl))->req##send(Js.some(string(encode_url!l)))|Some(`Form_contents(`FormDataf))->req##send_formDataf|Some(`Strings)->req##send(Js.some(Js.strings))|Some(`Blobb)->req##send_blobb);Lwt.on_cancelres(fun()->req##abort);resletperform_raw_url?(headers=[])?content_type?(get_args=[])?check_headers?progress?upload_progress?contents?override_mime_type?override_method?with_credentialsurl=perform_raw~headers?content_type~get_args?contents?check_headers?progress?upload_progress?override_mime_type?override_method?with_credentials~response_type:Defaulturlletperform?(headers=[])?content_type?(get_args=[])?check_headers?progress?upload_progress?contents?override_mime_type?override_method?with_credentialsurl=perform_raw~headers?content_type~get_args?contents?check_headers?progress?upload_progress?override_mime_type?override_method?with_credentials~response_type:Default(Url.string_of_urlurl)letgets=perform_raw_urls