File: headers.go

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (65 lines) | stat: -rw-r--r-- 1,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Package headers provides functionality related to HTTP headers
package headers

import (
	"net/http"
	"strconv"
)

// MaxDetectSize defines 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/-/issues/325074
// 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"
)

// ResponseHeaders contains a list of headers that are checked for presence
var ResponseHeaders = []string{
	XSendFileHeader,
	GitlabWorkhorseSendDataHeader,
	GitlabWorkhorseDetectContentTypeHeader,
}

// IsDetectContentTypeHeaderPresent checks if the detect content type header is present in the ResponseWriter
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)
	}
}