Przeglądaj źródła

fix(server): add timeouts to ACME challenge listener

Fred 2 dni temu
rodzic
commit
c317f84bc3
2 zmienionych plików z 47 dodań i 12 usunięć
  1. 15 12
      internal/http/server/server.go
  2. 32 0
      internal/http/server/server_test.go

+ 15 - 12
internal/http/server/server.go

@@ -60,14 +60,7 @@ func StartWebServer(store *storage.Storage, pool *worker.Pool) ([]*http.Server,
 	}
 
 	for _, t := range targets {
-		srv := &http.Server{
-			Addr:              t.address,
-			ReadTimeout:       config.Opts.HTTPServerTimeout(),
-			WriteTimeout:      config.Opts.HTTPServerTimeout(),
-			IdleTimeout:       config.Opts.HTTPServerTimeout(),
-			ReadHeaderTimeout: config.Opts.HTTPServerTimeout(),
-			Handler:           newRouter(store, pool),
-		}
+		srv := newHTTPServer(t.address, newRouter(store, pool))
 
 		switch t.mode {
 		case modeSystemd:
@@ -158,6 +151,19 @@ func anyTLS(targets []listenTarget) bool {
 	return false
 }
 
+func newHTTPServer(address string, handler http.Handler) *http.Server {
+	timeout := config.Opts.HTTPServerTimeout()
+
+	return &http.Server{
+		Addr:              address,
+		ReadTimeout:       timeout,
+		WriteTimeout:      timeout,
+		IdleTimeout:       timeout,
+		ReadHeaderTimeout: timeout,
+		Handler:           handler,
+	}
+}
+
 func setupAutocert(store *storage.Storage) (*tls.Config, *http.Server) {
 	certDomain := config.Opts.CertDomain()
 	if certDomain == "" {
@@ -176,10 +182,7 @@ func setupAutocert(store *storage.Storage) (*tls.Config, *http.Server) {
 	}
 	tlsConfig.GetCertificate = certManager.GetCertificate
 
-	challengeServer := &http.Server{
-		Handler: certManager.HTTPHandler(nil),
-		Addr:    ":http",
-	}
+	challengeServer := newHTTPServer(":http", certManager.HTTPHandler(nil))
 
 	slog.Info("Starting ACME HTTP challenge server", slog.String("address", challengeServer.Addr))
 	go func() {

+ 32 - 0
internal/http/server/server_test.go

@@ -4,9 +4,13 @@
 package server
 
 import (
+	"net/http"
 	"os"
 	"runtime"
 	"testing"
+	"time"
+
+	"miniflux.app/v2/internal/config"
 )
 
 func TestDetermineListenTargets(t *testing.T) {
@@ -190,6 +194,34 @@ func TestAnyTLS(t *testing.T) {
 	}
 }
 
+func TestNewHTTPServerConfiguresTimeouts(t *testing.T) {
+	previousOptions := config.Opts
+	config.Opts = config.NewConfigOptions()
+	t.Cleanup(func() { config.Opts = previousOptions })
+
+	handler := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
+	server := newHTTPServer(":8080", handler)
+	timeout := config.Opts.HTTPServerTimeout()
+
+	if server.Addr != ":8080" {
+		t.Errorf("server address = %q, want %q", server.Addr, ":8080")
+	}
+	if server.Handler == nil {
+		t.Error("server handler must not be nil")
+	}
+
+	for name, actual := range map[string]time.Duration{
+		"ReadTimeout":       server.ReadTimeout,
+		"WriteTimeout":      server.WriteTimeout,
+		"IdleTimeout":       server.IdleTimeout,
+		"ReadHeaderTimeout": server.ReadHeaderTimeout,
+	} {
+		if actual != timeout {
+			t.Errorf("%s = %v, want %v", name, actual, timeout)
+		}
+	}
+}
+
 func TestCreateUnixSocketListenerPermissions(t *testing.T) {
 	if runtime.GOOS == "windows" {
 		t.Skip("Unix sockets are not supported on Windows")