1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 23:51:06 +01:00

use http::Uri for uri parsing

This commit is contained in:
Nikolay Kim 2017-11-30 19:01:25 -08:00
parent 7135c0163b
commit 3fcd5f6935
10 changed files with 98 additions and 100 deletions

View File

@ -37,7 +37,7 @@ log = "0.3"
failure = "0.1" failure = "0.1"
failure_derive = "0.1" failure_derive = "0.1"
time = "0.1" time = "0.1"
http = "0.1" http = "^0.1.2"
httparse = "0.1" httparse = "0.1"
http-range = "0.1" http-range = "0.1"
mime = "0.3" mime = "0.3"

View File

@ -12,6 +12,7 @@ use httparse;
use failure::Fail; use failure::Fail;
use http2::Error as Http2Error; use http2::Error as Http2Error;
use http::{header, StatusCode, Error as HttpError}; use http::{header, StatusCode, Error as HttpError};
use http::uri::InvalidUriBytes;
use http_range::HttpRangeParseError; use http_range::HttpRangeParseError;
use serde_json::error::Error as JsonError; use serde_json::error::Error as JsonError;
@ -110,8 +111,8 @@ pub enum ParseError {
#[fail(display="Invalid Method specified")] #[fail(display="Invalid Method specified")]
Method, Method,
/// An invalid `Uri`, such as `exam ple.domain`. /// An invalid `Uri`, such as `exam ple.domain`.
#[fail(display="Uri error")] #[fail(display="Uri error: {}", _0)]
Uri, Uri(InvalidUriBytes),
/// An invalid `HttpVersion`, such as `HTP/1.1` /// An invalid `HttpVersion`, such as `HTP/1.1`
#[fail(display="Invalid HTTP version specified")] #[fail(display="Invalid HTTP version specified")]
Version, Version,

View File

@ -6,13 +6,12 @@ use std::collections::VecDeque;
use actix::Arbiter; use actix::Arbiter;
use httparse; use httparse;
use http::{Method, Version, HttpTryFrom, HeaderMap}; use http::{Uri, Method, Version, HttpTryFrom, HeaderMap};
use http::header::{self, HeaderName, HeaderValue}; use http::header::{self, HeaderName, HeaderValue};
use bytes::{Bytes, BytesMut, BufMut}; use bytes::{Bytes, BytesMut, BufMut};
use futures::{Future, Poll, Async}; use futures::{Future, Poll, Async};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use tokio_core::reactor::Timeout; use tokio_core::reactor::Timeout;
use percent_encoding;
use pipeline::Pipeline; use pipeline::Pipeline;
use encoding::PayloadType; use encoding::PayloadType;
@ -515,31 +514,8 @@ impl Reader {
let slice = buf.split_to(len).freeze(); let slice = buf.split_to(len).freeze();
let path = slice.slice(path.0, path.1); let path = slice.slice(path.0, path.1);
// path was found to be utf8 by httparse
// manually split path, path was found to be utf8 by httparse let uri = Uri::from_shared(path).map_err(ParseError::Uri)?;
let uri = {
if let Ok(path) = percent_encoding::percent_decode(&path).decode_utf8() {
let parts: Vec<&str> = path.splitn(2, '?').collect();
if parts.len() == 2 {
Some((parts[0].to_owned(), parts[1].to_owned()))
} else {
Some((parts[0].to_owned(), String::new()))
}
} else {
None
}
};
let (path, query) = if let Some(uri) = uri {
uri
} else {
let parts: Vec<&str> = unsafe{
std::str::from_utf8_unchecked(&path)}.splitn(2, '?').collect();
if parts.len() == 2 {
(parts[0].to_owned(), parts[1][1..].to_owned())
} else {
(parts[0].to_owned(), String::new())
}
};
// convert headers // convert headers
let mut headers = HeaderMap::with_capacity(headers_len); let mut headers = HeaderMap::with_capacity(headers_len);
@ -558,7 +534,7 @@ impl Reader {
} }
let (mut psender, payload) = Payload::new(false); let (mut psender, payload) = Payload::new(false);
let msg = HttpRequest::new(method, path, version, headers, query, payload); let msg = HttpRequest::new(method, uri, version, headers, payload);
let decoder = if msg.upgrade() { let decoder = if msg.upgrade() {
Decoder::eof() Decoder::eof()

View File

@ -216,14 +216,11 @@ impl Entry {
router: &Rc<Vec<H>>) -> Entry router: &Rc<Vec<H>>) -> Entry
where H: HttpHandler + 'static where H: HttpHandler + 'static
{ {
let path = parts.uri.path().to_owned();
let query = parts.uri.query().unwrap_or("").to_owned();
// Payload and Content-Encoding // Payload and Content-Encoding
let (psender, payload) = Payload::new(false); let (psender, payload) = Payload::new(false);
let mut req = HttpRequest::new( let mut req = HttpRequest::new(
parts.method, path, parts.version, parts.headers, query, payload); parts.method, parts.uri, parts.version, parts.headers, payload);
// set remote addr // set remote addr
req.set_remove_addr(addr); req.set_remove_addr(addr);

View File

@ -6,7 +6,7 @@ use std::collections::HashMap;
use bytes::BytesMut; use bytes::BytesMut;
use futures::{Async, Future, Stream, Poll}; use futures::{Async, Future, Stream, Poll};
use url::form_urlencoded; use url::form_urlencoded;
use http::{header, Method, Version, HeaderMap, Extensions}; use http::{header, Uri, Method, Version, HeaderMap, Extensions};
use {Cookie, HttpRange}; use {Cookie, HttpRange};
use recognizer::Params; use recognizer::Params;
@ -18,9 +18,8 @@ use error::{ParseError, PayloadError,
struct HttpMessage { struct HttpMessage {
version: Version, version: Version,
method: Method, method: Method,
path: String, uri: Uri,
prefix: usize, prefix: usize,
query: String,
headers: HeaderMap, headers: HeaderMap,
extensions: Extensions, extensions: Extensions,
params: Params, params: Params,
@ -35,9 +34,8 @@ impl Default for HttpMessage {
fn default() -> HttpMessage { fn default() -> HttpMessage {
HttpMessage { HttpMessage {
method: Method::GET, method: Method::GET,
path: String::new(), uri: Uri::default(),
prefix: 0, prefix: 0,
query: String::new(),
version: Version::HTTP_11, version: Version::HTTP_11,
headers: HeaderMap::new(), headers: HeaderMap::new(),
params: Params::empty(), params: Params::empty(),
@ -56,15 +54,14 @@ pub struct HttpRequest<S=()>(Rc<HttpMessage>, Rc<S>);
impl HttpRequest<()> { impl HttpRequest<()> {
/// Construct a new Request. /// Construct a new Request.
#[inline] #[inline]
pub fn new(method: Method, path: String, version: Version, pub fn new(method: Method, uri: Uri,
headers: HeaderMap, query: String, payload: Payload) -> HttpRequest version: Version, headers: HeaderMap, payload: Payload) -> HttpRequest
{ {
HttpRequest( HttpRequest(
Rc::new(HttpMessage { Rc::new(HttpMessage {
method: method, method: method,
path: path, uri: uri,
prefix: 0, prefix: 0,
query: query,
version: version, version: version,
headers: headers, headers: headers,
params: Params::empty(), params: Params::empty(),
@ -104,6 +101,10 @@ impl<S> HttpRequest<S> {
&mut self.as_mut().extensions &mut self.as_mut().extensions
} }
/// Read the Request Uri.
#[inline]
pub fn uri(&self) -> &Uri { &self.0.uri }
/// Read the Request method. /// Read the Request method.
#[inline] #[inline]
pub fn method(&self) -> &Method { &self.0.method } pub fn method(&self) -> &Method { &self.0.method }
@ -123,7 +124,7 @@ impl<S> HttpRequest<S> {
/// The target path of this Request. /// The target path of this Request.
#[inline] #[inline]
pub fn path(&self) -> &str { pub fn path(&self) -> &str {
&self.0.path self.0.uri.path()
} }
pub(crate) fn set_prefix(&mut self, idx: usize) { pub(crate) fn set_prefix(&mut self, idx: usize) {
@ -155,9 +156,11 @@ impl<S> HttpRequest<S> {
#[inline] #[inline]
pub fn query(&self) -> HashMap<String, String> { pub fn query(&self) -> HashMap<String, String> {
let mut q: HashMap<String, String> = HashMap::new(); let mut q: HashMap<String, String> = HashMap::new();
for (key, val) in form_urlencoded::parse(self.0.query.as_ref()) { if let Some(query) = self.0.uri.query().as_ref() {
for (key, val) in form_urlencoded::parse(query.as_ref()) {
q.insert(key.to_string(), val.to_string()); q.insert(key.to_string(), val.to_string());
} }
}
q q
} }
@ -166,7 +169,11 @@ impl<S> HttpRequest<S> {
/// E.g., id=10 /// E.g., id=10
#[inline] #[inline]
pub fn query_string(&self) -> &str { pub fn query_string(&self) -> &str {
&self.0.query if let Some(query) = self.0.uri.query().as_ref() {
query
} else {
""
}
} }
/// Return request cookies. /// Return request cookies.
@ -364,7 +371,7 @@ impl<S> Clone for HttpRequest<S> {
impl<S> fmt::Debug for HttpRequest<S> { impl<S> fmt::Debug for HttpRequest<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let res = write!(f, "\nHttpRequest {:?} {}:{}\n", let res = write!(f, "\nHttpRequest {:?} {}:{}\n",
self.0.version, self.0.method, self.0.path); self.0.version, self.0.method, self.0.uri);
if !self.query_string().is_empty() { if !self.query_string().is_empty() {
let _ = write!(f, " query: ?{:?}\n", self.query_string()); let _ = write!(f, " query: ?{:?}\n", self.query_string());
} }
@ -418,7 +425,9 @@ impl Future for UrlEncoded {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::str::FromStr;
use payload::Payload; use payload::Payload;
use http::Uri;
#[test] #[test]
fn test_urlencoded_error() { fn test_urlencoded_error() {
@ -426,7 +435,8 @@ mod tests {
headers.insert(header::TRANSFER_ENCODING, headers.insert(header::TRANSFER_ENCODING,
header::HeaderValue::from_static("chunked")); header::HeaderValue::from_static("chunked"));
let mut req = HttpRequest::new( let mut req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, Payload::empty());
assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::Chunked); assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::Chunked);
@ -436,7 +446,8 @@ mod tests {
headers.insert(header::CONTENT_LENGTH, headers.insert(header::CONTENT_LENGTH,
header::HeaderValue::from_static("xxxx")); header::HeaderValue::from_static("xxxx"));
let mut req = HttpRequest::new( let mut req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11,
headers, Payload::empty());
assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::UnknownLength); assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::UnknownLength);
@ -446,7 +457,8 @@ mod tests {
headers.insert(header::CONTENT_LENGTH, headers.insert(header::CONTENT_LENGTH,
header::HeaderValue::from_static("1000000")); header::HeaderValue::from_static("1000000"));
let mut req = HttpRequest::new( let mut req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, Payload::empty());
assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::Overflow); assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::Overflow);
@ -456,7 +468,8 @@ mod tests {
headers.insert(header::CONTENT_LENGTH, headers.insert(header::CONTENT_LENGTH,
header::HeaderValue::from_static("10")); header::HeaderValue::from_static("10"));
let mut req = HttpRequest::new( let mut req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, Payload::empty());
assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::ContentType); assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::ContentType);
} }

View File

@ -288,8 +288,9 @@ impl<'a> fmt::Display for FormatDisplay<'a> {
mod tests { mod tests {
use Body; use Body;
use super::*; use super::*;
use std::str::FromStr;
use time; use time;
use http::{Method, Version, StatusCode}; use http::{Method, Version, StatusCode, Uri};
use http::header::{self, HeaderMap}; use http::header::{self, HeaderMap};
use payload::Payload; use payload::Payload;
@ -300,7 +301,8 @@ mod tests {
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB")); headers.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB"));
let mut req = HttpRequest::new( let mut req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, Payload::empty());
let resp = HttpResponse::build(StatusCode::OK) let resp = HttpResponse::build(StatusCode::OK)
.header("X-Test", "ttt") .header("X-Test", "ttt")
.force_close().body(Body::Empty).unwrap(); .force_close().body(Body::Empty).unwrap();
@ -331,7 +333,8 @@ mod tests {
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB")); headers.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB"));
let req = HttpRequest::new( let req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, Payload::empty());
let resp = HttpResponse::build(StatusCode::OK) let resp = HttpResponse::build(StatusCode::OK)
.force_close().body(Body::Empty).unwrap(); .force_close().body(Body::Empty).unwrap();
let entry_time = time::now(); let entry_time = time::now();
@ -348,8 +351,8 @@ mod tests {
assert!(s.contains("ACTIX-WEB")); assert!(s.contains("ACTIX-WEB"));
let req = HttpRequest::new( let req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, HeaderMap::new(), Method::GET, Uri::from_str("/?test").unwrap(),
"test".to_owned(), Payload::empty()); Version::HTTP_11, HeaderMap::new(), Payload::empty());
let resp = HttpResponse::build(StatusCode::OK) let resp = HttpResponse::build(StatusCode::OK)
.force_close().body(Body::Empty).unwrap(); .force_close().body(Body::Empty).unwrap();
let entry_time = time::now(); let entry_time = time::now();

View File

@ -131,7 +131,7 @@ fn parse(pattern: &str) -> String {
} }
if hard_stop { if hard_stop {
panic!("{id:*} section has to be last lection of pattern"); panic!("Tail '*' section has to be last lection of pattern");
} }
if in_param { if in_param {

View File

@ -335,33 +335,32 @@ impl WsWriter {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::str::FromStr;
use payload::Payload; use payload::Payload;
use http::{Method, HeaderMap, Version, header}; use http::{Method, HeaderMap, Version, Uri, header};
#[test] #[test]
fn test_handshake() { fn test_handshake() {
let req = HttpRequest::new(Method::POST, "/".to_owned(), let req = HttpRequest::new(Method::POST, Uri::from_str("/").unwrap(),
Version::HTTP_11, HeaderMap::new(), Version::HTTP_11, HeaderMap::new(), Payload::empty());
String::new(), Payload::empty());
assert_eq!(WsHandshakeError::GetMethodRequired, handshake(&req).err().unwrap()); assert_eq!(WsHandshakeError::GetMethodRequired, handshake(&req).err().unwrap());
let req = HttpRequest::new(Method::GET, "/".to_owned(), let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, HeaderMap::new(), Version::HTTP_11, HeaderMap::new(), Payload::empty());
String::new(), Payload::empty());
assert_eq!(WsHandshakeError::NoWebsocketUpgrade, handshake(&req).err().unwrap()); assert_eq!(WsHandshakeError::NoWebsocketUpgrade, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert(header::UPGRADE, headers.insert(header::UPGRADE,
header::HeaderValue::from_static("test")); header::HeaderValue::from_static("test"));
let req = HttpRequest::new(Method::GET, "/".to_owned(), let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, String::new(), Payload::empty()); Version::HTTP_11, headers, Payload::empty());
assert_eq!(WsHandshakeError::NoWebsocketUpgrade, handshake(&req).err().unwrap()); assert_eq!(WsHandshakeError::NoWebsocketUpgrade, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert(header::UPGRADE, headers.insert(header::UPGRADE,
header::HeaderValue::from_static("websocket")); header::HeaderValue::from_static("websocket"));
let req = HttpRequest::new(Method::GET, "/".to_owned(), let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, String::new(), Payload::empty()); Version::HTTP_11, headers, Payload::empty());
assert_eq!(WsHandshakeError::NoConnectionUpgrade, handshake(&req).err().unwrap()); assert_eq!(WsHandshakeError::NoConnectionUpgrade, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
@ -369,8 +368,8 @@ mod tests {
header::HeaderValue::from_static("websocket")); header::HeaderValue::from_static("websocket"));
headers.insert(header::CONNECTION, headers.insert(header::CONNECTION,
header::HeaderValue::from_static("upgrade")); header::HeaderValue::from_static("upgrade"));
let req = HttpRequest::new(Method::GET, "/".to_owned(), let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, String::new(), Payload::empty()); Version::HTTP_11, headers, Payload::empty());
assert_eq!(WsHandshakeError::NoVersionHeader, handshake(&req).err().unwrap()); assert_eq!(WsHandshakeError::NoVersionHeader, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
@ -380,8 +379,8 @@ mod tests {
header::HeaderValue::from_static("upgrade")); header::HeaderValue::from_static("upgrade"));
headers.insert(SEC_WEBSOCKET_VERSION, headers.insert(SEC_WEBSOCKET_VERSION,
header::HeaderValue::from_static("5")); header::HeaderValue::from_static("5"));
let req = HttpRequest::new(Method::GET, "/".to_owned(), let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, String::new(), Payload::empty()); Version::HTTP_11, headers, Payload::empty());
assert_eq!(WsHandshakeError::UnsupportedVersion, handshake(&req).err().unwrap()); assert_eq!(WsHandshakeError::UnsupportedVersion, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
@ -391,8 +390,8 @@ mod tests {
header::HeaderValue::from_static("upgrade")); header::HeaderValue::from_static("upgrade"));
headers.insert(SEC_WEBSOCKET_VERSION, headers.insert(SEC_WEBSOCKET_VERSION,
header::HeaderValue::from_static("13")); header::HeaderValue::from_static("13"));
let req = HttpRequest::new(Method::GET, "/".to_owned(), let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, String::new(), Payload::empty()); Version::HTTP_11, headers, Payload::empty());
assert_eq!(WsHandshakeError::BadWebsocketKey, handshake(&req).err().unwrap()); assert_eq!(WsHandshakeError::BadWebsocketKey, handshake(&req).err().unwrap());
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
@ -404,8 +403,8 @@ mod tests {
header::HeaderValue::from_static("13")); header::HeaderValue::from_static("13"));
headers.insert(SEC_WEBSOCKET_KEY, headers.insert(SEC_WEBSOCKET_KEY,
header::HeaderValue::from_static("13")); header::HeaderValue::from_static("13"));
let req = HttpRequest::new(Method::GET, "/".to_owned(), let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, String::new(), Payload::empty()); Version::HTTP_11, headers, Payload::empty());
assert_eq!(StatusCode::SWITCHING_PROTOCOLS, handshake(&req).unwrap().status()); assert_eq!(StatusCode::SWITCHING_PROTOCOLS, handshake(&req).unwrap().status());
} }
} }

View File

@ -3,16 +3,25 @@ extern crate http;
extern crate time; extern crate time;
use std::str; use std::str;
use std::str::FromStr;
use actix_web::*; use actix_web::*;
use actix_web::dev::*; use actix_web::dev::*;
use http::{header, Method, Version, HeaderMap}; use http::{header, Method, Version, HeaderMap, Uri};
#[test]
fn test_debug() {
let req = HttpRequest::new(
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11,
HeaderMap::new(), Payload::empty());
let _ = format!("{:?}", req);
}
#[test] #[test]
fn test_no_request_cookies() { fn test_no_request_cookies() {
let mut req = HttpRequest::new( let mut req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, HeaderMap::new(), Method::GET, Uri::from_str("/").unwrap(),
String::new(), Payload::empty()); Version::HTTP_11, HeaderMap::new(), Payload::empty());
assert!(req.cookies().is_empty()); assert!(req.cookies().is_empty());
let _ = req.load_cookies(); let _ = req.load_cookies();
assert!(req.cookies().is_empty()); assert!(req.cookies().is_empty());
@ -25,7 +34,8 @@ fn test_request_cookies() {
header::HeaderValue::from_static("cookie1=value1; cookie2=value2")); header::HeaderValue::from_static("cookie1=value1; cookie2=value2"));
let mut req = HttpRequest::new( let mut req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, Payload::empty());
assert!(req.cookies().is_empty()); assert!(req.cookies().is_empty());
{ {
let cookies = req.load_cookies().unwrap(); let cookies = req.load_cookies().unwrap();
@ -48,9 +58,8 @@ fn test_request_cookies() {
#[test] #[test]
fn test_no_request_range_header() { fn test_no_request_range_header() {
let req = HttpRequest::new(Method::GET, "/".to_owned(), let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, HeaderMap::new(), Version::HTTP_11, HeaderMap::new(), Payload::empty());
String::new(), Payload::empty());
let ranges = req.range(100).unwrap(); let ranges = req.range(100).unwrap();
assert!(ranges.is_empty()); assert!(ranges.is_empty());
} }
@ -61,8 +70,8 @@ fn test_request_range_header() {
headers.insert(header::RANGE, headers.insert(header::RANGE,
header::HeaderValue::from_static("bytes=0-4")); header::HeaderValue::from_static("bytes=0-4"));
let req = HttpRequest::new(Method::GET, "/".to_owned(), let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, String::new(), Payload::empty()); Version::HTTP_11, headers, Payload::empty());
let ranges = req.range(100).unwrap(); let ranges = req.range(100).unwrap();
assert_eq!(ranges.len(), 1); assert_eq!(ranges.len(), 1);
assert_eq!(ranges[0].start, 0); assert_eq!(ranges[0].start, 0);
@ -71,10 +80,8 @@ fn test_request_range_header() {
#[test] #[test]
fn test_request_query() { fn test_request_query() {
let req = HttpRequest::new(Method::GET, "/".to_owned(), let req = HttpRequest::new(Method::GET, Uri::from_str("/?id=test").unwrap(),
Version::HTTP_11, HeaderMap::new(), Version::HTTP_11, HeaderMap::new(), Payload::empty());
"id=test".to_owned(), Payload::empty());
assert_eq!(req.query_string(), "id=test"); assert_eq!(req.query_string(), "id=test");
let query = req.query(); let query = req.query();
assert_eq!(&query["id"], "test"); assert_eq!(&query["id"], "test");
@ -82,9 +89,8 @@ fn test_request_query() {
#[test] #[test]
fn test_request_match_info() { fn test_request_match_info() {
let mut req = HttpRequest::new(Method::GET, "/value/".to_owned(), let mut req = HttpRequest::new(Method::GET, Uri::from_str("/value/?id=test").unwrap(),
Version::HTTP_11, HeaderMap::new(), Version::HTTP_11, HeaderMap::new(), Payload::empty());
"?id=test".to_owned(), Payload::empty());
let rec = RouteRecognizer::new("/".to_owned(), vec![("/{key}/".to_owned(), 1)]); let rec = RouteRecognizer::new("/".to_owned(), vec![("/{key}/".to_owned(), 1)]);
let (params, _) = rec.recognize(req.path()).unwrap(); let (params, _) = rec.recognize(req.path()).unwrap();
@ -97,15 +103,16 @@ fn test_request_match_info() {
#[test] #[test]
fn test_chunked() { fn test_chunked() {
let req = HttpRequest::new( let req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, HeaderMap::new(), Method::GET, Uri::from_str("/").unwrap(),
String::new(), Payload::empty()); Version::HTTP_11, HeaderMap::new(), Payload::empty());
assert!(!req.chunked().unwrap()); assert!(!req.chunked().unwrap());
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert(header::TRANSFER_ENCODING, headers.insert(header::TRANSFER_ENCODING,
header::HeaderValue::from_static("chunked")); header::HeaderValue::from_static("chunked"));
let req = HttpRequest::new( let req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11,
headers, Payload::empty());
assert!(req.chunked().unwrap()); assert!(req.chunked().unwrap());
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
@ -114,6 +121,7 @@ fn test_chunked() {
headers.insert(header::TRANSFER_ENCODING, headers.insert(header::TRANSFER_ENCODING,
header::HeaderValue::from_str(s).unwrap()); header::HeaderValue::from_str(s).unwrap());
let req = HttpRequest::new( let req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, Payload::empty());
assert!(req.chunked().is_err()); assert!(req.chunked().is_err());
} }

View File

@ -3,8 +3,9 @@ extern crate http;
extern crate time; extern crate time;
use actix_web::*; use actix_web::*;
use std::str::FromStr;
use time::Duration; use time::Duration;
use http::{header, Method, Version, HeaderMap}; use http::{header, Method, Version, HeaderMap, Uri};
#[test] #[test]
@ -14,7 +15,7 @@ fn test_response_cookies() {
header::HeaderValue::from_static("cookie1=value1; cookie2=value2")); header::HeaderValue::from_static("cookie1=value1; cookie2=value2"));
let mut req = HttpRequest::new( let mut req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, Payload::empty());
let cookies = req.load_cookies().unwrap(); let cookies = req.load_cookies().unwrap();
let resp = httpcodes::HTTPOk let resp = httpcodes::HTTPOk