Compare commits

..

2 Commits

Author SHA1 Message Date
Bel LaPointe
ba2156133a cookie jar go 2022-01-27 11:00:05 -07:00
bel
60c88375ad wip 2022-01-27 10:38:41 -07:00
2 changed files with 111 additions and 1 deletions

View File

@@ -2,9 +2,15 @@ package broker
import (
"context"
"encoding/json"
"errors"
"local/storage"
"local/truckstop/config"
"local/truckstop/logtr"
"net/http"
"net/http/cookiejar"
"strings"
"time"
"golang.org/x/time/rate"
)
@@ -20,9 +26,54 @@ 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())
}
return http.DefaultClient.Do(r)
client := &http.Client{
Timeout: time.Hour,
}
newjar, err := cookiejar.New(&cookiejar.Options{})
if err != nil {
return nil, errors.New("failed to make a cookie jar: " + err.Error())
}
client.Jar = newjar
cookieJarKey := "cookies_" + r.URL.Host
cookies, err := getCookies(db, cookieJarKey)
if err != nil {
logtr.Errorf("failed to load cookies: %v", err)
} else {
client.Jar.SetCookies(r.URL, cookies)
}
resp, err := client.Do(r)
if err != nil {
return nil, err
}
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(db storage.DB, host string) ([]*http.Cookie, error) {
b, err := db.Get(host)
if err != nil {
return nil, err
}
var result []*http.Cookie
err = json.Unmarshal(b, &result)
return result, err
}
func setCookies(db storage.DB, host string, cookies []*http.Cookie) error {
b, err := json.Marshal(cookies)
if err != nil {
return err
}
return db.Set(host, b)
}

59
broker/broker_test.go Normal file
View File

@@ -0,0 +1,59 @@
package broker
import (
"fmt"
"local/storage"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"golang.org/x/time/rate"
)
func TestDoCookies(t *testing.T) {
limiter = rate.NewLimiter(rate.Limit(20.0), 1)
calls := 0
db := storage.NewMap()
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls += 1
if calls == 1 {
http.SetCookie(w, &http.Cookie{
Name: "name",
Value: "value",
Expires: time.Now().Add(time.Hour),
})
} else {
if !strings.Contains(fmt.Sprint(r.Header["Cookie"]), "name=value") {
w.WriteHeader(http.StatusBadRequest)
t.Error("cookie not set on calls after first")
}
}
}))
defer s.Close()
req, _ := http.NewRequest(http.MethodGet, s.URL, nil)
if cookies, err := getCookies(db, "cookies_"+req.URL.Host); err == nil {
t.Fatal(err)
} else if len(cookies) != 0 {
t.Fatal(cookies)
}
for i := 0; i < 3; i++ {
resp, err := _do(db, req)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
resp.Body.Close()
if cookies, err := getCookies(db, "cookies_"+req.URL.Host); err != nil {
t.Fatal(err)
} else if len(cookies) == 0 {
t.Fatal(cookies)
}
}
}