// Copyright (c) 2022 Nadeen Udantha . 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)) }