diff --git a/src/httprequest.rs b/src/httprequest.rs index fe5f7215d..90e2ecd17 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -1,11 +1,12 @@ -//! Pieces pertaining to the HTTP message protocol. -use std::{io, str}; +//! HTTP Request message related code. +use std::str; use url::form_urlencoded; use http::{header, Method, Version, Uri, HeaderMap}; use Params; use {Cookie, CookieParseError}; use {HttpRange, HttpRangeParseError}; +use error::ParseError; #[derive(Debug)] @@ -162,13 +163,12 @@ impl HttpRequest { } /// Check if request has chunked transfer encoding - pub fn chunked(&self) -> Result { + pub fn chunked(&self) -> Result { if let Some(encodings) = self.headers().get(header::TRANSFER_ENCODING) { if let Ok(s) = encodings.to_str() { Ok(s.to_lowercase().contains("chunked")) } else { - Err(io::Error::new( - io::ErrorKind::Other, "Can not read transfer-encoding header")) + Err(ParseError::Header) } } else { Ok(false) diff --git a/tests/test_httprequest.rs b/tests/test_httprequest.rs index f2276ebab..8d1c594e4 100644 --- a/tests/test_httprequest.rs +++ b/tests/test_httprequest.rs @@ -2,6 +2,7 @@ extern crate actix_web; extern crate http; extern crate time; +use std::str; use actix_web::*; use http::{header, Method, Uri, Version, HeaderMap, HttpTryFrom}; @@ -87,3 +88,26 @@ fn test_request_match_info() { let req = req.with_match_info(params); assert_eq!(req.match_info().find("key"), Some("value")); } + +#[test] +fn test_chunked() { + let req = HttpRequest::new( + Method::GET, Uri::try_from("/").unwrap(), Version::HTTP_11, HeaderMap::new()); + assert!(!req.chunked().unwrap()); + + let mut headers = HeaderMap::new(); + headers.insert(header::TRANSFER_ENCODING, + header::HeaderValue::from_static("chunked")); + let req = HttpRequest::new( + Method::GET, Uri::try_from("/").unwrap(), Version::HTTP_11, headers); + assert!(req.chunked().unwrap()); + + let mut headers = HeaderMap::new(); + let s = unsafe{str::from_utf8_unchecked(b"some va\xadscc\xacas0xsdasdlue".as_ref())}; + + headers.insert(header::TRANSFER_ENCODING, + header::HeaderValue::from_str(s).unwrap()); + let req = HttpRequest::new( + Method::GET, Uri::try_from("/").unwrap(), Version::HTTP_11, headers); + assert!(req.chunked().is_err()); +}