2022-09-07 12:50:21 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/imgproxy/imgproxy/v3/config"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/cookies"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/imagedata"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/imagetype"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/metrics"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/metrics/stats"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/options"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
streamReqHeaders = []string{
|
|
|
|
"If-None-Match",
|
|
|
|
"Accept-Encoding",
|
|
|
|
"Range",
|
|
|
|
}
|
|
|
|
|
|
|
|
streamRespHeaders = []string{
|
|
|
|
"Cache-Control",
|
|
|
|
"Expires",
|
|
|
|
"ETag",
|
|
|
|
"Content-Type",
|
|
|
|
"Content-Encoding",
|
|
|
|
"Content-Range",
|
2022-09-07 13:54:14 +02:00
|
|
|
"Accept-Ranges",
|
2023-05-03 17:21:46 +02:00
|
|
|
"Last-Modified",
|
2022-09-07 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
streamBufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
return &buf
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func streamOriginImage(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter, po *options.ProcessingOptions, imageURL string) {
|
|
|
|
stats.IncImagesInProgress()
|
|
|
|
defer stats.DecImagesInProgress()
|
|
|
|
|
|
|
|
defer metrics.StartStreamingSegment(ctx)()
|
|
|
|
|
|
|
|
var (
|
|
|
|
cookieJar *cookiejar.Jar
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
imgRequestHeader := make(http.Header)
|
|
|
|
|
|
|
|
for _, k := range streamReqHeaders {
|
|
|
|
if v := r.Header.Get(k); len(v) != 0 {
|
|
|
|
imgRequestHeader.Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.CookiePassthrough {
|
|
|
|
cookieJar, err = cookies.JarFromRequest(r)
|
|
|
|
checkErr(ctx, "streaming", err)
|
|
|
|
}
|
|
|
|
|
2023-03-21 19:58:16 +02:00
|
|
|
req, reqCancel, err := imagedata.BuildImageRequest(r.Context(), imageURL, imgRequestHeader, cookieJar)
|
|
|
|
defer reqCancel()
|
2022-09-07 12:50:21 +02:00
|
|
|
checkErr(ctx, "streaming", err)
|
|
|
|
|
|
|
|
res, err := imagedata.SendRequest(req)
|
2023-03-21 19:58:16 +02:00
|
|
|
if res != nil {
|
|
|
|
defer res.Body.Close()
|
|
|
|
}
|
2022-09-07 12:50:21 +02:00
|
|
|
checkErr(ctx, "streaming", err)
|
|
|
|
|
|
|
|
for _, k := range streamRespHeaders {
|
|
|
|
vv := res.Header.Values(k)
|
|
|
|
for _, v := range vv {
|
|
|
|
rw.Header().Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.ContentLength >= 0 {
|
|
|
|
rw.Header().Set("Content-Length", strconv.Itoa(int(res.ContentLength)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode < 300 {
|
2022-10-03 12:01:17 +02:00
|
|
|
var filename, ext, mimetype string
|
2022-09-07 12:50:21 +02:00
|
|
|
|
2022-10-03 12:01:17 +02:00
|
|
|
_, filename = filepath.Split(req.URL.Path)
|
|
|
|
ext = filepath.Ext(filename)
|
2022-09-07 12:50:21 +02:00
|
|
|
|
|
|
|
if len(po.Filename) > 0 {
|
2022-10-03 12:01:17 +02:00
|
|
|
filename = po.Filename
|
2022-09-07 12:50:21 +02:00
|
|
|
} else {
|
2022-10-03 12:01:17 +02:00
|
|
|
filename = filename[:len(filename)-len(ext)]
|
|
|
|
}
|
|
|
|
|
|
|
|
mimetype = rw.Header().Get("Content-Type")
|
|
|
|
|
|
|
|
if len(ext) == 0 && len(mimetype) > 0 {
|
|
|
|
if exts, err := mime.ExtensionsByType(mimetype); err == nil && len(exts) != 0 {
|
|
|
|
ext = exts[0]
|
|
|
|
}
|
2022-09-07 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 12:01:17 +02:00
|
|
|
rw.Header().Set("Content-Disposition", imagetype.ContentDisposition(filename, ext, po.ReturnAttachment))
|
2022-09-07 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
2023-02-24 18:44:03 +02:00
|
|
|
setCacheControl(rw, po.Expires, map[string]string{
|
2022-09-07 12:50:21 +02:00
|
|
|
"Cache-Control": rw.Header().Get("Cache-Control"),
|
|
|
|
"Expires": rw.Header().Get("Expires"),
|
|
|
|
})
|
|
|
|
setCanonical(rw, imageURL)
|
2023-02-25 17:58:44 +02:00
|
|
|
rw.Header().Set("Content-Security-Policy", "script-src 'none'")
|
2022-09-07 12:50:21 +02:00
|
|
|
|
|
|
|
rw.WriteHeader(res.StatusCode)
|
|
|
|
|
|
|
|
buf := streamBufPool.Get().(*[]byte)
|
|
|
|
defer streamBufPool.Put(buf)
|
|
|
|
|
2022-09-07 13:54:34 +02:00
|
|
|
_, copyerr := io.CopyBuffer(rw, res.Body, *buf)
|
2022-09-07 12:50:21 +02:00
|
|
|
|
|
|
|
router.LogResponse(
|
|
|
|
reqID, r, res.StatusCode, nil,
|
|
|
|
log.Fields{
|
|
|
|
"image_url": imageURL,
|
|
|
|
"processing_options": po,
|
|
|
|
},
|
|
|
|
)
|
2022-09-07 13:54:34 +02:00
|
|
|
|
|
|
|
if copyerr != nil {
|
|
|
|
panic(http.ErrAbortHandler)
|
|
|
|
}
|
2022-09-07 12:50:21 +02:00
|
|
|
}
|