Skip to content

Commit 499628b

Browse files
committed
Refactored header setting logic for HuggingFace and file info responses
1 parent 607ac8a commit 499628b

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

mirror.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,26 +209,15 @@ func (m *MirrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
209209
}
210210

211211
func (m *MirrorHandler) responseCache(rw http.ResponseWriter, r *http.Request, file string, info sss.FileInfo) {
212+
m.setHuggingFaceHeaders(rw, r)
212213
if m.NoRedirect {
213214
m.serveFromCache(rw, r, file, info)
214215
} else {
215216
m.redirect(rw, r, file, info)
216217
}
217218
}
218219

219-
func (m *MirrorHandler) redirect(rw http.ResponseWriter, r *http.Request, file string, info sss.FileInfo) {
220-
expires := m.LinkExpires
221-
var url string
222-
var err error
223-
224-
if sys := info.Sys(); sys != nil {
225-
if resp, ok := sys.(sss.FileInfoExpansion); ok {
226-
if etag := resp.ETag; etag != nil && *etag != "" {
227-
rw.Header().Set("Etag", *etag)
228-
}
229-
}
230-
}
231-
220+
func (m *MirrorHandler) setHuggingFaceHeaders(rw http.ResponseWriter, r *http.Request) {
232221
// Special handling for huggingface.co to add X-Repo-Commit header with HF_ENDPOINT
233222
if r.Host == "huggingface.co" {
234223
sl := strings.SplitN(r.URL.Path, "/", 6)
@@ -238,6 +227,22 @@ func (m *MirrorHandler) redirect(rw http.ResponseWriter, r *http.Request, file s
238227
rw.Header().Set("X-Repo-Commit", sl[4])
239228
}
240229
}
230+
}
231+
232+
func (m *MirrorHandler) setHeaders(rw http.ResponseWriter, info sss.FileInfo) {
233+
if sys := info.Sys(); sys != nil {
234+
if resp, ok := sys.(sss.FileInfoExpansion); ok {
235+
if etag := resp.ETag; etag != nil && *etag != "" {
236+
rw.Header().Set("Etag", *etag)
237+
}
238+
}
239+
}
240+
}
241+
242+
func (m *MirrorHandler) redirect(rw http.ResponseWriter, r *http.Request, file string, info sss.FileInfo) {
243+
expires := m.LinkExpires
244+
var url string
245+
var err error
241246

242247
if r.Method == http.MethodHead {
243248
if info == nil {
@@ -249,6 +254,7 @@ func (m *MirrorHandler) redirect(rw http.ResponseWriter, r *http.Request, file s
249254
}
250255
}
251256
if info != nil {
257+
m.setHeaders(rw, info)
252258
rw.Header().Set("Content-Type", "application/octet-stream")
253259
rw.Header().Set("Content-Length", fmt.Sprint(info.Size()))
254260
rw.Header().Set("Last-Modified", info.ModTime().Format(http.TimeFormat))
@@ -281,6 +287,8 @@ func (m *MirrorHandler) redirect(rw http.ResponseWriter, r *http.Request, file s
281287
// It reads the file from RemoteCache and streams it to the client.
282288
func (m *MirrorHandler) serveFromCache(rw http.ResponseWriter, r *http.Request, file string, info sss.FileInfo) {
283289
ctx := r.Context()
290+
m.setHeaders(rw, info)
291+
284292
if r.Method == http.MethodHead {
285293
// Get file info if not already provided
286294
if info == nil {

0 commit comments

Comments
 (0)