1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-26 18:21:01 +00:00

refactor(ui): use request builder in media proxy handler

builder is used seemingly everywhere but media proxy uses manual
transport construction
This commit is contained in:
gudvinr 2025-08-15 19:28:26 +03:00 committed by Frédéric Guillot
parent a16ac51326
commit 905d652511

View file

@ -20,6 +20,7 @@ import (
"miniflux.app/v2/internal/http/request" "miniflux.app/v2/internal/http/request"
"miniflux.app/v2/internal/http/response" "miniflux.app/v2/internal/http/response"
"miniflux.app/v2/internal/http/response/html" "miniflux.app/v2/internal/http/response/html"
"miniflux.app/v2/internal/reader/fetcher"
"miniflux.app/v2/internal/reader/rewrite" "miniflux.app/v2/internal/reader/rewrite"
) )
@ -84,33 +85,21 @@ func (h *handler) mediaProxy(w http.ResponseWriter, r *http.Request) {
slog.String("media_url", mediaURL), slog.String("media_url", mediaURL),
) )
req, err := http.NewRequest("GET", mediaURL, nil) requestBuilder := fetcher.NewRequestBuilder()
if err != nil { requestBuilder.WithTimeout(config.Opts.MediaProxyHTTPClientTimeout())
html.ServerError(w, r, err)
return
}
req.Header.Set("Connection", "close")
if referer := rewrite.GetRefererForURL(mediaURL); referer != "" { if referer := rewrite.GetRefererForURL(mediaURL); referer != "" {
req.Header.Set("Referer", referer) requestBuilder.WithHeader("Referer", referer)
} }
forwardedRequestHeader := [...]string{"Range", "Accept", "Accept-Encoding", "User-Agent"} forwardedRequestHeader := [...]string{"Range", "Accept", "Accept-Encoding", "User-Agent"}
for _, requestHeaderName := range forwardedRequestHeader { for _, requestHeaderName := range forwardedRequestHeader {
if r.Header.Get(requestHeaderName) != "" { if r.Header.Get(requestHeaderName) != "" {
req.Header.Set(requestHeaderName, r.Header.Get(requestHeaderName)) requestBuilder.WithHeader(requestHeaderName, r.Header.Get(requestHeaderName))
} }
} }
clt := &http.Client{ resp, err := requestBuilder.ExecuteRequest(mediaURL)
Transport: &http.Transport{
IdleConnTimeout: time.Duration(config.Opts.MediaProxyHTTPClientTimeout()) * time.Second,
},
Timeout: time.Duration(config.Opts.MediaProxyHTTPClientTimeout()) * time.Second,
}
resp, err := clt.Do(req)
if err != nil { if err != nil {
slog.Error("MediaProxy: Unable to initialize HTTP client", slog.Error("MediaProxy: Unable to initialize HTTP client",
slog.String("media_url", mediaURL), slog.String("media_url", mediaURL),