2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-11-12 19:23:39 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package httpd // import "miniflux.app/v2/internal/http/server"
|
2018-11-12 19:23:39 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-25 01:32:09 +02:00
|
|
|
"log/slog"
|
2018-11-12 19:23:39 +01:00
|
|
|
"net/http"
|
2023-09-25 01:32:09 +02:00
|
|
|
"time"
|
2018-11-12 19:23:39 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
"miniflux.app/v2/internal/http/request"
|
2018-11-12 19:23:39 +01:00
|
|
|
)
|
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
func middleware(next http.Handler) http.Handler {
|
2018-11-12 19:23:39 +01:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clientIP := request.FindClientIP(r)
|
|
|
|
ctx := r.Context()
|
|
|
|
ctx = context.WithValue(ctx, request.ClientIPContextKey, clientIP)
|
|
|
|
|
|
|
|
if r.Header.Get("X-Forwarded-Proto") == "https" {
|
2019-06-02 03:18:09 +02:00
|
|
|
config.Opts.HTTPS = true
|
2018-11-12 19:23:39 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:32:09 +02:00
|
|
|
t1 := time.Now()
|
|
|
|
defer func() {
|
|
|
|
slog.Debug("Incoming request",
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.Group("request",
|
|
|
|
slog.String("method", r.Method),
|
|
|
|
slog.String("uri", r.RequestURI),
|
|
|
|
slog.String("protocol", r.Proto),
|
|
|
|
slog.Duration("execution_time", time.Since(t1)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}()
|
2018-11-12 19:23:39 +01:00
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
if config.Opts.HTTPS && config.Opts.HasHSTS() {
|
2018-11-12 19:23:39 +01:00
|
|
|
w.Header().Set("Strict-Transport-Security", "max-age=31536000")
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|