Browse Source

fix redirect

custom
hunshcn 2 years ago
parent
commit
5d04e7cd62
  1. 1
      .gitignore
  2. 2
      go.mod
  3. 17
      main.go

1
.gitignore vendored

@ -0,0 +1 @@
.idea

2
go.mod

@ -1,3 +1,3 @@
module awesomeProject
module github.com/hunshcn/gh-proxy
go 1.18

17
main.go

@ -27,6 +27,10 @@ type Server struct {
enableLog bool
}
func buildHostPath(u *url.URL) string {
return "/" + u.Scheme + "://" + u.Host
}
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
targetUrl := r.URL.RequestURI()
targetUrl = targetUrl[1:]
@ -37,9 +41,6 @@ func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
r.Host = target.Host
r.URL = target
r.URL.Host = target.Host
r.URL.Path = target.Path
r.URL.RawPath = target.RawPath
mw := &MWriter{Next: s.proxy.ServeHTTP, EnableLog: s.enableLog}
if strings.HasSuffix(targetUrl[:strings.LastIndexByte(targetUrl, '/')], "/info/lfs/objects") {
@ -111,7 +112,15 @@ func (mw *MWriter) Write(data []byte) (int, error) {
func (mw *MWriter) WriteHeader(statusCode int) {
for i := range mw.Header()["Location"] {
mw.Header()["Location"][i] = HostPrefix + mw.Header()["Location"][0]
location := mw.Header()["Location"][i]
if locationUrl, _ := url.Parse(location); locationUrl != nil && locationUrl.Scheme != "" {
location = "/" + location
} else if location[0] == '/' {
location = buildHostPath(mw.request.URL) + location
} else if mw.request.URL.Path == "" { // fix relative path redirect rule
location = buildHostPath(mw.request.URL) + "/" + location
}
mw.Header()["Location"][i] = location
}
mw.Log()
if mw.ReplaceFunc == nil {

Loading…
Cancel
Save