mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-31 10:37:24 -04:00 
			
		
		
		
	
		
			Some checks failed
		
		
	
	Tests / test (./cmd/caddy/caddy, ~1.22.3, macos-14, 0, 1.22, mac) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy, ~1.23.0, macos-14, 0, 1.23, mac) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy.exe, ~1.22.3, windows-latest, True, 1.22, windows) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy.exe, ~1.23.0, windows-latest, True, 1.23, windows) (push) Waiting to run
				
			Lint / lint (macos-14, mac) (push) Waiting to run
				
			Lint / lint (windows-latest, windows) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy, ~1.22.3, ubuntu-latest, 0, 1.22, linux) (push) Failing after 2m20s
				
			Tests / test (./cmd/caddy/caddy, ~1.23.0, ubuntu-latest, 0, 1.23, linux) (push) Failing after 2m31s
				
			Tests / test (s390x on IBM Z) (push) Has been skipped
				
			Tests / goreleaser-check (push) Has been skipped
				
			Cross-Build / build (~1.22.3, 1.22, aix) (push) Successful in 2m47s
				
			Cross-Build / build (~1.22.3, 1.22, darwin) (push) Successful in 1m28s
				
			Cross-Build / build (~1.22.3, 1.22, dragonfly) (push) Successful in 2m9s
				
			Cross-Build / build (~1.22.3, 1.22, freebsd) (push) Successful in 1m34s
				
			Cross-Build / build (~1.22.3, 1.22, illumos) (push) Successful in 1m28s
				
			Cross-Build / build (~1.22.3, 1.22, linux) (push) Successful in 1m32s
				
			Cross-Build / build (~1.22.3, 1.22, netbsd) (push) Successful in 1m25s
				
			Cross-Build / build (~1.22.3, 1.22, openbsd) (push) Successful in 1m23s
				
			Cross-Build / build (~1.22.3, 1.22, solaris) (push) Successful in 1m25s
				
			Cross-Build / build (~1.22.3, 1.22, windows) (push) Successful in 1m25s
				
			Cross-Build / build (~1.23.0, 1.23, aix) (push) Successful in 3m1s
				
			Cross-Build / build (~1.23.0, 1.23, darwin) (push) Successful in 1m45s
				
			Cross-Build / build (~1.23.0, 1.23, dragonfly) (push) Successful in 1m47s
				
			Cross-Build / build (~1.23.0, 1.23, freebsd) (push) Successful in 1m17s
				
			Cross-Build / build (~1.23.0, 1.23, illumos) (push) Successful in 1m19s
				
			Cross-Build / build (~1.23.0, 1.23, linux) (push) Successful in 1m44s
				
			Cross-Build / build (~1.23.0, 1.23, netbsd) (push) Successful in 1m16s
				
			Cross-Build / build (~1.23.0, 1.23, openbsd) (push) Successful in 1m17s
				
			Cross-Build / build (~1.23.0, 1.23, solaris) (push) Successful in 1m14s
				
			Cross-Build / build (~1.23.0, 1.23, windows) (push) Successful in 1m14s
				
			Lint / lint (ubuntu-latest, linux) (push) Successful in 1m57s
				
			Lint / govulncheck (push) Successful in 3m37s
				
			* caddyhttp: Add `{prefixed_query}` placeholder
* fastcgi: Preserve query during canonical redirect
* Use orig_uri instead for the redirect, shorter Caddyfile shortcut
		
	
			
		
			
				
	
	
		
			103 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			103 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package httpcaddyfile
 | |
| 
 | |
| import (
 | |
| 	"regexp"
 | |
| 	"strings"
 | |
| 
 | |
| 	"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
 | |
| )
 | |
| 
 | |
| type ComplexShorthandReplacer struct {
 | |
| 	search  *regexp.Regexp
 | |
| 	replace string
 | |
| }
 | |
| 
 | |
| type ShorthandReplacer struct {
 | |
| 	complex []ComplexShorthandReplacer
 | |
| 	simple  *strings.Replacer
 | |
| }
 | |
| 
 | |
