diff --git a/proc_macros/proc_macros.rs b/proc_macros/proc_macros.rs index 0892817..d025bc0 100644 --- a/proc_macros/proc_macros.rs +++ b/proc_macros/proc_macros.rs @@ -378,22 +378,22 @@ pub fn filter_http_methods(item: TokenStream) -> TokenStream { .header(hyper::header::ACCESS_CONTROL_ALLOW_ORIGIN, origin.as_str()) .header(hyper::header::ACCESS_CONTROL_ALLOW_METHODS, #cors_methods_header); #expose_headers - return Ok(builder.status(200).header("Allow", #allow_header).body(#typ).unwrap()); + return Ok(builder.status(200).header("Allow", #allow_header).body(#typ)?); } crate::server::cors::CorsResult::AllowedAll => { let builder = builder .header(hyper::header::ACCESS_CONTROL_ALLOW_ORIGIN, "*") .header(hyper::header::ACCESS_CONTROL_ALLOW_METHODS, #cors_methods_header); #expose_headers - return Ok(builder.status(200).header("Allow", #allow_header).body(#typ).unwrap()); + return Ok(builder.status(200).header("Allow", #allow_header).body(#typ)?); } _ => { - return Ok(builder.status(400).header("Allow", #allow_header).body(#typ).unwrap()); + return Ok(builder.status(400).header("Allow", #allow_header).body(#typ)?); } } } None => { - return Ok(builder.status(200).header("Allow", #allow_header).body(#typ).unwrap()); + return Ok(builder.status(200).header("Allow", #allow_header).body(#typ)?); } } })); @@ -404,7 +404,10 @@ pub fn filter_http_methods(item: TokenStream) -> TokenStream { let headers = h.join(", "); let headers = LitStr::new(headers.as_str(), req.span()); quote!( - builder.headers_mut().unwrap().insert(hyper::header::ACCESS_CONTROL_EXPOSE_HEADERS, #headers.parse().unwrap()); + builder. + headers_mut() + .try_err(gettext("Failed to build response."))? + .insert(hyper::header::ACCESS_CONTROL_EXPOSE_HEADERS, #headers.parse()?); ) } None => quote!(), @@ -421,17 +424,25 @@ pub fn filter_http_methods(item: TokenStream) -> TokenStream { }; match origin { Some(origin) => { + use crate::ext::try_err::TryErr; + use crate::gettext; match #ctx.cors.matches(origin.as_str()) { crate::server::cors::CorsResult::Allowed => { - builder.headers_mut().unwrap().insert(hyper::header::ACCESS_CONTROL_ALLOW_ORIGIN, origin.parse().unwrap()); + builder + .headers_mut() + .try_err(gettext("Failed to build response."))? + .insert(hyper::header::ACCESS_CONTROL_ALLOW_ORIGIN, origin.parse()?); #expose_headers } crate::server::cors::CorsResult::AllowedAll => { - builder.headers_mut().unwrap().insert(hyper::header::ACCESS_CONTROL_ALLOW_ORIGIN, "*".parse().unwrap()); + builder + .headers_mut() + .try_err(gettext("Failed to build response."))? + .insert(hyper::header::ACCESS_CONTROL_ALLOW_ORIGIN, "*".parse()?); #expose_headers } _ => { - return Ok(builder.status(403).body(#typ).unwrap()); + return Ok(builder.status(403).body(#typ)?); } } } @@ -445,7 +456,7 @@ pub fn filter_http_methods(item: TokenStream) -> TokenStream { match #req.method() { #(#streams)* _ => { - return Ok(hyper::Response::builder().status(405).header("Allow", #allow_header).body(#typ).unwrap()) + return Ok(hyper::Response::builder().status(405).header("Allow", #allow_header).body(#typ)?) } } #post_stream diff --git a/src/error.rs b/src/error.rs index 967506c..460eb3e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,6 +12,7 @@ pub enum PixivDownloaderError { UgoiraError(UgoiraError), #[cfg(feature = "server")] Hyper(hyper::Error), + HTTP(http::Error), } impl From<&str> for PixivDownloaderError { @@ -20,6 +21,12 @@ impl From<&str> for PixivDownloaderError { } } +impl From for PixivDownloaderError { + fn from(v: http::header::InvalidHeaderValue) -> Self { + Self::HTTP(http::Error::from(v)) + } +} + #[macro_export] macro_rules! concat_pixiv_downloader_error { ($exp1:expr, $exp2:expr) => { diff --git a/src/server/version.rs b/src/server/version.rs index 8c40c9e..6555e1e 100644 --- a/src/server/version.rs +++ b/src/server/version.rs @@ -28,9 +28,7 @@ impl ResponseJsonFor for VersionContext { req: Request, ) -> Result, PixivDownloaderError> { filter_http_methods!(req, json::object! {}, true, self.ctx, GET, OPTIONS, POST); - Ok(builder - .body(json::object! {"version": [0, 0, 1, 0]}) - .unwrap()) + Ok(builder.body(json::object! {"version": [0, 0, 1, 0]})?) } }