mirror of
https://github.com/TwiN/gatus.git
synced 2026-02-04 12:56:48 +00:00
fix: handle HTTP header names case-insensitively (#1506)
Details: HTTP header names should be case-insensitive per RFC 7230. When the users configure the headers like 'user-agent' (lowercase), gatus was adding its own 'User-Agent' header because the case-sensitive map lookup failed to find the existing header. Fixed headers: - User-Agent - Content-Type - Host Fixes #1491" Co-authored-by: TwiN <twin@linux.com>
This commit is contained in:
@@ -221,12 +221,12 @@ func (e *Endpoint) ValidateAndSetDefaults() error {
|
||||
e.Headers = make(map[string]string)
|
||||
}
|
||||
// Automatically add user agent header if there isn't one specified in the endpoint configuration
|
||||
if _, userAgentHeaderExists := e.Headers[UserAgentHeader]; !userAgentHeaderExists {
|
||||
if !hasHeader(e.Headers, UserAgentHeader) {
|
||||
e.Headers[UserAgentHeader] = GatusUserAgent
|
||||
}
|
||||
// Automatically add "Content-Type: application/json" header if there's no Content-Type set
|
||||
// and endpoint.GraphQL is set to true
|
||||
if _, contentTypeHeaderExists := e.Headers[ContentTypeHeader]; !contentTypeHeaderExists && e.GraphQL {
|
||||
if !hasHeader(e.Headers, ContentTypeHeader) && e.GraphQL {
|
||||
e.Headers[ContentTypeHeader] = "application/json"
|
||||
}
|
||||
if len(e.Conditions) == 0 {
|
||||
@@ -493,8 +493,8 @@ func (e *Endpoint) call(result *Result) {
|
||||
wsHeaders[k] = v
|
||||
}
|
||||
}
|
||||
if _, exists := wsHeaders["User-Agent"]; !exists {
|
||||
wsHeaders["User-Agent"] = GatusUserAgent
|
||||
if !hasHeader(wsHeaders, UserAgentHeader) {
|
||||
wsHeaders[UserAgentHeader] = GatusUserAgent
|
||||
}
|
||||
result.Connected, result.Body, err = client.QueryWebSocket(e.URL, e.getParsedBody(), wsHeaders, e.ClientConfig)
|
||||
if err != nil {
|
||||
@@ -582,7 +582,7 @@ func (e *Endpoint) buildHTTPRequest() *http.Request {
|
||||
request, _ := http.NewRequest(e.Method, e.URL, bodyBuffer)
|
||||
for k, v := range e.Headers {
|
||||
request.Header.Set(k, v)
|
||||
if k == HostHeader {
|
||||
if strings.EqualFold(k, HostHeader) {
|
||||
request.Host = v
|
||||
}
|
||||
}
|
||||
@@ -626,3 +626,13 @@ func (e *Endpoint) needsToRetrieveIP() bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// hasHeader checks if a header exists in the map using a case-insensitive lookup
|
||||
func hasHeader(headers map[string]string, name string) bool {
|
||||
for k := range headers {
|
||||
if strings.EqualFold(k, name) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user