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.
This commit is contained in:
Peter De Wachter 2019-08-14 17:54:02 +02:00 committed by Frédéric Guillot
parent b94160df72
commit 937492f6f5
2 changed files with 25 additions and 9 deletions

View File

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

View File

@ -7,10 +7,10 @@ package ui // import "miniflux.app/ui"
import ( import (
"encoding/base64" "encoding/base64"
"errors" "errors"
"io/ioutil"
"net/http" "net/http"
"time" "time"
"miniflux.app/config"
"miniflux.app/crypto" "miniflux.app/crypto"
"miniflux.app/http/client" "miniflux.app/http/client"
"miniflux.app/http/request" "miniflux.app/http/request"
@ -37,24 +37,35 @@ func (h *handler) imageProxy(w http.ResponseWriter, r *http.Request) {
return return
} }
clt := client.New(string(decodedURL)) req, err := http.NewRequest("GET", string(decodedURL), nil)
resp, err := clt.Get()
if err != nil { if err != nil {
html.ServerError(w, r, err) html.ServerError(w, r, err)
return 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) html.NotFound(w, r)
return return
} }
body, _ := ioutil.ReadAll(resp.Body) etag := crypto.HashFromBytes(decodedURL)
etag := crypto.HashFromBytes(body)
response.New(w ,r).WithCaching(etag, 72*time.Hour, func(b *response.Builder) { response.New(w, r).WithCaching(etag, 72*time.Hour, func(b *response.Builder) {
b.WithHeader("Content-Type", resp.ContentType) b.WithHeader("Content-Type", resp.Header.Get("Content-Type"))
b.WithBody(body) b.WithBody(resp.Body)
b.WithoutCompression() b.WithoutCompression()
b.Write() b.Write()
}) })