123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104(*----------------------------------------------------------------------------
* Copyright (c) 2019, António Nuno Monteiro
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*---------------------------------------------------------------------------*)includeH2.HeadersmoduleWell_known=structmoduleHTTP1=structlethost="host"endmoduleHTTP2=structlethost=":authority"endletauthorization="authorization"letconnection="connection"letcontent_length="content-length"letcontent_type="content-type"letlocation="location"letupgrade="upgrade"lettransfer_encoding="transfer-encoding"moduleValues=structletclose="close"letchunked="chunked"endendletadd_length_related_headers~version~body_lengthheaders=(* TODO: check `Httpun.Response.body_length` because we may have to issue a
* 0-length response body. *)(* Don't step over an explicit `content-length` header. *)matchbody_lengthwith|`Fixedn->add_unless_existsheadersWell_known.content_length(Int64.to_stringn)|`Chunked->(* From RFC9113§8.2.2:
* An endpoint MUST NOT generate an HTTP/2 message containing
* connection-specific header fields. This includes the Connection header
* field and those listed as having connection-specific semantics in
* Section 7.6.1 of [HTTP] (that is, Proxy-Connection, Keep-Alive,
* Transfer-Encoding, and Upgrade).
*)(matchversionwith|Versions.HTTP.HTTP_2->headers|HTTP_1_0|HTTP_1_1->add_unless_existsheadersWell_known.transfer_encodingWell_known.Values.chunked)|`Close_delimited->add_unless_existsheadersWell_known.connectionWell_known.Values.close|`Error_|`Unknown->headers(* TODO: Add user-agent if not defined *)letcanonicalize_headers~body_length~host~versionheaders=letheaders=matchversionwith|Versions.HTTP.HTTP_2->of_list((Well_known.HTTP2.host,host)::List.map(fun(name,value)->String.lowercase_asciiname,value)headers)|HTTP_1_0|HTTP_1_1->add_unless_exists(of_listheaders)Well_known.HTTP1.hosthostinadd_length_related_headers~version~body_lengthheaderslethostt~version=matchversionwith|Versions.HTTP.HTTP_2->gettWell_known.HTTP2.host|HTTP_1_0|HTTP_1_1->gettWell_known.HTTP1.hostletof_http1headers=of_rev_list(Httpun.Headers.to_rev_listheaders)letto_http1headers=Httpun.Headers.of_rev_list(to_rev_listheaders)