mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-03 19:17:29 -05:00 
			
		
		
		
	httpcaddyfile: redir with "html" emits 200, no Location (fix #4940)
The intent of "html" is to redirect browser clients only, or those which can evaluate JS and/or meta tags. So return HTTP 200 and no Location header. See #4940.
This commit is contained in:
		
							parent
							
								
									085df25c7e
								
							
						
					
					
						commit
						91ab0e6066
					
				@ -540,8 +540,13 @@ func parseVars(h Helper) (caddyhttp.MiddlewareHandler, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// parseRedir parses the redir directive. Syntax:
 | 
					// parseRedir parses the redir directive. Syntax:
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
//     redir [<matcher>] <to> [<code>]
 | 
					//	redir [<matcher>] <to> [<code>]
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
 | 
					// <code> can be "permanent" for 301, "temporary" for 302 (default),
 | 
				
			||||||
 | 
					// a placeholder, or any number in the 3xx range or 401. The special
 | 
				
			||||||
 | 
					// code "html" can be used to redirect only browser clients (will
 | 
				
			||||||
 | 
					// respond with HTTP 200 and no Location header; redirect is performed
 | 
				
			||||||
 | 
					// with JS and a meta tag).
 | 
				
			||||||
func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
 | 
					func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
 | 
				
			||||||
	if !h.Next() {
 | 
						if !h.Next() {
 | 
				
			||||||
		return nil, h.ArgErr()
 | 
							return nil, h.ArgErr()
 | 
				
			||||||
@ -558,6 +563,7 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var body string
 | 
						var body string
 | 
				
			||||||
 | 
						var hdr http.Header
 | 
				
			||||||
	switch code {
 | 
						switch code {
 | 
				
			||||||
	case "permanent":
 | 
						case "permanent":
 | 
				
			||||||
		code = "301"
 | 
							code = "301"
 | 
				
			||||||
@ -578,7 +584,7 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
 | 
				
			|||||||
`
 | 
					`
 | 
				
			||||||
		safeTo := html.EscapeString(to)
 | 
							safeTo := html.EscapeString(to)
 | 
				
			||||||
		body = fmt.Sprintf(metaRedir, safeTo, safeTo, safeTo, safeTo)
 | 
							body = fmt.Sprintf(metaRedir, safeTo, safeTo, safeTo, safeTo)
 | 
				
			||||||
		code = "302"
 | 
							code = "200" // don't redirect non-browser clients
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		// Allow placeholders for the code
 | 
							// Allow placeholders for the code
 | 
				
			||||||
		if strings.HasPrefix(code, "{") {
 | 
							if strings.HasPrefix(code, "{") {
 | 
				
			||||||
@ -601,9 +607,14 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// don't redirect non-browser clients
 | 
				
			||||||
 | 
						if code != "200" {
 | 
				
			||||||
 | 
							hdr = http.Header{"Location": []string{to}}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return caddyhttp.StaticResponse{
 | 
						return caddyhttp.StaticResponse{
 | 
				
			||||||
		StatusCode: caddyhttp.WeakString(code),
 | 
							StatusCode: caddyhttp.WeakString(code),
 | 
				
			||||||
		Headers:    http.Header{"Location": []string{to}},
 | 
							Headers:    hdr,
 | 
				
			||||||
		Body:       body,
 | 
							Body:       body,
 | 
				
			||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user