just forward request and response
This commit is contained in:
parent
af903b8fee
commit
6e98e4f5d6
2 changed files with 24 additions and 59 deletions
|
|
@ -1,11 +1,9 @@
|
||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
@ -16,16 +14,10 @@ type Forgejo struct {
|
||||||
Branch string
|
Branch string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Info struct {
|
|
||||||
LastModified string
|
|
||||||
ETag string
|
|
||||||
Type string
|
|
||||||
Size string
|
|
||||||
}
|
|
||||||
|
|
||||||
var ErrNotFound = errors.New("404 not found")
|
var ErrNotFound = errors.New("404 not found")
|
||||||
|
|
||||||
func (f *Forgejo) getFile(ctx context.Context, user, repo, branch, file string) (info Info, content io.ReadCloser, err error) {
|
// headers must ot be nil
|
||||||
|
func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user, repo, branch, file string) (ret *http.Response, err error) {
|
||||||
u := f.Server
|
u := f.Server
|
||||||
u.Path = "/api/v1/repos/" + user + "/" + repo + "/raw/" + file
|
u.Path = "/api/v1/repos/" + user + "/" + repo + "/raw/" + file
|
||||||
p := u.Query()
|
p := u.Query()
|
||||||
|
|
@ -37,6 +29,12 @@ func (f *Forgejo) getFile(ctx context.Context, user, repo, branch, file string)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for k, v := range headers {
|
||||||
|
if v == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
req.Header.Set("Authorization", "token "+f.Token)
|
req.Header.Set("Authorization", "token "+f.Token)
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -47,36 +45,5 @@ func (f *Forgejo) getFile(ctx context.Context, user, repo, branch, file string)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
info.LastModified = resp.Header.Get("Last-Modified")
|
return resp, nil
|
||||||
info.ETag = resp.Header.Get("Etag")
|
|
||||||
info.Type = resp.Header.Get("X-Forgejo-Object-Type")
|
|
||||||
info.Size = resp.Header.Get("Content-Length")
|
|
||||||
content = resp.Body
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Forgejo) GetFile(ctx context.Context, user, repo, branch, file string) (info Info, content io.ReadCloser, err error) {
|
|
||||||
info, content, err = f.getFile(ctx, user, repo, branch, file)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for info.Type == "symlink" {
|
|
||||||
var data []byte
|
|
||||||
data, err = io.ReadAll(content)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
content.Close()
|
|
||||||
if bytes.HasPrefix(data, []byte("/")) {
|
|
||||||
err = ErrNotFound
|
|
||||||
return
|
|
||||||
}
|
|
||||||
info, content, err = f.getFile(ctx, user, repo, branch, string(data))
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
32
lib/web.go
32
lib/web.go
|
|
@ -18,7 +18,8 @@ func Run(bind string, f *Forgejo) (*http.Server, error) {
|
||||||
return s, nil
|
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 == "" {
|
if value == "" {
|
||||||
h.Del(key)
|
h.Del(key)
|
||||||
return
|
return
|
||||||
|
|
@ -44,26 +45,23 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) {
|
||||||
file = strings.Join(arr[2:], "/")
|
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 {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer content.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
trySet(w.Header(), "Etag", info.ETag)
|
trySet(w.Header(), "Etag", resp.Header)
|
||||||
trySet(w.Header(), "Last-Modified", info.LastModified)
|
trySet(w.Header(), "Last-Modified", resp.Header)
|
||||||
trySet(w.Header(), "Content-Length", info.Size)
|
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(resp.StatusCode)
|
||||||
w.WriteHeader(http.StatusNotModified)
|
io.Copy(w, resp.Body)
|
||||||
return
|
|
||||||
}
|
|
||||||
if r.Header.Get("If-Modified-Since") == info.LastModified {
|
|
||||||
w.WriteHeader(http.StatusNotModified)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
io.Copy(w, content)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue