Trait tk_http::client::Codec
[−]
[src]
pub trait Codec<S> { type Future: Future<Item = EncoderDone<S>, Error = Error>; fn start_write(&mut self, e: Encoder<S>) -> Self::Future; fn headers_received(&mut self, headers: &Head) -> Result<RecvMode, Error>; fn data_received(
&mut self,
data: &[u8],
end: bool
) -> Result<Async<usize>, Error>; }
This is a low-level interface to the http client
Your requests starts by sending a codec into a connection Sink or a connection pool. And then it's driven by a callbacks here.
If you don't have any special needs you might want to use
client::buffered::Buffered
codec implementation instead of implemeting
this trait manually.
Associated Types
type Future: Future<Item = EncoderDone<S>, Error = Error>
Future that start_write()
returns
Required Methods
fn start_write(&mut self, e: Encoder<S>) -> Self::Future
Start writing a request
This method is called when there is an open connection and there is some space in the output buffer.
Everything you write into a buffer might be flushed to the network immediately (or as fast as you yield to main loop). On the other hand we might buffer/pipeline multiple requests at once.
fn headers_received(&mut self, headers: &Head) -> Result<RecvMode, Error>
Received headers of a response
At this point we already extracted all the headers and other data
that we need to ensure correctness of the protocol. If you need
to handle some data from the headers you need to store them somewhere
(for example on self
) for further processing.
Note: headers might be received after request_line
is written, but
we don't ensure that request is fully written. You should write the
state machine as if request and response might be streamed a the
same time (including request headers (!) if your start_write
future
writes them incrementally)
fn data_received(
&mut self,
data: &[u8],
end: bool
) -> Result<Async<usize>, Error>
&mut self,
data: &[u8],
end: bool
) -> Result<Async<usize>, Error>
Chunk of the response body received
end
equals to true
for the last chunk of the data.
Method returns Async::Ready(x)
to denote that it has consumed x
bytes. If there are some bytes left in the buffer they will be passed
again on the call.
If the response is empty, or last chunk arrives later and it's empty
we call c.data_received(b"", true)
on every wakeup,
until Async::Ready(0)
is returned (this helps to drive future that
might complete on request completion without spawning another ones,
but note that next request can't start reading in the meantime).
Protocol panics if returned number of bytes larger than data.len()
.