forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
140 lines (111 loc) · 2.92 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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",
"Accept-Ranges",
}
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)
}
req, err := imagedata.BuildImageRequest(imageURL, imgRequestHeader, cookieJar)
checkErr(ctx, "streaming", err)
res, err := imagedata.SendRequest(req)
checkErr(ctx, "streaming", err)
defer res.Body.Close()
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 {
var filename, ext, mimetype string
_, filename = filepath.Split(req.URL.Path)
ext = filepath.Ext(filename)
if len(po.Filename) > 0 {
filename = po.Filename
} else {
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]
}
}
rw.Header().Set("Content-Disposition", imagetype.ContentDisposition(filename, ext, po.ReturnAttachment))
}
setCacheControl(rw, map[string]string{
"Cache-Control": rw.Header().Get("Cache-Control"),
"Expires": rw.Header().Get("Expires"),
})
setCanonical(rw, imageURL)
rw.WriteHeader(res.StatusCode)
buf := streamBufPool.Get().(*[]byte)
defer streamBufPool.Put(buf)
_, copyerr := io.CopyBuffer(rw, res.Body, *buf)
router.LogResponse(
reqID, r, res.StatusCode, nil,
log.Fields{
"image_url": imageURL,
"processing_options": po,
},
)
if copyerr != nil {
panic(http.ErrAbortHandler)
}
}