| func NewShorthandReplacer() ShorthandReplacer {
 | |
| 	// replace shorthand placeholders (which are convenient
 | |
| 	// when writing a Caddyfile) with their actual placeholder
 | |
| 	// identifiers or variable names
 | |
| 	replacer := strings.NewReplacer(placeholderShorthands()...)
 | |
| 
 | |
| 	// these are placeholders that allow a user-defined final
 | |
| 	// parameters, but we still want to provide a shorthand
 | |
| 	// for those, so we use a regexp to replace
 | |
| 	regexpReplacements := []ComplexShorthandReplacer{
 | |
| 		{regexp.MustCompile(`{header\.([\w-]*)}`), "{http.request.header.$1}"},
 | |
| 		{regexp.MustCompile(`{cookie\.([\w-]*)}`), "{http.request.cookie.$1}"},
 | |
| 		{regexp.MustCompile(`{labels\.([\w-]*)}`), "{http.request.host.labels.$1}"},
 | |
| 		{regexp.MustCompile(`{path\.([\w-]*)}`), "{http.request.uri.path.$1}"},
 | |
| 		{regexp.MustCompile(`{file\.([\w-]*)}`), "{http.request.uri.path.file.$1}"},
 | |
| 		{regexp.MustCompile(`{query\.([\w-]*)}`), "{http.request.uri.query.$1}"},
 | |
| 		{regexp.MustCompile(`{re\.([\w-\.]*)}`), "{http.regexp.$1}"},
 | |
| 		{regexp.MustCompile(`{vars\.([\w-]*)}`), "{http.vars.$1}"},
 | |
| 		{regexp.MustCompile(`{rp\.([\w-\.]*)}`), "{http.reverse_proxy.$1}"},
 | |
| 		{regexp.MustCompile(`{resp\.([\w-\.]*)}`), "{http.intercept.$1}"},
 | |
| 		{regexp.MustCompile(`{err\.([\w-\.]*)}`), "{http.error.$1}"},
 | |
| 		{regexp.MustCompile(`{file_match\.([\w-]*)}`), "{http.matchers.file.$1}"},
 | |
| 	}
 | |
| 
 | |
| 	return ShorthandReplacer{
 | |
| 		complex: regexpReplacements,
 | |
| 		simple:  replacer,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // placeholderShorthands returns a slice of old-new string pairs,
 | |
| // where the left of the pair is a placeholder shorthand that may
 | |
| // be used in the Caddyfile, and the right is the replacement.
 | |
| func placeholderShorthands() []string {
 | |
| 	return []string{
 | |
| 		"{host}", "{http.request.host}",
 | |
| 		"{hostport}", "{http.request.hostport}",
 | |
| 		"{port}", "{http.request.port}",
 | |
| 		"{orig_method}", "{http.request.orig_method}",
 | |
| 		"{orig_uri}", "{http.request.orig_uri}",
 | |
| 		"{orig_path}", "{http.request.orig_uri.path}",
 | |
| 		"{orig_dir}", "{http.request.orig_uri.path.dir}",
 | |
| 		"{orig_file}", "{http.request.orig_uri.path.file}",
 | |
| 		"{orig_query}", "{http.request.orig_uri.query}",
 | |
| 		"{orig_?query}", "{http.request.orig_uri.prefixed_query}",
 | |
| 		"{method}", "{http.request.method}",
 | |
| 		"{uri}", "{http.request.uri}",
 | |
| 		"{path}", "{http.request.uri.path}",
 | |
| 		"{dir}", "{http.request.uri.path.dir}",
 | |
| 		"{file}", "{http.request.uri.path.file}",
 | |
| 		"{query}", "{http.request.uri.query}",
 | |
| 		"{?query}", "{http.request.uri.prefixed_query}",
 | |
| 		"{remote}", "{http.request.remote}",
 | |
| 		"{remote_host}", "{http.request.remote.host}",
 | |
| 		"{remote_port}", "{http.request.remote.port}",
 | |
| 		"{scheme}", "{http.request.scheme}",
 | |
| 		"{uuid}", "{http.request.uuid}",
 | |
| 		"{tls_cipher}", "{http.request.tls.cipher_suite}",
 | |
| 		"{tls_version}", "{http.request.tls.version}",
 | |
| 		"{tls_client_fingerprint}", "{http.request.tls.client.fingerprint}",
 | |
| 		"{tls_client_issuer}", "{http.request.tls.client.issuer}",
 | |
| 		"{tls_client_serial}", "{http.request.tls.client.serial}",
 | |
| 		"{tls_client_subject}", "{http.request.tls.client.subject}",
 | |
| 		"{tls_client_certificate_pem}", "{http.request.tls.client.certificate_pem}",
 | |
| 		"{tls_client_certificate_der_base64}", "{http.request.tls.client.certificate_der_base64}",
 | |
| 		"{upstream_hostport}", "{http.reverse_proxy.upstream.hostport}",
 | |
| 		"{client_ip}", "{http.vars.client_ip}",
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // ApplyToSegment replaces shorthand placeholder to its full placeholder, understandable by Caddy.
 | |
| func (s ShorthandReplacer) ApplyToSegment(segment *caddyfile.Segment) {
 | |
| 	if segment != nil {
 | |
| 		for i := 0; i < len(*segment); i++ {
 | |
| 			// simple string replacements
 | |
| 			(*segment)[i].Text = s.simple.Replace((*segment)[i].Text)
 | |
| 			// complex regexp replacements
 | |
| 			for _, r := range s.complex {
 | |
| 				(*segment)[i].Text = r.search.ReplaceAllString((*segment)[i].Text, r.replace)
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| }
 |