Web/header.go
2025-03-05 12:23:12 -06:00

64 lines
2.0 KiB
Go

package web
// Header represents an HTTP header with key and value
type Header struct {
Key string
Value string
}
// Common HTTP header keys
const (
HeaderContentType = "Content-Type"
HeaderContentLength = "Content-Length"
HeaderHost = "Host"
HeaderAccept = "Accept"
HeaderUserAgent = "User-Agent"
HeaderAcceptEncoding = "Accept-Encoding"
HeaderAcceptLanguage = "Accept-Language"
HeaderConnection = "Connection"
HeaderCookie = "Cookie"
HeaderSetCookie = "Set-Cookie"
HeaderLocation = "Location"
HeaderAuthorization = "Authorization"
HeaderCacheControl = "Cache-Control"
HeaderOrigin = "Origin"
HeaderReferer = "Referer"
HeaderTransferEncoding = "Transfer-Encoding"
)
// Pre-allocated common headers
var (
// Content type headers
HeaderContentTypeJSON = Header{Key: HeaderContentType, Value: "application/json"}
HeaderContentTypeHTML = Header{Key: HeaderContentType, Value: "text/html"}
HeaderContentTypePlain = Header{Key: HeaderContentType, Value: "text/plain"}
HeaderContentTypeXML = Header{Key: HeaderContentType, Value: "application/xml"}
HeaderContentTypeForm = Header{Key: HeaderContentType, Value: "application/x-www-form-urlencoded"}
HeaderContentTypeMultipart = Header{Key: HeaderContentType, Value: "multipart/form-data"}
// Connection headers
HeaderConnectionClose = Header{Key: HeaderConnection, Value: "close"}
HeaderConnectionKeepAlive = Header{Key: HeaderConnection, Value: "keep-alive"}
)
// FindHeader looks for a header by key in a slice of headers
func FindHeader(headers []Header, key string) (string, bool) {
for _, h := range headers {
if h.Key == key {
return h.Value, true
}
}
return "", false
}
// SetHeader sets a header value in a slice of headers
func SetHeader(headers *[]Header, key string, value string) {
for i, h := range *headers {
if h.Key == key {
(*headers)[i].Value = value
return
}
}
*headers = append(*headers, Header{Key: key, Value: value})
}