@marcel91200 / caddy Mirror · read-only / commits / 2642bd72
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 usages with strings.Cut (#4930)

WilczyńskiT committed Aug 4, 2022 at 19:17 UTC 2642bd72b7ca35b8622824fdffced2aefe1aaf11
8 files changed +32 -33
caddyconfig/load.go
+2 -3
@@ -189,12 +189,11 @@ func adaptByContentType(contentType string, body []byte) ([]byte, []Warning, err
189 }
190
191 // adapter name should be suffix of MIME type
192 - slashIdx := strings.Index(ct, "/")
193 - if slashIdx < 0 {
192 + _, adapterName, slashFound := strings.Cut(ct, "/")
193 + if !slashFound {
194 return nil, nil, fmt.Errorf("malformed Content-Type")
195 }
196
197 - adapterName := ct[slashIdx+1:]
197 cfgAdapter := GetAdapter(adapterName)
198 if cfgAdapter == nil {
199 return nil, nil, fmt.Errorf("unrecognized config adapter '%s'", adapterName)
cmd/main.go
+3 -3
@@ -387,11 +387,11 @@ func parseEnvFile(envInput io.Reader) (map[string]string, error) {
387 }
388
389 // split line into key and value
390 - fields := strings.SplitN(line, "=", 2)
391 - if len(fields) != 2 {
390 + before, after, isCut := strings.Cut(line, "=")
391 + if !isCut {
392 return nil, fmt.Errorf("can't parse line %d; line should be in KEY=VALUE format", lineNumber)
393 }
394 - key, val := fields[0], fields[1]
394 + key, val := before, after
395
396 // sometimes keys are prefixed by "export " so file can be sourced in bash; ignore it here
397 key = strings.TrimPrefix(key, "export ")
listeners.go
+9 -8
@@ -468,15 +468,15 @@ func ParseNetworkAddress(addr string) (NetworkAddress, error) {
468 }
469 var start, end uint64
470 if port != "" {
471 - ports := strings.SplitN(port, "-", 2)
472 - if len(ports) == 1 {
473 - ports = append(ports, ports[0])
471 + before, after, found := strings.Cut(port, "-")
472 + if !found {
473 + after = before
474 }
475 - start, err = strconv.ParseUint(ports[0], 10, 16)
475 + start, err = strconv.ParseUint(before, 10, 16)
476 if err != nil {
477 return NetworkAddress{}, fmt.Errorf("invalid start port: %v", err)
478 }
479 - end, err = strconv.ParseUint(ports[1], 10, 16)
479 + end, err = strconv.ParseUint(after, 10, 16)
480 if err != nil {
481 return NetworkAddress{}, fmt.Errorf("invalid end port: %v", err)
482 }
@@ -498,9 +498,10 @@ func ParseNetworkAddress(addr string) (NetworkAddress, error) {
498 // SplitNetworkAddress splits a into its network, host, and port components.
499 // Note that port may be a port range (:X-Y), or omitted for unix sockets.
500 func SplitNetworkAddress(a string) (network, host, port string, err error) {
501 - if idx := strings.Index(a, "/"); idx >= 0 {
502 - network = strings.ToLower(strings.TrimSpace(a[:idx]))
503 - a = a[idx+1:]
501 + beforeSlash, afterSlash, slashFound := strings.Cut(a, "/")
502 + if slashFound {
503 + network = strings.ToLower(strings.TrimSpace(beforeSlash))
504 + a = afterSlash
505 }
506 if isUnixNetwork(network) {
507 host = a
modules.go
+3 -3
@@ -324,11 +324,11 @@ func ParseStructTag(tag string) (map[string]string, error) {
324 if pair == "" {
325 continue
326 }
327 - parts := strings.SplitN(pair, "=", 2)
328 - if len(parts) != 2 {
327 + before, after, isCut := strings.Cut(pair, "=")
328 + if !isCut {
329 return nil, fmt.Errorf("missing key in '%s' (pair %d)", pair, i)
330 }
331 - results[parts[0]] = parts[1]
331 + results[before] = after
332 }
333 return results, nil
334 }
modules/caddyhttp/matchers.go
+3 -3
@@ -610,11 +610,11 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
610 if query == "" {
611 continue
612 }
613 - parts := strings.SplitN(query, "=", 2)
614 - if len(parts) != 2 {
613 + before, after, found := strings.Cut(query, "=")
614 + if !found {
615 return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
616 }
617 - url.Values(*m).Add(parts[0], parts[1])
617 + url.Values(*m).Add(before, after)
618 }
619 if d.NextBlock(0) {
620 return d.Err("malformed query matcher: blocks are not supported")
modules/caddyhttp/push/link.go
+5 -6
@@ -52,17 +52,16 @@ func parseLinkHeader(header string) []linkResource {
52 l.uri = strings.TrimSpace(link[li+1 : ri])
53
54 for _, param := range strings.Split(strings.TrimSpace(link[ri+1:]), semicolon) {
55 - parts := strings.SplitN(strings.TrimSpace(param), equal, 2)
56 - key := strings.TrimSpace(parts[0])
55 + before, after, isCut := strings.Cut(strings.TrimSpace(param), equal)
56 + key := strings.TrimSpace(before)
57 if key == "" {
58 continue
59 }
60 - if len(parts) == 1 {
60 + if isCut {
61 + l.params[key] = strings.TrimSpace(after)
62 + } else {
63 l.params[key] = key
64 }
63 - if len(parts) == 2 {
64 - l.params[key] = strings.TrimSpace(parts[1])
65 - }
65 }
66
67 resources = append(resources, l)
modules/caddyhttp/reverseproxy/addresses.go
+3 -3
@@ -80,9 +80,9 @@ func parseUpstreamDialAddress(upstreamAddr string) (string, string, error) {
80 scheme, host, port = toURL.Scheme, toURL.Hostname(), toURL.Port()
81 } else {
82 // extract network manually, since caddy.ParseNetworkAddress() will always add one
83 - if idx := strings.Index(upstreamAddr, "/"); idx >= 0 {
84 - network = strings.ToLower(strings.TrimSpace(upstreamAddr[:idx]))
85 - upstreamAddr = upstreamAddr[idx+1:]
83 + if beforeSlash, afterSlash, slashFound := strings.Cut(upstreamAddr, "/"); slashFound {
84 + network = strings.ToLower(strings.TrimSpace(beforeSlash))
85 + upstreamAddr = afterSlash
86 }
87 var err error
88 host, port, err = net.SplitHostPort(upstreamAddr)
modules/caddyhttp/reverseproxy/fastcgi/client.go
+4 -4
@@ -424,13 +424,13 @@ func (c *FCGIClient) Request(p map[string]string, req io.Reader) (resp *http.Res
424 resp.Header = http.Header(mimeHeader)
425
426 if resp.Header.Get("Status") != "" {
427 - statusParts := strings.SplitN(resp.Header.Get("Status"), " ", 2)
428 - resp.StatusCode, err = strconv.Atoi(statusParts[0])
427 + statusNumber, statusInfo, statusIsCut := strings.Cut(resp.Header.Get("Status"), " ")
428 + resp.StatusCode, err = strconv.Atoi(statusNumber)
429 if err != nil {
430 return
431 }
432 - if len(statusParts) > 1 {
433 - resp.Status = statusParts[1]
432 + if statusIsCut {
433 + resp.Status = statusInfo
434 }
435
436 } else {