123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104(*{{{ Copyright (c) 2012-2013 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.
*
}}}*)openBaseopenAsync_kernelmoduleWriter=Async_unix.WritermoduleReader=Async_unix.ReadermoduleFormat=Caml.Formatletlog_src_name="cohttp.async.io"letsrc=Logs.Src.createlog_src_name~doc:"Cohttp Async IO module"moduleLog=(valLogs.src_logsrc:Logs.LOG)letdefault_reporter()=letfmtr,fmtr_flush=letb=Buffer.create512in(Fmt.with_buffer~like:Fmt.stderrb,fun()->letm=Buffer.contentsbinBuffer.resetb;m)inletreportsrc_level~overkmsgf=letk_=ifString.equal(Logs.Src.namesrc)log_src_namethen(Writer.write(Lazy.forceWriter.stderr)(fmtr_flush()));over();k()inmsgf@@fun?header:_?tags:_fmt->Format.kfprintfkfmtrCaml.("@["^^fmt^^"@]@.")in{Logs.report}letset_log=lazy((* If no reporter has been set by the application, set default one
that prints to stderr. This way a user will see logs when the debug
flag is set without adding a reporter. *)ifphys_equal(Logs.reporter())Logs.nop_reporterthenLogs.set_level@@SomeLogs.Debug;Logs.set_reporter(default_reporter());)letcheck_debugnorm_fndebug_fn=matchCaml.Sys.getenv"COHTTP_DEBUG"with|_->Lazy.forceset_log;debug_fn|exceptionCaml.Not_found->norm_fntype'at='aDeferred.tlet(>>=)=Deferred.(>>=)letreturn=Deferred.returntypeic=Reader.ttypeoc=Writer.ttypeconn=unitletread_line=check_debug(funic->Reader.read_lineic>>|function|`Oks->Somes|`Eof->None)(funic->Reader.read_lineic>>|function|`Oks->Log.debug(funfmt->fmt"<<< %s"s);Somes|`Eof->Log.debug(funfmt->fmt"<<<EOF");None)letreadiclen=letbuf=Bytes.createleninReader.readic~lenbuf>>|function|`Oklen'->Bytes.To_string.subbuf~pos:0~len:len'|`Eof->""letwrite=check_debug(funocbuf->Writer.writeocbuf;return())(funocbuf->Log.debug(funfmt->fmt"%4d >>> %s"(Unix.getpid())buf);Writer.writeocbuf;return())letflush=Writer.flushed