prosa_hyper/
lib.rs

1//! ProSA Hyper processor to handle HTTP client and server
2
3#![warn(missing_docs)]
4
5use std::{convert::Infallible, io};
6
7use bytes::Bytes;
8use http_body_util::combinators::BoxBody;
9use hyper::{Response, Version};
10use prosa::core::error::ProcError;
11use thiserror::Error;
12
13const H2: &[u8] = b"h2";
14
15/// Global Hyper processor error
16#[derive(Debug, Error)]
17pub enum HyperProcError {
18    /// IO Error
19    #[error("Hyper IO error: {0}")]
20    Io(#[from] io::Error),
21    /// Hyper Error
22    #[error("Hyper error: {0}")]
23    Hyper(#[from] hyper::Error),
24    /// Other Error
25    #[error("Other error: {0}")]
26    Other(String),
27}
28
29impl ProcError for HyperProcError {
30    fn recoverable(&self) -> bool {
31        match self {
32            HyperProcError::Io(error) => error.recoverable(),
33            HyperProcError::Hyper(_) => true,
34            HyperProcError::Other(_) => true,
35        }
36    }
37}
38
39/// Method to get a string version of the Hyper Version object
40fn hyper_version_str(version: Version) -> &'static str {
41    match version {
42        hyper::Version::HTTP_11 => "HTTP/1.1",
43        hyper::Version::HTTP_2 => "HTTP/2",
44        hyper::Version::HTTP_3 => "HTTP/3",
45        _ => "Unknown",
46    }
47}
48
49/// Enum to define all type of response to request it can be made
50#[derive(Debug)]
51pub enum HyperResp<M> {
52    /// Make an internal service request to have a response
53    SrvReq(String, M),
54    /// Make a direct HTTP response
55    HttpResp(Response<BoxBody<Bytes, Infallible>>),
56    /// Response with an HTTP error
57    HttpErr(hyper::Error),
58}
59
60pub mod server;