Update http_proxy_js_response.go

This commit is contained in:
yungtravla 2018-03-21 19:47:15 +10:00 committed by GitHub
parent 888ec3d624
commit 0b56b19134
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,14 +8,12 @@ import (
"strings" "strings"
"github.com/elazarl/goproxy" "github.com/elazarl/goproxy"
"github.com/bettercap/bettercap/core"
) )
type JSResponse struct { type JSResponse struct {
Status int Status int
ContentType string ContentType string
Headers string Headers []JSHeader
Body string Body string
refHash string refHash string
@ -25,17 +23,18 @@ type JSResponse struct {
func NewJSResponse(res *http.Response) *JSResponse { func NewJSResponse(res *http.Response) *JSResponse {
cType := "" cType := ""
headers := "" headers := make([]JSHeader, 0)
code := 200 code := 200
if res != nil { if res != nil {
code = res.StatusCode code = res.StatusCode
for name, values := range res.Header { for name, values := range res.Header {
for _, value := range values { for _, value := range values {
headers = append(headers, JSHeader{name, value})
if name == "Content-Type" { if name == "Content-Type" {
cType = value cType = value
} }
headers += name + ": " + value + "\r\n"
} }
} }
} }
@ -53,7 +52,11 @@ func NewJSResponse(res *http.Response) *JSResponse {
} }
func (j *JSResponse) NewHash() string { func (j *JSResponse) NewHash() string {
return fmt.Sprintf("%d.%s.%s", j.Status, j.ContentType, j.Headers) hash := fmt.Sprintf("%d.%s", j.Status, j.ContentType)
for _, h := range j.Headers {
hash += fmt.Sprintf(".%s-%s", h.Name, h.Value)
}
return hash
} }
func (j *JSResponse) UpdateHash() { func (j *JSResponse) UpdateHash() {
@ -73,29 +76,35 @@ func (j *JSResponse) WasModified() bool {
return false return false
} }
func (j *JSResponse) Header(name, deflt string) string { func (j *JSResponse) GetHeader(name, deflt string) string {
name = strings.ToLower(name) name = strings.ToLower(name)
for _, header := range strings.Split(j.Headers, "\n") { for _, h := range j.Headers {
parts := strings.SplitN(core.Trim(header), ":", 2) if name == strings.ToLower(h.Name) {
if len(parts) == 2 && strings.ToLower(parts[0]) == name { return h.Value
return parts[1]
} }
} }
return deflt return deflt
} }
func (j *JSResponse) SetHeader(name, value string) {
name = strings.ToLower(name)
found := false
for _, h := range j.Headers {
if name == strings.ToLower(h.Name) {
found = true
h.Value = value
}
}
if found == false {
j.Headers = append(j.Headers, JSHeader{name, value})
}
}
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 j.Headers != "" { if len(j.Headers) > 0 {
for _, header := range strings.Split(j.Headers, "\n") { for _, h := range j.Headers {
header = core.Trim(header) resp.Header.Add(h.Name, h.Value)
if header == "" {
continue
}
parts := strings.SplitN(header, ":", 2)
if len(parts) == 2 {
resp.Header.Add(parts[0], parts[1])
}
} }
} }
return return