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:
parent
43fb43eb8b
commit
fb4634da9c
@ -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 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) {
|
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 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 {
|
func (drv *Driver) Close() error {
|
||||||
|
@ -2,24 +2,27 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"hash/fnv"
|
||||||
|
|
||||||
"github.com/MontFerret/ferret/pkg/drivers"
|
"github.com/MontFerret/ferret/pkg/drivers"
|
||||||
"github.com/MontFerret/ferret/pkg/drivers/common"
|
"github.com/MontFerret/ferret/pkg/drivers/common"
|
||||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
"hash/fnv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTMLPage struct {
|
type HTMLPage struct {
|
||||||
document *HTMLDocument
|
document *HTMLDocument
|
||||||
cookies []drivers.HTTPCookie
|
cookies []drivers.HTTPCookie
|
||||||
frames *values.Array
|
frames *values.Array
|
||||||
|
response *drivers.HTTPResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTMLPage(
|
func NewHTMLPage(
|
||||||
qdoc *goquery.Document,
|
qdoc *goquery.Document,
|
||||||
url string,
|
url string,
|
||||||
cookies []drivers.HTTPCookie,
|
cookies []drivers.HTTPCookie,
|
||||||
|
response *drivers.HTTPResponse,
|
||||||
) (*HTMLPage, error) {
|
) (*HTMLPage, error) {
|
||||||
doc, err := NewRootHTMLDocument(qdoc, url)
|
doc, err := NewRootHTMLDocument(qdoc, url)
|
||||||
|
|
||||||
@ -31,6 +34,7 @@ func NewHTMLPage(
|
|||||||
p.document = doc
|
p.document = doc
|
||||||
p.cookies = cookies
|
p.cookies = cookies
|
||||||
p.frames = nil
|
p.frames = nil
|
||||||
|
p.response = response
|
||||||
|
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
@ -79,7 +83,12 @@ func (p *HTMLPage) Hash() uint64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *HTMLPage) Copy() core.Value {
|
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 {
|
if err != nil {
|
||||||
return values.None
|
return values.None
|
||||||
@ -166,6 +175,10 @@ func (p *HTMLPage) GetCookies(_ context.Context) (*values.Array, error) {
|
|||||||
return arr, nil
|
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 {
|
func (p *HTMLPage) SetCookies(_ context.Context, _ ...drivers.HTTPCookie) error {
|
||||||
return core.ErrNotSupported
|
return core.ErrNotSupported
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user