[web] [WIP] Unbreak the previous two commits

This commit is contained in:
Emi Simpson 2021-11-02 12:48:13 -04:00
parent b9620e95a8
commit 74e41de309
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847
2 changed files with 28 additions and 17 deletions

View file

@ -148,18 +148,26 @@ async fn handle_request(
Err(scgi_error) => { Err(scgi_error) => {
log::error!("Encountered a malformed SCGI request. It could be that your \ log::error!("Encountered a malformed SCGI request. It could be that your \
webserver is misconfigured. {}", scgi_error); webserver is misconfigured. {}", scgi_error);
return
}, },
}; };
if let Err(e) = stream.write_vectored( let response = route_request(&req)
route_request(&req) .generate_response(settings);
.generate_response(settings) let io_slices = response.into_io_slices();
.into_io_slices()
.as_ref() for slice in io_slices {
).await { if let Err(e) = stream.write_all(&slice).await {
log::warn!( log::warn!(
"Encountered an IO error while sending response: {}", e "Encountered an IO error while sending response: {}", e
); );
break;
}
}
if let Err(e) = stream.close().await {
log::warn!(
"Encountered an IO error while closing connection to server: {}", e
);
} }
} }
@ -338,7 +346,7 @@ impl Route {
.unwrap(); .unwrap();
Ok(Response { Ok(Response {
status: 200, status: b"200",
headers: Cow::Borrowed(&[]), headers: Cow::Borrowed(&[]),
body: body.into_bytes().into(), body: body.into_bytes().into(),
}) })
@ -359,7 +367,7 @@ impl Route {
.expect("Error encoding thumbnail to PNG"); .expect("Error encoding thumbnail to PNG");
Ok(Response { Ok(Response {
status: 200, status: b"200",
headers: Cow::Borrowed(&[ headers: Cow::Borrowed(&[
(b"Eat", Cow::Borrowed(b"the rich!")), (b"Eat", Cow::Borrowed(b"the rich!")),
]), ]),
@ -382,7 +390,7 @@ impl Route {
match error { match error {
BadRequest::MethodNotAllowed => { BadRequest::MethodNotAllowed => {
Response { Response {
status: 405, status: b"405",
headers: Cow::Borrowed(&[ headers: Cow::Borrowed(&[
(b"Cache-Control", Cow::Borrowed(b"max-age=999999")), (b"Cache-Control", Cow::Borrowed(b"max-age=999999")),
(b"And-Dont", Cow::Borrowed(b"Come Back!")), (b"And-Dont", Cow::Borrowed(b"Come Back!")),
@ -392,7 +400,7 @@ impl Route {
}, },
BadRequest::MalformedPrefstr(_) => { BadRequest::MalformedPrefstr(_) => {
Response { Response {
status: 404, status: b"404",
headers: Cow::Borrowed(&[ headers: Cow::Borrowed(&[
(b"Cache-Control", Cow::Borrowed(b"max-age=631")), (b"Cache-Control", Cow::Borrowed(b"max-age=631")),
(b"Help-Im", Cow::Borrowed(b"Trapped in an HTTP header factory!")), (b"Help-Im", Cow::Borrowed(b"Trapped in an HTTP header factory!")),
@ -409,7 +417,7 @@ impl Route {
); );
Response { Response {
status: 400, status: b"400",
headers: Cow::Borrowed(&[ headers: Cow::Borrowed(&[
(b"I-Killed-God", Cow::Borrowed(b"And drank His blood")), (b"I-Killed-God", Cow::Borrowed(b"And drank His blood")),
(b"Now", Cow::Borrowed(b"Realty quivers before my visage")), (b"Now", Cow::Borrowed(b"Realty quivers before my visage")),
@ -426,7 +434,7 @@ impl Route {
.unwrap(); .unwrap();
Response { Response {
status: 400, status: b"400",
headers: Cow::Borrowed(&[ headers: Cow::Borrowed(&[
(b"Cache-Control", Cow::Borrowed(b"max-age=540360")), (b"Cache-Control", Cow::Borrowed(b"max-age=540360")),
(b"Actually-Tho-Please-Dont-Yell-At-Me", Cow::Borrowed(b"ill cry :(")), (b"Actually-Tho-Please-Dont-Yell-At-Me", Cow::Borrowed(b"ill cry :(")),
@ -471,7 +479,10 @@ impl Route {
/// methods for serializing itself right back into a series of bytes /// methods for serializing itself right back into a series of bytes
pub struct Response { pub struct Response {
/// The status of the response. Exactly the same as an HTTP status code /// The status of the response. Exactly the same as an HTTP status code
status: u16, ///
/// This is a string of ASCII bytes. It can either be just the number (`b"200"`) or the number
/// and a description (`b"200 Ok"`). It's like this so that into_io_slices works
status: &'static [u8],
/// A series of HTTP headers to send to the client /// A series of HTTP headers to send to the client
/// ///
@ -484,7 +495,7 @@ pub struct Response {
} }
impl Response { impl Response {
fn into_io_slices(&self) -> Vec<IoSlice<'_>> { pub fn into_io_slices(&self) -> Vec<IoSlice<'_>> {
let mut output = Vec::with_capacity( let mut output = Vec::with_capacity(
// Is computing the exact length of the output necessary? No. // Is computing the exact length of the output necessary? No.
// Do we gain any even vaguely measurable performance gain by preventing reallocation? No. // Do we gain any even vaguely measurable performance gain by preventing reallocation? No.
@ -495,7 +506,7 @@ impl Response {
output.extend_from_slice(&[ output.extend_from_slice(&[
IoSlice::new(b"HTTP/1.1 "), IoSlice::new(b"HTTP/1.1 "),
IoSlice::new(self.status.to_string().as_bytes()), IoSlice::new(self.status),
IoSlice::new(b"\r\n"), IoSlice::new(b"\r\n"),
]); ]);
for (h1, h2) in self.headers.iter() { for (h1, h2) in self.headers.iter() {

View file

@ -30,7 +30,7 @@ impl StaticAsset {
// compile-time :( // compile-time :(
pub const fn generate_response(&self) -> Response { pub const fn generate_response(&self) -> Response {
Response { Response {
status: 200, status: b"200",
headers: Cow::Borrowed(StaticAsset::STATIC_HEADERS), headers: Cow::Borrowed(StaticAsset::STATIC_HEADERS),
body: Cow::Borrowed(self.bytes), body: Cow::Borrowed(self.bytes),
} }