Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match replace rule #63

Merged
merged 3 commits into from
Oct 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -107,7 +108,7 @@ func (p *Proxy) OnResponse(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Res

// perform match and replace
if p.options.ResponseMatchReplaceDSL != "" {
p.MatchReplaceResponse(resp)
resp = p.MatchReplaceResponse(resp)
}

p.logger.LogResponse(resp, userdata) //nolint
Expand All @@ -134,23 +135,13 @@ func (p *Proxy) MatchReplaceRequest(req *http.Request) *http.Request {
if v, err := dsl.EvalExpr(p.options.RequestMatchReplaceDSL, m); err != nil {
return req
} else {
reqbuffer := v.(string)

reqbuffer := fmt.Sprint(v)
// lazy mode - epic level - rebuild
bf := bufio.NewReader(strings.NewReader(reqbuffer))
requestNew, err := http.ReadRequest(bf)
if err != nil {
return req
}

requestNew.RequestURI = ""
u, err := url.Parse(req.RequestURI)
if err != nil {
return req
}
requestNew.URL = u

// swap requests
// closes old body to allow memory reuse
req.Body.Close()
return requestNew
Expand All @@ -171,8 +162,7 @@ func (p *Proxy) MatchReplaceResponse(resp *http.Response) *http.Response {
if v, err := dsl.EvalExpr(p.options.ResponseMatchReplaceDSL, m); err != nil {
return resp
} else {
respbuffer := v.(string)

respbuffer := fmt.Sprint(v)
// lazy mode - epic level - rebuild
bf := bufio.NewReader(strings.NewReader(respbuffer))
responseNew, err := http.ReadResponse(bf, nil)
Expand Down