From f48feddd008ec3bb3bb404a6d64dab78a3688a3a Mon Sep 17 00:00:00 2001 From: evilsocket Date: Mon, 14 May 2018 11:10:24 +0200 Subject: [PATCH] new: implemented JSResponse.ClearBody API (closes #272) --- modules/http_proxy_js_response.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/modules/http_proxy_js_response.go b/modules/http_proxy_js_response.go index 303d5182..84fd7906 100644 --- a/modules/http_proxy_js_response.go +++ b/modules/http_proxy_js_response.go @@ -16,9 +16,10 @@ type JSResponse struct { Headers []JSHeader Body string - refHash string - resp *http.Response - bodyRead bool + refHash string + resp *http.Response + bodyRead bool + bodyClear bool } func NewJSResponse(res *http.Response) *JSResponse { @@ -45,6 +46,7 @@ func NewJSResponse(res *http.Response) *JSResponse { Headers: headers, resp: res, bodyRead: false, + bodyClear: false, } resp.UpdateHash() @@ -67,6 +69,9 @@ func (j *JSResponse) WasModified() bool { if j.bodyRead { // body was read return true + } else if j.bodyClear { + // body was cleared manually + return true } else if j.Body != "" { // body was not read but just set return true @@ -105,6 +110,11 @@ func (j *JSResponse) RemoveHeader(name string) { } } +func (j *JSResponse) ClearBody() { + j.Body = "" + j.bodyClear = true +} + func (j *JSResponse) ToResponse(req *http.Request) (resp *http.Response) { resp = goproxy.NewResponse(req, j.ContentType, j.Status, j.Body) if len(j.Headers) > 0 { @@ -125,6 +135,7 @@ func (j *JSResponse) ReadBody() string { j.Body = string(raw) j.bodyRead = true + j.bodyClear = false // reset the response body to the original unread state j.resp.Body = ioutil.NopCloser(bytes.NewBuffer(raw))