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:
@@ -17,7 +17,6 @@ charset = ["encoding"]
|
|||||||
tls = ["rustls", "webpki", "webpki-roots"]
|
tls = ["rustls", "webpki", "webpki-roots"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ascii = "1"
|
|
||||||
base64 = "0.10"
|
base64 = "0.10"
|
||||||
chunked_transfer = "1"
|
chunked_transfer = "1"
|
||||||
cookie = { version = "0.12", features = ["percent-encode"] }
|
cookie = { version = "0.12", features = ["percent-encode"] }
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use ascii::{AsAsciiStr, AsciiString};
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
/// Wrapper type for a header line.
|
/// Wrapper type for a header line.
|
||||||
pub struct Header {
|
pub struct Header {
|
||||||
line: AsciiString,
|
line: String,
|
||||||
index: usize,
|
index: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,8 +16,7 @@ impl ::std::fmt::Debug for Header {
|
|||||||
|
|
||||||
impl Header {
|
impl Header {
|
||||||
pub fn new(name: &str, value: &str) -> Self {
|
pub fn new(name: &str, value: &str) -> Self {
|
||||||
let s = format!("{}: {}", name, value);
|
let line = format!("{}: {}", name, value);
|
||||||
let line = unsafe { s.as_ascii_str_unchecked().to_owned() };
|
|
||||||
let index = name.len();
|
let index = name.len();
|
||||||
Header { line, index }
|
Header { line, index }
|
||||||
}
|
}
|
||||||
@@ -88,7 +86,7 @@ impl FromStr for Header {
|
|||||||
type Err = Error;
|
type Err = Error;
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
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)?;
|
let index = s.find(':').ok_or_else(|| Error::BadHeader)?;
|
||||||
|
|
||||||
// no value?
|
// no value?
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use std::io::{Cursor, Error as IoError, ErrorKind, Read, Result as IoResult};
|
use std::io::{Cursor, Error as IoError, ErrorKind, Read, Result as IoResult};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use ascii::AsciiString;
|
|
||||||
use chunked_transfer::Decoder as ChunkDecoder;
|
use chunked_transfer::Decoder as ChunkDecoder;
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
@@ -40,7 +39,7 @@ pub const DEFAULT_CHARACTER_SET: &str = "utf-8";
|
|||||||
pub struct Response {
|
pub struct Response {
|
||||||
url: Option<String>,
|
url: Option<String>,
|
||||||
error: Option<Error>,
|
error: Option<Error>,
|
||||||
status_line: AsciiString,
|
status_line: String,
|
||||||
index: (usize, usize), // index into status_line where we split: HTTP/1.1 200 OK
|
index: (usize, usize), // index into status_line where we split: HTTP/1.1 200 OK
|
||||||
status: u16,
|
status: u16,
|
||||||
headers: Vec<Header>,
|
headers: Vec<Header>,
|
||||||
@@ -117,7 +116,7 @@ impl Response {
|
|||||||
pub fn headers_names(&self) -> Vec<String> {
|
pub fn headers_names(&self) -> Vec<String> {
|
||||||
self.headers
|
self.headers
|
||||||
.iter()
|
.iter()
|
||||||
.map(|h| h.name().to_ascii_lowercase())
|
.map(|h| h.name().to_lowercase())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -513,7 +512,7 @@ pub(crate) fn set_stream(resp: &mut Response, url: String, unit: Option<Unit>, s
|
|||||||
resp.stream = Some(stream);
|
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 buf = Vec::new();
|
||||||
let mut prev_byte_was_cr = false;
|
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 {
|
if byte == b'\n' && prev_byte_was_cr {
|
||||||
buf.pop(); // removing the '\r'
|
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"));
|
.map_err(|_| IoError::new(ErrorKind::InvalidInput, "Header is not in ASCII"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -134,9 +134,10 @@ fn non_ascii_header() {
|
|||||||
let resp = get("test://host/non_ascii_header")
|
let resp = get("test://host/non_ascii_header")
|
||||||
.set("Bäd", "Headör")
|
.set("Bäd", "Headör")
|
||||||
.call();
|
.call();
|
||||||
assert!(!resp.ok());
|
// surprisingly, this is ok, because this lib is not about enforcing standards.
|
||||||
assert_eq!(resp.status(), 500);
|
assert!(resp.ok());
|
||||||
assert_eq!(resp.status_text(), "Bad Header");
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(resp.status_text(), "OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user