Care the case of invalid header value

This commit is contained in:
Kitaiti Makoto 2021-02-04 15:44:06 +09:00
parent 348259a14b
commit d08d2c6958
2 changed files with 14 additions and 14 deletions

View file

@ -155,7 +155,7 @@ where
}; };
let host_header_value = HeaderValue::from_str(&url.host_str().expect("Unreachable")); let host_header_value = HeaderValue::from_str(&url.host_str().expect("Unreachable"));
if host_header_value.is_err() { if host_header_value.is_err() {
warn!("Header valid is invalid: {:?}", url.host_str()); warn!("Header value is invalid: {:?}", url.host_str());
continue; continue;
} }
headers.insert("Host", host_header_value.unwrap()); headers.insert("Host", host_header_value.unwrap());

View file

@ -3,6 +3,7 @@ use openssl::hash::{Hasher, MessageDigest};
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE, DATE, USER_AGENT}; use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE, DATE, USER_AGENT};
use std::ops::Deref; use std::ops::Deref;
use std::time::SystemTime; use std::time::SystemTime;
use tracing::warn;
use crate::activity_pub::sign::Signer; use crate::activity_pub::sign::Signer;
use crate::activity_pub::{ap_accept_header, AP_CONTENT_TYPE}; use crate::activity_pub::{ap_accept_header, AP_CONTENT_TYPE};
@ -129,25 +130,24 @@ pub fn signature<S: Signer>(
path.to_string() path.to_string()
}; };
let mut headers = headers let mut headers_vec = Vec::with_capacity(headers.len());
.iter() for (h, v) in headers.iter() {
.map(|(h, v)| { let v = v.to_str();
( if v.is_err() {
h.as_str().to_lowercase(), warn!("invalid header error: {:?}", v.unwrap_err());
v.to_str() return Err(Error());
.expect("request::signature: invalid header error"), }
) headers_vec.push((h.as_str().to_lowercase(), v.expect("Unreachable")));
}) }
.collect::<Vec<(String, &str)>>();
let request_target = format!("{} {}", method.to_lowercase(), origin_form); let request_target = format!("{} {}", method.to_lowercase(), origin_form);
headers.push(("(request-target)".to_string(), &request_target)); headers_vec.push(("(request-target)".to_string(), &request_target));
let signed_string = headers let signed_string = headers_vec
.iter() .iter()
.map(|(h, v)| format!("{}: {}", h, v)) .map(|(h, v)| format!("{}: {}", h, v))
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("\n"); .join("\n");
let signed_headers = headers let signed_headers = headers_vec
.iter() .iter()
.map(|(h, _)| h.as_ref()) .map(|(h, _)| h.as_ref())
.collect::<Vec<&str>>() .collect::<Vec<&str>>()