From fb4634da9c64f19f7fae4471fd7e6e10c503f0f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=20=D0=A4?= =?UTF-8?q?=D0=B5=D1=82=D0=B8=D1=81=D0=BE=D0=B2?= Date: Mon, 30 Sep 2019 21:09:51 +0300 Subject: [PATCH] implement GetResponse for http.HTMLPage --- pkg/drivers/http/driver.go | 11 +++++++++-- pkg/drivers/http/page.go | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) 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 }