1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00

implement GetResponse for http.HTMLPage

This commit is contained in:
Владимир Фетисов 2019-09-30 21:09:51 +03:00
parent 43fb43eb8b
commit fb4634da9c
2 changed files with 24 additions and 4 deletions

View File

@ -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 {

View File

@ -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
}