Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file media_type.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)includeResto_cohttp.Media_type.Make(RPC_encoding)(* emits chunks of size approx chunk_size_hint but occasionally a bit bigger *)letchunk_size_hint=4096letjson={name=Cohttp.Accept.MediaType("application","json");q=Some1000;pp=(fun_encppfraw->matchData_encoding.Json.from_stringrawwith|Errorerr->Format.fprintfppf"@[Invalid JSON:@ - @[<v 2>Error:@ %s@] - @[<v 2>Raw data:@ \
%s@]@]"errraw|Okjson->Data_encoding.Json.ppppfjson);construct=(funencv->Data_encoding.Json.to_string~newline:true~minify:true@@Data_encoding.Json.constructencv);construct_seq=(funencv->letbuffer=Bytes.createchunk_size_hintinData_encoding.Json.blit_instructions_seq_of_jsonm_lexeme_seq~newline:true~buffer@@Data_encoding.Json.construct_seqencv);destruct=(funencbody->matchData_encoding.Json.from_stringbodywith|Error_aserr->err|Okjson->(tryOk(Data_encoding.Json.destructencjson)with|(Stack_overflow|Out_of_memory)asexc->raiseexc|Data_encoding.Json.Cannot_destruct(_,exc)->Error(Format.asprintf"%a"(funfmt->Data_encoding.Json.print_errorfmt)exc)|exc->Error(Printexc.to_stringexc)));}letbson=letconstruct_bytesencv=Json_repr_bson.bson_to_bytes@@Data_encoding.Bson.constructencvin{name=Cohttp.Accept.MediaType("application","bson");q=Some100;pp=(fun_encppfraw->matchJson_repr_bson.bytes_to_bson~laziness:false~copy:false(Bytes.of_stringraw)with|exceptionJson_repr_bson.Bson_decoding_error(msg,_,_)->Format.fprintfppf"@[Invalid BSON:@ %s@]"msg|bson->letjson=Json_repr.convert(moduleJson_repr_bson.Repr)(moduleJson_repr.Ezjsonm)bsoninData_encoding.Json.ppppfjson);construct=(funenvv->Bytes.unsafe_to_string(construct_bytesenvv));construct_seq=(funencv->letb=construct_bytesencvinSeq.return(b,0,Bytes.lengthb));destruct=(funencbody->matchJson_repr_bson.bytes_to_bson~laziness:false~copy:false(Bytes.of_stringbody)with|exceptionJson_repr_bson.Bson_decoding_error(msg,_,pos)->Error(Format.asprintf"(at offset: %d) %s"posmsg)|bson->(tryOk(Data_encoding.Bson.destructencbson)with|(Stack_overflow|Out_of_memory)asexc->raiseexc|Data_encoding.Json.Cannot_destruct(_,exc)->Error(Format.asprintf"%a"(funfmt->Data_encoding.Json.print_errorfmt)exc)|exc->Error(Printexc.to_stringexc)));}letoctet_stream=letdynsizeenc=(* We add a size header to all the binary exchanges between server and
client *)Data_encoding.dynamic_sizeencinletconstruct_bytesencv=Data_encoding.Binary.to_bytes_exn(dynsizeenc)vinletconstruct_stringencv=Data_encoding.Binary.to_string_exn(dynsizeenc)vin{name=Cohttp.Accept.MediaType("application","octet-stream");q=Some200;pp=(funencppfraw->matchData_encoding.Binary.of_string(dynsizeenc)rawwith|ErrorData_encoding.Binary.Not_enough_data->Format.fprintfppf"Partial read (%d bytes), waiting for more data (Not enough \
data): %a"(String.lengthraw)Hex.pp(Hex.of_stringraw)|Errorre->Format.fprintfppf"Invalid binary data: %a."Data_encoding.Binary.pp_read_errorre|Okv->Format.fprintfppf";; binary equivalent of the following json@.%a"Data_encoding.Json.pp(Data_encoding.Json.construct(dynsizeenc)v));construct=construct_string;construct_seq=(funencv->letb=construct_bytesencvinSeq.return(b,0,Bytes.lengthb));destruct=(funencs->matchData_encoding.Binary.of_string(dynsizeenc)swith|Errorre->Error(Format.asprintf"Failed to parse binary data: %a."Data_encoding.Binary.pp_read_errorre)|Okdata->Okdata);}letall_media_types=[json;bson;octet_stream]letencoding:tRPC_encoding.t=Data_encoding.string_enum[("application/json",json);("application/bson",bson);("application/octet-stream",octet_stream);]moduleContent_type=structtypet=string*stringletjson=("application","json")letbson=("application","bson")letoctet_stream=("application","octet-stream")letppfmt(l,r)=Format.fprintffmt"%s/%s"lrendletof_content_typec=ifc=Content_type.jsonthenSomejsonelseifc=Content_type.bsonthenSomebsonelseifc=Content_type.octet_streamthenSomeoctet_streamelseNone