You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.4 KiB
63 lines
1.4 KiB
package nwweb
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"net/http"
|
|
)
|
|
|
|
type headers struct {
|
|
httpObj any
|
|
}
|
|
|
|
func newHeaders(httpObj any) headers {
|
|
return headers{httpObj: httpObj}
|
|
}
|
|
|
|
func (h headers) Get(key string) string {
|
|
switch h.httpObj.(type) {
|
|
case *http.Request:
|
|
return h.httpObj.(*http.Request).Header.Get(key)
|
|
case *http.Response:
|
|
return h.httpObj.(*http.Response).Header.Get(key)
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
func (h headers) ContentLength() int {
|
|
return stringToInt(h.Get("Content-Length"))
|
|
}
|
|
|
|
func (h headers) Set(key string, value any) {
|
|
switch h.httpObj.(type) {
|
|
case *http.Request:
|
|
h.httpObj.(*http.Request).Header.Set(key, anyToString(value))
|
|
case http.ResponseWriter:
|
|
h.httpObj.(http.ResponseWriter).Header().Set(key, anyToString(value))
|
|
}
|
|
}
|
|
|
|
func (h headers) SetContentType(mimeType string) {
|
|
h.Set("Content-Type", mimeType)
|
|
}
|
|
|
|
func (h headers) SetContentLength(contentLength int) {
|
|
switch h.httpObj.(type) {
|
|
case *http.Request:
|
|
h.httpObj.(*http.Request).ContentLength = int64(contentLength)
|
|
//http.ResponseWriter automatically sets its content length
|
|
}
|
|
}
|
|
|
|
func (h headers) SetBasicAuth(username, password string) {
|
|
h.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
|
|
}
|
|
|
|
func (h headers) SetBearerAuth(token string) {
|
|
h.Set("Authorization", "Bearer "+token)
|
|
}
|
|
|
|
func (h headers) SetUserAgent(userAgent string) {
|
|
h.Set("User-Agent", userAgent)
|
|
}
|