Skip to content

Commit

Permalink
Provide the request for intercepted responses
Browse files Browse the repository at this point in the history
  • Loading branch information
pipeline committed Jul 29, 2023
1 parent 07675aa commit 3482b0f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
10 changes: 7 additions & 3 deletions internal/proxy/http11webproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func onHttp11RequestReceived(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Re
}

if interceptSettings.BrowserToServer {
interceptedRequest := interceptRequest(request, "", "browser_to_server", requestBytes, hookRun)
interceptedRequest := interceptRequest(request, "", "browser_to_server", requestBytes, hookRun, nil)
<-interceptedRequest.ResponseReady

modifiedRequestData := request.GetRequestResponseData("Request", true)
Expand Down Expand Up @@ -212,7 +212,11 @@ func onHttp11ResponseReceived(resp *http.Response, ctx *goproxy.ProxyCtx) *http.
}

if shouldIntercept {
interceptedResponse := interceptRequest(request, "", "server_to_browser", responseBytes, hookRun)
reqBytes := request.GetRequestResponseData("Request", false)
if len(reqBytes) == 0 {
reqBytes = request.GetRequestResponseData("Request", false)
}
interceptedResponse := interceptRequest(request, "", "server_to_browser", responseBytes, hookRun, reqBytes)
<-interceptedResponse.ResponseReady

responseBytes = request.GetRequestResponseData("Response", true)
Expand Down Expand Up @@ -303,7 +307,7 @@ func onWebsocketPacketReceived(data []byte, direction goproxy.WebsocketDirection
interceptDirection = "server_to_browser"
}

interceptedRequest := interceptRequest(request, dataPacketGuid, interceptDirection, data, false)
interceptedRequest := interceptRequest(request, dataPacketGuid, interceptDirection, data, false, nil)
<-interceptedRequest.ResponseReady

for _, dataPacket := range request.DataPackets {
Expand Down
5 changes: 3 additions & 2 deletions internal/proxy/intercept_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ type InterceptedRequestResponse struct {
var interceptedRequests []*project.InterceptedRequest
var interceptedRequestsLock sync.Mutex

func interceptRequest(request *project.Request, guid string, direction string, requestData []byte, hookRun bool) *project.InterceptedRequest {
func interceptRequest(request *project.Request, guid string, direction string, requestResponseData []byte, hookRun bool, requestData []byte) *project.InterceptedRequest {
interceptedRequest := &project.InterceptedRequest{
Request: request,
GUID: guid,
Body: base64.StdEncoding.EncodeToString(requestData),
Body: base64.StdEncoding.EncodeToString(requestResponseData),
RequestBody: base64.StdEncoding.EncodeToString(requestData),
Direction: direction,
ResponseReady: make(chan bool),
IsUTF8: utf8.Valid(requestData),
Expand Down
1 change: 1 addition & 0 deletions pkg/project/intercepted_request_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type InterceptedRequest struct {
Request *Request
GUID string
Body string `example:"<base64 encoded body>"`
RequestBody string `example:"<base64 encoded body> (for responses)"`
Direction string `example:"Either browser_to_server or server_to_browser"`
ResponseReady chan bool `json:"-"`
ObjectType string
Expand Down

0 comments on commit 3482b0f

Please sign in to comment.