diff options
Diffstat (limited to '')
-rw-r--r-- | ui/proxy.go | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/ui/proxy.go b/ui/proxy.go index 6f43086b..5dd50697 100644 --- a/ui/proxy.go +++ b/ui/proxy.go @@ -20,8 +20,8 @@ import ( "miniflux.app/logger" ) -func (h *handler) imageProxy(w http.ResponseWriter, r *http.Request) { - // If we receive a "If-None-Match" header, we assume the image is already stored in browser cache. +func (h *handler) mediaProxy(w http.ResponseWriter, r *http.Request) { + // If we receive a "If-None-Match" header, we assume the media is already stored in browser cache. if r.Header.Get("If-None-Match") != "" { w.WriteHeader(http.StatusNotModified) return @@ -55,10 +55,10 @@ func (h *handler) imageProxy(w http.ResponseWriter, r *http.Request) { return } - imageURL := string(decodedURL) - logger.Debug(`[Proxy] Fetching %q`, imageURL) + mediaURL := string(decodedURL) + logger.Debug(`[Proxy] Fetching %q`, mediaURL) - req, err := http.NewRequest("GET", imageURL, nil) + req, err := http.NewRequest("GET", mediaURL, nil) if err != nil { html.ServerError(w, r, err) return @@ -67,8 +67,18 @@ func (h *handler) imageProxy(w http.ResponseWriter, r *http.Request) { // Note: User-Agent HTTP header is omitted to avoid being blocked by bot protection mechanisms. req.Header.Add("Connection", "close") + forwardedRequestHeader := []string{"Range", "Accept", "Accept-Encoding"} + for _, requestHeaderName := range forwardedRequestHeader { + if r.Header.Get(requestHeaderName) != "" { + req.Header.Add(requestHeaderName, r.Header.Get(requestHeaderName)) + } + } + clt := &http.Client{ - Timeout: time.Duration(config.Opts.HTTPClientTimeout()) * time.Second, + Transport: &http.Transport{ + IdleConnTimeout: time.Duration(config.Opts.ProxyHTTPClientTimeout()) * time.Second, + }, + Timeout: time.Duration(config.Opts.ProxyHTTPClientTimeout()) * time.Second, } resp, err := clt.Do(req) @@ -78,8 +88,13 @@ func (h *handler) imageProxy(w http.ResponseWriter, r *http.Request) { } defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - logger.Error(`[Proxy] Status Code is %d for URL %q`, resp.StatusCode, imageURL) + if resp.StatusCode == http.StatusRequestedRangeNotSatisfiable { + logger.Error(`[Proxy] Status Code is %d for URL %q`, resp.StatusCode, mediaURL) + html.RequestedRangeNotSatisfiable(w, r, resp.Header.Get("Content-Range")) + return + } + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent { + logger.Error(`[Proxy] Status Code is %d for URL %q`, resp.StatusCode, mediaURL) html.NotFound(w, r) return } @@ -87,8 +102,15 @@ func (h *handler) imageProxy(w http.ResponseWriter, r *http.Request) { etag := crypto.HashFromBytes(decodedURL) response.New(w, r).WithCaching(etag, 72*time.Hour, func(b *response.Builder) { + b.WithStatus(resp.StatusCode) b.WithHeader("Content-Security-Policy", `default-src 'self'`) b.WithHeader("Content-Type", resp.Header.Get("Content-Type")) + forwardedResponseHeader := []string{"Content-Encoding", "Content-Type", "Content-Length", "Accept-Ranges", "Content-Range"} + for _, responseHeaderName := range forwardedResponseHeader { + if resp.Header.Get(responseHeaderName) != "" { + b.WithHeader(responseHeaderName, resp.Header.Get(responseHeaderName)) + } + } b.WithBody(resp.Body) b.WithoutCompression() b.Write() |