aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFrancis Lavoie <[email protected]>2024-11-13 03:55:51 -0500
committerGitHub <[email protected]>2024-11-13 03:55:51 -0500
commit315715e90ffa25c4ad0d8a96e828dbdf6f638583 (patch)
tree3b22815c57acc730c90b6dcaa740a34a7073a36b
parent238f1108e6600049d9041abc88db24526ee4f882 (diff)
downloadcaddy-315715e90ffa25c4ad0d8a96e828dbdf6f638583.tar.gz
caddy-315715e90ffa25c4ad0d8a96e828dbdf6f638583.zip
core: Implement FastAbs to avoid repeated os.Getwd calls (#6687)
* core: Implement FastAbs to avoid repeated os.Getwd calls * Lint * Rename files
-rw-r--r--caddyconfig/caddyfile/parse.go4
-rw-r--r--filepath.go37
-rw-r--r--filepath_windows.go25
-rw-r--r--filesystem.go14
-rw-r--r--listen.go2
-rw-r--r--modules/caddyhttp/fileserver/staticfiles.go6
-rw-r--r--modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go2
-rw-r--r--modules/caddyhttp/reverseproxy/reverseproxy.go10
-rw-r--r--modules/caddytls/connpolicy.go3
-rw-r--r--modules/logging/filewriter.go3
10 files changed, 90 insertions, 16 deletions
diff --git a/caddyconfig/caddyfile/parse.go b/caddyconfig/caddyfile/parse.go
index e19b3b97d..d04a1ac46 100644
--- a/caddyconfig/caddyfile/parse.go
+++ b/caddyconfig/caddyfile/parse.go
@@ -423,7 +423,7 @@ func (p *parser) doImport(nesting int) error {
// make path relative to the file of the _token_ being processed rather
// than current working directory (issue #867) and then use glob to get
// list of matching filenames
- absFile, err := filepath.Abs(p.Dispenser.File())
+ absFile, err := caddy.FastAbs(p.Dispenser.File())
if err != nil {
return p.Errf("Failed to get absolute path of file: %s: %v", p.Dispenser.File(), err)
}
@@ -622,7 +622,7 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
// Tack the file path onto these tokens so errors show the imported file's name
// (we use full, absolute path to avoid bugs: issue #1892)
- filename, err := filepath.Abs(importFile)
+ filename, err := caddy.FastAbs(importFile)
if err != nil {
return nil, p.Errf("Failed to get absolute path of file: %s: %v", importFile, err)
}
diff --git a/filepath.go b/filepath.go
new file mode 100644
index 000000000..ae9d54051
--- /dev/null
+++ b/filepath.go
@@ -0,0 +1,37 @@
+// Copyright 2015 Matthew Holt and The Caddy Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//go:build !windows
+
+package caddy
+
+import (
+ "os"
+ "path/filepath"
+)
+
+// FastAbs is an optimized version of filepath.Abs for Unix systems,
+// since we don't expect the working directory to ever change once
+// Caddy is running. Avoid the os.Getwd() syscall overhead.
+func FastAbs(path string) (string, error) {
+ if filepath.IsAbs(path) {
+ return filepath.Clean(path), nil
+ }
+ if wderr != nil {
+ return "", wderr
+ }
+ return filepath.Join(wd, path), nil
+}
+
+var wd, wderr = os.Getwd()
diff --git a/filepath_windows.go b/filepath_windows.go
new file mode 100644
index 000000000..4e46b0445
--- /dev/null
+++ b/filepath_windows.go
@@ -0,0 +1,25 @@
+// Copyright 2015 Matthew Holt and The Caddy Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package caddy
+
+import (
+ "path/filepath"
+)
+
+// FastAbs can't be optimized on Windows because the
+// syscall.FullPath function takes an input.
+func FastAbs(path string) (string, error) {
+ return filepath.Abs(path)
+}
diff --git a/filesystem.go b/filesystem.go
index 9785f57d4..d6679e90b 100644
--- a/filesystem.go
+++ b/filesystem.go
@@ -1,3 +1,17 @@
+// Copyright 2015 Matthew Holt and The Caddy Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
package caddy
import "io/fs"
diff --git a/listen.go b/listen.go
index f5c2086a6..1a7051bbf 100644
--- a/listen.go
+++ b/listen.go
@@ -30,7 +30,7 @@ import (
"go.uber.org/zap"
)
-func reuseUnixSocket(network, addr string) (any, error) {
+func reuseUnixSocket(_, _ string) (any, error) {
return nil, nil
}
diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go
index 4ae69b647..2b0caecfc 100644
--- a/modules/caddyhttp/fileserver/staticfiles.go
+++ b/modules/caddyhttp/fileserver/staticfiles.go
@@ -204,7 +204,7 @@ func (fsrv *FileServer) Provision(ctx caddy.Context) error {
// absolute paths before the server starts for very slight performance improvement
for i, h := range fsrv.Hide {
if !strings.Contains(h, "{") && strings.Contains(h, separator) {
- if abs, err := filepath.Abs(h); err == nil {
+ if abs, err := caddy.FastAbs(h); err == nil {
fsrv.Hide[i] = abs
}
}
@@ -636,7 +636,7 @@ func (fsrv *FileServer) transformHidePaths(repl *caddy.Replacer) []string {
for i := range fsrv.Hide {
hide[i] = repl.ReplaceAll(fsrv.Hide[i], "")
if strings.Contains(hide[i], separator) {
- abs, err := filepath.Abs(hide[i])
+ abs, err := caddy.FastAbs(hide[i])
if err == nil {
hide[i] = abs
}
@@ -655,7 +655,7 @@ func fileHidden(filename string, hide []string) bool {
}
// all path comparisons use the complete absolute path if possible
- filenameAbs, err := filepath.Abs(filename)
+ filenameAbs, err := caddy.FastAbs(filename)
if err == nil {
filename = filenameAbs
}
diff --git a/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go b/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go
index 3985465ba..d451dd380 100644
--- a/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go
+++ b/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go
@@ -228,7 +228,7 @@ func (t Transport) buildEnv(r *http.Request) (envVars, error) {
ip = strings.Replace(ip, "]", "", 1)
// make sure file root is absolute
- root, err := filepath.Abs(repl.ReplaceAll(t.Root, "."))
+ root, err := caddy.FastAbs(repl.ReplaceAll(t.Root, "."))
if err != nil {
return nil, err
}
diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go
index 8dd3ba1b6..30d9af101 100644
--- a/modules/caddyhttp/reverseproxy/reverseproxy.go
+++ b/modules/caddyhttp/reverseproxy/reverseproxy.go
@@ -549,7 +549,7 @@ func (h *Handler) proxyLoopIteration(r *http.Request, origReq *http.Request, w h
// ding the health status of the upstream (an error can still
// occur after the roundtrip if, for example, a response handler
// after the roundtrip returns an error)
- if succ, ok := proxyErr.(roundtripSucceeded); ok {
+ if succ, ok := proxyErr.(roundtripSucceededError); ok {
return true, succ.error
}
@@ -953,10 +953,10 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
res.Body.Close()
}
- // wrap any route error in roundtripSucceeded so caller knows that
+ // wrap any route error in roundtripSucceededError so caller knows that
// the roundtrip was successful and to not retry
if routeErr != nil {
- return roundtripSucceeded{routeErr}
+ return roundtripSucceededError{routeErr}
}
// we're done handling the response, and we don't want to
@@ -1433,9 +1433,9 @@ type TLSTransport interface {
EnableTLS(base *TLSConfig) error
}
-// roundtripSucceeded is an error type that is returned if the
+// roundtripSucceededError is an error type that is returned if the
// roundtrip succeeded, but an error occurred after-the-fact.
-type roundtripSucceeded struct{ error }
+type roundtripSucceededError struct{ error }
// bodyReadCloser is a reader that, upon closing, will return
// its buffer to the pool and close the underlying body reader.
diff --git a/modules/caddytls/connpolicy.go b/modules/caddytls/connpolicy.go
index 46afc6693..9332cf2ed 100644
--- a/modules/caddytls/connpolicy.go
+++ b/modules/caddytls/connpolicy.go
@@ -24,7 +24,6 @@ import (
"fmt"
"io"
"os"
- "path/filepath"
"strings"
"github.com/mholt/acmez/v2"
@@ -358,7 +357,7 @@ func (p *ConnectionPolicy) buildStandardTLSConfig(ctx caddy.Context) error {
if err != nil {
return err
}
- filename, err = filepath.Abs(filename)
+ filename, err = caddy.FastAbs(filename)
if err != nil {
return err
}
diff --git a/modules/logging/filewriter.go b/modules/logging/filewriter.go
index 44c0feb67..62d500dca 100644
--- a/modules/logging/filewriter.go
+++ b/modules/logging/filewriter.go
@@ -20,7 +20,6 @@ import (
"io"
"math"
"os"
- "path/filepath"
"strconv"
"github.com/dustin/go-humanize"
@@ -133,7 +132,7 @@ func (fw *FileWriter) Provision(ctx caddy.Context) error {
}
func (fw FileWriter) String() string {
- fpath, err := filepath.Abs(fw.Filename)
+ fpath, err := caddy.FastAbs(fw.Filename)
if err == nil {
return fpath
}