#![cfg_attr(not(unix), allow(dead_code))]
#![allow(clippy::result_large_err)]
#[cfg(unix)]
pub mod asks;
#[cfg(unix)]
pub mod audit;
pub mod body;
#[cfg(test)]
mod gh_claim;
#[cfg(unix)]
pub mod github;
pub mod graphql;
pub mod guard;
pub mod head;
#[cfg(unix)]
pub mod intercept;
pub mod lfs;
#[cfg(unix)]
pub mod orgs;
#[cfg(unix)]
pub mod policy;
pub mod presence;
pub mod redact;
pub mod refusal;
pub mod rewrite;
pub mod route;
#[cfg(unix)]
pub mod serve;
pub mod upstream;
use std::fmt;
use std::io;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HttpError {
HeadTooLarge,
TooManyHeaders,
Truncated,
BareLineEnding,
Malformed,
UnsupportedVersion,
BadTarget,
Connect,
LengthAndChunked,
BadContentLength,
BadTransferEncoding,
Upgrade,
Expectation,
BadChunk,
Trailers,
Browser,
Host,
}
impl HttpError {
pub fn status(&self) -> u16 {
match self {
HttpError::HeadTooLarge | HttpError::TooManyHeaders => 431,
HttpError::UnsupportedVersion => 505,
HttpError::Expectation => 417,
HttpError::Browser | HttpError::Host => 403,
_ => 400,
}
}
}
impl fmt::Display for HttpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
HttpError::HeadTooLarge => "request head too large",
HttpError::TooManyHeaders => "too many header fields",
HttpError::Truncated => "connection closed inside a request head",
HttpError::BareLineEnding => "line ending other than CRLF",
HttpError::Malformed => "malformed request head",
HttpError::UnsupportedVersion => "HTTP version other than 1.1 or 1.0",
HttpError::BadTarget => "request target in neither origin-form nor absolute-form",
HttpError::Connect => "CONNECT is not brokered",
HttpError::LengthAndChunked => "both Content-Length and Transfer-Encoding",
HttpError::BadContentLength => "invalid or conflicting Content-Length",
HttpError::BadTransferEncoding => "Transfer-Encoding other than chunked",
HttpError::Upgrade => "protocol upgrades are not brokered",
HttpError::Expectation => "unsupported Expect",
HttpError::BadChunk => "malformed chunk",
HttpError::Trailers => "chunked trailers are not accepted",
HttpError::Browser => "browser requests (Origin, Sec-Fetch-Site) are refused",
HttpError::Host => "Host is not this listener's authority",
})
}
}
impl std::error::Error for HttpError {}
impl From<HttpError> for io::Error {
fn from(e: HttpError) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, e)
}
}
pub fn refusal(e: &io::Error) -> Option<&HttpError> {
e.get_ref()?.downcast_ref()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn refusal_round_trips_through_io_error() {
let e: io::Error = HttpError::LengthAndChunked.into();
assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(refusal(&e), Some(&HttpError::LengthAndChunked));
assert_eq!(refusal(&io::Error::from(io::ErrorKind::UnexpectedEof)), None);
}
#[test]
fn statuses() {
assert_eq!(HttpError::BadChunk.status(), 400);
assert_eq!(HttpError::HeadTooLarge.status(), 431);
assert_eq!(HttpError::UnsupportedVersion.status(), 505);
assert_eq!(HttpError::Expectation.status(), 417);
assert_eq!(HttpError::Host.status(), 403);
}
}