Module crary_body

This module provides functions for detecting, reading, and writing HTTP bodies and encodings such as chunking.

Copyright © 2007, 2008 Scott Parish <srp@srparish.net>

Authors: Scott Parish (srp@srparish.net).

Description

This module provides functions for detecting, reading, and writing HTTP bodies and encodings such as chunking.

If you try to do chunked writes for a pre-http-1.1 client, this module will do it by falling back to writing a streamed body with no content-length. Obviously the connection has to be closed to end the data stream. This means you can using a stream to incrementally generate a webpage, or write out a large file, and browsers such as w3m and lynx will still be able to work fine.

Function Index

done_writing/1Writing the closing chunk.
done_writing/2Writing the Trailers and the closing chunk.
has_body/1Does this request have a body that needs to be read? It determins this by checking for content-length or transfer-encoding headers.
new_reader/1Return a new chunk reader.
new_writer/1Return a new chunk writer.
read/1Read and return the next available chunk.
read/2Read and return Len bytes.
read_all/1Read and return the full body.
with_writer/2Call F(Writer) with a new writer, automatically closing the writer when F returns, and writing an error message if F throws an exception.
write/2Write a chunk of data.

Function Details

done_writing/1

done_writing(Crary_req::crary:crary_req()) -> ok

Writing the closing chunk. For pre-http-1.1 streaming this also closes the socket.

done_writing/2

done_writing(Req::crary:crary_req(), Trailers::crary_headers:headerish()) -> ok

Writing the Trailers and the closing chunk.

has_body/1

has_body(Req::crary:crary_req()) -> bool()

Does this request have a body that needs to be read? It determins this by checking for content-length or transfer-encoding headers.

new_reader/1

new_reader(Req::crary:crary_req()) -> pid()

Return a new chunk reader.

new_writer/1

new_writer(Req::crary:crary_req()) -> pid()

Return a new chunk writer.

It may be observed that currently this call just returns the same Req that was passed to it. Don't depend on this, it will likely be changed to a pid() or similar in the future for supporting buffering or other encodings.

See also: with_writer/2, crary:r/4.

read/1

read(S::pid()) -> binary()

Read and return the next available chunk.

read/2

read(S::pid(), Len::integer()) -> binary()

Read and return Len bytes.

read_all/1

read_all(Req::crary:crary_req()) -> binary()

Read and return the full body. It doesn't matter if the body is chunked or fixed length, this will read it all in and return it as one binary. Probably great for PUT bodies for forms. Probably not great for reading in a large amount of data.

with_writer/2

with_writer(Req::crary:crary_req(), F::function()) -> pid()

Call F(Writer) with a new writer, automatically closing the writer when F returns, and writing an error message if F throws an exception.

Since the response line has almost certainly already been writen out, the best this function can do is append an error message into the output, and hope that it will be seen. If this is not good behavior for your application, use a try/catch form in F to keep errors from making it down the stack to here.

write/2

write(Req::crary:crary_req(), Data::iolist()) -> ok

Write a chunk of data. At the moment, this data is immediately written as a chunk, regardless of the size. In the future writes may get buffered, probably with a configurable buffer size.


Generated by EDoc, Mar 14 2008, 22:35:53.