mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-25 09:40:13 +08:00
Replace strings.Index usages with strings.Cut (#4930)
This commit is contained in:
parent
17ae5acaba
commit
2642bd72b7
|
@ -189,12 +189,11 @@ func adaptByContentType(contentType string, body []byte) ([]byte, []Warning, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// adapter name should be suffix of MIME type
|
// adapter name should be suffix of MIME type
|
||||||
slashIdx := strings.Index(ct, "/")
|
_, adapterName, slashFound := strings.Cut(ct, "/")
|
||||||
if slashIdx < 0 {
|
if !slashFound {
|
||||||
return nil, nil, fmt.Errorf("malformed Content-Type")
|
return nil, nil, fmt.Errorf("malformed Content-Type")
|
||||||
}
|
}
|
||||||
|
|
||||||
adapterName := ct[slashIdx+1:]
|
|
||||||
cfgAdapter := GetAdapter(adapterName)
|
cfgAdapter := GetAdapter(adapterName)
|
||||||
if cfgAdapter == nil {
|
if cfgAdapter == nil {
|
||||||
return nil, nil, fmt.Errorf("unrecognized config adapter '%s'", adapterName)
|
return nil, nil, fmt.Errorf("unrecognized config adapter '%s'", adapterName)
|
||||||
|
|
|
@ -387,11 +387,11 @@ func parseEnvFile(envInput io.Reader) (map[string]string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// split line into key and value
|
// split line into key and value
|
||||||
fields := strings.SplitN(line, "=", 2)
|
before, after, isCut := strings.Cut(line, "=")
|
||||||
if len(fields) != 2 {
|
if !isCut {
|
||||||
return nil, fmt.Errorf("can't parse line %d; line should be in KEY=VALUE format", lineNumber)
|
return nil, fmt.Errorf("can't parse line %d; line should be in KEY=VALUE format", lineNumber)
|
||||||
}
|
}
|
||||||
key, val := fields[0], fields[1]
|
key, val := before, after
|
||||||
|
|
||||||
// sometimes keys are prefixed by "export " so file can be sourced in bash; ignore it here
|
// sometimes keys are prefixed by "export " so file can be sourced in bash; ignore it here
|
||||||
key = strings.TrimPrefix(key, "export ")
|
key = strings.TrimPrefix(key, "export ")
|
||||||
|
|
17
listeners.go
17
listeners.go
|
@ -468,15 +468,15 @@ func ParseNetworkAddress(addr string) (NetworkAddress, error) {
|
||||||
}
|
}
|
||||||
var start, end uint64
|
var start, end uint64
|
||||||
if port != "" {
|
if port != "" {
|
||||||
ports := strings.SplitN(port, "-", 2)
|
before, after, found := strings.Cut(port, "-")
|
||||||
if len(ports) == 1 {
|
if !found {
|
||||||
ports = append(ports, ports[0])
|
after = before
|
||||||
}
|
}
|
||||||
start, err = strconv.ParseUint(ports[0], 10, 16)
|
start, err = strconv.ParseUint(before, 10, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return NetworkAddress{}, fmt.Errorf("invalid start port: %v", err)
|
return NetworkAddress{}, fmt.Errorf("invalid start port: %v", err)
|
||||||
}
|
}
|
||||||
end, err = strconv.ParseUint(ports[1], 10, 16)
|
end, err = strconv.ParseUint(after, 10, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return NetworkAddress{}, fmt.Errorf("invalid end port: %v", err)
|
return NetworkAddress{}, fmt.Errorf("invalid end port: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -498,9 +498,10 @@ func ParseNetworkAddress(addr string) (NetworkAddress, error) {
|
||||||
// SplitNetworkAddress splits a into its network, host, and port components.
|
// SplitNetworkAddress splits a into its network, host, and port components.
|
||||||
// Note that port may be a port range (:X-Y), or omitted for unix sockets.
|
// Note that port may be a port range (:X-Y), or omitted for unix sockets.
|
||||||
func SplitNetworkAddress(a string) (network, host, port string, err error) {
|
func SplitNetworkAddress(a string) (network, host, port string, err error) {
|
||||||
if idx := strings.Index(a, "/"); idx >= 0 {
|
beforeSlash, afterSlash, slashFound := strings.Cut(a, "/")
|
||||||
network = strings.ToLower(strings.TrimSpace(a[:idx]))
|
if slashFound {
|
||||||
a = a[idx+1:]
|
network = strings.ToLower(strings.TrimSpace(beforeSlash))
|
||||||
|
a = afterSlash
|
||||||
}
|
}
|
||||||
if isUnixNetwork(network) {
|
if isUnixNetwork(network) {
|
||||||
host = a
|
host = a
|
||||||
|
|
|
@ -324,11 +324,11 @@ func ParseStructTag(tag string) (map[string]string, error) {
|
||||||
if pair == "" {
|
if pair == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
parts := strings.SplitN(pair, "=", 2)
|
before, after, isCut := strings.Cut(pair, "=")
|
||||||
if len(parts) != 2 {
|
if !isCut {
|
||||||
return nil, fmt.Errorf("missing key in '%s' (pair %d)", pair, i)
|
return nil, fmt.Errorf("missing key in '%s' (pair %d)", pair, i)
|
||||||
}
|
}
|
||||||
results[parts[0]] = parts[1]
|
results[before] = after
|
||||||
}
|
}
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -610,11 +610,11 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||||
if query == "" {
|
if query == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
parts := strings.SplitN(query, "=", 2)
|
before, after, found := strings.Cut(query, "=")
|
||||||
if len(parts) != 2 {
|
if !found {
|
||||||
return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
|
return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
|
||||||
}
|
}
|
||||||
url.Values(*m).Add(parts[0], parts[1])
|
url.Values(*m).Add(before, after)
|
||||||
}
|
}
|
||||||
if d.NextBlock(0) {
|
if d.NextBlock(0) {
|
||||||
return d.Err("malformed query matcher: blocks are not supported")
|
return d.Err("malformed query matcher: blocks are not supported")
|
||||||
|
|
|
@ -52,17 +52,16 @@ func parseLinkHeader(header string) []linkResource {
|
||||||
l.uri = strings.TrimSpace(link[li+1 : ri])
|
l.uri = strings.TrimSpace(link[li+1 : ri])
|
||||||
|
|
||||||
for _, param := range strings.Split(strings.TrimSpace(link[ri+1:]), semicolon) {
|
for _, param := range strings.Split(strings.TrimSpace(link[ri+1:]), semicolon) {
|
||||||
parts := strings.SplitN(strings.TrimSpace(param), equal, 2)
|
before, after, isCut := strings.Cut(strings.TrimSpace(param), equal)
|
||||||
key := strings.TrimSpace(parts[0])
|
key := strings.TrimSpace(before)
|
||||||
if key == "" {
|
if key == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(parts) == 1 {
|
if isCut {
|
||||||
|
l.params[key] = strings.TrimSpace(after)
|
||||||
|
} else {
|
||||||
l.params[key] = key
|
l.params[key] = key
|
||||||
}
|
}
|
||||||
if len(parts) == 2 {
|
|
||||||
l.params[key] = strings.TrimSpace(parts[1])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resources = append(resources, l)
|
resources = append(resources, l)
|
||||||
|
|
|
@ -80,9 +80,9 @@ func parseUpstreamDialAddress(upstreamAddr string) (string, string, error) {
|
||||||
scheme, host, port = toURL.Scheme, toURL.Hostname(), toURL.Port()
|
scheme, host, port = toURL.Scheme, toURL.Hostname(), toURL.Port()
|
||||||
} else {
|
} else {
|
||||||
// extract network manually, since caddy.ParseNetworkAddress() will always add one
|
// extract network manually, since caddy.ParseNetworkAddress() will always add one
|
||||||
if idx := strings.Index(upstreamAddr, "/"); idx >= 0 {
|
if beforeSlash, afterSlash, slashFound := strings.Cut(upstreamAddr, "/"); slashFound {
|
||||||
network = strings.ToLower(strings.TrimSpace(upstreamAddr[:idx]))
|
network = strings.ToLower(strings.TrimSpace(beforeSlash))
|
||||||
upstreamAddr = upstreamAddr[idx+1:]
|
upstreamAddr = afterSlash
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
host, port, err = net.SplitHostPort(upstreamAddr)
|
host, port, err = net.SplitHostPort(upstreamAddr)
|
||||||
|
|
|
@ -424,13 +424,13 @@ func (c *FCGIClient) Request(p map[string]string, req io.Reader) (resp *http.Res
|
||||||
resp.Header = http.Header(mimeHeader)
|
resp.Header = http.Header(mimeHeader)
|
||||||
|
|
||||||
if resp.Header.Get("Status") != "" {
|
if resp.Header.Get("Status") != "" {
|
||||||
statusParts := strings.SplitN(resp.Header.Get("Status"), " ", 2)
|
statusNumber, statusInfo, statusIsCut := strings.Cut(resp.Header.Get("Status"), " ")
|
||||||
resp.StatusCode, err = strconv.Atoi(statusParts[0])
|
resp.StatusCode, err = strconv.Atoi(statusNumber)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(statusParts) > 1 {
|
if statusIsCut {
|
||||||
resp.Status = statusParts[1]
|
resp.Status = statusInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user