12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394(*{{{ Copyright (c) 2012-2014 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.
*
}}}*)exceptionIO_errorofexnlet()=Printexc.register_printer(function|IO_errore->Some("IO error: "^Printexc.to_stringe)|_->None);ifSys.os_type<>"Win32"thenSys.(set_signalsigpipeSignal_ignore);type'at='aLwt.tlet(>>=)=Lwt.bindletreturn=Lwt.returntypeic=Lwt_io.input_channeltypeoc=Lwt_io.output_channeltypeconn=Conduit_lwt_unix.flowletsrc=Logs.Src.create"cohttp.lwt.io"~doc:"Cohttp Lwt IO module"moduleLog=(valLogs.src_logsrc:Logs.LOG)letwrap_readf~if_closed=(* TODO Use [Lwt_io.is_closed] when available:
https://github.com/ocsigen/lwt/pull/635 *)Lwt.catchf(function|Lwt_io.Channel_closed_->Lwt.returnif_closed|Unix.Unix_error_ase->Lwt.fail(IO_errore)|exn->raiseexn)letwrap_writef=Lwt.catchf(function|Unix.Unix_error_ase->Lwt.fail(IO_errore)|exn->raiseexn)letread_lineic=wrap_read~if_closed:None(fun()->Lwt_io.read_line_optic>>=function|None->Log.debug(funf->f"<<< EOF");Lwt.return_none|Somelasx->Log.debug(funf->f"<<< %s"l);Lwt.returnx)letreadiccount=letcount=mincountSys.max_string_lengthinwrap_read~if_closed:""(fun()->Lwt_io.read~countic>>=funbuf->Log.debug(funf->f"<<<[%d] %s"countbuf);Lwt.returnbuf)letwriteocbuf=wrap_write@@fun()->Log.debug(funf->f">>> %s"(String.trimbuf));Lwt_io.writeocbufletflushoc=wrap_write@@fun()->Lwt_io.flushoctypeerror=exnletcatchf=Lwt.try_bindfLwt.return_ok(function|IO_errore->Lwt.return_errore|ex->Lwt.failex)letpp_error=Fmt.exn