pub struct OutgoingResponse { /* private fields */ }
Expand description
Represents an outgoing HTTP response.
OutgoingResponse is used in conjunction with ResponseOutparam in cases where you want to stream the response body. In cases where you don’t need to stream, it is often simpler to use Response.
§Examples
Send a streaming response to an incoming request:
async fn handle_request(req: IncomingRequest, response_outparam: ResponseOutparam) {
use futures::SinkExt;
use std::io::Read;
let response_headers = Fields::from_list(&[
("content-type".to_owned(), "text/plain".into())
]).unwrap();
let response = OutgoingResponse::new(response_headers);
response.set_status_code(200).unwrap();
let mut response_body = response.take_body();
response_outparam.set(response);
let mut file = std::fs::File::open("war-and-peace.txt").unwrap();
loop {
let mut buf = vec![0; 1024];
let count = file.read(&mut buf).unwrap();
if count == 0 {
break; // end of file
}
buf.truncate(count);
response_body.send(buf).await.unwrap();
}
}
Send a response then continue processing:
async fn handle_request(req: IncomingRequest, response_outparam: ResponseOutparam) {
use futures::SinkExt;
let response_headers = Fields::from_list(&[
("content-type".to_owned(), "text/plain".into())
]).unwrap();
let response = OutgoingResponse::new(response_headers);
response.set_status_code(200).unwrap();
let mut response_body = response.take_body();
response_outparam.set(response);
response_body
.send("Request accepted".as_bytes().to_vec())
.await
.unwrap();
// End the HTTP response so the client deems it complete.
response_body.flush().await.unwrap();
response_body.close().await.unwrap();
drop(response_body);
// Perform any additional processing
println!("While the cat's away, the mice will play");
}
Represents an outgoing HTTP Response.
Implementations§
Source§impl OutgoingResponse
impl OutgoingResponse
Sourcepub fn status_code(&self) -> StatusCode
pub fn status_code(&self) -> StatusCode
Get the HTTP Status Code for the Response.
Source§impl OutgoingResponse
impl OutgoingResponse
Sourcepub fn set_status_code(&self, status_code: StatusCode) -> Result<(), ()>
pub fn set_status_code(&self, status_code: StatusCode) -> Result<(), ()>
Set the HTTP Status Code for the Response. Fails if the status-code given is not a valid http status code.
Source§impl OutgoingResponse
impl OutgoingResponse
Sourcepub fn headers(&self) -> Headers
pub fn headers(&self) -> Headers
Get the headers associated with the Request.
The returned headers
resource is immutable: set
, append
, and
delete
operations will fail with header-error.immutable
.
This headers resource is a child: it must be dropped before the parent
outgoing-request
is dropped, or its ownership is transfered to
another component by e.g. outgoing-handler.handle
.
Source§impl OutgoingResponse
impl OutgoingResponse
Sourcepub fn body(&self) -> Result<OutgoingBody, ()>
pub fn body(&self) -> Result<OutgoingBody, ()>
Returns the resource corresponding to the outgoing Body for this Response.
Returns success on the first call: the outgoing-body
resource for
this outgoing-response
can be retrieved at most once. Subsequent
calls will return error.