From 691c70a380812c0ecd1627360a9cc3c1c118d806 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 23 Dec 2014 12:55:53 -0800 Subject: [PATCH] update str::from_utf8 --- Cargo.toml | 2 ++ src/header/common/accept.rs | 4 ++-- src/header/common/authorization.rs | 4 ++-- src/header/common/cookie.rs | 4 ++-- src/header/common/set_cookie.rs | 2 +- src/header/common/util.rs | 8 ++++---- src/header/mod.rs | 6 +++--- src/http.rs | 6 +++--- src/lib.rs | 2 +- 9 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 00d0a603..86139a95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,8 @@ typeable = "*" cookie = "*" time = "*" mucell = "*" +log = "*" +rustc-serialize = "*" [dev-dependencies] curl = "*" diff --git a/src/header/common/accept.rs b/src/header/common/accept.rs index 3080ebbd..1a9580a0 100644 --- a/src/header/common/accept.rs +++ b/src/header/common/accept.rs @@ -34,7 +34,7 @@ impl Header for Accept { let mut mimes: Vec = vec![]; for mimes_raw in raw.iter() { match from_utf8(mimes_raw.as_slice()) { - Some(mimes_str) => { + Ok(mimes_str) => { for mime_str in mimes_str.split(',') { match from_str(mime_str.trim()) { Some(mime) => mimes.push(mime), @@ -42,7 +42,7 @@ impl Header for Accept { } } }, - None => return None + Err(_) => return None }; } diff --git a/src/header/common/authorization.rs b/src/header/common/authorization.rs index b6708b95..99fdf505 100644 --- a/src/header/common/authorization.rs +++ b/src/header/common/authorization.rs @@ -27,11 +27,11 @@ impl Header for Authorization { fn parse_header(raw: &[Vec]) -> Option> { if raw.len() == 1 { match (from_utf8(unsafe { raw[].unsafe_get(0)[] }), Scheme::scheme(None::)) { - (Some(header), Some(scheme)) + (Ok(header), Some(scheme)) if header.starts_with(scheme) && header.len() > scheme.len() + 1 => { from_str::(header[scheme.len() + 1..]).map(|s| Authorization(s)) }, - (Some(header), None) => from_str::(header).map(|s| Authorization(s)), + (Ok(header), None) => from_str::(header).map(|s| Authorization(s)), _ => None } } else { diff --git a/src/header/common/cookie.rs b/src/header/common/cookie.rs index a4717995..3496d000 100644 --- a/src/header/common/cookie.rs +++ b/src/header/common/cookie.rs @@ -27,7 +27,7 @@ impl Header for Cookies { let mut cookies = Vec::with_capacity(raw.len()); for cookies_raw in raw.iter() { match from_utf8(cookies_raw[]) { - Some(cookies_str) => { + Ok(cookies_str) => { for cookie_str in cookies_str.split(';') { match from_str(cookie_str.trim()) { Some(cookie) => cookies.push(cookie), @@ -35,7 +35,7 @@ impl Header for Cookies { } } }, - None => return None + Err(_) => return None }; } diff --git a/src/header/common/set_cookie.rs b/src/header/common/set_cookie.rs index aee732d1..ea7b6212 100644 --- a/src/header/common/set_cookie.rs +++ b/src/header/common/set_cookie.rs @@ -24,7 +24,7 @@ impl Header for SetCookie { let mut set_cookies = Vec::with_capacity(raw.len()); for set_cookies_raw in raw.iter() { match from_utf8(set_cookies_raw[]) { - Some(s) if !s.is_empty() => { + Ok(s) if !s.is_empty() => { match from_str(s) { Some(cookie) => set_cookies.push(cookie), None => () diff --git a/src/header/common/util.rs b/src/header/common/util.rs index b6bb4d3c..5cb69961 100644 --- a/src/header/common/util.rs +++ b/src/header/common/util.rs @@ -11,8 +11,8 @@ pub fn from_one_raw_str(raw: &[Vec]) -> Option { } // we JUST checked that raw.len() == 1, so raw[0] WILL exist. match from_utf8(unsafe { raw[].unsafe_get(0)[] }) { - Some(s) => FromStr::from_str(s), - None => None + Ok(s) => FromStr::from_str(s), + Err(_) => None } } @@ -29,13 +29,13 @@ pub fn from_comma_delimited(raw: &[Vec]) -> Option> { /// Reads a comma-delimited raw string into a Vec. pub fn from_one_comma_delimited(raw: &[u8]) -> Option> { match from_utf8(raw) { - Some(s) => { + Ok(s) => { Some(s.as_slice() .split([',', ' '].as_slice()) .filter_map(from_str) .collect()) } - None => None + Err(_) => None } } diff --git a/src/header/mod.rs b/src/header/mod.rs index 01cb3f80..a483139a 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -12,7 +12,7 @@ use std::intrinsics::TypeId; use std::raw::TraitObject; use std::str::{SendStr, FromStr}; use std::collections::HashMap; -use std::collections::hash_map::{Entries, Occupied, Vacant}; +use std::collections::hash_map::{Entries, Entry}; use std::{hash, mem}; use mucell::MuCell; @@ -130,8 +130,8 @@ impl Headers { debug!("raw header: {}={}", name, value[].to_ascii()); let name = CaseInsensitive(Owned(name)); let mut item = match headers.data.entry(name) { - Vacant(entry) => entry.set(MuCell::new(Item::raw(vec![]))), - Occupied(entry) => entry.into_mut() + Entry::Vacant(entry) => entry.set(MuCell::new(Item::raw(vec![]))), + Entry::Occupied(entry) => entry.into_mut() }; match &mut item.borrow_mut().raw { diff --git a/src/http.rs b/src/http.rs index b02a09a1..e62dff16 100644 --- a/src/http.rs +++ b/src/http.rs @@ -622,7 +622,7 @@ pub fn read_status(stream: &mut R) -> HttpResult { try!(stream.read_byte()), ]; - let code = match str::from_utf8(code.as_slice()).and_then(from_str::) { + let code = match str::from_utf8(code.as_slice()).ok().and_then(from_str::) { Some(num) => num, None => return Err(HttpStatusError) }; @@ -662,8 +662,8 @@ pub fn read_status(stream: &mut R) -> HttpResult { } let reason = match str::from_utf8(buf[]) { - Some(s) => s.trim(), - None => return Err(HttpStatusError) + Ok(s) => s.trim(), + Err(_) => return Err(HttpStatusError) }; let reason = match from_u16::(code) { diff --git a/src/lib.rs b/src/lib.rs index 3e199ce6..7ee41025 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -125,7 +125,7 @@ //! implement `Reader` and can be read to get the data out of a `Response`. //! -extern crate serialize; +extern crate "rustc-serialize" as serialize; extern crate time; extern crate url; extern crate openssl;