boihttp/headers.go
Nadeen Udantha 4a3d59dea9 boi
2022-06-24 09:35:10 +05:30

66 lines
1.3 KiB
Go

// Copyright (c) 2022 Nadeen Udantha <me@nadeen.lk>. All rights reserved.
package boihttp
import (
"net/textproto"
"strconv"
"strings"
)
type Headers map[string][]string
func (h Headers) Add(key, value string) {
textproto.MIMEHeader(h).Add(key, value)
}
func (h Headers) Get(key string) string {
return textproto.MIMEHeader(h).Get(key)
}
func (h Headers) Has(key string) bool {
return len(textproto.MIMEHeader(h).Values(key)) > 0
}
func (h Headers) Set(key string, value string) {
textproto.MIMEHeader(h).Set(key, value)
}
func (h Headers) Del(key string) {
textproto.MIMEHeader(h).Del(key)
}
func (h Headers) Clone() Headers {
x := make(Headers)
for k, v := range h {
y := make([]string, len(v))
copy(y, v)
x[k] = y
}
return x
}
func (h Headers) SetKeepAlive(keepalive bool) {
if keepalive {
h.Set("Connection", "keep-alive")
} else {
h.Set("Connection", "close")
}
}
func (h Headers) KeepAlive() bool {
return strings.ToLower(h.Get("Connection")) == "keep-alive"
}
func (h Headers) ContentLength() int {
x, err := strconv.Atoi(h.Get("Content-Length"))
if err != nil {
return 0
}
return x
}
func (h Headers) SetContentLength(x int) {
h.Set("Content-Length", strconv.Itoa(x))
}