new: implemented JSResponse.ClearBody API (closes #272)

This commit is contained in:
evilsocket 2018-05-14 11:10:24 +02:00
commit f48feddd00
No known key found for this signature in database
GPG key ID: 1564D7F30393A456

View file

@ -16,9 +16,10 @@ type JSResponse struct {
Headers []JSHeader Headers []JSHeader
Body string Body string
refHash string refHash string
resp *http.Response resp *http.Response
bodyRead bool bodyRead bool
bodyClear bool
} }
func NewJSResponse(res *http.Response) *JSResponse { func NewJSResponse(res *http.Response) *JSResponse {
@ -45,6 +46,7 @@ func NewJSResponse(res *http.Response) *JSResponse {
Headers: headers, Headers: headers,
resp: res, resp: res,
bodyRead: false, bodyRead: false,
bodyClear: false,
} }
resp.UpdateHash() resp.UpdateHash()
@ -67,6 +69,9 @@ func (j *JSResponse) WasModified() bool {
if j.bodyRead { if j.bodyRead {
// body was read // body was read
return true return true
} else if j.bodyClear {
// body was cleared manually
return true
} else if j.Body != "" { } else if j.Body != "" {
// body was not read but just set // body was not read but just set
return true 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) { func (j *JSResponse) ToResponse(req *http.Request) (resp *http.Response) {
resp = goproxy.NewResponse(req, j.ContentType, j.Status, j.Body) resp = goproxy.NewResponse(req, j.ContentType, j.Status, j.Body)
if len(j.Headers) > 0 { if len(j.Headers) > 0 {
@ -125,6 +135,7 @@ func (j *JSResponse) ReadBody() string {
j.Body = string(raw) j.Body = string(raw)
j.bodyRead = true j.bodyRead = true
j.bodyClear = false
// reset the response body to the original unread state // reset the response body to the original unread state
j.resp.Body = ioutil.NopCloser(bytes.NewBuffer(raw)) j.resp.Body = ioutil.NopCloser(bytes.NewBuffer(raw))