1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.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. *)(* *)(*****************************************************************************)typet={allowed_headers:stringlist;allowed_origins:stringlist}letdefault={allowed_headers=[];allowed_origins=[]}letcheck_origin_matchesoriginallowed_origin=String.equal"*"allowed_origin||String.equalallowed_originorigin||letallowed_w_slash=allowed_origin^"/"inletlen_a_w_s=String.lengthallowed_w_slashinletlen_o=String.lengthorigininlen_o>=len_a_w_s&&(String.equalallowed_w_slash@@String.suborigin0len_a_w_s)letfind_matching_originallowed_originsorigin=letmatching_origins=List.filter(check_origin_matchesorigin)allowed_originsinletcompare_by_length_negab=~-(compare(String.lengtha)(String.lengthb))inletmatching_origins_sorted=List.sortcompare_by_length_negmatching_originsinmatchmatching_origins_sortedwith[]->None|x::_->Somexletadd_allow_originheaderscorsorigin_header=matchorigin_headerwith|None->headers|Someorigin->(matchfind_matching_origincors.allowed_originsoriginwith|None->headers|Someallowed_origin->Cohttp.Header.addheaders"Access-Control-Allow-Origin"allowed_origin)letadd_headersheaderscorsorigin_header=letcors_headers=Cohttp.Header.add_multiheaders"Access-Control-Allow-Headers"cors.allowed_headersinadd_allow_origincors_headerscorsorigin_headerletcheck_hostheaderscors=matchCohttp.Header.getheaders"Host"with|None->List.mem"*"cors.allowed_origins|Somehost->(matchfind_matching_origincors.allowed_originshostwith|None->false|Some_->true)