just forward request and response

This commit is contained in:
Ronmi Ren 2025-01-07 22:13:25 +08:00
commit 6e98e4f5d6
2 changed files with 24 additions and 59 deletions

View file

@ -18,7 +18,8 @@ func Run(bind string, f *Forgejo) (*http.Server, error) {
return s, nil
}
func trySet(h http.Header, key, value string) {
func trySet(h http.Header, key string, src http.Header) {
value := src.Get(key)
if value == "" {
h.Del(key)
return
@ -44,26 +45,23 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) {
file = strings.Join(arr[2:], "/")
}
info, content, err := f.GetFile(r.Context(), user, repo, f.Branch, file)
headers := map[string]string{}
headers["If-None-Match"] = r.Header.Get("If-None-Match")
headers["If-Modified-Since"] = r.Header.Get("If-Modified-Since")
headers["If-Range"] = r.Header.Get("If-Range")
headers["Range"] = r.Header.Get("Range")
resp, err := f.GetFile(r.Context(), headers, user, repo, f.Branch, file)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
defer content.Close()
defer resp.Body.Close()
trySet(w.Header(), "Etag", info.ETag)
trySet(w.Header(), "Last-Modified", info.LastModified)
trySet(w.Header(), "Content-Length", info.Size)
trySet(w.Header(), "Etag", resp.Header)
trySet(w.Header(), "Last-Modified", resp.Header)
trySet(w.Header(), "Content-Length", resp.Header)
trySet(w.Header(), "Content-Range", resp.Header)
if r.Header.Get("If-None-Match") == info.ETag {
w.WriteHeader(http.StatusNotModified)
return
}
if r.Header.Get("If-Modified-Since") == info.LastModified {
w.WriteHeader(http.StatusNotModified)
return
}
w.WriteHeader(http.StatusOK)
io.Copy(w, content)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}