tiny_httpd/dev/tiny_httpd/Tiny_httpd_stream/index.html
2023-06-21 04:18:42 +00:00

18 lines
No EOL
17 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Tiny_httpd_stream (tiny_httpd.Tiny_httpd_stream)</title><link rel="stylesheet" href="../../odoc.support/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.0"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../odoc.support/highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> <a href="../index.html">tiny_httpd</a> &#x00BB; Tiny_httpd_stream</nav><header class="odoc-preamble"><h1>Module <code><span>Tiny_httpd_stream</span></code></h1><p>Byte streams.</p><p>Streams are used to represent a series of bytes that can arrive progressively. For example, an uploaded file will be sent as a series of chunks.</p><p>These used to live in <a href="../Tiny_httpd/index.html"><code>Tiny_httpd</code></a> but are now in their own module.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.12</li></ul></header><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-hidden"><a href="#type-hidden" class="anchor"></a><code><span><span class="keyword">type</span> hidden</span></code></div><div class="spec-doc"><p>Type used to make <a href="#type-t"><code>t</code></a> unbuildable via a record literal. Use <a href="#val-make"><code>make</code></a> instead.</p></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-t"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> t</span><span> = </span><span>{</span></code><ol><li id="type-t.bs" class="def record field anchored"><a href="#type-t.bs" class="anchor"></a><code><span><span class="keyword">mutable</span> bs : bytes;</span></code><div class="def-doc"><span class="comment-delim">(*</span><p>The bytes</p><span class="comment-delim">*)</span></div></li><li id="type-t.off" class="def record field anchored"><a href="#type-t.off" class="anchor"></a><code><span><span class="keyword">mutable</span> off : int;</span></code><div class="def-doc"><span class="comment-delim">(*</span><p>Beginning of valid slice in <a href="#type-t.bs"><code>bs</code></a></p><span class="comment-delim">*)</span></div></li><li id="type-t.len" class="def record field anchored"><a href="#type-t.len" class="anchor"></a><code><span><span class="keyword">mutable</span> len : int;</span></code><div class="def-doc"><span class="comment-delim">(*</span><p>Length of valid slice in <a href="#type-t.bs"><code>bs</code></a>. If <code>len = 0</code> after a call to <code>fill</code>, then the stream is finished.</p><span class="comment-delim">*)</span></div></li><li id="type-t.fill_buf" class="def record field anchored"><a href="#type-t.fill_buf" class="anchor"></a><code><span>fill_buf : <span>unit <span class="arrow">&#45;&gt;</span></span> unit;</span></code><div class="def-doc"><span class="comment-delim">(*</span><p>See the current slice of the internal buffer as <code>bytes, i, len</code>, where the slice is <code>bytes[i] .. [bytes[i+len-1]]</code>. Can block to refill the buffer if there is currently no content. If <code>len=0</code> then there is no more data.</p><span class="comment-delim">*)</span></div></li><li id="type-t.consume" class="def record field anchored"><a href="#type-t.consume" class="anchor"></a><code><span>consume : <span>int <span class="arrow">&#45;&gt;</span></span> unit;</span></code><div class="def-doc"><span class="comment-delim">(*</span><p>Consume <code>n</code> bytes from the buffer. This should only be called with <code>n &lt;= len</code>.</p><span class="comment-delim">*)</span></div></li><li id="type-t.close" class="def record field anchored"><a href="#type-t.close" class="anchor"></a><code><span>close : <span>unit <span class="arrow">&#45;&gt;</span></span> unit;</span></code><div class="def-doc"><span class="comment-delim">(*</span><p>Close the stream.</p><span class="comment-delim">*)</span></div></li><li id="type-t._rest" class="def record field anchored"><a href="#type-t._rest" class="anchor"></a><code><span>_rest : <a href="#type-hidden">hidden</a>;</span></code><div class="def-doc"><span class="comment-delim">(*</span><p>Use <a href="#val-make"><code>make</code></a> to build a stream.</p><span class="comment-delim">*)</span></div></li></ol><code><span>}</span></code></div><div class="spec-doc"><p>A buffered stream, with a view into the current buffer (or refill if empty), and a function to consume <code>n</code> bytes.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-close"><a href="#val-close" class="anchor"></a><code><span><span class="keyword">val</span> close : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Close stream</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-empty"><a href="#val-empty" class="anchor"></a><code><span><span class="keyword">val</span> empty : <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Stream with 0 bytes inside</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_input"><a href="#val-of_input" class="anchor"></a><code><span><span class="keyword">val</span> of_input : <span>?buf_size:int <span class="arrow">&#45;&gt;</span></span> <span><a href="../Tiny_httpd_io/In_channel/index.html#type-t">Tiny_httpd_io.In_channel.t</a> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Make a buffered stream from the given channel.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> NEXT_RELEASE</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_chan"><a href="#val-of_chan" class="anchor"></a><code><span><span class="keyword">val</span> of_chan : <span>?buf_size:int <span class="arrow">&#45;&gt;</span></span> <span><span class="xref-unresolved">Stdlib</span>.in_channel <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Make a buffered stream from the given channel.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_chan_close_noerr"><a href="#val-of_chan_close_noerr" class="anchor"></a><code><span><span class="keyword">val</span> of_chan_close_noerr : <span>?buf_size:int <span class="arrow">&#45;&gt;</span></span> <span><span class="xref-unresolved">Stdlib</span>.in_channel <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Same as <a href="#val-of_chan"><code>of_chan</code></a> but the <code>close</code> method will never fail.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_fd"><a href="#val-of_fd" class="anchor"></a><code><span><span class="keyword">val</span> of_fd : <span>?buf_size:int <span class="arrow">&#45;&gt;</span></span> <span><span class="xref-unresolved">Unix</span>.file_descr <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Make a buffered stream from the given file descriptor.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_fd_close_noerr"><a href="#val-of_fd_close_noerr" class="anchor"></a><code><span><span class="keyword">val</span> of_fd_close_noerr : <span>?buf_size:int <span class="arrow">&#45;&gt;</span></span> <span><span class="xref-unresolved">Unix</span>.file_descr <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Same as <a href="#val-of_fd"><code>of_fd</code></a> but the <code>close</code> method will never fail.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_bytes"><a href="#val-of_bytes" class="anchor"></a><code><span><span class="keyword">val</span> of_bytes : <span>?i:int <span class="arrow">&#45;&gt;</span></span> <span>?len:int <span class="arrow">&#45;&gt;</span></span> <span>bytes <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>A stream that just returns the slice of bytes starting from <code>i</code> and of length <code>len</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_string"><a href="#val-of_string" class="anchor"></a><code><span><span class="keyword">val</span> of_string : <span>string <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-iter"><a href="#val-iter" class="anchor"></a><code><span><span class="keyword">val</span> iter : <span><span>(<span>bytes <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Iterate on the chunks of the stream</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.3</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-to_chan"><a href="#val-to_chan" class="anchor"></a><code><span><span class="keyword">val</span> to_chan : <span><span class="xref-unresolved">Stdlib</span>.out_channel <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Write the stream to the channel.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.3</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-to_chan'"><a href="#val-to_chan'" class="anchor"></a><code><span><span class="keyword">val</span> to_chan' : <span><a href="../Tiny_httpd_io/Out_channel/index.html#type-t">Tiny_httpd_io.Out_channel.t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Write to the IO channel.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> NEXT_RELEASE</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-make"><a href="#val-make" class="anchor"></a><code><span><span class="keyword">val</span> make :
<span>?bs:bytes <span class="arrow">&#45;&gt;</span></span>
<span>?close:<span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span>consume:<span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span>fill:<span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span>unit <span class="arrow">&#45;&gt;</span></span>
<a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>make ~fill ()</code> creates a byte stream.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">fill</span> <p>is used to refill the buffer, and is called initially.</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">close</span> <p>optional closing.</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">init_size</span> <p>size of the buffer.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_file"><a href="#val-with_file" class="anchor"></a><code><span><span class="keyword">val</span> with_file : <span>?buf_size:int <span class="arrow">&#45;&gt;</span></span> <span>string <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Open a file with given name, and obtain an input stream on its content. When the function returns, the stream (and file) are closed.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-read_line"><a href="#val-read_line" class="anchor"></a><code><span><span class="keyword">val</span> read_line : <span>?buf:<a href="../Tiny_httpd_buf/index.html#type-t">Tiny_httpd_buf.t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> string</span></code></div><div class="spec-doc"><p>Read a line from the stream.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">buf</span> <p>a buffer to (re)use. Its content will be cleared.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-read_all"><a href="#val-read_all" class="anchor"></a><code><span><span class="keyword">val</span> read_all : <span>?buf:<a href="../Tiny_httpd_buf/index.html#type-t">Tiny_httpd_buf.t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> string</span></code></div><div class="spec-doc"><p>Read the whole stream into a string.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">buf</span> <p>a buffer to (re)use. Its content will be cleared.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-limit_size_to"><a href="#val-limit_size_to" class="anchor"></a><code><span><span class="keyword">val</span> limit_size_to :
<span>close_rec:bool <span class="arrow">&#45;&gt;</span></span>
<span>max_size:int <span class="arrow">&#45;&gt;</span></span>
<span>too_big:<span>(<span>int <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span>
<a href="#type-t">t</a></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-read_chunked"><a href="#val-read_chunked" class="anchor"></a><code><span><span class="keyword">val</span> read_chunked : <span>?buf:<a href="../Tiny_httpd_buf/index.html#type-t">Tiny_httpd_buf.t</a> <span class="arrow">&#45;&gt;</span></span> <span>fail:<span>(<span>string <span class="arrow">&#45;&gt;</span></span> exn)</span> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Convert a stream into a stream of byte chunks using the chunked encoding. The size of chunks is not specified.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">buf</span> <p>buffer used for intermediate storage.</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">fail</span> <p>used to build an exception if reading fails.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-read_exactly"><a href="#val-read_exactly" class="anchor"></a><code><span><span class="keyword">val</span> read_exactly :
<span>close_rec:bool <span class="arrow">&#45;&gt;</span></span>
<span>size:int <span class="arrow">&#45;&gt;</span></span>
<span>too_short:<span>(<span>int <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span>
<a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>read_exactly ~size bs</code> returns a new stream that reads exactly <code>size</code> bytes from <code>bs</code>, and then closes.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">close_rec</span> <p>if true, closing the resulting stream also closes <code>bs</code></p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">too_short</span> <p>is called if <code>bs</code> closes with still <code>n</code> bytes remaining</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-output_chunked"><a href="#val-output_chunked" class="anchor"></a><code><span><span class="keyword">val</span> output_chunked : <span><span class="xref-unresolved">Stdlib</span>.out_channel <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Write the stream into the channel, using the chunked encoding.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-output_chunked'"><a href="#val-output_chunked'" class="anchor"></a><code><span><span class="keyword">val</span> output_chunked' : <span><a href="../Tiny_httpd_io/Out_channel/index.html#type-t">Tiny_httpd_io.Out_channel.t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Write the stream into the channel, using the chunked encoding.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> NEXT_RELEASE</li></ul></div></div></div></body></html>