Skip to content

Commit

Permalink
Add helpers for token validation
Browse files Browse the repository at this point in the history
  • Loading branch information
apasel422 committed Feb 12, 2025
1 parent 1324bde commit f3717f4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl<'a> Parser<'a> {
// https://httpwg.org/specs/rfc8941.html#parse-token

if let Some(first_char) = self.input.peek() {
if !first_char.is_ascii_alphabetic() && first_char != &b'*' {
if !utils::is_token_start(*first_char) {
return Err("parse_token: first character is not ALPHA or '*'");
}
} else {
Expand All @@ -299,7 +299,7 @@ impl<'a> Parser<'a> {

let mut output_string = String::from("");
while let Some(curr_char) = self.input.peek() {
if !utils::is_tchar(*curr_char) && curr_char != &b':' && curr_char != &b'/' {
if !utils::is_token_continue(*curr_char) {
return Ok(output_string);
}

Expand Down
13 changes: 6 additions & 7 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,18 @@ impl Serializer {
return Err("serialize_token: non-ascii character");
}

match value.chars().next() {
let mut bytes = value.bytes();

match bytes.next() {
None => return Err("serialize_token: token is empty"),
Some(char) => {
if !(char.is_ascii_alphabetic() || char == '*') {
Some(c) => {
if !utils::is_token_start(c) {
return Err("serialize_token: first character is not ALPHA or '*'");
}
}
}

if value
.bytes()
.any(|c| !(utils::is_tchar(c) || c == b':' || c == b'/'))
{
if bytes.any(|c| !utils::is_token_continue(c)) {
return Err("serialize_token: disallowed character");
}

Expand Down
10 changes: 9 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ pub(crate) const BASE64: engine::GeneralPurpose = engine::GeneralPurpose::new(
.with_encode_padding(true),
);

pub(crate) fn is_tchar(c: u8) -> bool {
fn is_tchar(c: u8) -> bool {
// See tchar values list in https://tools.ietf.org/html/rfc7230#section-3.2.6
let tchars = b"!#$%&'*+-.^_`|~";
tchars.contains(&c) || c.is_ascii_alphanumeric()
}

pub(crate) fn is_token_start(c: u8) -> bool {
c.is_ascii_alphabetic() || c == b'*'
}

pub(crate) fn is_token_continue(c: u8) -> bool {
is_tchar(c) || c == b':' || c == b'/'
}

0 comments on commit f3717f4

Please sign in to comment.