Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add additional information around errors for missing content type header #1576

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 12 additions & 9 deletions internal/api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,21 @@ func (a *API) runHTTPHook(r *http.Request, hookConfig conf.ExtensibilityPointCon
}

defer rsp.Body.Close()
// Header.Get is case insensitive
contentType := rsp.Header.Get("Content-Type")
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, internalServerError("Invalid Content-Type header")
}
if mediaType != "application/json" {
return nil, internalServerError("Invalid JSON response. Received content-type: " + contentType)
}

switch rsp.StatusCode {
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
// Header.Get is case insensitive
contentType := rsp.Header.Get("Content-Type")
if contentType == "" {
return nil, internalServerError("Invalid Content-Type: Missing Content-Type header")
}
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, internalServerError(fmt.Sprintf("Invalid Content-Type header: %s", err.Error()))
}
if mediaType != "application/json" {
return nil, internalServerError("Invalid JSON response. Received content-type: " + contentType)
}
if rsp.Body == nil {
return nil, nil
}
Expand Down