Do not buffer responses in the image proxy

The image proxy buffered the whole image before sending it to the
browser. If the image is large and/or hosted on a slow server, this
caused a long delay before the user's browser could display anything.
pull/444/head
Peter De Wachter 5 years ago committed by Frédéric Guillot
parent b94160df72
commit 937492f6f5
  1. 5
      http/response/builder.go
  2. 29
      ui/proxy.go

@ -8,6 +8,7 @@ import (
"compress/flate"
"compress/gzip"
"fmt"
"io"
"net/http"
"strings"
"time"
@ -84,6 +85,10 @@ func (b *Builder) Write() {
b.compress([]byte(v))
case error:
b.compress([]byte(v.Error()))
case io.Reader:
// Compression not implemented in this case
b.writeHeaders()
io.Copy(b.w, v)
}
}

@ -7,10 +7,10 @@ package ui // import "miniflux.app/ui"
import (
"encoding/base64"
"errors"
"io/ioutil"
"net/http"
"time"
"miniflux.app/config"
"miniflux.app/crypto"
"miniflux.app/http/client"
"miniflux.app/http/request"
@ -37,24 +37,35 @@ func (h *handler) imageProxy(w http.ResponseWriter, r *http.Request) {
return
}
clt := client.New(string(decodedURL))
resp, err := clt.Get()
req, err := http.NewRequest("GET", string(decodedURL), nil)
if err != nil {
html.ServerError(w, r, err)
return
}
req.Header.Add("User-Agent", client.DefaultUserAgent)
req.Header.Add("Connection", "close")
if resp.HasServerFailure() {
clt := &http.Client{
Timeout: time.Duration(config.Opts.HTTPClientTimeout()) * time.Second,
}
resp, err := clt.Do(req)
if err != nil {
html.ServerError(w, r, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
html.NotFound(w, r)
return
}
body, _ := ioutil.ReadAll(resp.Body)
etag := crypto.HashFromBytes(body)
etag := crypto.HashFromBytes(decodedURL)
response.New(w ,r).WithCaching(etag, 72*time.Hour, func(b *response.Builder) {
b.WithHeader("Content-Type", resp.ContentType)
b.WithBody(body)
response.New(w, r).WithCaching(etag, 72*time.Hour, func(b *response.Builder) {
b.WithHeader("Content-Type", resp.Header.Get("Content-Type"))
b.WithBody(resp.Body)
b.WithoutCompression()
b.Write()
})

Loading…
Cancel
Save