From 931c3fbbc238c487d31acda29ef6f70f7476b9d3 Mon Sep 17 00:00:00 2001 From: panicbit Date: Wed, 18 Nov 2020 21:26:27 +0100 Subject: [PATCH] add temporary redirects and bad requests --- src/types/response.rs | 13 +++++++++++++ src/types/response_header.rs | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/types/response.rs b/src/types/response.rs index 74e36a3..c883148 100644 --- a/src/types/response.rs +++ b/src/types/response.rs @@ -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>) -> 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) -> 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) diff --git a/src/types/response_header.rs b/src/types/response_header.rs index 824401e..56f2af3 100644 --- a/src/types/response_header.rs +++ b/src/types/response_header.rs @@ -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>) -> 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) -> Result { Ok(Self { status: Status::PERMANENT_FAILURE, @@ -52,6 +67,13 @@ impl ResponseHeader { } } + pub fn bad_request_lossy(reason: impl Cowy) -> Self { + Self { + status: Status::BAD_REQUEST, + meta: Meta::new_lossy(reason), + } + } + pub fn client_certificate_required() -> Self { Self { status: Status::CLIENT_CERTIFICATE_REQUIRED,