diff options
Diffstat (limited to '')
-rw-r--r-- | src/args.rs | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/src/args.rs b/src/args.rs index 53f1638..ecfe151 100644 --- a/src/args.rs +++ b/src/args.rs @@ -119,8 +119,8 @@ struct CliArgs { title: Option<String>, /// Custom header from user - #[structopt(long = "header")] - header: Option<String>, + #[structopt(long = "header", parse(try_from_str = parse_header))] + header: Option<Header>, } /// Checks wether an interface is valid, i.e. it can be parsed into an IP address @@ -174,6 +174,39 @@ fn parse_auth(src: &str) -> Result<auth::RequiredAuth, ContextualError> { }) } +/// A own header modified from [httparse](https://docs.rs/httparse/1.3.5/src/httparse/lib.rs.html#415-425) +#[derive(Clone, Eq, PartialEq, Debug)] +pub struct Header { + /// The name portion of a header. + /// + /// A header name must be valid ASCII-US, so it's safe to store as a `String`. + pub name: String, + /// The value portion of a header. + /// + /// While headers **should** be ASCII-US, the specification allows for + /// values that may not be, and so the value is stored as bytes. + pub value: Vec<u8>, +} + +impl Header { + fn new(header: &httparse::Header) -> Self { + Header { + name: header.name.to_string(), + value: header.value.to_vec(), + } + } +} + +fn parse_header(src: &str) -> Result<Header, httparse::Error> { + let mut headers = [httparse::EMPTY_HEADER; 1]; + let mut header = src.to_string(); + header.push('\n'); + httparse::parse_headers(header.as_bytes(), &mut headers)?; + + let header = Header::new(&headers[0]); + Ok(header) +} + /// Parses the command line arguments pub fn parse_args() -> crate::MiniserveConfig { let args = CliArgs::from_args(); |