github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/network/helpers.go (about) 1 package network 2 3 import ( 4 "encoding/json" 5 "regexp" 6 "strings" 7 "time" 8 9 "github.com/mafredri/cdp/protocol/network" 10 "github.com/mafredri/cdp/protocol/page" 11 "github.com/rs/zerolog/log" 12 13 "github.com/MontFerret/ferret/pkg/drivers" 14 ) 15 16 var emptyExpires = time.Time{} 17 18 func toDriverBody(body *string) []byte { 19 if body == nil { 20 return nil 21 } 22 23 return []byte(*body) 24 } 25 26 func toDriverHeaders(headers network.Headers) *drivers.HTTPHeaders { 27 result := drivers.NewHTTPHeaders() 28 deserialized := make(map[string]string) 29 30 if len(headers) > 0 { 31 err := json.Unmarshal(headers, &deserialized) 32 33 if err != nil { 34 log.Trace().Err(err).Msg("failed to deserialize responseReceivedEvent headers") 35 } 36 } 37 38 for key, value := range deserialized { 39 result.Set(key, value) 40 } 41 42 return result 43 } 44 45 func toDriverResponse(resp network.Response, body []byte) *drivers.HTTPResponse { 46 return &drivers.HTTPResponse{ 47 URL: resp.URL, 48 StatusCode: resp.Status, 49 Status: resp.StatusText, 50 Headers: toDriverHeaders(resp.Headers), 51 Body: body, 52 ResponseTime: float64(resp.ResponseTime), 53 } 54 } 55 56 func toDriverRequest(req network.Request) *drivers.HTTPRequest { 57 return &drivers.HTTPRequest{ 58 URL: req.URL, 59 Method: req.Method, 60 Headers: toDriverHeaders(req.Headers), 61 Body: toDriverBody(req.PostData), 62 } 63 } 64 65 func fromDriverCookie(url string, cookie drivers.HTTPCookie) network.CookieParam { 66 sameSite := network.CookieSameSiteNotSet 67 68 switch cookie.SameSite { 69 case drivers.SameSiteLaxMode: 70 sameSite = network.CookieSameSiteLax 71 case drivers.SameSiteStrictMode: 72 sameSite = network.CookieSameSiteStrict 73 } 74 75 if cookie.Expires == emptyExpires { 76 cookie.Expires = time.Now().Add(time.Duration(24) + time.Hour) 77 } 78 79 normalizedURL := normalizeCookieURL(url) 80 81 return network.CookieParam{ 82 URL: &normalizedURL, 83 Name: cookie.Name, 84 Value: cookie.Value, 85 Secure: &cookie.Secure, 86 Path: &cookie.Path, 87 Domain: &cookie.Domain, 88 HTTPOnly: &cookie.HTTPOnly, 89 SameSite: sameSite, 90 Expires: network.TimeSinceEpoch(cookie.Expires.Unix()), 91 } 92 } 93 94 func fromDriverCookieDelete(url string, cookie drivers.HTTPCookie) *network.DeleteCookiesArgs { 95 normalizedURL := normalizeCookieURL(url) 96 97 return &network.DeleteCookiesArgs{ 98 URL: &normalizedURL, 99 Name: cookie.Name, 100 Path: &cookie.Path, 101 Domain: &cookie.Domain, 102 } 103 } 104 105 func toDriverCookie(c network.Cookie) drivers.HTTPCookie { 106 sameSite := drivers.SameSiteDefaultMode 107 108 switch c.SameSite { 109 case network.CookieSameSiteLax: 110 sameSite = drivers.SameSiteLaxMode 111 case network.CookieSameSiteStrict: 112 sameSite = drivers.SameSiteStrictMode 113 } 114 115 return drivers.HTTPCookie{ 116 Name: c.Name, 117 Value: c.Value, 118 Path: c.Path, 119 Domain: c.Domain, 120 Expires: time.Unix(int64(c.Expires), 0), 121 SameSite: sameSite, 122 Secure: c.Secure, 123 HTTPOnly: c.HTTPOnly, 124 } 125 } 126 127 func normalizeCookieURL(url string) string { 128 const httpPrefix = "http://" 129 const httpsPrefix = "https://" 130 131 if strings.HasPrefix(url, httpPrefix) || strings.HasPrefix(url, httpsPrefix) { 132 return url 133 } 134 135 return httpPrefix + url 136 } 137 138 func isURLMatched(url string, pattern *regexp.Regexp) bool { 139 var matched bool 140 141 // if a URL pattern is provided 142 if pattern != nil { 143 matched = pattern.MatchString(url) 144 } else { 145 // otherwise, just match 146 matched = true 147 } 148 149 return matched 150 } 151 152 func isFrameMatched(current, target page.FrameID) bool { 153 // if frameID is empty string or equals to the current one 154 if len(target) == 0 { 155 return true 156 } 157 158 return target == current 159 }