1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 23:51:06 +01: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. //! HTTP Request message related code.
use std::{io, str}; use std::str;
use url::form_urlencoded; use url::form_urlencoded;
use http::{header, Method, Version, Uri, HeaderMap}; use http::{header, Method, Version, Uri, HeaderMap};
use Params; use Params;
use {Cookie, CookieParseError}; use {Cookie, CookieParseError};
use {HttpRange, HttpRangeParseError}; use {HttpRange, HttpRangeParseError};
use error::ParseError;
#[derive(Debug)] #[derive(Debug)]
@ -162,13 +163,12 @@ impl HttpRequest {
} }
/// Check if request has chunked transfer encoding /// 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 Some(encodings) = self.headers().get(header::TRANSFER_ENCODING) {
if let Ok(s) = encodings.to_str() { if let Ok(s) = encodings.to_str() {
Ok(s.to_lowercase().contains("chunked")) Ok(s.to_lowercase().contains("chunked"))
} else { } else {
Err(io::Error::new( Err(ParseError::Header)
io::ErrorKind::Other, "Can not read transfer-encoding header"))
} }
} else { } else {
Ok(false) Ok(false)

View File

@ -2,6 +2,7 @@ extern crate actix_web;
extern crate http; extern crate http;
extern crate time; extern crate time;
use std::str;
use actix_web::*; use actix_web::*;
use http::{header, Method, Uri, Version, HeaderMap, HttpTryFrom}; use http::{header, Method, Uri, Version, HeaderMap, HttpTryFrom};
@ -87,3 +88,26 @@ fn test_request_match_info() {
let req = req.with_match_info(params); let req = req.with_match_info(params);
assert_eq!(req.match_info().find("key"), Some("value")); 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());
}