mirror of
https://github.com/aykhans/dodo.git
synced 2025-09-01 00:53:34 +00:00
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package types
|
|
|
|
import "strings"
|
|
|
|
type Header KeyValue[string, []string]
|
|
|
|
type Headers []Header
|
|
|
|
// Has checks if a header with the given key exists.
|
|
func (headers Headers) Has(key string) bool {
|
|
for i := range headers {
|
|
if headers[i].Key == key {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (headers Headers) GetValue(key string) *[]string {
|
|
for i := range headers {
|
|
if headers[i].Key == key {
|
|
return &headers[i].Value
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (headers *Headers) Append(header Header) {
|
|
if item := headers.GetValue(header.Key); item != nil {
|
|
*item = append(*item, header.Value...)
|
|
} else {
|
|
*headers = append(*headers, header)
|
|
}
|
|
}
|
|
|
|
func (headers *Headers) Parse(rawValues ...string) {
|
|
for _, rawValue := range rawValues {
|
|
headers.Append(*ParseHeader(rawValue))
|
|
}
|
|
}
|
|
|
|
func ParseHeader(rawValue string) *Header {
|
|
parts := strings.SplitN(rawValue, ": ", 2)
|
|
switch len(parts) {
|
|
case 1:
|
|
return &Header{Key: parts[0], Value: []string{""}}
|
|
case 2:
|
|
return &Header{Key: parts[0], Value: []string{parts[1]}}
|
|
default:
|
|
return &Header{Key: "", Value: []string{""}}
|
|
}
|
|
}
|