mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-31 10:37:24 -04:00 
			
		
		
		
	Proper Location redirects are disadvantageous in some situations. For example, you may want a developer to know that a resource is available via https, but you don't want an insecure call to the API to succeed.
		
			
				
	
	
		
			84 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package setup
 | |
| 
 | |
| import (
 | |
| 	"net/http"
 | |
| 
 | |
| 	"github.com/mholt/caddy/middleware"
 | |
| 	"github.com/mholt/caddy/middleware/redirect"
 | |
| )
 | |
| 
 | |
| // Redir configures a new Redirect middleware instance.
 | |
| func Redir(c *Controller) (middleware.Middleware, error) {
 | |
| 	rules, err := redirParse(c)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return func(next middleware.Handler) middleware.Handler {
 | |
| 		return redirect.Redirect{Next: next, Rules: rules}
 | |
| 	}, nil
 | |
| }
 | |
| 
 | |
| func redirParse(c *Controller) ([]redirect.Rule, error) {
 | |
| 	var redirects []redirect.Rule
 | |
| 
 | |
| 	for c.Next() {
 | |
| 		var rule redirect.Rule
 | |
| 		args := c.RemainingArgs()
 | |
| 
 | |
| 		// Always set the default Code, then overwrite
 | |
| 		rule.Code = http.StatusMovedPermanently
 | |
| 
 | |
| 		switch len(args) {
 | |
| 		case 1:
 | |
| 			// To specified
 | |
| 			rule.From = "/"
 | |
| 			rule.To = args[0]
 | |
| 		case 2:
 | |
| 			// To and Code specified
 | |
| 			rule.From = "/"
 | |
| 			rule.To = args[0]
 | |
| 			if "meta" == args[1] {
 | |
| 				rule.Meta = true
 | |
| 			} else if code, ok := httpRedirs[args[1]]; !ok {
 | |
| 				return redirects, c.Err("Invalid redirect code '" + args[1] + "'")
 | |
| 			} else {
 | |
| 				rule.Code = code
 | |
| 			}
 | |
| 		case 3:
 | |
| 			// From, To, and Code specified
 | |
| 			rule.From = args[0]
 | |
| 			rule.To = args[1]
 | |
| 			if "meta" == args[2] {
 | |
| 				rule.Meta = true
 | |
| 			} else if code, ok := httpRedirs[args[2]]; !ok {
 | |
| 				return redirects, c.Err("Invalid redirect code '" + args[2] + "'")
 | |
| 			} else {
 | |
| 				rule.Code = code
 | |
| 			}
 | |
| 		default:
 | |
| 			return redirects, c.ArgErr()
 | |
| 		}
 | |
| 
 | |
| 		if rule.From == rule.To {
 | |
| 			return redirects, c.Err("Redirect rule cannot allow From and To arguments to be the same.")
 | |
| 		}
 | |
| 
 | |
| 		redirects = append(redirects, rule)
 | |
| 	}
 | |
| 
 | |
| 	return redirects, nil
 | |
| }
 | |
| 
 | |
| // httpRedirs is a list of supported HTTP redirect codes.
 | |
| var httpRedirs = map[string]int{
 | |
| 	"300": 300,
 | |
| 	"301": 301,
 | |
| 	"302": 302,
 | |
| 	"303": 303,
 | |
| 	"304": 304,
 | |
| 	"305": 305,
 | |
| 	"307": 307,
 | |
| 	"308": 308,
 | |
| }
 |