mirror of
https://github.com/aykhans/dodo.git
synced 2025-09-05 10:43:37 +00:00
Here we go again...
This commit is contained in:
52
pkg/types/header.go
Normal file
52
pkg/types/header.go
Normal file
@@ -0,0 +1,52 @@
|
||||
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{""}}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user