@@ -81,6 +81,7 @@ func (p *Proxy) handleRequest(req *http.Request, ctx *goproxy.ProxyCtx) (*http.R
81
81
p .logger .Error ("failed to get new cache item" , "error" , err , "url" , req .URL , "method" , req .Method )
82
82
return req , nil
83
83
}
84
+ ctx .UserData = cacheItem
84
85
85
86
exists , err := cacheItem .Exists ()
86
87
if err != nil {
@@ -116,9 +117,11 @@ func (p *Proxy) handleResponse(resp *http.Response, ctx *goproxy.ProxyCtx) *http
116
117
}
117
118
req := resp .Request
118
119
119
- cacheItem , err := cache .NewItem (p .cacheDir (), req )
120
- if err != nil {
121
- p .logger .Error ("failed to get cache item" , "error" , err , "url" , req .URL , "method" , req .Method )
120
+ var cacheItem * cache.Item
121
+ if i , ok := ctx .UserData .(* cache.Item ); ok && i != nil {
122
+ cacheItem = i
123
+ } else {
124
+ p .logger .Error ("missing cache item in response handler user data" , "url" , req .URL )
122
125
return resp
123
126
}
124
127
@@ -142,7 +145,7 @@ func (p *Proxy) handleResponse(resp *http.Response, ctx *goproxy.ProxyCtx) *http
142
145
}
143
146
144
147
if resp .StatusCode == http .StatusPartialContent {
145
- go p .download (req )
148
+ go p .download (req , cacheItem )
146
149
return resp
147
150
}
148
151
@@ -165,11 +168,8 @@ func (p *Proxy) handleResponse(resp *http.Response, ctx *goproxy.ProxyCtx) *http
165
168
return clonedResponse
166
169
}
167
170
168
- func (p * Proxy ) download (req * http.Request ) {
169
- tempCacheItem , err := cache .NewItem (p .tempDir (), req )
170
- if err != nil {
171
- p .logger .Error ("download failed" , "error" , err , "url" , req .URL , "method" , req .Method )
172
- }
171
+ func (p * Proxy ) download (req * http.Request , cacheItem * cache.Item ) {
172
+ tempCacheItem := cacheItem .Rebase (p .tempDir ())
173
173
174
174
cacheKey := tempCacheItem .Key ()
175
175
if _ , alreadyDownloading := p .downloading .LoadOrStore (cacheKey , true ); alreadyDownloading {
@@ -181,7 +181,7 @@ func (p *Proxy) download(req *http.Request) {
181
181
clonedRequest := req .Clone (context .Background ())
182
182
clonedRequest .Header .Del ("Range" )
183
183
184
- p .logger .Debug ("starting download" , "url" , clonedRequest .URL , "method" , req .Method )
184
+ p .logger .Info ("starting download" , "url" , clonedRequest .URL , "method" , req .Method )
185
185
resp , err := http .DefaultClient .Do (clonedRequest )
186
186
if err != nil {
187
187
p .logger .Error ("download request failed" , "error" , err , "url" , clonedRequest .URL , "method" , req .Method )
@@ -202,7 +202,7 @@ func (p *Proxy) download(req *http.Request) {
202
202
if _ , err := tempCacheItem .Move (p .cacheDir ()); err != nil {
203
203
p .logger .Error ("failed to rename download" , "error" , err , "url" , clonedRequest .URL , "method" , req .Method )
204
204
}
205
- p .logger .Debug ("download complete" , "url" , clonedRequest .URL , "method" , req .Method )
205
+ p .logger .Info ("download complete" , "url" , clonedRequest .URL , "method" , req .Method )
206
206
}
207
207
208
208
func hostMatches (u * url.URL , hosts []string ) bool {
0 commit comments