From 2969e87b5262b0c75d9298f8a2bf4a82336e918d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Garc=C3=ADa?=
 <dani-garcia@users.noreply.github.com>
Date: Sun, 14 Mar 2021 23:24:47 +0100
Subject: [PATCH] Add separate host-only fromrequest handler

---
 src/auth.rs | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/src/auth.rs b/src/auth.rs
index eaf4c707..4041cc6b 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -222,13 +222,12 @@ use crate::db::{
     DbConn,
 };
 
-pub struct Headers {
-    pub host: String,
-    pub device: Device,
-    pub user: User,
+pub struct Host {
+    pub host: String
 }
 
-impl<'a, 'r> FromRequest<'a, 'r> for Headers {
+
+impl<'a, 'r> FromRequest<'a, 'r> for Host {
     type Error = &'static str;
 
     fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
@@ -262,6 +261,28 @@ impl<'a, 'r> FromRequest<'a, 'r> for Headers {
             format!("{}://{}", protocol, host)
         };
 
+        Outcome::Success(Host { host })
+    }
+}
+
+pub struct Headers {
+    pub host: String,
+    pub device: Device,
+    pub user: User,
+}
+
+impl<'a, 'r> FromRequest<'a, 'r> for Headers {
+    type Error = &'static str;
+
+    fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
+        let headers = request.headers();
+
+        let host = match Host::from_request(request) {
+            Outcome::Forward(_) => return Outcome::Forward(()),
+            Outcome::Failure(f) => return Outcome::Failure(f),
+            Outcome::Success(host) => host.host,
+        };
+
         // Get access_token
         let access_token: &str = match headers.get_one("Authorization") {
             Some(a) => match a.rsplit("Bearer ").next() {
-- 
GitLab