github.com/MontFerret/ferret@v0.18.0/pkg/drivers/http/helpers.go (about)

     1  package http
     2  
     3  import (
     4  	HTTP "net/http"
     5  
     6  	"github.com/PuerkitoBio/goquery"
     7  	"golang.org/x/net/html"
     8  
     9  	"github.com/MontFerret/ferret/pkg/drivers"
    10  	"github.com/MontFerret/ferret/pkg/runtime/core"
    11  )
    12  
    13  func fromSelectionToNode(selection *goquery.Selection) *html.Node {
    14  	if selection.Length() > 0 {
    15  		return selection.Get(0)
    16  	}
    17  
    18  	return nil
    19  }
    20  
    21  func toDriverCookies(cookies []*HTTP.Cookie) (*drivers.HTTPCookies, error) {
    22  	res := drivers.NewHTTPCookies()
    23  
    24  	for _, c := range cookies {
    25  		dc, err := toDriverCookie(c)
    26  
    27  		if err != nil {
    28  			return nil, err
    29  		}
    30  
    31  		res.Set(dc)
    32  	}
    33  
    34  	return res, nil
    35  }
    36  
    37  func toDriverCookie(cookie *HTTP.Cookie) (drivers.HTTPCookie, error) {
    38  	res := drivers.HTTPCookie{}
    39  
    40  	if cookie == nil {
    41  		return res, core.Error(core.ErrMissedArgument, "cookie")
    42  	}
    43  
    44  	res.Name = cookie.Name
    45  	res.Value = cookie.Value
    46  	res.Path = cookie.Path
    47  	res.Domain = cookie.Domain
    48  	res.Expires = cookie.Expires
    49  	res.MaxAge = cookie.MaxAge
    50  	res.Secure = cookie.Secure
    51  	res.HTTPOnly = cookie.HttpOnly
    52  
    53  	switch cookie.SameSite {
    54  	case HTTP.SameSiteLaxMode:
    55  		res.SameSite = drivers.SameSiteLaxMode
    56  	case HTTP.SameSiteStrictMode:
    57  		res.SameSite = drivers.SameSiteStrictMode
    58  	default:
    59  		res.SameSite = drivers.SameSiteDefaultMode
    60  	}
    61  
    62  	return res, nil
    63  }
    64  
    65  func fromDriverCookie(cookie drivers.HTTPCookie) *HTTP.Cookie {
    66  	res := &HTTP.Cookie{}
    67  
    68  	res.Name = cookie.Name
    69  	res.Value = cookie.Value
    70  	res.Path = cookie.Path
    71  	res.Domain = cookie.Domain
    72  	res.Expires = cookie.Expires
    73  	res.MaxAge = cookie.MaxAge
    74  	res.Secure = cookie.Secure
    75  	res.HttpOnly = cookie.HTTPOnly
    76  
    77  	switch cookie.SameSite {
    78  	case drivers.SameSiteLaxMode:
    79  		res.SameSite = HTTP.SameSiteLaxMode
    80  	case drivers.SameSiteStrictMode:
    81  		res.SameSite = HTTP.SameSiteStrictMode
    82  	default:
    83  		res.SameSite = HTTP.SameSiteDefaultMode
    84  	}
    85  
    86  	return res
    87  }