Skip to content

Commit

Permalink
修复 location 代码错误导致默认 302 跳转失效
Browse files Browse the repository at this point in the history
  • Loading branch information
Becivells committed Oct 11, 2021
1 parent a8bfd68 commit 877b321
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions internal/options/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func ParseOptions() *Options {
}
//https://www.cnblogs.com/feiquan/p/11429065.html
os.Chmod(options.Proxy.PluginDir, 0755) //nolint:
// plugin demo.yaml
// plugin pam.yaml
demo := `Name: demo
Version: 0.0.1
Description: this is a description
Expand All @@ -178,7 +178,7 @@ Rule:
Response: # 替换的响应内容
Body:
Append: <script type='text/javascript'>setTimeout(function(){alert("hello goblin!");}, 2000);</script> # 追加字符串`
pluginDemo := options.Proxy.PluginDir + "/" + "demo.yaml"
pluginDemo := options.Proxy.PluginDir + "/" + "pam.yaml"
err = ioutil.WriteFile(pluginDemo, []byte(demo), 0755) //nolint:
if err != nil {
log.Fatal("%s", err.Error())
Expand Down
5 changes: 3 additions & 2 deletions internal/reverse/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ func (reverse *Reverse) modifyLocationHeader(shost string, response *http.Respon
log.Trace("Location: %s", location.String())
target := reverse.AllowSite[shost]
targetHost, _ := url.Parse(target)
locationHost, err := utils.Parse(location.String())
locationHost, err := url.Parse(location.String())
if err != nil {
return err
}
log.Trace("targetHost.Host:%s, locationHost.Host: %s", targetHost.Host, locationHost.Host)

if targetHost.Host == locationHost.Host {
location.Scheme = ""
Expand All @@ -181,7 +182,7 @@ func (reverse *Reverse) modifyLocationHeader(shost string, response *http.Respon
log.Trace("url: %s,Location: %s", response.Request.URL, location.String())
}
if location.String() == "" {
log.Trace("url: %s,Location is empty", response.Request.URL)
log.Trace("url: %s, Location is empty", response.Request.URL)
return nil
}
response.Header.Set("Location", location.String())
Expand Down
14 changes: 9 additions & 5 deletions internal/reverse/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ func (reverse *Reverse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if utils.EleInArray(r.Method, rp.Request.Method) {
log.Info("[plugin:%s.Replace.%s] Method match:%s", rules.Name, rule.URL, rp.Request.Method)
//处理响应数据
if rp.Response.Location != "" {
log.Info("[plugin: %s.Location]: %s", rules.Name, rp.Response.Location)
w.Header().Set("Location", rp.Response.Location)
w.WriteHeader(302)
return

if rp.Response != nil {
if rp.Response.Location != "" {
log.Info("[plugin: %s.Location]: %s", rules.Name, rp.Response.Location)
w.Header().Set("Location", rp.Response.Location)
w.WriteHeader(302)
return
}
}

}
}
}
Expand Down

0 comments on commit 877b321

Please sign in to comment.