2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2021-02-28 00:54:27 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module http
|
|
|
|
|
2021-04-08 03:12:46 +03:00
|
|
|
import strings
|
|
|
|
|
2021-09-29 19:26:43 +03:00
|
|
|
// Header represents the key-value pairs in an HTTP header
|
|
|
|
[noinit]
|
|
|
|
pub struct Header {
|
|
|
|
mut:
|
|
|
|
data map[string][]string
|
|
|
|
// map of lowercase header keys to their original keys
|
|
|
|
// in order of appearance
|
|
|
|
keys map[string][]string
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:54:27 +03:00
|
|
|
// CommonHeader is an enum of the most common HTTP headers
|
|
|
|
pub enum CommonHeader {
|
|
|
|
accept
|
|
|
|
accept_ch
|
|
|
|
accept_charset
|
|
|
|
accept_ch_lifetime
|
|
|
|
accept_encoding
|
|
|
|
accept_language
|
|
|
|
accept_patch
|
|
|
|
accept_post
|
|
|
|
accept_ranges
|
|
|
|
access_control_allow_credentials
|
|
|
|
access_control_allow_headers
|
|
|
|
access_control_allow_methods
|
|
|
|
access_control_allow_origin
|
|
|
|
access_control_expose_headers
|
|
|
|
access_control_max_age
|
|
|
|
access_control_request_headers
|
|
|
|
access_control_request_method
|
|
|
|
age
|
|
|
|
allow
|
|
|
|
alt_svc
|
|
|
|
authorization
|
2021-10-17 06:40:12 +03:00
|
|
|
authority
|
2021-02-28 00:54:27 +03:00
|
|
|
cache_control
|
|
|
|
clear_site_data
|
|
|
|
connection
|
|
|
|
content_disposition
|
|
|
|
content_encoding
|
|
|
|
content_language
|
|
|
|
content_length
|
|
|
|
content_location
|
|
|
|
content_range
|
|
|
|
content_security_policy
|
|
|
|
content_security_policy_report_only
|
|
|
|
content_type
|
|
|
|
cookie
|
|
|
|
cross_origin_embedder_policy
|
|
|
|
cross_origin_opener_policy
|
|
|
|
cross_origin_resource_policy
|
|
|
|
date
|
|
|
|
device_memory
|
|
|
|
digest
|
|
|
|
dnt
|
|
|
|
early_data
|
|
|
|
etag
|
|
|
|
expect
|
|
|
|
expect_ct
|
|
|
|
expires
|
|
|
|
feature_policy
|
|
|
|
forwarded
|
|
|
|
from
|
|
|
|
host
|
|
|
|
if_match
|
|
|
|
if_modified_since
|
|
|
|
if_none_match
|
|
|
|
if_range
|
|
|
|
if_unmodified_since
|
|
|
|
index
|
|
|
|
keep_alive
|
|
|
|
large_allocation
|
|
|
|
last_modified
|
|
|
|
link
|
|
|
|
location
|
|
|
|
nel
|
|
|
|
origin
|
|
|
|
pragma
|
|
|
|
proxy_authenticate
|
|
|
|
proxy_authorization
|
|
|
|
range
|
|
|
|
referer
|
|
|
|
referrer_policy
|
|
|
|
retry_after
|
|
|
|
save_data
|
|
|
|
sec_fetch_dest
|
|
|
|
sec_fetch_mode
|
|
|
|
sec_fetch_site
|
|
|
|
sec_fetch_user
|
|
|
|
sec_websocket_accept
|
|
|
|
server
|
|
|
|
server_timing
|
|
|
|
set_cookie
|
|
|
|
sourcemap
|
|
|
|
strict_transport_security
|
|
|
|
te
|
|
|
|
timing_allow_origin
|
|
|
|
tk
|
|
|
|
trailer
|
|
|
|
transfer_encoding
|
|
|
|
upgrade
|
|
|
|
upgrade_insecure_requests
|
|
|
|
user_agent
|
|
|
|
vary
|
|
|
|
via
|
|
|
|
want_digest
|
|
|
|
warning
|
|
|
|
www_authenticate
|
|
|
|
x_content_type_options
|
|
|
|
x_dns_prefetch_control
|
|
|
|
x_forwarded_for
|
|
|
|
x_forwarded_host
|
|
|
|
x_forwarded_proto
|
|
|
|
x_frame_options
|
|
|
|
x_xss_protection
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (h CommonHeader) str() string {
|
|
|
|
return match h {
|
|
|
|
.accept { 'Accept' }
|
|
|
|
.accept_ch { 'Accept-CH' }
|
|
|
|
.accept_charset { 'Accept-Charset' }
|
|
|
|
.accept_ch_lifetime { 'Accept-CH-Lifetime' }
|
|
|
|
.accept_encoding { 'Accept-Encoding' }
|
|
|
|
.accept_language { 'Accept-Language' }
|
|
|
|
.accept_patch { 'Accept-Patch' }
|
|
|
|
.accept_post { 'Accept-Post' }
|
|
|
|
.accept_ranges { 'Accept-Ranges' }
|
|
|
|
.access_control_allow_credentials { 'Access-Control-Allow-Credentials' }
|
|
|
|
.access_control_allow_headers { 'Access-Control-Allow-Headers' }
|
|
|
|
.access_control_allow_methods { 'Access-Control-Allow-Methods' }
|
|
|
|
.access_control_allow_origin { 'Access-Control-Allow-Origin' }
|
|
|
|
.access_control_expose_headers { 'Access-Control-Expose-Headers' }
|
|
|
|
.access_control_max_age { 'Access-Control-Max-Age' }
|
|
|
|
.access_control_request_headers { 'Access-Control-Request-Headers' }
|
|
|
|
.access_control_request_method { 'Access-Control-Request-Method' }
|
|
|
|
.age { 'Age' }
|
|
|
|
.allow { 'Allow' }
|
|
|
|
.alt_svc { 'Alt-Svc' }
|
|
|
|
.authorization { 'Authorization' }
|
2021-10-17 06:40:12 +03:00
|
|
|
.authority { 'Authority' }
|
2021-02-28 00:54:27 +03:00
|
|
|
.cache_control { 'Cache-Control' }
|
|
|
|
.clear_site_data { 'Clear-Site-Data' }
|
|
|
|
.connection { 'Connection' }
|
|
|
|
.content_disposition { 'Content-Disposition' }
|
|
|
|
.content_encoding { 'Content-Encoding' }
|
|
|
|
.content_language { 'Content-Language' }
|
|
|
|
.content_length { 'Content-Length' }
|
|
|
|
.content_location { 'Content-Location' }
|
|
|
|
.content_range { 'Content-Range' }
|
|
|
|
.content_security_policy { 'Content-Security-Policy' }
|
|
|
|
.content_security_policy_report_only { 'Content-Security-Policy-Report-Only' }
|
|
|
|
.content_type { 'Content-Type' }
|
|
|
|
.cookie { 'Cookie' }
|
|
|
|
.cross_origin_embedder_policy { 'Cross-Origin-Embedder-Policy' }
|
|
|
|
.cross_origin_opener_policy { 'Cross-Origin-Opener-Policy' }
|
|
|
|
.cross_origin_resource_policy { 'Cross-Origin-Resource-Policy' }
|
|
|
|
.date { 'Date' }
|
|
|
|
.device_memory { 'Device-Memory' }
|
|
|
|
.digest { 'Digest' }
|
|
|
|
.dnt { 'DNT' }
|
|
|
|
.early_data { 'Early-Data' }
|
|
|
|
.etag { 'ETag' }
|
|
|
|
.expect { 'Expect' }
|
|
|
|
.expect_ct { 'Expect-CT' }
|
|
|
|
.expires { 'Expires' }
|
|
|
|
.feature_policy { 'Feature-Policy' }
|
|
|
|
.forwarded { 'Forwarded' }
|
|
|
|
.from { 'From' }
|
|
|
|
.host { 'Host' }
|
|
|
|
.if_match { 'If-Match' }
|
|
|
|
.if_modified_since { 'If-Modified-Since' }
|
|
|
|
.if_none_match { 'If-None-Match' }
|
|
|
|
.if_range { 'If-Range' }
|
|
|
|
.if_unmodified_since { 'If-Unmodified-Since' }
|
|
|
|
.index { 'Index' }
|
|
|
|
.keep_alive { 'Keep-Alive' }
|
|
|
|
.large_allocation { 'Large-Allocation' }
|
|
|
|
.last_modified { 'Last-Modified' }
|
|
|
|
.link { 'Link' }
|
|
|
|
.location { 'Location' }
|
|
|
|
.nel { 'NEL' }
|
|
|
|
.origin { 'Origin' }
|
|
|
|
.pragma { 'Pragma' }
|
|
|
|
.proxy_authenticate { 'Proxy-Authenticate' }
|
|
|
|
.proxy_authorization { 'Proxy-Authorization' }
|
|
|
|
.range { 'Range' }
|
|
|
|
.referer { 'Referer' }
|
|
|
|
.referrer_policy { 'Referrer-Policy' }
|
|
|
|
.retry_after { 'Retry-After' }
|
|
|
|
.save_data { 'Save-Data' }
|
|
|
|
.sec_fetch_dest { 'Sec-Fetch-Dest' }
|
|
|
|
.sec_fetch_mode { 'Sec-Fetch-Mode' }
|
|
|
|
.sec_fetch_site { 'Sec-Fetch-Site' }
|
|
|
|
.sec_fetch_user { 'Sec-Fetch-User' }
|
|
|
|
.sec_websocket_accept { 'Sec-WebSocket-Accept' }
|
|
|
|
.server { 'Server' }
|
|
|
|
.server_timing { 'Server-Timing' }
|
|
|
|
.set_cookie { 'Set-Cookie' }
|
|
|
|
.sourcemap { 'SourceMap' }
|
|
|
|
.strict_transport_security { 'Strict-Transport-Security' }
|
|
|
|
.te { 'TE' }
|
|
|
|
.timing_allow_origin { 'Timing-Allow-Origin' }
|
|
|
|
.tk { 'Tk' }
|
|
|
|
.trailer { 'Trailer' }
|
|
|
|
.transfer_encoding { 'Transfer-Encoding' }
|
|
|
|
.upgrade { 'Upgrade' }
|
|
|
|
.upgrade_insecure_requests { 'Upgrade-Insecure-Requests' }
|
|
|
|
.user_agent { 'User-Agent' }
|
|
|
|
.vary { 'Vary' }
|
|
|
|
.via { 'Via' }
|
|
|
|
.want_digest { 'Want-Digest' }
|
|
|
|
.warning { 'Warning' }
|
|
|
|
.www_authenticate { 'WWW-Authenticate' }
|
|
|
|
.x_content_type_options { 'X-Content-Type-Options' }
|
|
|
|
.x_dns_prefetch_control { 'X-DNS-Prefetch-Control' }
|
|
|
|
.x_forwarded_for { 'X-Forwarded-For' }
|
|
|
|
.x_forwarded_host { 'X-Forwarded-Host' }
|
|
|
|
.x_forwarded_proto { 'X-Forwarded-Proto' }
|
|
|
|
.x_frame_options { 'X-Frame-Options' }
|
|
|
|
.x_xss_protection { 'X-XSS-Protection' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 12:44:41 +03:00
|
|
|
const common_header_map = {
|
2021-02-28 00:54:27 +03:00
|
|
|
'accept': CommonHeader.accept
|
|
|
|
'accept-ch': .accept_ch
|
|
|
|
'accept-charset': .accept_charset
|
|
|
|
'accept-ch-lifetime': .accept_ch_lifetime
|
|
|
|
'accept-encoding': .accept_encoding
|
|
|
|
'accept-language': .accept_language
|
|
|
|
'accept-patch': .accept_patch
|
|
|
|
'accept-post': .accept_post
|
|
|
|
'accept-ranges': .accept_ranges
|
|
|
|
'access-control-allow-credentials': .access_control_allow_credentials
|
|
|
|
'access-control-allow-headers': .access_control_allow_headers
|
|
|
|
'access-control-allow-methods': .access_control_allow_methods
|
|
|
|
'access-control-allow-origin': .access_control_allow_origin
|
|
|
|
'access-control-expose-headers': .access_control_expose_headers
|
|
|
|
'access-control-max-age': .access_control_max_age
|
|
|
|
'access-control-request-headers': .access_control_request_headers
|
|
|
|
'access-control-request-method': .access_control_request_method
|
|
|
|
'age': .age
|
|
|
|
'allow': .allow
|
|
|
|
'alt-svc': .alt_svc
|
|
|
|
'authorization': .authorization
|
|
|
|
'cache-control': .cache_control
|
|
|
|
'clear-site-data': .clear_site_data
|
|
|
|
'connection': .connection
|
|
|
|
'content-disposition': .content_disposition
|
|
|
|
'content-encoding': .content_encoding
|
|
|
|
'content-language': .content_language
|
|
|
|
'content-length': .content_length
|
|
|
|
'content-location': .content_location
|
|
|
|
'content-range': .content_range
|
|
|
|
'content-security-policy': .content_security_policy
|
|
|
|
'content-security-policy-report-only': .content_security_policy_report_only
|
|
|
|
'content-type': .content_type
|
|
|
|
'cookie': .cookie
|
|
|
|
'cross-origin-embedder-policy': .cross_origin_embedder_policy
|
|
|
|
'cross-origin-opener-policy': .cross_origin_opener_policy
|
|
|
|
'cross-origin-resource-policy': .cross_origin_resource_policy
|
|
|
|
'date': .date
|
|
|
|
'device-memory': .device_memory
|
|
|
|
'digest': .digest
|
|
|
|
'dnt': .dnt
|
|
|
|
'early-data': .early_data
|
|
|
|
'etag': .etag
|
|
|
|
'expect': .expect
|
|
|
|
'expect-ct': .expect_ct
|
|
|
|
'expires': .expires
|
|
|
|
'feature-policy': .feature_policy
|
|
|
|
'forwarded': .forwarded
|
|
|
|
'from': .from
|
|
|
|
'host': .host
|
|
|
|
'if-match': .if_match
|
|
|
|
'if-modified-since': .if_modified_since
|
|
|
|
'if-none-match': .if_none_match
|
|
|
|
'if-range': .if_range
|
|
|
|
'if-unmodified-since': .if_unmodified_since
|
|
|
|
'index': .index
|
|
|
|
'keep-alive': .keep_alive
|
|
|
|
'large-allocation': .large_allocation
|
|
|
|
'last-modified': .last_modified
|
|
|
|
'link': .link
|
|
|
|
'location': .location
|
|
|
|
'nel': .nel
|
|
|
|
'origin': .origin
|
|
|
|
'pragma': .pragma
|
|
|
|
'proxy-authenticate': .proxy_authenticate
|
|
|
|
'proxy-authorization': .proxy_authorization
|
|
|
|
'range': .range
|
|
|
|
'referer': .referer
|
|
|
|
'referrer-policy': .referrer_policy
|
|
|
|
'retry-after': .retry_after
|
|
|
|
'save-data': .save_data
|
|
|
|
'sec-fetch-dest': .sec_fetch_dest
|
|
|
|
'sec-fetch-mode': .sec_fetch_mode
|
|
|
|
'sec-fetch-site': .sec_fetch_site
|
|
|
|
'sec-fetch-user': .sec_fetch_user
|
|
|
|
'sec-websocket-accept': .sec_websocket_accept
|
|
|
|
'server': .server
|
|
|
|
'server-timing': .server_timing
|
|
|
|
'set-cookie': .set_cookie
|
|
|
|
'sourcemap': .sourcemap
|
|
|
|
'strict-transport-security': .strict_transport_security
|
|
|
|
'te': .te
|
|
|
|
'timing-allow-origin': .timing_allow_origin
|
|
|
|
'tk': .tk
|
|
|
|
'trailer': .trailer
|
|
|
|
'transfer-encoding': .transfer_encoding
|
|
|
|
'upgrade': .upgrade
|
|
|
|
'upgrade-insecure-requests': .upgrade_insecure_requests
|
|
|
|
'user-agent': .user_agent
|
|
|
|
'vary': .vary
|
|
|
|
'via': .via
|
|
|
|
'want-digest': .want_digest
|
|
|
|
'warning': .warning
|
|
|
|
'www-authenticate': .www_authenticate
|
|
|
|
'x-content-type-options': .x_content_type_options
|
|
|
|
'x-dns-prefetch-control': .x_dns_prefetch_control
|
|
|
|
'x-forwarded-for': .x_forwarded_for
|
|
|
|
'x-forwarded-host': .x_forwarded_host
|
|
|
|
'x-forwarded-proto': .x_forwarded_proto
|
|
|
|
'x-frame-options': .x_frame_options
|
|
|
|
'x-xss-protection': .x_xss_protection
|
|
|
|
}
|
|
|
|
|
2021-03-17 03:33:31 +03:00
|
|
|
pub fn (mut h Header) free() {
|
|
|
|
unsafe {
|
|
|
|
h.data.free()
|
2021-04-08 03:12:46 +03:00
|
|
|
h.keys.free()
|
2021-03-17 03:33:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:54:27 +03:00
|
|
|
pub struct HeaderConfig {
|
|
|
|
key CommonHeader
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new Header object
|
|
|
|
pub fn new_header(kvs ...HeaderConfig) Header {
|
|
|
|
mut h := Header{
|
|
|
|
data: map[string][]string{}
|
|
|
|
}
|
|
|
|
for kv in kvs {
|
|
|
|
h.add(kv.key, kv.value)
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// new_header_from_map creates a Header from key value pairs
|
|
|
|
pub fn new_header_from_map(kvs map[CommonHeader]string) Header {
|
|
|
|
mut h := new_header()
|
|
|
|
h.add_map(kvs)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// new_custom_header_from_map creates a Header from string key value pairs
|
|
|
|
pub fn new_custom_header_from_map(kvs map[string]string) ?Header {
|
|
|
|
mut h := new_header()
|
2022-05-13 06:56:21 +03:00
|
|
|
h.add_custom_map(kvs)?
|
2021-07-12 12:16:41 +03:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// add appends a value to the header key.
|
2021-02-28 00:54:27 +03:00
|
|
|
pub fn (mut h Header) add(key CommonHeader, value string) {
|
2021-04-08 03:12:46 +03:00
|
|
|
k := key.str()
|
|
|
|
h.data[k] << value
|
|
|
|
h.add_key(k)
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// add_custom appends a value to a custom header key. This function will
|
|
|
|
// return an error if the key contains invalid header characters.
|
2021-04-08 03:12:46 +03:00
|
|
|
pub fn (mut h Header) add_custom(key string, value string) ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
is_valid(key)?
|
2021-04-08 03:12:46 +03:00
|
|
|
h.data[key] << value
|
|
|
|
h.add_key(key)
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// add_map appends the value for each header key.
|
|
|
|
pub fn (mut h Header) add_map(kvs map[CommonHeader]string) {
|
|
|
|
for k, v in kvs {
|
|
|
|
h.add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add_custom_map appends the value for each custom header key.
|
|
|
|
pub fn (mut h Header) add_custom_map(kvs map[string]string) ? {
|
|
|
|
for k, v in kvs {
|
2022-05-13 06:56:21 +03:00
|
|
|
h.add_custom(k, v)?
|
2021-07-12 12:16:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set sets the key-value pair. This function will clear any other values
|
2021-02-28 00:54:27 +03:00
|
|
|
// that exist for the CommonHeader.
|
|
|
|
pub fn (mut h Header) set(key CommonHeader, value string) {
|
2021-04-08 03:12:46 +03:00
|
|
|
k := key.str()
|
|
|
|
h.data[k] = [value]
|
|
|
|
h.add_key(k)
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// set_custom sets the key-value pair for a custom header key. This
|
|
|
|
// function will clear any other values that exist for the header. This
|
|
|
|
// function will return an error if the key contains invalid header
|
|
|
|
// characters.
|
2021-04-08 03:12:46 +03:00
|
|
|
pub fn (mut h Header) set_custom(key string, value string) ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
is_valid(key)?
|
2021-04-08 03:12:46 +03:00
|
|
|
h.data[key] = [value]
|
|
|
|
h.add_key(key)
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// delete deletes all values for a key.
|
2021-02-28 00:54:27 +03:00
|
|
|
pub fn (mut h Header) delete(key CommonHeader) {
|
2021-04-08 03:12:46 +03:00
|
|
|
h.delete_custom(key.str())
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// delete_custom deletes all values for a custom header key.
|
2021-04-08 03:12:46 +03:00
|
|
|
pub fn (mut h Header) delete_custom(key string) {
|
|
|
|
h.data.delete(key)
|
|
|
|
|
|
|
|
// remove key from keys metadata
|
|
|
|
kl := key.to_lower()
|
|
|
|
if kl in h.keys {
|
|
|
|
h.keys[kl] = h.keys[kl].filter(it != key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 10:45:27 +03:00
|
|
|
[params]
|
2021-04-08 03:12:46 +03:00
|
|
|
pub struct HeaderCoerceConfig {
|
|
|
|
canonicalize bool
|
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// coerce coerces data in the Header by joining keys that match
|
|
|
|
// case-insensitively into one entry.
|
2021-10-26 10:45:27 +03:00
|
|
|
pub fn (mut h Header) coerce(flags HeaderCoerceConfig) {
|
2021-04-08 03:12:46 +03:00
|
|
|
for kl, data_keys in h.keys {
|
2021-10-26 10:45:27 +03:00
|
|
|
master_key := if flags.canonicalize { canonicalize(kl) } else { data_keys[0] }
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
// save master data
|
|
|
|
master_data := h.data[master_key]
|
|
|
|
h.data.delete(master_key)
|
|
|
|
|
|
|
|
for key in data_keys {
|
|
|
|
if key == master_key {
|
|
|
|
h.data[master_key] << master_data
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
h.data[master_key] << h.data[key]
|
|
|
|
h.data.delete(key)
|
|
|
|
}
|
|
|
|
h.keys[kl] = [master_key]
|
|
|
|
}
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// contains returns whether the header key exists in the map.
|
2021-02-28 00:54:27 +03:00
|
|
|
pub fn (h Header) contains(key CommonHeader) bool {
|
2021-04-08 03:12:46 +03:00
|
|
|
return h.contains_custom(key.str())
|
|
|
|
}
|
|
|
|
|
2021-10-26 10:45:27 +03:00
|
|
|
[params]
|
2021-04-08 03:12:46 +03:00
|
|
|
pub struct HeaderQueryConfig {
|
|
|
|
exact bool
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// contains_custom returns whether the custom header key exists in the map.
|
2021-10-26 10:45:27 +03:00
|
|
|
pub fn (h Header) contains_custom(key string, flags HeaderQueryConfig) bool {
|
|
|
|
if flags.exact {
|
2021-04-08 03:12:46 +03:00
|
|
|
return key in h.data
|
|
|
|
}
|
|
|
|
return key.to_lower() in h.keys
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// get gets the first value for the CommonHeader, or none if the key
|
|
|
|
// does not exist.
|
2021-02-28 00:54:27 +03:00
|
|
|
pub fn (h Header) get(key CommonHeader) ?string {
|
2021-04-08 03:12:46 +03:00
|
|
|
return h.get_custom(key.str())
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// get_custom gets the first value for the custom header, or none if
|
|
|
|
// the key does not exist.
|
2021-10-26 10:45:27 +03:00
|
|
|
pub fn (h Header) get_custom(key string, flags HeaderQueryConfig) ?string {
|
2021-04-08 03:12:46 +03:00
|
|
|
mut data_key := key
|
2021-10-26 10:45:27 +03:00
|
|
|
if !flags.exact {
|
2021-04-08 03:12:46 +03:00
|
|
|
// get the first key from key metadata
|
|
|
|
k := key.to_lower()
|
|
|
|
if h.keys[k].len == 0 {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
data_key = h.keys[k][0]
|
|
|
|
}
|
|
|
|
if h.data[data_key].len == 0 {
|
2021-02-28 00:54:27 +03:00
|
|
|
return none
|
|
|
|
}
|
2021-04-08 03:12:46 +03:00
|
|
|
return h.data[data_key][0]
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// starting_with gets the first header starting with key, or none if
|
|
|
|
// the key does not exist.
|
2021-06-07 11:18:40 +03:00
|
|
|
pub fn (h Header) starting_with(key string) ?string {
|
|
|
|
for k, _ in h.data {
|
|
|
|
if k.starts_with(key) {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// values gets all values for the CommonHeader.
|
2021-02-28 00:54:27 +03:00
|
|
|
pub fn (h Header) values(key CommonHeader) []string {
|
2021-04-08 03:12:46 +03:00
|
|
|
return h.custom_values(key.str())
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// custom_values gets all values for the custom header.
|
2021-10-26 10:45:27 +03:00
|
|
|
pub fn (h Header) custom_values(key string, flags HeaderQueryConfig) []string {
|
|
|
|
if flags.exact {
|
2021-04-08 03:12:46 +03:00
|
|
|
return h.data[key]
|
|
|
|
}
|
|
|
|
// case insensitive lookup
|
|
|
|
mut values := []string{cap: 10}
|
|
|
|
for k in h.keys[key.to_lower()] {
|
|
|
|
values << h.data[k]
|
|
|
|
}
|
|
|
|
return values
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// keys gets all header keys as strings
|
2021-03-01 13:50:52 +03:00
|
|
|
pub fn (h Header) keys() []string {
|
|
|
|
return h.data.keys()
|
|
|
|
}
|
|
|
|
|
2021-09-14 00:05:30 +03:00
|
|
|
[params]
|
2021-04-08 03:12:46 +03:00
|
|
|
pub struct HeaderRenderConfig {
|
|
|
|
version Version
|
|
|
|
coerce bool
|
|
|
|
canonicalize bool
|
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// render renders the Header into a string for use in sending HTTP
|
|
|
|
// requests. All header lines will end in `\r\n`
|
2021-04-08 03:12:46 +03:00
|
|
|
[manualfree]
|
|
|
|
pub fn (h Header) render(flags HeaderRenderConfig) string {
|
|
|
|
// estimate ~48 bytes per header
|
|
|
|
mut sb := strings.new_builder(h.data.len * 48)
|
|
|
|
if flags.coerce {
|
|
|
|
for kl, data_keys in h.keys {
|
|
|
|
key := if flags.version == .v2_0 {
|
|
|
|
kl
|
|
|
|
} else if flags.canonicalize {
|
|
|
|
canonicalize(kl)
|
|
|
|
} else {
|
|
|
|
data_keys[0]
|
|
|
|
}
|
2021-08-23 00:38:09 +03:00
|
|
|
for k in data_keys {
|
2021-04-08 03:12:46 +03:00
|
|
|
for v in h.data[k] {
|
2021-08-23 00:38:09 +03:00
|
|
|
sb.write_string(key)
|
|
|
|
sb.write_string(': ')
|
2021-04-08 03:12:46 +03:00
|
|
|
sb.write_string(v)
|
2021-08-23 00:38:09 +03:00
|
|
|
sb.write_string('\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-23 00:38:09 +03:00
|
|
|
for k, vs in h.data {
|
2021-04-08 03:12:46 +03:00
|
|
|
key := if flags.version == .v2_0 {
|
|
|
|
k.to_lower()
|
|
|
|
} else if flags.canonicalize {
|
|
|
|
canonicalize(k.to_lower())
|
|
|
|
} else {
|
|
|
|
k
|
|
|
|
}
|
2021-08-23 00:38:09 +03:00
|
|
|
for v in vs {
|
|
|
|
sb.write_string(key)
|
|
|
|
sb.write_string(': ')
|
|
|
|
sb.write_string(v)
|
|
|
|
sb.write_string('\r\n')
|
|
|
|
}
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 03:12:46 +03:00
|
|
|
res := sb.str()
|
|
|
|
unsafe { sb.free() }
|
|
|
|
return res
|
|
|
|
}
|
2021-02-28 00:54:27 +03:00
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// join combines two Header structs into a new Header struct
|
|
|
|
pub fn (h Header) join(other Header) Header {
|
|
|
|
mut combined := Header{
|
|
|
|
data: h.data.clone()
|
|
|
|
keys: h.keys.clone()
|
|
|
|
}
|
|
|
|
for k in other.keys() {
|
|
|
|
for v in other.custom_values(k, exact: true) {
|
|
|
|
combined.add_custom(k, v) or {
|
|
|
|
// panic because this should never fail
|
|
|
|
panic('unexpected error: $err')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return combined
|
|
|
|
}
|
|
|
|
|
|
|
|
// canonicalize canonicalizes an HTTP header key
|
2021-04-08 03:12:46 +03:00
|
|
|
// Common headers are determined by the common_header_map
|
|
|
|
// Custom headers are capitalized on the first letter and any letter after a '-'
|
|
|
|
// NOTE: Assumes sl is lowercase, since the caller usually already has the lowercase key
|
|
|
|
fn canonicalize(sl string) string {
|
2021-02-28 00:54:27 +03:00
|
|
|
// check if we have a common header
|
|
|
|
if sl in http.common_header_map {
|
|
|
|
return http.common_header_map[sl].str()
|
|
|
|
}
|
2021-04-08 03:12:46 +03:00
|
|
|
return sl.split('-').map(it.capitalize()).join('-')
|
|
|
|
}
|
2021-02-28 00:54:27 +03:00
|
|
|
|
2021-04-08 03:12:46 +03:00
|
|
|
// Helper function to add a key to the keys map
|
|
|
|
fn (mut h Header) add_key(key string) {
|
|
|
|
kl := key.to_lower()
|
|
|
|
if !h.keys[kl].contains(key) {
|
|
|
|
h.keys[kl] << key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 09:50:07 +03:00
|
|
|
// Custom error struct for invalid header tokens
|
|
|
|
struct HeaderKeyError {
|
2022-02-11 16:52:33 +03:00
|
|
|
Error
|
2021-06-14 10:08:41 +03:00
|
|
|
code int
|
|
|
|
header string
|
2022-04-15 18:25:45 +03:00
|
|
|
invalid_char u8
|
2021-06-03 09:50:07 +03:00
|
|
|
}
|
|
|
|
|
2022-02-11 16:52:33 +03:00
|
|
|
pub fn (err HeaderKeyError) msg() string {
|
|
|
|
return "Invalid header key: '$err.header'"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (err HeaderKeyError) code() int {
|
|
|
|
return err.code
|
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// is_valid checks if the header token contains all valid bytes
|
2021-04-08 03:12:46 +03:00
|
|
|
fn is_valid(header string) ? {
|
|
|
|
for _, c in header {
|
|
|
|
if int(c) >= 128 || !is_token(c) {
|
2021-06-03 09:50:07 +03:00
|
|
|
return IError(HeaderKeyError{
|
|
|
|
code: 1
|
|
|
|
header: header
|
|
|
|
invalid_char: c
|
|
|
|
})
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
}
|
2021-07-24 11:31:33 +03:00
|
|
|
if header.len == 0 {
|
|
|
|
return IError(HeaderKeyError{
|
|
|
|
code: 2
|
|
|
|
header: header
|
|
|
|
invalid_char: 0
|
|
|
|
})
|
|
|
|
}
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// is_token checks if the byte is valid for a header token
|
2022-04-15 18:25:45 +03:00
|
|
|
fn is_token(b u8) bool {
|
2021-02-28 00:54:27 +03:00
|
|
|
return match b {
|
|
|
|
33, 35...39, 42, 43, 45, 46, 48...57, 65...90, 94...122, 124, 126 { true }
|
|
|
|
else { false }
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 03:12:46 +03:00
|
|
|
|
2021-07-12 12:16:41 +03:00
|
|
|
// str returns the headers string as seen in HTTP/1.1 requests.
|
|
|
|
// Key order is not guaranteed.
|
2021-04-08 03:12:46 +03:00
|
|
|
pub fn (h Header) str() string {
|
|
|
|
return h.render(version: .v1_1)
|
|
|
|
}
|
2021-07-24 11:31:33 +03:00
|
|
|
|
|
|
|
// parse_headers parses a newline delimited string into a Header struct
|
|
|
|
fn parse_headers(s string) ?Header {
|
|
|
|
mut h := new_header()
|
|
|
|
mut last_key := ''
|
|
|
|
mut last_value := ''
|
|
|
|
for line in s.split_into_lines() {
|
|
|
|
if line.len == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// handle header fold
|
|
|
|
if line[0] == ` ` || line[0] == `\t` {
|
|
|
|
last_value += ' ${line.trim(' \t')}'
|
|
|
|
continue
|
|
|
|
} else if last_key != '' {
|
2022-05-13 06:56:21 +03:00
|
|
|
h.add_custom(last_key, last_value)?
|
2021-07-24 11:31:33 +03:00
|
|
|
}
|
2022-05-13 06:56:21 +03:00
|
|
|
last_key, last_value = parse_header(line)?
|
2021-07-24 11:31:33 +03:00
|
|
|
}
|
2022-05-13 06:56:21 +03:00
|
|
|
h.add_custom(last_key, last_value)?
|
2021-07-24 11:31:33 +03:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_header(s string) ?(string, string) {
|
|
|
|
if !s.contains(':') {
|
|
|
|
return error('missing colon in header')
|
|
|
|
}
|
|
|
|
words := s.split_nth(':', 2)
|
|
|
|
// TODO: parse quoted text according to the RFC
|
|
|
|
return words[0], words[1].trim(' \t')
|
|
|
|
}
|