Skip to main content

parse_optional

Function parse_optional 

Source
pub fn parse_optional<'a>(
    s: &'a [u8],
    limits: &Limits,
) -> Result<(OptionalParams<'a>, usize), ParseError>
Expand description

Parses parameters whose value may be left out, at the start of s: *( OWS ";" OWS [ token [ "=" ( token / quoted-string ) ] ] ), the extension-param of RFC 6455 section 9.1. permessage-deflate (RFC 7692) sends client_max_window_bits and server_no_context_takeover this way.

Everything else is as parse: the same names, values, empty parameters, bytes used, count limits, and duplicate policy, and no whitespace around =. A name followed by OWS and then anything but = is a parameter without a value; what follows it is the caller’s, as after any parameter. Wherever parse succeeds, this succeeds with the same parameters. RFC 6455 also asks that a quoted value unescape to a token; that check is the caller’s.

§Errors

As parse, except that a name with no = is not an error. ErrorKind::MissingValue remains for = with no value, and for a quoted string straight after a name.

§Examples

use parse_net::{param, ErrorKind, Limits};

let offer = b"; client_max_window_bits; server_max_window_bits=10, x-webkit";
let (p, used) = param::parse_optional(offer, &Limits::DEFAULT)?;
assert_eq!((p.len(), used), (2, 51));
assert!(p.find("client_max_window_bits").is_some_and(|x| x.value.is_none()));
assert!(p.find("Server_Max_Window_Bits").is_some_and(|x| x.value.is_some_and(|v| v.eq_bytes(b"10"))));
assert!(!p.contains("server_no_context_takeover"));

let dup = param::parse_optional(b";a;A=1", &Limits::DEFAULT).unwrap_err();
assert_eq!((dup.kind(), dup.offset()), (ErrorKind::DuplicateParameter, 3));
assert_eq!(param::parse_optional(b";a=", &Limits::DEFAULT).unwrap_err().kind(), ErrorKind::MissingValue);