12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697(*----------------------------------------------------------------------------
Copyright (c) 2017 Inhabited Type LLC.
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 author nor the names of his contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE 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 AUTHORS 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.
----------------------------------------------------------------------------*)typet={version:Version.t;status:Status.t;reason:string;headers:Headers.t}letcreate?reason?(version=Version.v1_1)?(headers=Headers.empty)status=letreason=matchreasonwith|Somereason->reason|None->beginmatchstatuswith|#Status.standardasstatus->Status.default_reason_phrasestatus|`Code_->"Non-standard status code"endin{version;status;reason;headers}letpersistent_connection?proxy{version;headers;_}=Message.persistent_connection?proxyversionheadersletproxy_error=`Error`Bad_gatewayletserver_error=`Error`Internal_server_errorletbody_length?(proxy=false)~request_method{status;headers;_}=matchstatus,request_methodwith|(`No_content|`Not_modified),_->(* From RFC7230§3.3.2:
A server MAY send a Content-Length header field in a 304 (Not
Modified) response to a conditional GET request (Section 4.1 of
[RFC7232]); a server MUST NOT send Content-Length in such a response
unless its field-value equals the decimal number of octets that would
have been sent in the payload body of a 200 (OK) response to the same
request. *)`Fixed0L|_,`HEAD->(* From RFC7230§3.3.2:
A server MAY send a Content-Length header field in a response to a
HEAD request (Section 4.3.2 of [RFC7231]); a server MUST NOT send
Content-Length in such a response unless its field-value equals the
decimal number of octets that would have been sent in the payload body
of a response if the same request had used the GET method. *)`Fixed0L|s,_whenStatus.is_informationals->`Fixed0L|s,`CONNECTwhenStatus.is_successfuls->`Close_delimited|_,_->beginmatchHeaders.get_multiheaders"transfer-encoding"with|"chunked"::_->`Chunked|_::eswhenList.mem"chunked"es->`Close_delimited|[]|_->beginmatchMessage.unique_content_length_valuesheaderswith|[]->`Close_delimited|[len]->letlen=Message.content_length_of_stringleniniflen>=0Lthen`Fixedlenelseifproxythenproxy_errorelseserver_error|_->ifproxythenproxy_errorelseserver_errorendendletpp_humfmt{version;status;reason;headers}=Format.fprintffmt"((version \"%a\") (status %a) (reason %S) (headers %a))"Version.pp_humversionStatus.pp_humstatusreasonHeaders.pp_humheaders