123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202(*----------------------------------------------------------------------------
Copyright (c) 2018 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.
----------------------------------------------------------------------------*)(* XXX(dpatti): A [Body.t] is kind of a reader body and writer body stitched
together into a single structure, but only half of it is used at any given
time. The two uses are also quite different in that a writer body is always
wired up to some [Writer.t] by httpaf internals at time of creation, whereas
a reader body is given to the user as-is and the user is expected to drive
the feeding of data into the body. It feels like they should simply be two
separate types. *)type_t={faraday:Faraday.t;mutableread_scheduled:bool;mutablewrite_final_if_chunked:bool;mutableon_eof:unit->unit;mutableon_read:Bigstringaf.t->off:int->len:int->unit;mutablewhen_ready:Optional_thunk.t;buffered_bytes:intref}letdefault_on_eof=Sys.opaque_identity(fun()->())letdefault_on_read=Sys.opaque_identity(fun_~off:_~len:_->())letof_faradayfaradaywhen_ready={faraday;read_scheduled=false;write_final_if_chunked=true;on_eof=default_on_eof;on_read=default_on_read;when_ready;buffered_bytes=ref0}letcreatebufferwhen_ready=of_faraday(Faraday.of_bigstringbuffer)when_readyletcreate_empty()=lett=createBigstringaf.emptyOptional_thunk.noneint.write_final_if_chunked<-false;Faraday.closet.faraday;tletempty=create_empty()letset_non_chunkedt=t.write_final_if_chunked<-falseletwrite_chartc=Faraday.write_chart.faradaycletwrite_stringt?off?lens=Faraday.write_string?off?lent.faradaysletwrite_bigstringt?off?lenb=Faraday.write_bigstring?off?lent.faradaybletschedule_bigstringt?off?len(b:Bigstringaf.t)=Faraday.schedule_bigstring?off?lent.faradaybletreadyt=Optional_thunk.call_if_somet.when_readyletflushtkontinue=Faraday.flusht.faradaykontinue;readytletis_closedt=Faraday.is_closedt.faradayletclose_writert=Faraday.closet.faraday;readyt;;;letunsafe_faradayt=t.faradayletrecdo_execute_readton_eofon_read=matchFaraday.operationt.faradaywith|`Yield->()|`Close->t.read_scheduled<-false;t.on_eof<-default_on_eof;t.on_read<-default_on_read;on_eof()|`Writev[]->assertfalse|`Writev(iovec::_)->t.read_scheduled<-false;t.on_eof<-default_on_eof;t.on_read<-default_on_read;let{IOVec.buffer;off;len}=iovecinFaraday.shiftt.faradaylen;on_readbuffer~off~len;execute_readtandexecute_readt=ift.read_scheduledthendo_execute_readtt.on_eoft.on_readletschedule_readt~on_eof~on_read=ift.read_scheduledthenfailwith"Body.schedule_read: reader already scheduled";ifis_closedtthendo_execute_readton_eofon_readelsebegint.read_scheduled<-true;t.on_eof<-on_eof;t.on_read<-on_read;readyt;endletis_read_scheduledt=t.read_scheduledlethas_pending_outputt=(* Force another write poll to make sure that the final chunk is emitted for
chunk-encoded bodies.
Note that the body data type does not keep track of encodings, so it is
necessary for [transfer_to_writer_with_encoding] to check the encoding and
clear the [write_final_if_chunked] field when outputting a fixed or
close-delimited body. *)Faraday.has_pending_outputt.faraday||(Faraday.is_closedt.faraday&&t.write_final_if_chunked)letrequires_outputt=not(is_closedt)||has_pending_outputtletclose_readert=Faraday.closet.faraday;execute_readt;readyt;;;lettransfer_to_writer_with_encodingt~encodingwriter=letfaraday=t.faradayinbeginmatcht.write_final_if_chunked,encodingwith|true,(`Fixed_|`Close_delimited)->(* Play nicely with [has_pending_output] in the case of a fixed or
close-delimited encoding.
Immediately set `t.write_final_if_chunked` to `false` because we may
not have another opportunity to do so before advancing the request
queue. *)t.write_final_if_chunked<-false;|false,(`Fixed_|`Close_delimited)|_,`Chunked->(* Handled explicitly later when closing the writer. *)()end;beginmatchFaraday.operationfaradaywith|`Yield->()|`Close->letmust_write_the_final_chunk=t.write_final_if_chunkedint.write_final_if_chunked<-false;ifmust_write_the_final_chunkthenSerialize.Writer.schedule_chunkwriter[];Serialize.Writer.unyieldwriter;|`Writeviovecs->letbuffered=t.buffered_bytesinbeginmatchIOVec.shiftviovecs!bufferedwith|[]->()|iovecs->letlengthv=IOVec.lengthviovecsinbuffered:=!buffered+lengthv;beginmatchencodingwith|`Fixed_|`Close_delimited->Serialize.Writer.schedule_fixedwriteriovecs|`Chunked->Serialize.Writer.schedule_chunkwriteriovecsend;Serialize.Writer.flushwriter(fun()->Faraday.shiftfaradaylengthv;buffered:=!buffered-lengthv)endend