Removing AsciiString dep and one unsafe

This library is not about enforcing standards, so the internal use of
AsciiString for headers and status lines is not necessary.
This commit is contained in:
Martin Algesten
2019-10-20 10:40:30 +02:00
parent ee40b2cff0
commit ed999b579d
4 changed files with 11 additions and 14 deletions

View File

@@ -1,11 +1,10 @@
use crate::error::Error;
use ascii::{AsAsciiStr, AsciiString};
use std::str::FromStr;
#[derive(Clone)]
/// Wrapper type for a header line.
pub struct Header {
line: AsciiString,
line: String,
index: usize,
}
@@ -17,8 +16,7 @@ impl ::std::fmt::Debug for Header {
impl Header {
pub fn new(name: &str, value: &str) -> Self {
let s = format!("{}: {}", name, value);
let line = unsafe { s.as_ascii_str_unchecked().to_owned() };
let line = format!("{}: {}", name, value);
let index = name.len();
Header { line, index }
}
@@ -88,7 +86,7 @@ impl FromStr for Header {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
//
let line = AsciiString::from_str(s).map_err(|_| Error::BadHeader)?;
let line = s.to_string();
let index = s.find(':').ok_or_else(|| Error::BadHeader)?;
// no value?

View File

@@ -1,7 +1,6 @@
use std::io::{Cursor, Error as IoError, ErrorKind, Read, Result as IoResult};
use std::str::FromStr;
use ascii::AsciiString;
use chunked_transfer::Decoder as ChunkDecoder;
use crate::error::Error;
@@ -40,7 +39,7 @@ pub const DEFAULT_CHARACTER_SET: &str = "utf-8";
pub struct Response {
url: Option<String>,
error: Option<Error>,
status_line: AsciiString,
status_line: String,
index: (usize, usize), // index into status_line where we split: HTTP/1.1 200 OK
status: u16,
headers: Vec<Header>,
@@ -117,7 +116,7 @@ impl Response {
pub fn headers_names(&self) -> Vec<String> {
self.headers
.iter()
.map(|h| h.name().to_ascii_lowercase())
.map(|h| h.name().to_lowercase())
.collect()
}
@@ -513,7 +512,7 @@ pub(crate) fn set_stream(resp: &mut Response, url: String, unit: Option<Unit>, s
resp.stream = Some(stream);
}
fn read_next_line<R: Read>(reader: &mut R) -> IoResult<AsciiString> {
fn read_next_line<R: Read>(reader: &mut R) -> IoResult<String> {
let mut buf = Vec::new();
let mut prev_byte_was_cr = false;
@@ -527,7 +526,7 @@ fn read_next_line<R: Read>(reader: &mut R) -> IoResult<AsciiString> {
if byte == b'\n' && prev_byte_was_cr {
buf.pop(); // removing the '\r'
return AsciiString::from_ascii(buf)
return String::from_utf8(buf)
.map_err(|_| IoError::new(ErrorKind::InvalidInput, "Header is not in ASCII"));
}

View File

@@ -134,9 +134,10 @@ fn non_ascii_header() {
let resp = get("test://host/non_ascii_header")
.set("Bäd", "Headör")
.call();
assert!(!resp.ok());
assert_eq!(resp.status(), 500);
assert_eq!(resp.status_text(), "Bad Header");
// surprisingly, this is ok, because this lib is not about enforcing standards.
assert!(resp.ok());
assert_eq!(resp.status(), 200);
assert_eq!(resp.status_text(), "OK");
}
#[test]