@marcel91200 / caddy Mirror · read-only / commits / 315715e9
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 12, 05:45 UTC

core: Implement FastAbs to avoid repeated os.Getwd calls (#6687)

* core: Implement FastAbs to avoid repeated os.Getwd calls * Lint * Rename files

Francis Lavoie committed Nov 13, 2024 at 03:55 UTC 315715e90ffa25c4ad0d8a96e828dbdf6f638583
10 files changed +90 -16
caddyconfig/caddyfile/parse.go
+2 -2
@@ -423,7 +423,7 @@ func (p *parser) doImport(nesting int) error {
423 // make path relative to the file of the _token_ being processed rather
424 // than current working directory (issue #867) and then use glob to get
425 // list of matching filenames
426 - absFile, err := filepath.Abs(p.Dispenser.File())
426 + absFile, err := caddy.FastAbs(p.Dispenser.File())
427 if err != nil {
428 return p.Errf("Failed to get absolute path of file: %s: %v", p.Dispenser.File(), err)
429 }
@@ -622,7 +622,7 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
622
623 // Tack the file path onto these tokens so errors show the imported file's name
624 // (we use full, absolute path to avoid bugs: issue #1892)
625 - filename, err := filepath.Abs(importFile)
625 + filename, err := caddy.FastAbs(importFile)
626 if err != nil {
627 return nil, p.Errf("Failed to get absolute path of file: %s: %v", importFile, err)
628 }
filepath.go new
+37
@@ -0,0 +1,37 @@
1 +// Copyright 2015 Matthew Holt and The Caddy Authors
2 +//
3 +// Licensed under the Apache License, Version 2.0 (the "License");
4 +// you may not use this file except in compliance with the License.
5 +// You may obtain a copy of the License at
6 +//
7 +// http://www.apache.org/licenses/LICENSE-2.0
8 +//
9 +// Unless required by applicable law or agreed to in writing, software
10 +// distributed under the License is distributed on an "AS IS" BASIS,
11 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 +// See the License for the specific language governing permissions and
13 +// limitations under the License.
14 +
15 +//go:build !windows
16 +
17 +package caddy
18 +
19 +import (
20 + "os"
21 + "path/filepath"
22 +)
23 +
24 +// FastAbs is an optimized version of filepath.Abs for Unix systems,
25 +// since we don't expect the working directory to ever change once
26 +// Caddy is running. Avoid the os.Getwd() syscall overhead.
27 +func FastAbs(path string) (string, error) {
28 + if filepath.IsAbs(path) {
29 + return filepath.Clean(path), nil
30 + }
31 + if wderr != nil {
32 + return "", wderr
33 + }
34 + return filepath.Join(wd, path), nil
35 +}
36 +
37 +var wd, wderr = os.Getwd()
filepath_windows.go new
+25
@@ -0,0 +1,25 @@
1 +// Copyright 2015 Matthew Holt and The Caddy Authors
2 +//
3 +// Licensed under the Apache License, Version 2.0 (the "License");
4 +// you may not use this file except in compliance with the License.
5 +// You may obtain a copy of the License at
6 +//
7 +// http://www.apache.org/licenses/LICENSE-2.0
8 +//
9 +// Unless required by applicable law or agreed to in writing, software
10 +// distributed under the License is distributed on an "AS IS" BASIS,
11 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 +// See the License for the specific language governing permissions and
13 +// limitations under the License.
14 +
15 +package caddy
16 +
17 +import (
18 + "path/filepath"
19 +)
20 +
21 +// FastAbs can't be optimized on Windows because the
22 +// syscall.FullPath function takes an input.
23 +func FastAbs(path string) (string, error) {
24 + return filepath.Abs(path)
25 +}
filesystem.go
+14
@@ -1,3 +1,17 @@
1 +// Copyright 2015 Matthew Holt and The Caddy Authors
2 +//
3 +// Licensed under the Apache License, Version 2.0 (the "License");
4 +// you may not use this file except in compliance with the License.
5 +// You may obtain a copy of the License at
6 +//
7 +// http://www.apache.org/licenses/LICENSE-2.0
8 +//
9 +// Unless required by applicable law or agreed to in writing, software
10 +// distributed under the License is distributed on an "AS IS" BASIS,
11 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 +// See the License for the specific language governing permissions and
13 +// limitations under the License.
14 +
15 package caddy
16
17 import "io/fs"
listen.go
+1 -1
@@ -30,7 +30,7 @@ import (
30 "go.uber.org/zap"
31 )
32
33 -func reuseUnixSocket(network, addr string) (any, error) {
33 +func reuseUnixSocket(_, _ string) (any, error) {
34 return nil, nil
35 }
36
modules/caddyhttp/fileserver/staticfiles.go
+3 -3
@@ -204,7 +204,7 @@ func (fsrv *FileServer) Provision(ctx caddy.Context) error {
204 // absolute paths before the server starts for very slight performance improvement
205 for i, h := range fsrv.Hide {
206 if !strings.Contains(h, "{") && strings.Contains(h, separator) {
207 - if abs, err := filepath.Abs(h); err == nil {
207 + if abs, err := caddy.FastAbs(h); err == nil {
208 fsrv.Hide[i] = abs
209 }
210 }
@@ -636,7 +636,7 @@ func (fsrv *FileServer) transformHidePaths(repl *caddy.Replacer) []string {
636 for i := range fsrv.Hide {
637 hide[i] = repl.ReplaceAll(fsrv.Hide[i], "")
638 if strings.Contains(hide[i], separator) {
639 - abs, err := filepath.Abs(hide[i])
639 + abs, err := caddy.FastAbs(hide[i])
640 if err == nil {
641 hide[i] = abs
642 }
@@ -655,7 +655,7 @@ func fileHidden(filename string, hide []string) bool {
655 }
656
657 // all path comparisons use the complete absolute path if possible
658 - filenameAbs, err := filepath.Abs(filename)
658 + filenameAbs, err := caddy.FastAbs(filename)
659 if err == nil {
660 filename = filenameAbs
661 }
modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go
+1 -1
@@ -228,7 +228,7 @@ func (t Transport) buildEnv(r *http.Request) (envVars, error) {
228 ip = strings.Replace(ip, "]", "", 1)
229
230 // make sure file root is absolute
231 - root, err := filepath.Abs(repl.ReplaceAll(t.Root, "."))
231 + root, err := caddy.FastAbs(repl.ReplaceAll(t.Root, "."))
232 if err != nil {
233 return nil, err
234 }
modules/caddyhttp/reverseproxy/reverseproxy.go
+5 -5
@@ -549,7 +549,7 @@ func (h *Handler) proxyLoopIteration(r *http.Request, origReq *http.Request, w h
549 // ding the health status of the upstream (an error can still
550 // occur after the roundtrip if, for example, a response handler
551 // after the roundtrip returns an error)
552 - if succ, ok := proxyErr.(roundtripSucceeded); ok {
552 + if succ, ok := proxyErr.(roundtripSucceededError); ok {
553 return true, succ.error
554 }
555
@@ -953,10 +953,10 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
953 res.Body.Close()
954 }
955
956 - // wrap any route error in roundtripSucceeded so caller knows that
956 + // wrap any route error in roundtripSucceededError so caller knows that
957 // the roundtrip was successful and to not retry
958 if routeErr != nil {
959 - return roundtripSucceeded{routeErr}
959 + return roundtripSucceededError{routeErr}
960 }
961
962 // we're done handling the response, and we don't want to
@@ -1433,9 +1433,9 @@ type TLSTransport interface {
1433 EnableTLS(base *TLSConfig) error
1434 }
1435
1436 -// roundtripSucceeded is an error type that is returned if the
1436 +// roundtripSucceededError is an error type that is returned if the
1437 // roundtrip succeeded, but an error occurred after-the-fact.
1438 -type roundtripSucceeded struct{ error }
1438 +type roundtripSucceededError struct{ error }
1439
1440 // bodyReadCloser is a reader that, upon closing, will return
1441 // its buffer to the pool and close the underlying body reader.
modules/caddytls/connpolicy.go
+1 -2
@@ -24,7 +24,6 @@ import (
24 "fmt"
25 "io"
26 "os"
27 - "path/filepath"
27 "strings"
28
29 "github.com/mholt/acmez/v2"
@@ -358,7 +357,7 @@ func (p *ConnectionPolicy) buildStandardTLSConfig(ctx caddy.Context) error {
357 if err != nil {
358 return err
359 }
361 - filename, err = filepath.Abs(filename)
360 + filename, err = caddy.FastAbs(filename)
361 if err != nil {
362 return err
363 }
modules/logging/filewriter.go
+1 -2
@@ -20,7 +20,6 @@ import (
20 "io"
21 "math"
22 "os"
23 - "path/filepath"
23 "strconv"
24
25 "github.com/dustin/go-humanize"
@@ -133,7 +132,7 @@ func (fw *FileWriter) Provision(ctx caddy.Context) error {
132 }
133
134 func (fw FileWriter) String() string {
136 - fpath, err := filepath.Abs(fw.Filename)
135 + fpath, err := caddy.FastAbs(fw.Filename)
136 if err == nil {
137 return fpath
138 }