123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101(*{{{ Copyright (c) 2012 Anil Madhavapeddy <anil@recoil.org>
*
* 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.
*
}}}*)openCohttpopenLwttypet=[Body.t|`Streamof(stringLwt_stream.t[@sexp.opaque])][@@derivingsexp]letempty=(Body.empty:>t)letcreate_streamfnarg=letfin=reffalseinLwt_stream.from(fun()->match!finwith|true->return_none|false->(fnarg>>=function|Transfer.Done->return_none|Transfer.Final_chunkc->fin:=true;return(Somec)|Transfer.Chunkc->return(Somec)))letis_empty(body:t)=matchbodywith|#Body.tasbody->return(Body.is_emptybody)|`Streams->Lwt_stream.get_while(funx->x="")s>>=fun_->Lwt_stream.is_emptysletto_string(body:t)=matchbodywith|#Body.tasbody->return(Body.to_stringbody)|`Streams->letb=Buffer.create1024inLwt_stream.iter(Buffer.add_stringb)s>>=fun()->return(Buffer.contentsb)letto_string_list(body:t)=matchbodywith|#Body.tasbody->return(Body.to_string_listbody)|`Streams->Lwt_stream.to_listsletof_strings=(Body.of_strings:>t)letto_stream(body:t)=matchbodywith|`Empty->Lwt_stream.of_list[]|`Streams->s|`Strings->Lwt_stream.of_list[s]|`Stringssl->Lwt_stream.of_listslletdrain_body(body:t)=matchbodywith|`Empty|`String_|`Strings_->return_unit|`Streams->Lwt_stream.junk_while(fun_->true)sletof_string_listl=`Stringslletof_streams=`Streamslettransfer_encoding=function|#Body.tast->Body.transfer_encodingt|`Stream_->Transfer.Chunked(* This will consume the body and return a length, and a
* new body that should be used instead of the input *)letlength(body:t):(int64*t)Lwt.t=matchbodywith|#Body.tasbody->return(Body.lengthbody,body)|`Stream_->to_stringbody>>=funbuf->letlen=Int64.of_int(String.lengthbuf)inreturn(len,`Stringbuf)letwrite_bodyfn=function|`Empty->return_unit|`Streamst->Lwt_stream.iter_sfnst|`Strings->fns|`Stringssl->Lwt_list.iter_sfnslletmapft=matchtwith|#Body.tast->(Body.mapft:>t)|`Streams->`Stream(Lwt_stream.mapfs)letto_form(body:t)=to_stringbody>|=Uri.query_of_encodedletof_form?schemef=Uri.encoded_of_query?schemef|>of_string