diff --git a/pkg/drivers/http/driver.go b/pkg/drivers/http/driver.go index 919531c9..533f6698 100644 --- a/pkg/drivers/http/driver.go +++ b/pkg/drivers/http/driver.go @@ -141,7 +141,14 @@ func (drv *Driver) Open(ctx context.Context, params drivers.Params) (drivers.HTM return nil, errors.Wrapf(err, "failed to parse a document %s", params.URL) } - return NewHTMLPage(doc, params.URL, params.Cookies) + // HTTPResponse is temporarily unavailable when the status code != OK + r := drivers.HTTPResponse{ + StatusCode: resp.StatusCode, + Status: resp.Status, + Headers: drivers.HTTPHeaders(resp.Header), + } + + return NewHTMLPage(doc, params.URL, params.Cookies, &r) } func (drv *Driver) Parse(_ context.Context, str values.String) (drivers.HTMLPage, error) { @@ -153,7 +160,7 @@ func (drv *Driver) Parse(_ context.Context, str values.String) (drivers.HTMLPage return nil, errors.Wrap(err, "failed to parse a document") } - return NewHTMLPage(doc, "#blank", nil) + return NewHTMLPage(doc, "#blank", nil, nil) } func (drv *Driver) Close() error { diff --git a/pkg/drivers/http/page.go b/pkg/drivers/http/page.go index c5f6cccd..23e5e313 100644 --- a/pkg/drivers/http/page.go +++ b/pkg/drivers/http/page.go @@ -2,24 +2,27 @@ package http import ( "context" + "hash/fnv" + "github.com/MontFerret/ferret/pkg/drivers" "github.com/MontFerret/ferret/pkg/drivers/common" "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/values" "github.com/PuerkitoBio/goquery" - "hash/fnv" ) type HTMLPage struct { document *HTMLDocument cookies []drivers.HTTPCookie frames *values.Array + response *drivers.HTTPResponse } func NewHTMLPage( qdoc *goquery.Document, url string, cookies []drivers.HTTPCookie, + response *drivers.HTTPResponse, ) (*HTMLPage, error) { doc, err := NewRootHTMLDocument(qdoc, url) @@ -31,6 +34,7 @@ func NewHTMLPage( p.document = doc p.cookies = cookies p.frames = nil + p.response = response return p, nil } @@ -79,7 +83,12 @@ func (p *HTMLPage) Hash() uint64 { } func (p *HTMLPage) Copy() core.Value { - page, err := NewHTMLPage(p.document.doc, p.document.GetURL().String(), p.cookies[:]) + page, err := NewHTMLPage( + p.document.doc, + p.document.GetURL().String(), + p.cookies, + p.response, + ) if err != nil { return values.None @@ -166,6 +175,10 @@ func (p *HTMLPage) GetCookies(_ context.Context) (*values.Array, error) { return arr, nil } +func (p *HTMLPage) GetResponse(_ context.Context) (*drivers.HTTPResponse, error) { + return p.response, nil +} + func (p *HTMLPage) SetCookies(_ context.Context, _ ...drivers.HTTPCookie) error { return core.ErrNotSupported }