-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaderrules.go
More file actions
64 lines (55 loc) · 1.45 KB
/
headerrules.go
File metadata and controls
64 lines (55 loc) · 1.45 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
package zeaburcaddyextension
import (
"bufio"
"fmt"
"strings"
)
// HeaderConfig stores the headers for a specific path
type HeaderConfig struct {
Path string
Headers map[string]string
}
// ParseHeaderConfig parses the header configuration from a given string
func ParseHeaderConfig(config string) ([]HeaderConfig, error) {
var configs []HeaderConfig
scanner := bufio.NewScanner(strings.NewReader(config))
var currentPath string
headers := make(map[string]string)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Skip empty lines and comments
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Detect path line
if strings.HasPrefix(line, "/") || strings.HasPrefix(line, "http") {
// If we have collected headers for the previous path, save them
if currentPath != "" {
configs = append(configs, HeaderConfig{
Path: currentPath,
Headers: headers,
})
headers = make(map[string]string)
}
currentPath = line
} else {
// Parse header line
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid header line: %s", line)
}
headers[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
}
// Add the last collected headers
if currentPath != "" {
configs = append(configs, HeaderConfig{
Path: currentPath,
Headers: headers,
})
}
if err := scanner.Err(); err != nil {
return nil, err
}
return configs, nil
}