@marcel91200 / caddy Mirror · read-only / commits / c48fadc4
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 14, 04:00 UTC

Move from deprecated ioutil to os and io packages (#4364)

KallyDev committed Sep 30, 2021 at 01:17 UTC c48fadc4a7655008d13076c7f757c36368e2ca13
26 files changed +71 -84
.golangci.yml
+1 -1
@@ -1,6 +1,6 @@
1 linters-settings:
2 errcheck:
3 - ignore: fmt:.*,io/ioutil:^Read.*,go.uber.org/zap/zapcore:^Add.*
3 + ignore: fmt:.*,go.uber.org/zap/zapcore:^Add.*
4 ignoretests: true
5
6 linters:
admin.go
+1 -2
@@ -26,7 +26,6 @@ import (
26 "expvar"
27 "fmt"
28 "io"
29 - "io/ioutil"
29 "net"
30 "net/http"
31 "net/http/pprof"
@@ -1202,7 +1201,7 @@ var (
1201 // will get deleted before the process gracefully exits.
1202 func PIDFile(filename string) error {
1203 pid := []byte(strconv.Itoa(os.Getpid()) + "\n")
1205 - err := ioutil.WriteFile(filename, pid, 0600)
1204 + err := os.WriteFile(filename, pid, 0600)
1205 if err != nil {
1206 return err
1207 }
caddy.go
+3 -4
@@ -20,7 +20,6 @@ import (
20 "encoding/json"
21 "fmt"
22 "io"
23 - "io/ioutil"
23 "log"
24 "net/http"
25 "os"
@@ -300,7 +299,7 @@ func unsyncedDecodeAndRun(cfgJSON []byte, allowPersist bool) error {
299 zap.String("dir", dir),
300 zap.Error(err))
301 } else {
303 - err := ioutil.WriteFile(ConfigAutosavePath, cfgJSON, 0600)
302 + err := os.WriteFile(ConfigAutosavePath, cfgJSON, 0600)
303 if err == nil {
304 Log().Info("autosaved config (load with --resume flag)", zap.String("file", ConfigAutosavePath))
305 } else {
@@ -700,13 +699,13 @@ func ParseDuration(s string) (time.Duration, error) {
699 // have its own unique ID.
700 func InstanceID() (uuid.UUID, error) {
701 uuidFilePath := filepath.Join(AppDataDir(), "instance.uuid")
703 - uuidFileBytes, err := ioutil.ReadFile(uuidFilePath)
702 + uuidFileBytes, err := os.ReadFile(uuidFilePath)
703 if os.IsNotExist(err) {
704 uuid, err := uuid.NewRandom()
705 if err != nil {
706 return uuid, err
707 }
709 - err = ioutil.WriteFile(uuidFilePath, []byte(uuid.String()), 0600)
708 + err = os.WriteFile(uuidFilePath, []byte(uuid.String()), 0600)
709 return uuid, err
710 } else if err != nil {
711 return [16]byte{}, err
caddyconfig/caddyfile/parse.go
+2 -2
@@ -17,7 +17,7 @@ package caddyfile
17 import (
18 "bytes"
19 "fmt"
20 - "io/ioutil"
20 + "io"
21 "log"
22 "os"
23 "path/filepath"
@@ -447,7 +447,7 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
447 return nil, p.Errf("Could not import %s: is a directory", importFile)
448 }
449
450 - input, err := ioutil.ReadAll(file)
450 + input, err := io.ReadAll(file)
451 if err != nil {
452 return nil, p.Errf("Could not read imported file %s: %v", importFile, err)
453 }
caddyconfig/caddyfile/parse_test.go
+5 -6
@@ -16,7 +16,6 @@ package caddyfile
16
17 import (
18 "bytes"
19 - "io/ioutil"
19 "os"
20 "path/filepath"
21 "testing"
@@ -280,7 +279,7 @@ func TestRecursiveImport(t *testing.T) {
279 }
280
281 // test relative recursive import
283 - err = ioutil.WriteFile(recursiveFile1, []byte(
282 + err = os.WriteFile(recursiveFile1, []byte(
283 `localhost
284 dir1
285 import recursive_import_test2`), 0644)
@@ -289,7 +288,7 @@ func TestRecursiveImport(t *testing.T) {
288 }
289 defer os.Remove(recursiveFile1)
290
292 - err = ioutil.WriteFile(recursiveFile2, []byte("dir2 1"), 0644)
291 + err = os.WriteFile(recursiveFile2, []byte("dir2 1"), 0644)
292 if err != nil {
293 t.Fatal(err)
294 }
@@ -314,7 +313,7 @@ func TestRecursiveImport(t *testing.T) {
313 }
314
315 // test absolute recursive import
317 - err = ioutil.WriteFile(recursiveFile1, []byte(
316 + err = os.WriteFile(recursiveFile1, []byte(
317 `localhost
318 dir1
319 import `+recursiveFile2), 0644)
@@ -370,7 +369,7 @@ func TestDirectiveImport(t *testing.T) {
369 t.Fatal(err)
370 }
371
373 - err = ioutil.WriteFile(directiveFile, []byte(`prop1 1
372 + err = os.WriteFile(directiveFile, []byte(`prop1 1
373 prop2 2`), 0644)
374 if err != nil {
375 t.Fatal(err)
@@ -633,7 +632,7 @@ func TestSnippets(t *testing.T) {
632 }
633
634 func writeStringToTempFileOrDie(t *testing.T, str string) (pathToFile string) {
636 - file, err := ioutil.TempFile("", t.Name())
635 + file, err := os.CreateTemp("", t.Name())
636 if err != nil {
637 panic(err) // get a stack trace so we know where this was called from.
638 }
caddyconfig/httpcaddyfile/builtins.go
+2 -2
@@ -19,8 +19,8 @@ import (
19 "encoding/pem"
20 "fmt"
21 "html"
22 - "io/ioutil"
22 "net/http"
23 + "os"
24 "reflect"
25 "strconv"
26 "strings"
@@ -230,7 +230,7 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
230 return nil, h.ArgErr()
231 }
232 filename := h.Val()
233 - certDataPEM, err := ioutil.ReadFile(filename)
233 + certDataPEM, err := os.ReadFile(filename)
234 if err != nil {
235 return nil, err
236 }
caddyconfig/httploader.go
+4 -3
@@ -18,8 +18,9 @@ import (
18 "crypto/tls"
19 "crypto/x509"
20 "fmt"
21 - "io/ioutil"
21 + "io"
22 "net/http"
23 + "os"
24 "time"
25
26 "github.com/caddyserver/caddy/v2"
@@ -95,7 +96,7 @@ func (hl HTTPLoader) LoadConfig(ctx caddy.Context) ([]byte, error) {
96 return nil, fmt.Errorf("server responded with HTTP %d", resp.StatusCode)
97 }
98
98 - body, err := ioutil.ReadAll(resp.Body)
99 + body, err := io.ReadAll(resp.Body)
100 if err != nil {
101 return nil, err
102 }
@@ -144,7 +145,7 @@ func (hl HTTPLoader) makeClient(ctx caddy.Context) (*http.Client, error) {
145 if len(hl.TLS.RootCAPEMFiles) > 0 {
146 rootPool := x509.NewCertPool()
147 for _, pemFile := range hl.TLS.RootCAPEMFiles {
147 - pemData, err := ioutil.ReadFile(pemFile)
148 + pemData, err := os.ReadFile(pemFile)
149 if err != nil {
150 return nil, fmt.Errorf("failed reading ca cert: %v", err)
151 }
caddytest/caddytest.go
+5 -5
@@ -7,7 +7,7 @@ import (
7 "encoding/json"
8 "errors"
9 "fmt"
10 - "io/ioutil"
10 + "io"
11 "log"
12 "net"
13 "net/http"
@@ -129,7 +129,7 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
129 return
130 }
131 defer res.Body.Close()
132 - body, _ := ioutil.ReadAll(res.Body)
132 + body, _ := io.ReadAll(res.Body)
133
134 var out bytes.Buffer
135 _ = json.Indent(&out, body, "", " ")
@@ -162,7 +162,7 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
162 timeElapsed(start, "caddytest: config load time")
163
164 defer res.Body.Close()
165 - body, err := ioutil.ReadAll(res.Body)
165 + body, err := io.ReadAll(res.Body)
166 if err != nil {
167 tc.t.Errorf("unable to read response. %s", err)
168 return err
@@ -202,7 +202,7 @@ func (tc *Tester) ensureConfigRunning(rawConfig string, configType string) error
202 return nil
203 }
204 defer resp.Body.Close()
205 - actualBytes, err := ioutil.ReadAll(resp.Body)
205 + actualBytes, err := io.ReadAll(resp.Body)
206 if err != nil {
207 return nil
208 }
@@ -471,7 +471,7 @@ func (tc *Tester) AssertResponse(req *http.Request, expectedStatusCode int, expe
471 resp := tc.AssertResponseCode(req, expectedStatusCode)
472
473 defer resp.Body.Close()
474 - bytes, err := ioutil.ReadAll(resp.Body)
474 + bytes, err := io.ReadAll(resp.Body)
475 if err != nil {
476 tc.t.Fatalf("unable to read the response body %s", err)
477 }
caddytest/integration/caddyfile_adapt_test.go
+3 -3
@@ -3,7 +3,7 @@ package integration
3 import (
4 jsonMod "encoding/json"
5 "fmt"
6 - "io/ioutil"
6 + "os"
7 "path/filepath"
8 "regexp"
9 "strings"
@@ -14,7 +14,7 @@ import (
14
15 func TestCaddyfileAdaptToJSON(t *testing.T) {
16 // load the list of test files from the dir
17 - files, err := ioutil.ReadDir("./caddyfile_adapt")
17 + files, err := os.ReadDir("./caddyfile_adapt")
18 if err != nil {
19 t.Errorf("failed to read caddyfile_adapt dir: %s", err)
20 }
@@ -29,7 +29,7 @@ func TestCaddyfileAdaptToJSON(t *testing.T) {
29
30 // read the test file
31 filename := f.Name()
32 - data, err := ioutil.ReadFile("./caddyfile_adapt/" + filename)
32 + data, err := os.ReadFile("./caddyfile_adapt/" + filename)
33 if err != nil {
34 t.Errorf("failed to read %s dir: %s", filename, err)
35 }
caddytest/integration/reverseproxy_test.go
+3 -4
@@ -2,7 +2,6 @@ package integration
2
3 import (
4 "fmt"
5 - "io/ioutil"
5 "net"
6 "net/http"
7 "os"
@@ -85,7 +84,7 @@ func TestDialWithPlaceholderUnix(t *testing.T) {
84 t.SkipNow()
85 }
86
88 - f, err := ioutil.TempFile("", "*.sock")
87 + f, err := os.CreateTemp("", "*.sock")
88 if err != nil {
89 t.Errorf("failed to create TempFile: %s", err)
90 return
@@ -387,7 +386,7 @@ func TestReverseProxyHealthCheckUnixSocket(t *testing.T) {
386 t.SkipNow()
387 }
388 tester := caddytest.NewTester(t)
390 - f, err := ioutil.TempFile("", "*.sock")
389 + f, err := os.CreateTemp("", "*.sock")
390 if err != nil {
391 t.Errorf("failed to create TempFile: %s", err)
392 return
@@ -442,7 +441,7 @@ func TestReverseProxyHealthCheckUnixSocketWithoutPort(t *testing.T) {
441 t.SkipNow()
442 }
443 tester := caddytest.NewTester(t)
445 - f, err := ioutil.TempFile("", "*.sock")
444 + f, err := os.CreateTemp("", "*.sock")
445 if err != nil {
446 t.Errorf("failed to create TempFile: %s", err)
447 return
caddytest/integration/stream_test.go
+5 -6
@@ -6,7 +6,6 @@ import (
6 "crypto/rand"
7 "fmt"
8 "io"
9 - "io/ioutil"
9 "net/http"
10 "net/http/httputil"
11 "net/url"
@@ -110,7 +109,7 @@ func TestH2ToH2CStream(t *testing.T) {
109 r, w := io.Pipe()
110 req := &http.Request{
111 Method: "PUT",
113 - Body: ioutil.NopCloser(r),
112 + Body: io.NopCloser(r),
113 URL: &url.URL{
114 Scheme: "https",
115 Host: "127.0.0.1:9443",
@@ -134,7 +133,7 @@ func TestH2ToH2CStream(t *testing.T) {
133 }()
134
135 defer resp.Body.Close()
137 - bytes, err := ioutil.ReadAll(resp.Body)
136 + bytes, err := io.ReadAll(resp.Body)
137 if err != nil {
138 t.Fatalf("unable to read the response body %s", err)
139 }
@@ -319,7 +318,7 @@ func TestH2ToH1ChunkedResponse(t *testing.T) {
318 r, w := io.Pipe()
319 req := &http.Request{
320 Method: "PUT",
322 - Body: ioutil.NopCloser(r),
321 + Body: io.NopCloser(r),
322 URL: &url.URL{
323 Scheme: "https",
324 Host: "127.0.0.1:9443",
@@ -342,7 +341,7 @@ func TestH2ToH1ChunkedResponse(t *testing.T) {
341 }
342
343 defer resp.Body.Close()
345 - bytes, err := ioutil.ReadAll(resp.Body)
344 + bytes, err := io.ReadAll(resp.Body)
345 if err != nil {
346 t.Fatalf("unable to read the response body %s", err)
347 }
@@ -370,7 +369,7 @@ func testH2ToH1ChunkedResponseServeH1(t *testing.T) *http.Server {
369 }
370
371 defer r.Body.Close()
373 - bytes, err := ioutil.ReadAll(r.Body)
372 + bytes, err := io.ReadAll(r.Body)
373 if err != nil {
374 t.Fatalf("unable to read the response body %s", err)
375 }
cmd/commandfuncs.go
+7 -8
@@ -22,7 +22,6 @@ import (
22 "errors"
23 "fmt"
24 "io"
25 - "io/ioutil"
25 "log"
26 "net"
27 "net/http"
@@ -181,7 +180,7 @@ func cmdRun(fl Flags) (int, error) {
180 var config []byte
181 var err error
182 if runCmdResumeFlag {
184 - config, err = ioutil.ReadFile(caddy.ConfigAutosavePath)
183 + config, err = os.ReadFile(caddy.ConfigAutosavePath)
184 if os.IsNotExist(err) {
185 // not a bad error; just can't resume if autosave file doesn't exist
186 caddy.Log().Info("no autosave file exists", zap.String("autosave_file", caddy.ConfigAutosavePath))
@@ -219,7 +218,7 @@ func cmdRun(fl Flags) (int, error) {
218 // if we are to report to another process the successful start
219 // of the server, do so now by echoing back contents of stdin
220 if runCmdPingbackFlag != "" {
222 - confirmationBytes, err := ioutil.ReadAll(os.Stdin)
221 + confirmationBytes, err := io.ReadAll(os.Stdin)
222 if err != nil {
223 return caddy.ExitCodeFailedStartup,
224 fmt.Errorf("reading confirmation bytes from stdin: %v", err)
@@ -457,7 +456,7 @@ func cmdAdaptConfig(fl Flags) (int, error) {
456 fmt.Errorf("unrecognized config adapter: %s", adaptCmdAdapterFlag)
457 }
458
460 - input, err := ioutil.ReadFile(adaptCmdInputFlag)
459 + input, err := os.ReadFile(adaptCmdInputFlag)
460 if err != nil {
461 return caddy.ExitCodeFailedStartup,
462 fmt.Errorf("reading input file: %v", err)
@@ -541,7 +540,7 @@ func cmdFmt(fl Flags) (int, error) {
540
541 // as a special case, read from stdin if the file name is "-"
542 if formatCmdConfigFile == "-" {
544 - input, err := ioutil.ReadAll(os.Stdin)
543 + input, err := io.ReadAll(os.Stdin)
544 if err != nil {
545 return caddy.ExitCodeFailedStartup,
546 fmt.Errorf("reading stdin: %v", err)
@@ -550,7 +549,7 @@ func cmdFmt(fl Flags) (int, error) {
549 return caddy.ExitCodeSuccess, nil
550 }
551
553 - input, err := ioutil.ReadFile(formatCmdConfigFile)
552 + input, err := os.ReadFile(formatCmdConfigFile)
553 if err != nil {
554 return caddy.ExitCodeFailedStartup,
555 fmt.Errorf("reading input file: %v", err)
@@ -559,7 +558,7 @@ func cmdFmt(fl Flags) (int, error) {
558 output := caddyfile.Format(input)
559
560 if fl.Bool("overwrite") {
562 - if err := ioutil.WriteFile(formatCmdConfigFile, output, 0600); err != nil {
561 + if err := os.WriteFile(formatCmdConfigFile, output, 0600); err != nil {
562 return caddy.ExitCodeFailedStartup, nil
563 }
564 } else {
@@ -699,7 +698,7 @@ func apiRequest(adminAddr, method, uri string, headers http.Header, body io.Read
698
699 // if it didn't work, let the user know
700 if resp.StatusCode >= 400 {
702 - respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1024*10))
701 + respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1024*10))
702 if err != nil {
703 return fmt.Errorf("HTTP %d: reading error message: %v", resp.StatusCode, err)
704 }
cmd/main.go
+4 -5
@@ -20,7 +20,6 @@ import (
20 "flag"
21 "fmt"
22 "io"
23 - "io/ioutil"
23 "log"
24 "net"
25 "os"
@@ -94,7 +93,7 @@ func Main() {
93 // the bytes in expect, or returns an error if it doesn't.
94 func handlePingbackConn(conn net.Conn, expect []byte) error {
95 defer conn.Close()
97 - confirmationBytes, err := ioutil.ReadAll(io.LimitReader(conn, 32))
96 + confirmationBytes, err := io.ReadAll(io.LimitReader(conn, 32))
97 if err != nil {
98 return err
99 }
@@ -124,9 +123,9 @@ func loadConfig(configFile, adapterName string) ([]byte, string, error) {
123 var err error
124 if configFile != "" {
125 if configFile == "-" {
127 - config, err = ioutil.ReadAll(os.Stdin)
126 + config, err = io.ReadAll(os.Stdin)
127 } else {
129 - config, err = ioutil.ReadFile(configFile)
128 + config, err = os.ReadFile(configFile)
129 }
130 if err != nil {
131 return nil, "", fmt.Errorf("reading config file: %v", err)
@@ -140,7 +139,7 @@ func loadConfig(configFile, adapterName string) ([]byte, string, error) {
139 // plugged in, and if so, try using a default Caddyfile
140 cfgAdapter = caddyconfig.GetAdapter("caddyfile")
141 if cfgAdapter != nil {
143 - config, err = ioutil.ReadFile("Caddyfile")
142 + config, err = os.ReadFile("Caddyfile")
143 if os.IsNotExist(err) {
144 // okay, no default Caddyfile; pretend like this never happened
145 cfgAdapter = nil
logging.go
+2 -3
@@ -18,7 +18,6 @@ import (
18 "encoding/json"
19 "fmt"
20 "io"
21 - "io/ioutil"
21 "log"
22 "os"
23 "strings"
@@ -630,9 +629,9 @@ func (StderrWriter) OpenWriter() (io.WriteCloser, error) {
629 return notClosable{os.Stderr}, nil
630 }
631
633 -// OpenWriter returns ioutil.Discard that can't be closed.
632 +// OpenWriter returns io.Discard that can't be closed.
633 func (DiscardWriter) OpenWriter() (io.WriteCloser, error) {
635 - return notClosable{ioutil.Discard}, nil
634 + return notClosable{io.Discard}, nil
635 }
636
637 // notClosable is an io.WriteCloser that can't be closed.
modules/caddyhttp/replacer.go
+1 -2
@@ -28,7 +28,6 @@ import (
28 "encoding/pem"
29 "fmt"
30 "io"
31 - "io/ioutil"
31 "net"
32 "net/http"
33 "net/textproto"
@@ -162,7 +161,7 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
161 return "", true
162 }
163 // replace real body with buffered data
165 - req.Body = ioutil.NopCloser(buf)
164 + req.Body = io.NopCloser(buf)
165 return buf.String(), true
166
167 // original request, before any internal changes
modules/caddyhttp/reverseproxy/fastcgi/client.go
+1 -2
@@ -30,7 +30,6 @@ import (
30 "encoding/binary"
31 "errors"
32 "io"
33 - "io/ioutil"
33 "mime/multipart"
34 "net"
35 "net/http"
@@ -445,7 +444,7 @@ func (c *FCGIClient) Request(p map[string]string, req io.Reader) (resp *http.Res
444 if chunked(resp.TransferEncoding) {
445 resp.Body = clientCloser{c, httputil.NewChunkedReader(rb)}
446 } else {
448 - resp.Body = clientCloser{c, ioutil.NopCloser(rb)}
447 + resp.Body = clientCloser{c, io.NopCloser(rb)}
448 }
449 return
450 }
modules/caddyhttp/reverseproxy/fastcgi/client_test.go
+1 -2
@@ -26,7 +26,6 @@ import (
26 "encoding/binary"
27 "fmt"
28 "io"
29 - "io/ioutil"
29 "log"
30 "math/rand"
31 "net"
@@ -166,7 +165,7 @@ func sendFcgi(reqType int, fcgiParams map[string]string, data []byte, posts map[
165 }
166
167 defer resp.Body.Close()
169 - content, _ = ioutil.ReadAll(resp.Body)
168 + content, _ = io.ReadAll(resp.Body)
169
170 log.Println("c: send data length ≈", length, string(content))
171 fcgi.Close()
modules/caddyhttp/reverseproxy/healthchecks.go
+2 -3
@@ -18,7 +18,6 @@ import (
18 "context"
19 "fmt"
20 "io"
21 - "io/ioutil"
21 "log"
22 "net"
23 "net/http"
@@ -282,7 +281,7 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, host H
281 }
282 defer func() {
283 // drain any remaining body so connection could be re-used
285 - _, _ = io.Copy(ioutil.Discard, body)
284 + _, _ = io.Copy(io.Discard, body)
285 resp.Body.Close()
286 }()
287
@@ -313,7 +312,7 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, host H
312
313 // if body does not match regex, mark down
314 if h.HealthChecks.Active.bodyRegexp != nil {
316 - bodyBytes, err := ioutil.ReadAll(body)
315 + bodyBytes, err := io.ReadAll(body)
316 if err != nil {
317 h.HealthChecks.Active.logger.Info("failed to read response body",
318 zap.String("host", hostAddr),
modules/caddyhttp/reverseproxy/httptransport.go
+2 -2
@@ -20,10 +20,10 @@ import (
20 "crypto/x509"
21 "encoding/base64"
22 "fmt"
23 - "io/ioutil"
23 weakrand "math/rand"
24 "net"
25 "net/http"
26 + "os"
27 "reflect"
28 "time"
29
@@ -364,7 +364,7 @@ func (t TLSConfig) MakeTLSClientConfig(ctx caddy.Context) (*tls.Config, error) {
364 rootPool.AddCert(caCert)
365 }
366 for _, pemFile := range t.RootCAPEMFiles {
367 - pemData, err := ioutil.ReadFile(pemFile)
367 + pemData, err := os.ReadFile(pemFile)
368 if err != nil {
369 return nil, fmt.Errorf("failed reading ca cert: %v", err)
370 }
modules/caddyhttp/staticresp_test.go
+2 -2
@@ -16,7 +16,7 @@ package caddyhttp
16
17 import (
18 "context"
19 - "io/ioutil"
19 + "io"
20 "net/http"
21 "net/http/httptest"
22 "strconv"
@@ -44,7 +44,7 @@ func TestStaticResponseHandler(t *testing.T) {
44 }
45
46 resp := w.Result()
47 - respBody, _ := ioutil.ReadAll(resp.Body)
47 + respBody, _ := io.ReadAll(resp.Body)
48
49 if resp.StatusCode != http.StatusNotFound {
50 t.Errorf("expected status %d but got %d", http.StatusNotFound, resp.StatusCode)
modules/caddyhttp/templates/tplcontext_test.go
+4 -5
@@ -17,7 +17,6 @@ package templates
17 import (
18 "bytes"
19 "fmt"
20 - "io/ioutil"
20 "net/http"
21 "os"
22 "path/filepath"
@@ -118,7 +117,7 @@ func TestImport(t *testing.T) {
117 // create files for test case
118 if test.fileName != "" {
119 absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName)
121 - if err := ioutil.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
120 + if err := os.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
121 os.Remove(absFilePath)
122 t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
123 }
@@ -198,7 +197,7 @@ func TestInclude(t *testing.T) {
197 // create files for test case
198 if test.fileName != "" {
199 absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName)
201 - if err := ioutil.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
200 + if err := os.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
201 os.Remove(absFilePath)
202 t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
203 }
@@ -357,13 +356,13 @@ func TestFileListing(t *testing.T) {
356
357 // create files for test case
358 if test.fileNames != nil {
360 - dirPath, err = ioutil.TempDir(fmt.Sprintf("%s", context.Root), "caddy_ctxtest")
359 + dirPath, err = os.MkdirTemp(fmt.Sprintf("%s", context.Root), "caddy_ctxtest")
360 if err != nil {
361 t.Fatalf("Test %d: Expected no error creating directory, got: '%s'", i, err.Error())
362 }
363 for _, name := range test.fileNames {
364 absFilePath := filepath.Join(dirPath, name)
366 - if err = ioutil.WriteFile(absFilePath, []byte(""), os.ModePerm); err != nil {
365 + if err = os.WriteFile(absFilePath, []byte(""), os.ModePerm); err != nil {
366 os.RemoveAll(dirPath)
367 t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
368 }
modules/caddypki/crypto.go
+3 -3
@@ -23,7 +23,7 @@ import (
23 "crypto/x509"
24 "encoding/pem"
25 "fmt"
26 - "io/ioutil"
26 + "os"
27 "strings"
28 )
29
@@ -137,11 +137,11 @@ type KeyPair struct {
137 func (kp KeyPair) Load() (*x509.Certificate, interface{}, error) {
138 switch kp.Format {
139 case "", "pem_file":
140 - certData, err := ioutil.ReadFile(kp.Certificate)
140 + certData, err := os.ReadFile(kp.Certificate)
141 if err != nil {
142 return nil, nil, err
143 }
144 - keyData, err := ioutil.ReadFile(kp.PrivateKey)
144 + keyData, err := os.ReadFile(kp.PrivateKey)
145 if err != nil {
146 return nil, nil, err
147 }
modules/caddytls/acmeissuer.go
+2 -2
@@ -18,8 +18,8 @@ import (
18 "context"
19 "crypto/x509"
20 "fmt"
21 - "io/ioutil"
21 "net/url"
22 + "os"
23 "strconv"
24 "time"
25
@@ -152,7 +152,7 @@ func (iss *ACMEIssuer) Provision(ctx caddy.Context) error {
152 if len(iss.TrustedRootsPEMFiles) > 0 {
153 iss.rootPool = x509.NewCertPool()
154 for _, pemFile := range iss.TrustedRootsPEMFiles {
155 - pemData, err := ioutil.ReadFile(pemFile)
155 + pemData, err := os.ReadFile(pemFile)
156 if err != nil {
157 return fmt.Errorf("loading trusted root CA's PEM file: %s: %v", pemFile, err)
158 }
modules/caddytls/connpolicy.go
+2 -2
@@ -19,7 +19,7 @@ import (
19 "crypto/x509"
20 "encoding/base64"
21 "fmt"
22 - "io/ioutil"
22 + "os"
23 "strings"
24
25 "github.com/caddyserver/caddy/v2"
@@ -369,7 +369,7 @@ func (clientauth *ClientAuthentication) ConfigureTLSConfig(cfg *tls.Config) erro
369 caPool.AddCert(clientCA)
370 }
371 for _, pemFile := range clientauth.TrustedCACertPEMFiles {
372 - pemContents, err := ioutil.ReadFile(pemFile)
372 + pemContents, err := os.ReadFile(pemFile)
373 if err != nil {
374 return fmt.Errorf("reading %s: %v", pemFile, err)
375 }
modules/caddytls/fileloader.go
+3 -3
@@ -17,7 +17,7 @@ package caddytls
17 import (
18 "crypto/tls"
19 "fmt"
20 - "io/ioutil"
20 + "os"
21
22 "github.com/caddyserver/caddy/v2"
23 )
@@ -59,11 +59,11 @@ type CertKeyFilePair struct {
59 func (fl FileLoader) LoadCertificates() ([]Certificate, error) {
60 certs := make([]Certificate, 0, len(fl))
61 for _, pair := range fl {
62 - certData, err := ioutil.ReadFile(pair.Certificate)
62 + certData, err := os.ReadFile(pair.Certificate)
63 if err != nil {
64 return nil, err
65 }
66 - keyData, err := ioutil.ReadFile(pair.Key)
66 + keyData, err := os.ReadFile(pair.Key)
67 if err != nil {
68 return nil, err
69 }
modules/caddytls/folderloader.go
+1 -2
@@ -19,7 +19,6 @@ import (
19 "crypto/tls"
20 "encoding/pem"
21 "fmt"
22 - "io/ioutil"
22 "os"
23 "path/filepath"
24 "strings"
@@ -79,7 +78,7 @@ func (fl FolderLoader) LoadCertificates() ([]Certificate, error) {
78 }
79
80 func x509CertFromCertAndKeyPEMFile(fpath string) (tls.Certificate, error) {
82 - bundle, err := ioutil.ReadFile(fpath)
81 + bundle, err := os.ReadFile(fpath)
82 if err != nil {
83 return tls.Certificate{}, err
84 }