-
Notifications
You must be signed in to change notification settings - Fork 35
/
parse.go
41 lines (34 loc) · 1.02 KB
/
parse.go
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
package main
import "regexp"
type Config struct {
From string
To string
RedirectState string
}
var configRE = regexp.MustCompile(`Redirects?(\s+.*)`)
var fromRE = regexp.MustCompile(`\s+from\s+(/\S*)`)
var toRE = regexp.MustCompile(`\s+to\s+((?:http\://|https\://|ftp\://|mailto\:|magnet\:)\S+|/\S*)`)
var stateRE = regexp.MustCompile(`\s+(permanently|temporarily)|\s+with\s+(301|302|307|308)`)
func Parse(record string) *Config {
configMatches := configRE.FindStringSubmatch(record)
if len(configMatches) == 0 {
return nil
}
fromMatches := fromRE.FindStringSubmatch(configMatches[1])
toMatches := toRE.FindStringSubmatch(configMatches[1])
stateMatches := stateRE.FindStringSubmatch(configMatches[1])
config := new(Config)
if len(fromMatches) > 0 {
config.From = fromMatches[1]
}
if len(toMatches) > 0 {
config.To = toMatches[1]
}
if len(stateMatches) > 0 {
config.RedirectState = stateMatches[1]
if config.RedirectState == "" {
config.RedirectState = stateMatches[2]
}
}
return config
}