adjust RawHeaderLine to be (String, Vec<u8>)

This commit is contained in:
Sean McArthur
2014-10-07 15:30:02 -07:00
parent 233d7c4190
commit 50a2112caf
2 changed files with 6 additions and 13 deletions

View File

@@ -10,8 +10,7 @@ use std::hash;
use std::intrinsics::TypeId; use std::intrinsics::TypeId;
use std::mem::{transmute, transmute_copy}; use std::mem::{transmute, transmute_copy};
use std::raw::TraitObject; use std::raw::TraitObject;
use std::str::{from_utf8, SendStr, Slice, Owned}; use std::str::{SendStr, Slice, Owned};
use std::string::raw;
use std::collections::hashmap::{HashMap, Entries, Occupied, Vacant}; use std::collections::hashmap::{HashMap, Entries, Occupied, Vacant};
use std::sync::RWLock; use std::sync::RWLock;
@@ -95,12 +94,6 @@ impl Headers {
loop { loop {
match try!(read_header(rdr)) { match try!(read_header(rdr)) {
Some((name, value)) => { Some((name, value)) => {
// read_header already checks that name is a token, which
// means its safe utf8
let name = unsafe {
raw::from_utf8(name)
};
let name = CaseInsensitive(Owned(name)); let name = CaseInsensitive(Owned(name));
let item = match headers.data.entry(name) { let item = match headers.data.entry(name) {
Vacant(entry) => entry.set(RWLock::new(Raw(vec![]))), Vacant(entry) => entry.set(RWLock::new(Raw(vec![]))),

View File

@@ -520,9 +520,9 @@ pub fn read_http_version<R: Reader>(stream: &mut R) -> HttpResult<HttpVersion> {
/// The raw bytes when parsing a header line. /// The raw bytes when parsing a header line.
/// ///
/// 2 vectors of u8s, divided by COLON (`:`). The first vector is guaranteed /// A String and Vec<u8>, divided by COLON (`:`). The String is guaranteed
/// to be all `token`s. See `is_token_char` source for all valid characters. /// to be all `token`s. See `is_token_char` source for all valid characters.
pub type RawHeaderLine = (Vec<u8>, Vec<u8>); pub type RawHeaderLine = (String, Vec<u8>);
/// Read a RawHeaderLine from a Reader. /// Read a RawHeaderLine from a Reader.
/// ///
@@ -545,7 +545,7 @@ pub type RawHeaderLine = (Vec<u8>, Vec<u8>);
/// > ; see Section 3.2.4 /// > ; see Section 3.2.4
/// > ``` /// > ```
pub fn read_header<R: Reader>(stream: &mut R) -> HttpResult<Option<RawHeaderLine>> { pub fn read_header<R: Reader>(stream: &mut R) -> HttpResult<Option<RawHeaderLine>> {
let mut name = vec![]; let mut name = String::new();
let mut value = vec![]; let mut value = vec![];
loop { loop {
@@ -557,7 +557,7 @@ pub fn read_header<R: Reader>(stream: &mut R) -> HttpResult<Option<RawHeaderLine
} }
}, },
b':' => break, b':' => break,
b if is_token(b) => name.push(b), b if is_token(b) => name.push(b as char),
_nontoken => return Err(HttpHeaderError) _nontoken => return Err(HttpHeaderError)
}; };
} }
@@ -745,7 +745,7 @@ mod tests {
assert_eq!(read_header(&mut mem(s)), result); assert_eq!(read_header(&mut mem(s)), result);
} }
read("Host: rust-lang.org\r\n", Ok(Some(("Host".as_bytes().to_vec(), read("Host: rust-lang.org\r\n", Ok(Some(("Host".to_string(),
"rust-lang.org".as_bytes().to_vec())))); "rust-lang.org".as_bytes().to_vec()))));
} }