1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-13 02:39:32 +00:00

use ParseError for HttpRequest::chunked()

This commit is contained in:
Nikolay Kim 2017-10-16 10:31:31 -07:00
parent 95fa70d19e
commit ff6779a38e
2 changed files with 29 additions and 5 deletions

View file

@ -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<bool, io::Error> {
pub fn chunked(&self) -> Result<bool, ParseError> {
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)

View file

@ -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());
}