123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143(* From
https://github.com/lyrm/ocaml-httpadapter/blob/master/src-httpaf/accept.ml
Copyright (c) 2019 Carine Morel <carine@tarides.com>
Permission to use, copy, modify, and distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright
notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. *)openAngstromopenPrintftypeencoding=|Encodingofstring|Gzip|Compress|Deflate|Identity|Anytypep=string*stringtypeq=inttype'aqlist=(q*'a)list(** Lexer *)letis_space=function' '|'\t'->true|_->falseletis_token=function|'\000'..'\031'|'\127'|')'|'('|'<'|'>'|'@'|','|';'|':'|'"'|'/'|'['|']'|'?'|'='|'{'|'}'|' '->false|_s->trueletows=skipis_space<|>return()lettoken=take_while1is_tokenletsep_by1_commavalue_parser=sep_by1(char',')value_parser<*end_of_inputleteval_parserparserdefault_value=function|None->[(1000,default_value)]|Somestr->(matchparse_string~consume:Angstrom.Consume.Allparserstrwith|Okv->v|Errormsg->failwithmsg)(** Parser for header parameters like defined in rfc
https://tools.ietf.org/html/rfc7231#section-5.3.2 *)typeparam=Qofint|Kvofpletq_of_strings=truncate(1000.*.float_of_strings)(* More restrictive than cohttp counterpart *)letqs=char'"'*>token<*char'"'(* a header parameter can be : OWS ; OWS q=[value] OWS ; OWS [name]=[value] OWS
; OWS [name]="[value]" *)letparam:paramt=ows*>char';'*>ows*>(* OWS ; OWS q=[value] OWS ; OWS [name]=[value]*)(lift2(funnv->ifn="q"thenQ(q_of_stringv)elseKv(n,v))token(char'='*>token)<|>(* OWS ; OWS [name]="[value]" *)lift2(funnv->Kv(n,v))token(char'='*>qs))letparams=manyparamletrecget_qparams=matchparamswith[]->1000|Qq::_->q|_::r->get_qr(** Parser for values of Accept-encoding header. Example: Accept-Encoding:
compress, gzip Accept-Encoding: Accept-Encoding: * Accept-Encoding:
compress;q=0.5, gzip;q=1.0 Accept-Encoding: gzip;q=1.0, identity; q=0.5,
*;q=0 *)letencoding_value_parser=ows*>(char'*'*>return(Any:encoding)<|>lift(funs->matchString.lowercase_asciiswith|"gzip"->Gzip|"compress"->Compress|"deflate"->Deflate|"identity"->Identity|enc->Encodingenc)token)letencoding_parser=lift2(funvalueq->(q,value))encoding_value_parser(liftget_qparams)letencodings_parser=sep_by1_commaencoding_parserletencodings=eval_parserencodings_parserAny(** Other functions (from Cohttp.Accept) *)letrecstring_of_pl=function|[]->""|(k,v)::r->lete=Stringext.quotevinifv=ethensprintf";%s=%s%s"kv(string_of_plr)elsesprintf";%s=\"%s\"%s"ke(string_of_plr)letstring_of_q=function|qwhenq<0->invalid_arg(Printf.sprintf"qvalue %d must be positive"q)|qwhenq>1000->invalid_arg(Printf.sprintf"qvalue %d must be less than 1000"q)|1000->"1"|q->Printf.sprintf"0.%03d"qletaccept_el?qelpl=matchqwith|Someq->sprintf"%s;q=%s%s"el(string_of_qq)(string_of_plpl)|None->elletstring_of_encoding?q=function|Encodinge->accept_el?qe[]|Gzip->accept_el?q"gzip"[]|Compress->accept_el?q"compress"[]|Deflate->accept_el?q"deflate"[]|Identity->accept_el?q"identity"[]|Any->accept_el?q"*"[]letstring_of_lists_of_el=letrecauxs=function|[(q,el)]->s^s_of_elelq|[]->s|(q,el)::r->aux(s^s_of_elelq^",")rinaux""letstring_of_encodings=string_of_list(funelq->string_of_encoding~qel)letqsortl=letcompare((i:int),_)(i',_)=comparei'iinList.stable_sortcomparel