[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,17 +148,25 @@ async fn handle_request(
Err(scgi_error) => {
log::error!("Encountered a malformed SCGI request. It could be that your \
webserver is misconfigured. {}", scgi_error);
return
},
};
if let Err(e) = stream.write_vectored(
route_request(&req)
.generate_response(settings)
.into_io_slices()
.as_ref()
).await {
let response = route_request(&req)
.generate_response(settings);
let io_slices = response.into_io_slices();
for slice in io_slices {
if let Err(e) = stream.write_all(&slice).await {
log::warn!(
"Encountered an IO error while sending response: {}", e
);
break;
}
}
if let Err(e) = stream.close().await {
log::warn!(
"Encountered an IO error while sending response: {}", e
"Encountered an IO error while closing connection to server: {}", e
);
}
}
@ -338,7 +346,7 @@ impl Route {
.unwrap();
Ok(Response {
status: 200,
status: b"200",
headers: Cow::Borrowed(&[]),
body: body.into_bytes().into(),
})
@ -359,7 +367,7 @@ impl Route {
.expect("Error encoding thumbnail to PNG");
Ok(Response {
status: 200,
status: b"200",
headers: Cow::Borrowed(&[
(b"Eat", Cow::Borrowed(b"the rich!")),
]),
@ -382,7 +390,7 @@ impl Route {
match error {
BadRequest::MethodNotAllowed => {
Response {
status: 405,
status: b"405",
headers: Cow::Borrowed(&[
(b"Cache-Control", Cow::Borrowed(b"max-age=999999")),
(b"And-Dont", Cow::Borrowed(b"Come Back!")),
@ -392,7 +400,7 @@ impl Route {
},
BadRequest::MalformedPrefstr(_) => {
Response {
status: 404,
status: b"404",
headers: Cow::Borrowed(&[
(b"Cache-Control", Cow::Borrowed(b"max-age=631")),
(b"Help-Im", Cow::Borrowed(b"Trapped in an HTTP header factory!")),
@ -409,7 +417,7 @@ impl Route {
);
Response {
status: 400,
status: b"400",
headers: Cow::Borrowed(&[
(b"I-Killed-God", Cow::Borrowed(b"And drank His blood")),
(b"Now", Cow::Borrowed(b"Realty quivers before my visage")),
@ -426,7 +434,7 @@ impl Route {
.unwrap();
Response {
status: 400,
status: b"400",
headers: Cow::Borrowed(&[
(b"Cache-Control", Cow::Borrowed(b"max-age=540360")),
(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
pub struct Response {
/// 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
///
@ -484,7 +495,7 @@ pub struct Response {
}
impl Response {
fn into_io_slices(&self) -> Vec<IoSlice<'_>> {
pub fn into_io_slices(&self) -> Vec<IoSlice<'_>> {
let mut output = Vec::with_capacity(
// Is computing the exact length of the output necessary? No.
// Do we gain any even vaguely measurable performance gain by preventing reallocation? No.
@ -495,7 +506,7 @@ impl Response {
output.extend_from_slice(&[
IoSlice::new(b"HTTP/1.1 "),
IoSlice::new(self.status.to_string().as_bytes()),
IoSlice::new(self.status),
IoSlice::new(b"\r\n"),
]);
for (h1, h2) in self.headers.iter() {

View File

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