Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
redirect.name/translate.go
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
65 lines (53 sloc)
1.22 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"regexp" | |
"strings" | |
) | |
type Redirect struct { | |
Location string | |
Status int | |
} | |
func Translate(uri string, config *Config) *Redirect { | |
if uri == "" { | |
return nil | |
} | |
if config == nil { | |
return nil | |
} | |
if config.To == "" { | |
return nil | |
} | |
redirect := &Redirect{Location: config.To} | |
switch config.RedirectState { | |
case "301", "permanently": | |
redirect.Status = 301 | |
case "302", "temporarily": | |
redirect.Status = 302 | |
case "307": | |
redirect.Status = 307 | |
case "308": | |
redirect.Status = 308 | |
default: | |
redirect.Status = 302 | |
} | |
// no `From` assumes catch-all, so redirect immediately to `Location` | |
if config.From == "" { | |
return redirect | |
} | |
count := strings.Count(config.From, `*`) | |
var exp bytes.Buffer | |
exp.WriteString(`^`) | |
exp.WriteString(strings.Replace(regexp.QuoteMeta(config.From), `\*`, `(.*)`, 1)) | |
exp.WriteString(`$`) | |
fromRE := regexp.MustCompile(exp.String()) | |
// if we can't find the pattern, return to continue to next record | |
if !fromRE.MatchString(uri) { | |
return nil | |
} | |
// wildcard replacement of `uri` if there's a wildcard in our `From` path | |
if count > 0 { | |
redirect.Location = fromRE.ReplaceAllString(uri, strings.Replace(redirect.Location, `*`, `${1}`, 1)) | |
} | |
return redirect | |
} |