Skip to content

Commit

Permalink
Bump Rocket
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed Feb 12, 2024
1 parent 8ae85f4 commit 1b77745
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dotenv = "0.15.0"
addr = "0.15.3"

# Webserver
rocket = { version = "0.5.0-rc.2", features = ["json"] }
rocket = { version = "0.5.0", features = ["json"] }

# Serialization/Deserialization
quick-xml = { version = "0.27.1", default-features = false, features = [ "serialize" ] }
Expand Down
8 changes: 6 additions & 2 deletions src/host_header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Source: https://github.com/SergioBenitez/Rocket/issues/823#issuecomment-1157761369
use addr::parse_domain_name;
use rocket::http::Status;
use rocket::request::FromRequest;
use rocket::request::Outcome;
use rocket::Request;
Expand All @@ -23,7 +24,7 @@ fn get_base_domain<'a>(header: &'a str) -> String {

#[rocket::async_trait]
impl<'r> FromRequest<'r> for HostHeader<'r> {
type Error = ();
type Error = String;

async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let header_name: &str = match req.headers().contains("X-Forwarded-Host") {
Expand All @@ -36,7 +37,10 @@ impl<'r> FromRequest<'r> for HostHeader<'r> {
host: h,
base_domain: get_base_domain(h),
}),
None => Outcome::Forward(()),
None => Outcome::Error((
Status::UnprocessableEntity,
"Missing a Host or X-Forwarded-Host header".to_string(),
)),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn rocket() -> _ {
routes::autoconfig::post_mail_autodiscover_microsoft_case,
routes::autoconfig::post_mail_autodiscover_microsoft_camel_case,
routes::autoconfig::post_mail_autodiscover_microsoft_json,
routes::autoconfig::post_mail_autodiscover_microsoft_json_legacy,
],
);
if cfg!(feature = "apple") {
Expand Down
10 changes: 5 additions & 5 deletions src/ressources/AutoDiscoverXml.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use quick_xml::de::from_str;
use quick_xml::DeError;
use rocket::data::ByteUnit;
use rocket::data::{self, Capped, Data, FromData};
use rocket::http::{ContentType, Status};
use rocket::request::Request;
use rocket::response::{self, Responder, Response};
use rocket_dyn_templates::Template;
use serde::Deserialize;
use quick_xml::de::from_str;
use quick_xml::DeError;
use std::io;

#[derive(Debug)]
Expand Down Expand Up @@ -45,7 +45,7 @@ impl<'r> FromData<'r> for AutoDiscoverXmlPayload {
type Error = XmlError;

async fn from_data(req: &'r Request<'_>, data: Data<'r>) -> data::Outcome<'r, Self> {
use rocket::outcome::Outcome::*;
use rocket::outcome::Outcome::{Error, Success};
let size_limit = req.limits().get("xml").unwrap_or(ByteUnit::Kilobyte(4096));

let contents: Result<Capped<std::string::String>, std::io::Error> =
Expand All @@ -55,13 +55,13 @@ impl<'r> FromData<'r> for AutoDiscoverXmlPayload {
let payload: Result<AutoDiscoverXmlPayload, DeError> = from_str(&dd);
match payload {
Ok(d) => Success(d),
Err(e) => Failure((
Err(e) => Error((
Status::UnprocessableEntity,
XmlError::Parse(dd.to_string(), e),
)),
}
}
Err(e) => Failure((Status::UnprocessableEntity, XmlError::Io(e))),
Err(e) => Error((Status::UnprocessableEntity, XmlError::Io(e))),
}
}
}
Expand Down
33 changes: 32 additions & 1 deletion src/routes/autoconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,43 @@ pub fn well_known_mail_config_v11(host: HostHeader, emailaddress: Option<&str>)
// Example: /autodiscover/autodiscover.json?Email=test%40wdes.fr&Protocol=ActiveSync&RedirectCount=1
// Example: /autodiscover/autodiscover.json?Email=test%40wdes.fr&Protocol=Autodiscoverv1&RedirectCount=1
#[get("/autodiscover/autodiscover.json?<Email>&<Protocol>&<RedirectCount>")]
#[allow(unused_variables)]
#[allow(non_snake_case)]
pub fn post_mail_autodiscover_microsoft_json(
host: HostHeader,
Email: Option<&str>,
Protocol: Option<&str>,
RedirectCount: Option<&str>,
) -> Result<Json<AutoDiscoverJson>, AutoDiscoverJsonError> {
match Protocol {
Some("AutodiscoverV1") => Ok(Json(AutoDiscoverJson {
Protocol: "AutodiscoverV1".to_string(),
Url: "https://".to_owned() + &host.host + "/Autodiscover/Autodiscover.xml",
})),
Some("Autodiscoverv1") => Ok(Json(AutoDiscoverJson {
Protocol: "Autodiscoverv1".to_string(),
Url: "https://".to_owned() + &host.host + "/Autodiscover/Autodiscover.xml",
})),
/*
Some("ActiveSync") => Some(AutoDiscoverJson {
Protocol: "ActiveSync".to_string(),
Url: "https://".to_owned() + &host.host + "/Microsoft-Server-ActiveSync",
}),*/
_ => Err(AutoDiscoverJsonError {
ErrorCode: "InvalidProtocol".to_string(),
ErrorMessage:
"The given protocol value is invalid. Supported values are \"AutodiscoverV1\" or \"Autodiscoverv1\"."
.to_string(),
}),
}
}

// Used by Microsoft Office 2009 (to be confirmed)
// Example: /autodiscover/autodiscover.json/v1.0/infos%40domain.tld?Protocol=ActiveSync&RedirectCount=1
#[get("/autodiscover/autodiscover.json/v1.0/infos?<Email>&<Protocol>&<RedirectCount>")]
#[allow(unused_variables)]
#[allow(non_snake_case)]
pub fn post_mail_autodiscover_microsoft_json(
pub fn post_mail_autodiscover_microsoft_json_legacy(
host: HostHeader,
Email: Option<&str>,
Protocol: Option<&str>,
Expand Down

0 comments on commit 1b77745

Please sign in to comment.