add temporary redirects and bad requests

This commit is contained in:
panicbit 2020-11-18 21:26:27 +01:00
parent 0daf01fd3e
commit 931c3fbbc2
2 changed files with 35 additions and 0 deletions

View File

@ -1,4 +1,7 @@
use std::convert::TryInto;
use anyhow::*;
use uriparse::URIReference;
use crate::types::{ResponseHeader, Body, Mime, Document};
use crate::util::Cowy;
use crate::GEMINI_MIME;
@ -35,6 +38,11 @@ impl Response {
Self::new(header)
}
pub fn redirect_temporary_lossy<'a>(location: impl TryInto<URIReference<'a>>) -> Self {
let header = ResponseHeader::redirect_temporary_lossy(location);
Self::new(header)
}
/// Create a successful response with a preconfigured body
///
/// This is equivilent to:
@ -58,6 +66,11 @@ impl Response {
Self::new(header)
}
pub fn bad_request_lossy(reason: impl Cowy<str>) -> Self {
let header = ResponseHeader::bad_request_lossy(reason);
Self::new(header)
}
pub fn client_certificate_required() -> Self {
let header = ResponseHeader::client_certificate_required();
Self::new(header)

View File

@ -1,4 +1,7 @@
use std::convert::TryInto;
use anyhow::*;
use uriparse::URIReference;
use crate::Mime;
use crate::util::Cowy;
use crate::types::{Status, Meta};
@ -31,6 +34,18 @@ impl ResponseHeader {
}
}
pub fn redirect_temporary_lossy<'a>(location: impl TryInto<URIReference<'a>>) -> Self {
let location = match location.try_into() {
Ok(location) => location,
Err(_) => return Self::bad_request_lossy("Invalid redirect location"),
};
Self {
status: Status::REDIRECT_TEMPORARY,
meta: Meta::new_lossy(location.to_string()),
}
}
pub fn server_error(reason: impl Cowy<str>) -> Result<Self> {
Ok(Self {
status: Status::PERMANENT_FAILURE,
@ -52,6 +67,13 @@ impl ResponseHeader {
}
}
pub fn bad_request_lossy(reason: impl Cowy<str>) -> Self {
Self {
status: Status::BAD_REQUEST,
meta: Meta::new_lossy(reason),
}
}
pub fn client_certificate_required() -> Self {
Self {
status: Status::CLIENT_CERTIFICATE_REQUIRED,