Skip to content

Commit

Permalink
body 文件缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
Becivells committed Sep 9, 2021
1 parent d33688c commit 49075a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
17 changes: 15 additions & 2 deletions internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (base *BasePlugin) setInitConfig() {
}
fname = "/" + PluginVariable.Static + "/" + fname
if _, ok := StaticFiles[fname]; ok {
log.Fatal("fname: %s 重复请检查", fname)
log.Fatal("fname: %s duplicate files pleace check", fname)
}
rule.InjectJs.EvilJs = fname
StaticFiles[fname] = b
Expand Down Expand Up @@ -182,7 +182,20 @@ func (base *BasePlugin) setInitConfig() {
rp.Response.Header["Location"] = tpl.String()
}
}
// ReplaceStr
// BodyFile
if rp.Response.Body.File != "" {
fname := rp.Response.Body.File
b, err := ioutil.ReadFile(fname) // just pass the file name
if err != nil {
log.Fatal("%s", err.Error())
}
if _, ok := replace.BodyFiles[fname]; ok {
log.Trace("[plugin] fname: %s duplicate files", fname)
} else {
log.Trace("[plugin] BodyFile load %s", fname)
replace.BodyFiles[fname] = b
}
}
// replace 可能为nil
if rp.Response.Body.ReplaceStr != nil {
// str 处理
Expand Down
8 changes: 2 additions & 6 deletions internal/plugin/replace/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,11 @@ func (rpRule *Response) Response(maxContentLength int, response *http.Response)
}
// file 处理 有 file 了就直接处理结束不要替换追加
if strings.TrimSpace(rpRule.Body.File) != "" {
// todo 并发有问题需要处理加到缓存里面
b, err := ioutil.ReadFile(rpRule.Body.File) // just pass the file name
if err != nil {
return err
}
// todo 后期建议加到缓存里面
b := BodyFiles[rpRule.Body.File]
response.Body = ioutil.NopCloser(bytes.NewReader(b))
response.ContentLength = int64(len(b))
response.Header.Set("Content-Length", strconv.Itoa(len(b)))
return nil
}
// 检查 Content-Length 如果大于 MaxContentLength 直接返回不处理当 maxCountlength 小于 0 放行
if cleng == -1 && maxContentLength > 0 {
Expand Down
4 changes: 4 additions & 0 deletions internal/plugin/replace/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (

var Method = []string{"GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "TRACE"}

var (
BodyFiles = make(map[string][]byte) //插件系统注入的js静态文件
)

type Replace struct {
Request *Request `yaml:"Request"`
Response *Response `yaml:"Response"`
Expand Down

0 comments on commit 49075a3

Please sign in to comment.