2017-10-07 06:48:14 +02:00
|
|
|
//! Error and Result module.
|
|
|
|
use std::error::Error as StdError;
|
|
|
|
use std::fmt;
|
|
|
|
use std::io::Error as IoError;
|
|
|
|
use std::str::Utf8Error;
|
|
|
|
use std::string::FromUtf8Error;
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
use cookie;
|
2017-10-07 06:48:14 +02:00
|
|
|
use httparse;
|
2017-10-13 23:43:17 +02:00
|
|
|
use http::{StatusCode, Error as HttpError};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-15 07:52:38 +02:00
|
|
|
use HttpRangeParseError;
|
2017-10-20 01:22:21 +02:00
|
|
|
use multipart::MultipartError;
|
2017-10-24 08:25:32 +02:00
|
|
|
use body::Body;
|
|
|
|
use httpresponse::{HttpResponse};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2017-10-14 01:33:23 +02:00
|
|
|
/// A set of errors that can occur during parsing HTTP streams.
|
2017-10-07 06:48:14 +02:00
|
|
|
#[derive(Debug)]
|
2017-10-13 23:43:17 +02:00
|
|
|
pub enum ParseError {
|
2017-10-07 06:48:14 +02:00
|
|
|
/// An invalid `Method`, such as `GE,T`.
|
|
|
|
Method,
|
|
|
|
/// An invalid `Uri`, such as `exam ple.domain`.
|
|
|
|
Uri,
|
|
|
|
/// An invalid `HttpVersion`, such as `HTP/1.1`
|
|
|
|
Version,
|
|
|
|
/// An invalid `Header`.
|
|
|
|
Header,
|
|
|
|
/// A message head is too large to be reasonable.
|
|
|
|
TooLarge,
|
|
|
|
/// A message reached EOF, but is not complete.
|
|
|
|
Incomplete,
|
|
|
|
/// An invalid `Status`, such as `1337 ELITE`.
|
|
|
|
Status,
|
|
|
|
/// A timeout occurred waiting for an IO event.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
Timeout,
|
|
|
|
/// An `io::Error` that occurred while trying to read or write to a network stream.
|
|
|
|
Io(IoError),
|
|
|
|
/// Parsing a field as string failed
|
|
|
|
Utf8(Utf8Error),
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
impl fmt::Display for ParseError {
|
2017-10-07 06:48:14 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2017-10-13 23:43:17 +02:00
|
|
|
ParseError::Io(ref e) => fmt::Display::fmt(e, f),
|
|
|
|
ParseError::Utf8(ref e) => fmt::Display::fmt(e, f),
|
2017-10-07 06:48:14 +02:00
|
|
|
ref e => f.write_str(e.description()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
impl StdError for ParseError {
|
2017-10-07 06:48:14 +02:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
2017-10-13 23:43:17 +02:00
|
|
|
ParseError::Method => "Invalid Method specified",
|
|
|
|
ParseError::Version => "Invalid HTTP version specified",
|
|
|
|
ParseError::Header => "Invalid Header provided",
|
|
|
|
ParseError::TooLarge => "Message head is too large",
|
|
|
|
ParseError::Status => "Invalid Status provided",
|
|
|
|
ParseError::Incomplete => "Message is incomplete",
|
|
|
|
ParseError::Timeout => "Timeout",
|
|
|
|
ParseError::Uri => "Uri error",
|
|
|
|
ParseError::Io(ref e) => e.description(),
|
|
|
|
ParseError::Utf8(ref e) => e.description(),
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&StdError> {
|
|
|
|
match *self {
|
2017-10-13 23:43:17 +02:00
|
|
|
ParseError::Io(ref error) => Some(error),
|
|
|
|
ParseError::Utf8(ref error) => Some(error),
|
2017-10-07 06:48:14 +02:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<IoError> for ParseError {
|
|
|
|
fn from(err: IoError) -> ParseError {
|
|
|
|
ParseError::Io(err)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<Utf8Error> for ParseError {
|
|
|
|
fn from(err: Utf8Error) -> ParseError {
|
|
|
|
ParseError::Utf8(err)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<FromUtf8Error> for ParseError {
|
|
|
|
fn from(err: FromUtf8Error) -> ParseError {
|
|
|
|
ParseError::Utf8(err.utf8_error())
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<httparse::Error> for ParseError {
|
|
|
|
fn from(err: httparse::Error) -> ParseError {
|
2017-10-07 06:48:14 +02:00
|
|
|
match err {
|
|
|
|
httparse::Error::HeaderName |
|
|
|
|
httparse::Error::HeaderValue |
|
|
|
|
httparse::Error::NewLine |
|
2017-10-13 23:43:17 +02:00
|
|
|
httparse::Error::Token => ParseError::Header,
|
|
|
|
httparse::Error::Status => ParseError::Status,
|
|
|
|
httparse::Error::TooManyHeaders => ParseError::TooLarge,
|
|
|
|
httparse::Error::Version => ParseError::Version,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 01:33:23 +02:00
|
|
|
/// Return `BadRequest` for `ParseError`
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<ParseError> for HttpResponse {
|
|
|
|
fn from(err: ParseError) -> Self {
|
2017-10-22 18:13:29 +02:00
|
|
|
HttpResponse::from_error(StatusCode::BAD_REQUEST, err)
|
2017-10-13 23:43:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 01:33:23 +02:00
|
|
|
/// Return `InternalServerError` for `HttpError`,
|
|
|
|
/// Response generation can return `HttpError`, so it is internal error
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<HttpError> for HttpResponse {
|
|
|
|
fn from(err: HttpError) -> Self {
|
2017-10-22 18:13:29 +02:00
|
|
|
HttpResponse::from_error(StatusCode::INTERNAL_SERVER_ERROR, err)
|
2017-10-13 23:43:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 05:39:59 +01:00
|
|
|
/// Return `InternalServerError` for `io::Error`
|
|
|
|
impl From<IoError> for HttpResponse {
|
|
|
|
fn from(err: IoError) -> Self {
|
|
|
|
HttpResponse::from_error(StatusCode::INTERNAL_SERVER_ERROR, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 01:33:23 +02:00
|
|
|
/// Return `BadRequest` for `cookie::ParseError`
|
2017-10-13 23:43:17 +02:00
|
|
|
impl From<cookie::ParseError> for HttpResponse {
|
|
|
|
fn from(err: cookie::ParseError) -> Self {
|
2017-10-22 18:13:29 +02:00
|
|
|
HttpResponse::from_error(StatusCode::BAD_REQUEST, err)
|
2017-10-13 23:43:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
/// Return `BadRequest` for `MultipartError`
|
|
|
|
impl From<MultipartError> for HttpResponse {
|
|
|
|
fn from(err: MultipartError) -> Self {
|
2017-10-22 18:13:29 +02:00
|
|
|
HttpResponse::from_error(StatusCode::BAD_REQUEST, err)
|
2017-10-20 01:22:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 07:52:38 +02:00
|
|
|
/// Return `BadRequest` for `HttpRangeParseError`
|
|
|
|
impl From<HttpRangeParseError> for HttpResponse {
|
|
|
|
fn from(_: HttpRangeParseError) -> Self {
|
2017-10-24 08:25:32 +02:00
|
|
|
HttpResponse::new(
|
|
|
|
StatusCode::BAD_REQUEST, Body::from("Invalid Range header provided"))
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::error::Error as StdError;
|
|
|
|
use std::io;
|
|
|
|
use httparse;
|
2017-10-22 18:45:53 +02:00
|
|
|
use http::{StatusCode, Error as HttpError};
|
2017-10-15 08:14:26 +02:00
|
|
|
use cookie::ParseError as CookieParseError;
|
2017-10-22 18:45:53 +02:00
|
|
|
use super::{ParseError, HttpResponse, HttpRangeParseError, MultipartError};
|
2017-10-15 08:14:26 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into_response() {
|
|
|
|
let resp: HttpResponse = ParseError::Incomplete.into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let resp: HttpResponse = HttpRangeParseError::InvalidRange.into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let resp: HttpResponse = CookieParseError::EmptyName.into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2017-10-22 18:45:53 +02:00
|
|
|
|
|
|
|
let resp: HttpResponse = MultipartError::Boundary.into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
let err: HttpError = StatusCode::from_u16(10000).err().unwrap().into();
|
|
|
|
let resp: HttpResponse = err.into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cause() {
|
|
|
|
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
|
|
|
let desc = orig.description().to_owned();
|
2017-10-14 01:33:23 +02:00
|
|
|
let e = ParseError::Io(orig);
|
2017-10-07 06:48:14 +02:00
|
|
|
assert_eq!(e.cause().unwrap().description(), desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! from {
|
|
|
|
($from:expr => $error:pat) => {
|
2017-10-14 01:33:23 +02:00
|
|
|
match ParseError::from($from) {
|
2017-10-07 06:48:14 +02:00
|
|
|
e @ $error => {
|
|
|
|
assert!(e.description().len() >= 5);
|
|
|
|
} ,
|
|
|
|
e => panic!("{:?}", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! from_and_cause {
|
|
|
|
($from:expr => $error:pat) => {
|
2017-10-14 01:33:23 +02:00
|
|
|
match ParseError::from($from) {
|
2017-10-07 06:48:14 +02:00
|
|
|
e @ $error => {
|
|
|
|
let desc = e.cause().unwrap().description();
|
|
|
|
assert_eq!(desc, $from.description().to_owned());
|
|
|
|
assert_eq!(desc, e.description());
|
|
|
|
},
|
|
|
|
_ => panic!("{:?}", $from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from() {
|
2017-10-14 01:33:23 +02:00
|
|
|
from_and_cause!(io::Error::new(io::ErrorKind::Other, "other") => ParseError::Io(..));
|
|
|
|
|
|
|
|
from!(httparse::Error::HeaderName => ParseError::Header);
|
|
|
|
from!(httparse::Error::HeaderName => ParseError::Header);
|
|
|
|
from!(httparse::Error::HeaderValue => ParseError::Header);
|
|
|
|
from!(httparse::Error::NewLine => ParseError::Header);
|
|
|
|
from!(httparse::Error::Status => ParseError::Status);
|
|
|
|
from!(httparse::Error::Token => ParseError::Header);
|
|
|
|
from!(httparse::Error::TooManyHeaders => ParseError::TooLarge);
|
|
|
|
from!(httparse::Error::Version => ParseError::Version);
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|