diff --git a/v2/pkg/protocols/headless/engine/page.go b/v2/pkg/protocols/headless/engine/page.go index 7c7ad1cc..dbe9935f 100644 --- a/v2/pkg/protocols/headless/engine/page.go +++ b/v2/pkg/protocols/headless/engine/page.go @@ -3,6 +3,7 @@ package engine import ( "net/url" "strings" + "sync" "time" "github.com/go-rod/rod" @@ -11,11 +12,12 @@ import ( // Page is a single page in an isolated browser instance type Page struct { - page *rod.Page - rules []requestRule - instance *Instance - router *rod.HijackRouter - History []HistoryData + page *rod.Page + rules []requestRule + instance *Instance + router *rod.HijackRouter + historyMutex sync.RWMutex + History []HistoryData } // HistoryData contains the page request/response pairs @@ -92,6 +94,9 @@ func (p *Page) URL() string { // DumpHistory returns the full page navigation history func (p *Page) DumpHistory() string { + p.historyMutex.RLock() + defer p.historyMutex.RUnlock() + var historyDump strings.Builder for _, historyData := range p.History { historyDump.WriteString(historyData.RawRequest) @@ -99,3 +104,11 @@ func (p *Page) DumpHistory() string { } return historyDump.String() } + +// addToHistory adds a request/response pair to the page history +func (p *Page) addToHistory(historyData HistoryData) { + p.historyMutex.Lock() + defer p.historyMutex.Unlock() + + p.History = append(p.History, historyData) +} diff --git a/v2/pkg/protocols/headless/engine/rules.go b/v2/pkg/protocols/headless/engine/rules.go index 17b66ee4..5aa0c91d 100644 --- a/v2/pkg/protocols/headless/engine/rules.go +++ b/v2/pkg/protocols/headless/engine/rules.go @@ -64,9 +64,11 @@ func (p *Page) routingRuleHandler(ctx *rod.Hijack) { var rawResp strings.Builder respPayloads := ctx.Response.Payload() if respPayloads != nil { - rawResp.WriteString(fmt.Sprintf("HTTP/1.1 %d %s\n", respPayloads.ResponseCode, respPayloads.ResponsePhrase)) + rawResp.WriteString("HTTP/1.1 ") + rawResp.WriteString(fmt.Sprint(respPayloads.ResponseCode)) + rawResp.WriteString(" " + respPayloads.ResponsePhrase + "+\n") for _, header := range respPayloads.ResponseHeaders { - rawResp.WriteString(fmt.Sprintf("%s: %s\n", header.Name, header.Value)) + rawResp.WriteString(header.Name + ": " + header.Value + "\n") } rawResp.WriteString("\n") rawResp.WriteString(ctx.Response.Body()) @@ -77,5 +79,5 @@ func (p *Page) routingRuleHandler(ctx *rod.Hijack) { RawRequest: rawReq, RawResponse: rawResp.String(), } - p.History = append(p.History, historyData) + p.addToHistory(historyData) }