pub struct BodyWriter<'a> { /* private fields */ }
Expand description
A writer that writes to a Unit shared memory response buffer.
This object is created using Request::write_chunks()
or
Request::send_chunks_with_writer()
.
A chunk will be immediately sent to the client once the writer’s memory
buffer reaches chunk_size
, or flush()
is
called on the writer, and a new shared memory buffer will be allocated.
The writer will also flush when dropped, but any errors that happen during a drop will panic.
Implementations§
Source§impl<'a> BodyWriter<'a>
impl<'a> BodyWriter<'a>
Sourcepub fn copy_from_reader<R: Read>(&mut self, r: R) -> Result<()>
pub fn copy_from_reader<R: Read>(&mut self, r: R) -> Result<()>
Copy from a reader to this writer without using an intermediary buffer.
Normally the Write
trait receives an input buffer to
copy from, and the ResponseWriter
writer will copy from it into Unit’s
shared memory.
This method will instead give Unit’s shared memory buffer directly to
the Read
trait in order to skip copying to a third
temporary buffer (such as when using std::io::copy
).
Examples found in repository?
16fn request_handler(req: Request) -> UnitResult<()> {
17 // Create and send a response.
18 let headers = &[("Content-Type", "text/plain")];
19 req.send_response(200, headers, "Hello world!\n")?;
20
21 // NGINX Unit uses "Transfer-Encoding: chunked" by default, and can send
22 // additional chunks after the initial response was already sent to the
23 // client.
24 req.send_chunks_with_writer(4096, |w| {
25 write!(w, "Request data:\n")?;
26 write!(w, " Method: {}\n", req.method())?;
27 write!(w, " Protocol: {}\n", req.version())?;
28 write!(w, " Remote addr: {}\n", req.remote())?;
29 write!(w, " Local addr: {}\n", req.local())?;
30 write!(w, " Server name: {}\n", req.server_name())?;
31 write!(w, " Target: {}\n", req.target())?;
32 write!(w, " Path: {}\n", req.path())?;
33 write!(w, " Query: {}\n", req.query())?;
34 write!(w, " Fields:\n")?;
35 for (name, value) in req.fields() {
36 write!(w, " {}: {}\n", name, value).unwrap();
37 }
38 write!(w, " Body:\n ").unwrap();
39
40 w.copy_from_reader(req.body())?;
41
42 Ok(())
43 })?;
44
45 Ok(())
46}
Trait Implementations§
Source§impl Drop for BodyWriter<'_>
impl Drop for BodyWriter<'_>
Source§impl Write for BodyWriter<'_>
impl Write for BodyWriter<'_>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)