net: use custom error for invalid headers (#10323)

pull/10341/head
Miccah 2021-06-03 01:50:07 -05:00 committed by GitHub
parent fedf07ddd8
commit c8d5e783a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -566,11 +566,24 @@ fn (mut h Header) add_key(key string) {
}
}
// Custom error struct for invalid header tokens
struct HeaderKeyError {
msg string
code int
header string
invalid_char byte
}
// Checks if the header token is valid
fn is_valid(header string) ? {
for _, c in header {
if int(c) >= 128 || !is_token(c) {
return error('Invalid header key')
return IError(HeaderKeyError{
msg: "Invalid header key: '$header'"
code: 1
header: header
invalid_char: c
})
}
}
}

View File

@ -291,7 +291,7 @@ pub fn parse_response(resp string) Response {
pos := h.index(':') or { continue }
mut key := h[..pos]
val := h[pos + 2..].trim_space()
header.add_custom(key, val) or { eprintln('error parsing header: $err') }
header.add_custom(key, val) or { eprintln('$err; skipping header') }
}
// set cookies
for cookie in header.values(.set_cookie) {