cookie jar go

This commit is contained in:
Bel LaPointe
2022-01-27 11:00:05 -07:00
parent 60c88375ad
commit ba2156133a
2 changed files with 52 additions and 18 deletions

View File

@@ -26,6 +26,10 @@ type Broker interface {
}
func do(r *http.Request) (*http.Response, error) {
return _do(config.Get().DB(), r)
}
func _do(db storage.DB, r *http.Request) (*http.Response, error) {
limiter.Wait(context.Background())
if strings.Contains(strings.ToLower(r.URL.Path), "login") {
authlimiter.Wait(context.Background())
@@ -39,8 +43,8 @@ func do(r *http.Request) (*http.Response, error) {
}
client.Jar = newjar
cookieJarKey := r.URL.Host
cookies, err := getCookies(cookieJarKey)
cookieJarKey := "cookies_" + r.URL.Host
cookies, err := getCookies(db, cookieJarKey)
if err != nil {
logtr.Errorf("failed to load cookies: %v", err)
} else {
@@ -50,17 +54,13 @@ func do(r *http.Request) (*http.Response, error) {
if err != nil {
return nil, err
}
if err := setCookies(cookieJarKey, client.Jar.Cookies(r.URL)); err != nil {
if err := setCookies(db, cookieJarKey, client.Jar.Cookies(r.URL)); err != nil {
logtr.Errorf("failed to set cookies: %v", err)
}
return resp, err
}
func getCookies(host string) ([]*http.Cookie, error) {
return _getCookies(config.Get().DB(), host)
}
func _getCookies(db storage.DB, host string) ([]*http.Cookie, error) {
func getCookies(db storage.DB, host string) ([]*http.Cookie, error) {
b, err := db.Get(host)
if err != nil {
return nil, err
@@ -70,11 +70,7 @@ func _getCookies(db storage.DB, host string) ([]*http.Cookie, error) {
return result, err
}
func setCookies(host string, cookies []*http.Cookie) error {
return _setCookies(config.Get().DB(), host, cookies)
}
func _setCookies(db storage.DB, host string, cookies []*http.Cookie) error {
func setCookies(db storage.DB, host string, cookies []*http.Cookie) error {
b, err := json.Marshal(cookies)
if err != nil {
return err