diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
commit | 8b573c94895dc0ac0e1d9d59cf3e8745e8b539ca (patch) | |
tree | 544930fb309b30317ae9797a9683768705d664c4 /workhorse/internal/headers/headers.go | |
parent | 4b1de649d0168371549608993deac953eb692019 (diff) |
Add latest changes from gitlab-org/gitlab@13-7-stable-eev13.7.0-rc42
Diffstat (limited to 'workhorse/internal/headers/headers.go')
-rw-r--r-- | workhorse/internal/headers/headers.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/workhorse/internal/headers/headers.go b/workhorse/internal/headers/headers.go new file mode 100644 index 00000000000..63b39a6aa41 --- /dev/null +++ b/workhorse/internal/headers/headers.go @@ -0,0 +1,62 @@ +package headers + +import ( + "net/http" + "strconv" +) + +// Max number of bytes that http.DetectContentType needs to get the content type +// Fixme: Go back to 512 bytes once https://gitlab.com/gitlab-org/gitlab-workhorse/issues/208 +// has been merged +const MaxDetectSize = 4096 + +// HTTP Headers +const ( + ContentDispositionHeader = "Content-Disposition" + ContentTypeHeader = "Content-Type" + + // Workhorse related headers + GitlabWorkhorseSendDataHeader = "Gitlab-Workhorse-Send-Data" + XSendFileHeader = "X-Sendfile" + XSendFileTypeHeader = "X-Sendfile-Type" + + // Signal header that indicates Workhorse should detect and set the content headers + GitlabWorkhorseDetectContentTypeHeader = "Gitlab-Workhorse-Detect-Content-Type" +) + +var ResponseHeaders = []string{ + XSendFileHeader, + GitlabWorkhorseSendDataHeader, + GitlabWorkhorseDetectContentTypeHeader, +} + +func IsDetectContentTypeHeaderPresent(rw http.ResponseWriter) bool { + header, err := strconv.ParseBool(rw.Header().Get(GitlabWorkhorseDetectContentTypeHeader)) + if err != nil || !header { + return false + } + + return true +} + +// AnyResponseHeaderPresent checks in the ResponseWriter if there is any Response Header +func AnyResponseHeaderPresent(rw http.ResponseWriter) bool { + // If this header is not present means that we want the old behavior + if !IsDetectContentTypeHeaderPresent(rw) { + return false + } + + for _, header := range ResponseHeaders { + if rw.Header().Get(header) != "" { + return true + } + } + return false +} + +// RemoveResponseHeaders removes any ResponseHeader from the ResponseWriter +func RemoveResponseHeaders(rw http.ResponseWriter) { + for _, header := range ResponseHeaders { + rw.Header().Del(header) + } +} |