@marcel91200 / caddy Mirror · read-only / commits / d2655931
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 15:30 UTC

Replace strings.Index with strings.Cut (#4932)

Chirag Maheshwari committed Aug 7, 2022 at 09:33 UTC d26559316fa6fae880351637365f07947a2b79f6
3 files changed +6 -9
cmd/main.go
+2 -5
@@ -408,11 +408,8 @@ func parseEnvFile(envInput io.Reader) (map[string]string, error) {
408 }
409
410 // remove any trailing comment after value
411 - if commentStart := strings.Index(val, "#"); commentStart > 0 {
412 - before := val[commentStart-1]
413 - if before == '\t' || before == ' ' {
414 - val = strings.TrimRight(val[:commentStart], " \t")
415 - }
411 + if commentStart, _, found := strings.Cut(val, "#"); found {
412 + val = strings.TrimRight(commentStart, " \t")
413 }
414
415 // quoted value: support newlines
modules/caddyhttp/reverseproxy/reverseproxy.go
+2 -2
@@ -658,8 +658,8 @@ func (h Handler) addForwardedHeaders(req *http.Request) error {
658
659 // Client IP may contain a zone if IPv6, so we need
660 // to pull that out before parsing the IP
661 - if idx := strings.IndexByte(clientIP, '%'); idx >= 0 {
662 - clientIP = clientIP[:idx]
661 + if before, _, found := strings.Cut(clientIP, "%"); found {
662 + clientIP = before
663 }
664 ip := net.ParseIP(clientIP)
665 if ip == nil {
modules/caddyhttp/rewrite/rewrite.go
+2 -2
@@ -194,10 +194,10 @@ func (rewr Rewrite) Rewrite(r *http.Request, repl *caddy.Replacer) bool {
194
195 // before continuing, we need to check if a query string
196 // snuck into the path component during replacements
197 - if quPos := strings.Index(newPath, "?"); quPos > -1 {
197 + if before, after, found := strings.Cut(newPath, "?"); found {
198 // recompute; new path contains a query string
199 var injectedQuery string
200 - newPath, injectedQuery = newPath[:quPos], newPath[quPos+1:]
200 + newPath, injectedQuery = before, after
201 // don't overwrite explicitly-configured query string
202 if query == "" {
203 query = injectedQuery