-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathheaders.go
More file actions
67 lines (55 loc) · 2.09 KB
/
Copy pathheaders.go
File metadata and controls
67 lines (55 loc) · 2.09 KB
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
66
67
package main
import (
"os"
"regexp"
"strings"
)
// parseAdditionalHeaders reads the environment for values like SRC_HEADER_NAME=VALUE or
// SRC_HEADERS and creates a `{NAME: VALUE}` map. These headers should be applied to each
// request to the Sourcegraph instance, as some private instances require special auth or
// additional proxy values to be passed along with each request.
func parseAdditionalHeaders() map[string]string {
return parseAdditionalHeadersFromEnviron(os.Environ())
}
const additionalHeaderPrefix = "SRC_HEADER_"
const additionalHeadersKey = "SRC_HEADERS"
func parseAdditionalHeadersFromEnviron(environ []string) map[string]string {
additionalHeaders := map[string]string{}
for _, value := range environ {
parts := strings.SplitN(value, "=", 2)
if len(parts) != 2 {
continue
}
if parts[0] == additionalHeadersKey && parts[1] != "" {
// This regex removes all leading and trailing spaces from the environment variable. We need to do this
// because most shells add quotes to a string when it contains a new line. Tested with `bash, fish & zsh`
// and they all have the same behavior.
re := regexp.MustCompile(`^"|"$`)
headers := re.ReplaceAllString(parts[1], "")
// Most shell convert line breaks added to a string, so we need to replace all occurence of the stringified line
// breaks to actual line breaks.
headers = strings.ReplaceAll(headers, `\n`, "\n")
splitHeaders := strings.SplitSeq(headers, "\n")
for h := range splitHeaders {
p := strings.SplitN(h, ":", 2)
if len(p) != 2 || p[1] == "" {
continue
}
key := strings.Trim(strings.ToLower(p[0]), " ")
value := strings.Trim(p[1], " ")
additionalHeaders[key] = value
}
continue
}
// Ensure we'll have a non-empty key after trimming
if !strings.HasPrefix(parts[0], additionalHeaderPrefix) || len(parts[0]) <= len(additionalHeaderPrefix) {
continue
}
// Ensure we have a non-empty value
if parts[1] == "" {
continue
}
additionalHeaders[strings.ToLower(strings.TrimPrefix(parts[0], additionalHeaderPrefix))] = parts[1]
}
return additionalHeaders
}