avoid panic for headers
This commit is contained in:
@@ -119,7 +119,7 @@ impl Agent {
|
||||
{
|
||||
add_header(
|
||||
&mut self.headers,
|
||||
Header::new(&header.into(), &value.into()).expect("Failed to parse header"),
|
||||
Header::new(&header.into(), &value.into()),
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use ascii::AsciiString;
|
||||
use ascii::{AsAsciiStr, AsciiString};
|
||||
use error::Error;
|
||||
use std::str::FromStr;
|
||||
|
||||
@@ -16,11 +16,11 @@ impl ::std::fmt::Debug for Header {
|
||||
}
|
||||
|
||||
impl Header {
|
||||
pub fn new(name: &str, value: &str) -> Result<Self, Error> {
|
||||
let line =
|
||||
AsciiString::from_str(&format!("{}: {}", name, value)).map_err(|_| Error::BadHeader)?;
|
||||
pub fn new(name: &str, value: &str) -> Self {
|
||||
let s = format!("{}: {}", name, value);
|
||||
let line = unsafe { s.as_ascii_str_unchecked().to_owned() };
|
||||
let index = name.len();
|
||||
Ok(Header { line, index })
|
||||
Header { line, index }
|
||||
}
|
||||
|
||||
/// The header name.
|
||||
|
||||
@@ -190,10 +190,7 @@ impl Request {
|
||||
K: Into<String>,
|
||||
V: Into<String>,
|
||||
{
|
||||
add_header(
|
||||
&mut self.headers,
|
||||
Header::new(&header.into(), &value.into()).expect("Failed to parse header"),
|
||||
);
|
||||
add_header(&mut self.headers, Header::new(&header.into(), &value.into()));
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ impl Unit {
|
||||
// also don't write this if the user has set it themselves
|
||||
if !is_chunked && !req.has("content-length") {
|
||||
if let Some(size) = body.size {
|
||||
extra.push(Header::new("Content-Length", &format!("{}", size)).unwrap());
|
||||
extra.push(Header::new("Content-Length", &format!("{}", size)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ impl Unit {
|
||||
let password = url.password().unwrap_or("");
|
||||
if (username != "" || password != "") && !req.has("authorization") {
|
||||
let encoded = base64::encode(&format!("{}:{}", username, password));
|
||||
extra.push(Header::new("Authorization", &format!("Basic {}", encoded)).unwrap());
|
||||
extra.push(Header::new("Authorization", &format!("Basic {}", encoded)));
|
||||
}
|
||||
|
||||
extra
|
||||
@@ -204,10 +204,8 @@ fn match_cookies<'a>(jar: &'a CookieJar, domain: &str, path: &str, is_secure: bo
|
||||
let name = c.name().to_string();
|
||||
let value = c.value().to_string();
|
||||
let nameval = Cookie::new(name, value).encoded().to_string();
|
||||
Header::new("Cookie", &nameval).ok()
|
||||
Header::new("Cookie", &nameval)
|
||||
})
|
||||
.filter(|o| o.is_some())
|
||||
.map(|o| o.unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user