pub struct Response {
pub kind: ResponseKind,
pub code: u16,
pub content_type: ContentType,
pub headers: HeaderList,
pub body: ResponseBody,
}Fields§
§kind: ResponseKind§code: u16§content_type: ContentType§headers: HeaderList§body: ResponseBodyImplementations§
source§impl Response
impl Response
pub fn new(code: u16) -> Self
sourcepub fn drop_connection() -> Self
pub fn drop_connection() -> Self
Return this and the server will drop the connection.
sourcepub fn get_body_and_reprocess(max_len: u64) -> Self
pub fn get_body_and_reprocess(max_len: u64) -> Self
Return this and the server will read the request body from the client and call the request handler again.
If the request body is larger than max_len bytes, it sends 413 Payload Too Large.
Examples found in repository?
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
fn put(state: Arc<State>, req: Request) -> Result<Response, Error> {
if req.body.is_pending() {
return Ok(Response::get_body_and_reprocess(1024 * 1024));
}
let body_len = req.body.reader()?.bytes().count();
state.upload_count.fetch_add(1, Ordering::AcqRel);
Ok(Response::text(
200,
format!(
"Upload received, body_len={}, upload_count={}\n",
body_len,
state.upload_count.load(Ordering::Acquire)
),
))
}pub fn html(code: u16, body: impl Into<ResponseBody>) -> Self
sourcepub fn event_stream() -> (EventSender, Response)
pub fn event_stream() -> (EventSender, Response)
sourcepub fn text(code: u16, body: impl Into<ResponseBody>) -> Self
pub fn text(code: u16, body: impl Into<ResponseBody>) -> Self
Examples found in repository?
More examples
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
fn put(state: Arc<State>, req: Request) -> Result<Response, Error> {
if req.body.is_pending() {
return Ok(Response::get_body_and_reprocess(1024 * 1024));
}
let body_len = req.body.reader()?.bytes().count();
state.upload_count.fetch_add(1, Ordering::AcqRel);
Ok(Response::text(
200,
format!(
"Upload received, body_len={}, upload_count={}\n",
body_len,
state.upload_count.load(Ordering::Acquire)
),
))
}
fn handle_req(state: Arc<State>, req: Request) -> Result<Response, Error> {
match (req.method(), req.url().path()) {
("GET", "/health") => Ok(Response::text(200, "ok")),
("PUT", "/upload") => put(state, req),
(_, "/upload") => Ok(Response::method_not_allowed_405(&["PUT"])),
_ => Ok(Response::text(404, "Not found")),
}
}pub fn ok_200() -> Self
pub fn no_content_204() -> Self
sourcepub fn redirect_301(location: impl AsRef<str>) -> Self
pub fn redirect_301(location: impl AsRef<str>) -> Self
Tell the client to GET location.
The client should store this redirect.
Panics
Panics when location is not US-ASCII.
sourcepub fn redirect_303(location: impl AsRef<str>) -> Self
pub fn redirect_303(location: impl AsRef<str>) -> Self
Tell the client to GET location.
The client should not store this redirect.
A PUT or POST handler usually returns this.
Panics
Panics when location is not US-ASCII.
pub fn forbidden_403() -> Self
sourcepub fn not_found_404() -> Self
pub fn not_found_404() -> Self
sourcepub fn method_not_allowed_405(allowed_methods: &[&'static str]) -> Self
pub fn method_not_allowed_405(allowed_methods: &[&'static str]) -> Self
Panics
Panics when any of allowed_methods are not US-ASCII.
pub fn length_required_411() -> Self
pub fn payload_too_large_413() -> Self
pub fn unprocessable_entity_422(body: impl Into<String>) -> Self
pub fn internal_server_errror_500() -> Self
pub fn not_implemented_501() -> Self
pub fn with_body(self, b: impl Into<ResponseBody>) -> Self
sourcepub fn with_max_age_seconds(self, seconds: u32) -> Self
pub fn with_max_age_seconds(self, seconds: u32) -> Self
Adds a Cache-Control: max-age=N header.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
sourcepub fn with_no_store(self) -> Self
pub fn with_no_store(self) -> Self
Adds a Cache-Control: no-store header.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
Adds a Set-Cookie header.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
sourcepub fn with_header(self, name: impl AsRef<str>, value: AsciiString) -> Self
pub fn with_header(self, name: impl AsRef<str>, value: AsciiString) -> Self
Adds a header.
You can call this multiple times to add multiple headers with the same name.
The HTTP spec limits header names to US-ASCII and header values to US-ASCII or ISO-8859-1.
Panics
Panics when name is not US-ASCII.
Example
use servlin::Response;
return Response::new(200)
.with_header("header1", "value1".to_string().try_into().unwrap());