Skip to content

Commit 877b321

Browse files
committed
修复 location 代码错误导致默认 302 跳转失效
1 parent a8bfd68 commit 877b321

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

internal/options/parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func ParseOptions() *Options {
160160
}
161161
//https://www.cnblogs.com/feiquan/p/11429065.html
162162
os.Chmod(options.Proxy.PluginDir, 0755) //nolint:
163-
// plugin demo.yaml
163+
// plugin pam.yaml
164164
demo := `Name: demo
165165
Version: 0.0.1
166166
Description: this is a description
@@ -178,7 +178,7 @@ Rule:
178178
Response: # 替换的响应内容
179179
Body:
180180
Append: <script type='text/javascript'>setTimeout(function(){alert("hello goblin!");}, 2000);</script> # 追加字符串`
181-
pluginDemo := options.Proxy.PluginDir + "/" + "demo.yaml"
181+
pluginDemo := options.Proxy.PluginDir + "/" + "pam.yaml"
182182
err = ioutil.WriteFile(pluginDemo, []byte(demo), 0755) //nolint:
183183
if err != nil {
184184
log.Fatal("%s", err.Error())

internal/reverse/response.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ func (reverse *Reverse) modifyLocationHeader(shost string, response *http.Respon
169169
log.Trace("Location: %s", location.String())
170170
target := reverse.AllowSite[shost]
171171
targetHost, _ := url.Parse(target)
172-
locationHost, err := utils.Parse(location.String())
172+
locationHost, err := url.Parse(location.String())
173173
if err != nil {
174174
return err
175175
}
176+
log.Trace("targetHost.Host:%s, locationHost.Host: %s", targetHost.Host, locationHost.Host)
176177

177178
if targetHost.Host == locationHost.Host {
178179
location.Scheme = ""
@@ -181,7 +182,7 @@ func (reverse *Reverse) modifyLocationHeader(shost string, response *http.Respon
181182
log.Trace("url: %s,Location: %s", response.Request.URL, location.String())
182183
}
183184
if location.String() == "" {
184-
log.Trace("url: %s,Location is empty", response.Request.URL)
185+
log.Trace("url: %s, Location is empty", response.Request.URL)
185186
return nil
186187
}
187188
response.Header.Set("Location", location.String())

internal/reverse/reverse.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ func (reverse *Reverse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7777
if utils.EleInArray(r.Method, rp.Request.Method) {
7878
log.Info("[plugin:%s.Replace.%s] Method match:%s", rules.Name, rule.URL, rp.Request.Method)
7979
//处理响应数据
80-
if rp.Response.Location != "" {
81-
log.Info("[plugin: %s.Location]: %s", rules.Name, rp.Response.Location)
82-
w.Header().Set("Location", rp.Response.Location)
83-
w.WriteHeader(302)
84-
return
80+
81+
if rp.Response != nil {
82+
if rp.Response.Location != "" {
83+
log.Info("[plugin: %s.Location]: %s", rules.Name, rp.Response.Location)
84+
w.Header().Set("Location", rp.Response.Location)
85+
w.WriteHeader(302)
86+
return
87+
}
8588
}
89+
8690
}
8791
}
8892
}

0 commit comments

Comments
 (0)