Files
sarin/internal/types/header.go
Aykhan Shahsuvarov d346067e8a Rename Append to Merge, replace strings_Join with slice_Join, and auto-detect non-TTY output
- Rename Append to Merge on Cookies, Headers, and Params types and simplify Parse to use direct append
- Replace strings_Join(sep, ...values) with slice_Join(slice, sep) for consistency with slice-based template functions
- Auto-enable quiet mode when stdout is not a terminal
- Remove ErrCLINoArgs check to allow running sarin without arguments
- Add -it flag to Docker examples in docs
2026-02-15 02:56:32 +04:00

50 lines
996 B
Go

package types
import "strings"
type Header KeyValue[string, []string]
type Headers []Header
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) Merge(header ...Header) {
for _, h := range header {
if item := headers.GetValue(h.Key); item != nil {
*item = append(*item, h.Value...)
} else {
*headers = append(*headers, h)
}
}
}
func (headers *Headers) Parse(rawValues ...string) {
for _, rawValue := range rawValues {
*headers = append(*headers, *ParseHeader(rawValue))
}
}
func ParseHeader(rawValue string) *Header {
parts := strings.SplitN(rawValue, ": ", 2)
if len(parts) == 1 {
return &Header{Key: parts[0], Value: []string{""}}
}
return &Header{Key: parts[0], Value: []string{parts[1]}}
}