mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-03 20:00:31 +08:00
Remove unwrap in filter_http_methods macro
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<http::header::InvalidHeaderValue> 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) => {
|
||||
|
||||
@@ -28,9 +28,7 @@ impl ResponseJsonFor<Body> for VersionContext {
|
||||
req: Request<Body>,
|
||||
) -> Result<Response<JsonValue>, 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]})?)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user