mirror of
https://github.com/MontFerret/ferret.git
synced 2025-07-05 00:49:00 +02:00
#593 handling non-200 status codes
This commit is contained in:
@ -2,6 +2,7 @@ package html
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -27,10 +28,10 @@ type PageLoadParams struct {
|
||||
// @param {Boolean} [params.keepCookies=False] - Boolean value indicating whether to use cookies from previous sessions i.e. not to open a page in the Incognito mode.
|
||||
// @param {HTTPCookies} [params.cookies] - Set of HTTP cookies to use during page loading.
|
||||
// @param {HTTPHeaders} [params.headers] - Set of HTTP headers to use during page loading.
|
||||
// @param {Object} [params.disable] - Set of parameters to disable some page functionality or behavior.
|
||||
// @param {Object[]} [params.disable.resources] - Collection of rules to disable resources during page load and navigation.
|
||||
// @param {String} [params.disable.resources.*.url] - Resource url pattern. If set, requests for matching urls will be blocked. Wildcards ('*' -> zero or more, '?' -> exactly one) are allowed. Escape character is backslash. Omitting is equivalent to "*".
|
||||
// @param {String} [params.disable.resources.*.type] - Resource type. If set, requests for matching resource types will be blocked.
|
||||
// @param {Object} [params.ignore] - Set of parameters to ignore some page functionality or behavior.
|
||||
// @param {Object[]} [params.ignore.resources] - Collection of rules to ignore resources during page load and navigation.
|
||||
// @param {String} [params.ignore.resources.*.url] - Resource url pattern. If set, requests for matching urls will be blocked. Wildcards ('*' -> zero or more, '?' -> exactly one) are allowed. Escape character is backslash. Omitting is equivalent to "*".
|
||||
// @param {String} [params.ignore.resources.*.type] - Resource type. If set, requests for matching resource types will be blocked.
|
||||
// @param {Object} [params.viewport] - Viewport params.
|
||||
// @param {Int} [params.viewport.height] - Viewport height.
|
||||
// @param {Int} [params.viewport.width] - Viewport width.
|
||||
@ -191,16 +192,16 @@ func newPageLoadParams(url values.String, arg core.Value) (PageLoadParams, error
|
||||
res.Viewport = viewport
|
||||
}
|
||||
|
||||
disable, exists := obj.Get(values.NewString("disable"))
|
||||
ignore, exists := obj.Get(values.NewString("ignore"))
|
||||
|
||||
if exists {
|
||||
disable, err := parseDisable(disable)
|
||||
ignore, err := parseIgnore(ignore)
|
||||
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.Disable = disable
|
||||
res.Ignore = ignore
|
||||
}
|
||||
case types.String:
|
||||
res.Driver = arg.(values.String).String()
|
||||
@ -408,16 +409,16 @@ func parseViewport(value core.Value) (*drivers.Viewport, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func parseDisable(value core.Value) (*drivers.Disable, error) {
|
||||
func parseIgnore(value core.Value) (*drivers.Ignore, error) {
|
||||
if err := core.ValidateType(value, types.Object); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := &drivers.Disable{}
|
||||
res := &drivers.Ignore{}
|
||||
|
||||
disable := value.(*values.Object)
|
||||
ignore := value.(*values.Object)
|
||||
|
||||
resources, exists := disable.Get("resources")
|
||||
resources, exists := ignore.Get("resources")
|
||||
|
||||
if exists {
|
||||
if err := core.ValidateType(resources, types.Array); err != nil {
|
||||
@ -458,5 +459,43 @@ func parseDisable(value core.Value) (*drivers.Disable, error) {
|
||||
}
|
||||
}
|
||||
|
||||
statusCodes, exists := ignore.Get("statusCodes")
|
||||
|
||||
if exists {
|
||||
if err := core.ValidateType(statusCodes, types.Array); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
statusCodes := statusCodes.(*values.Array)
|
||||
|
||||
res.StatusCodes = make([]drivers.StatusCodeFilter, 0, statusCodes.Length())
|
||||
|
||||
var e error
|
||||
|
||||
statusCodes.ForEach(func(el core.Value, idx int) bool {
|
||||
if e = core.ValidateType(el, types.Object); e != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
pattern := el.(*values.Object)
|
||||
|
||||
url := pattern.MustGetOr("url", values.NewString(""))
|
||||
code, codeExists := pattern.Get("code")
|
||||
|
||||
// ignore element
|
||||
if !codeExists {
|
||||
e = errors.New("http code is required")
|
||||
return false
|
||||
}
|
||||
|
||||
res.StatusCodes = append(res.StatusCodes, drivers.StatusCodeFilter{
|
||||
URL: url.String(),
|
||||
Code: int(values.ToInt(code)),
|
||||
})
|
||||
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user