Add cookie header string constructor

This commit is contained in:
y a t s
2024-08-27 12:10:57 -04:00
parent bdba9d8e3d
commit 01b6837eea
5 changed files with 38 additions and 12 deletions

2
go.mod
View File

@@ -2,6 +2,6 @@ module github.com/y-a-t-s/libkiwi
go 1.23.0
require github.com/y-a-t-s/firebird v0.0.0-20240826182749-4000da15aa11
require github.com/y-a-t-s/firebird v0.0.0-20240827160906-63b3ab4765b6
require golang.org/x/net v0.28.0 // indirect

4
go.sum
View File

@@ -1,4 +1,4 @@
github.com/y-a-t-s/firebird v0.0.0-20240826182749-4000da15aa11 h1:K9RQObDfoXwiSSwxDYA8iRQeBCEQuxIJMtgXxzs4JdI=
github.com/y-a-t-s/firebird v0.0.0-20240826182749-4000da15aa11/go.mod h1:iaOF6bre/MgFaBH513kb3RxcJ+x8vSPPAeOjN7RNkn8=
github.com/y-a-t-s/firebird v0.0.0-20240827160906-63b3ab4765b6 h1:V720EMlceDtw5k7CKbYzvQjseyZuCB6Evssp6m9dXkE=
github.com/y-a-t-s/firebird v0.0.0-20240827160906-63b3ab4765b6/go.mod h1:iaOF6bre/MgFaBH513kb3RxcJ+x8vSPPAeOjN7RNkn8=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=

12
jar.go
View File

@@ -1,6 +1,7 @@
package libkiwi
import (
"fmt"
"net/http"
"net/url"
"sync"
@@ -78,6 +79,17 @@ func (kj *KiwiJar) Cookies(u *url.URL) []*http.Cookie {
return <-res
}
func (kj *KiwiJar) CookieString(u *url.URL) (cookies string) {
cs := kj.Cookies(u)
for _, c := range cs {
cookies += fmt.Sprintf("; %s=%s", c.Name, c.Value)
}
// Remove leading semicolon+space.
cookies = cookies[2:]
return
}
func (kj *KiwiJar) GetCookie(u *url.URL, name string) *http.Cookie {
kj.checkAlloc(u)

View File

@@ -10,7 +10,7 @@ import (
)
type KF struct {
client http.Client
Client http.Client
domain *url.URL
}
@@ -32,7 +32,7 @@ func NewKF(hc http.Client, host string, cookies string) (kf *KF, err error) {
hc.Jar = jar
kf = &KF{
client: hc,
Client: hc,
domain: u,
}
@@ -51,7 +51,7 @@ func (kf *KF) GetPage(u *url.URL) (resp *http.Response, err error) {
}
// req.Header.Set("Cookie", kf.cookies)
resp, err = kf.client.Do(req)
resp, err = kf.Client.Do(req)
if err != nil {
return
}
@@ -71,7 +71,7 @@ func (kf *KF) GetPage(u *url.URL) (resp *http.Response, err error) {
func (kf *KF) RefreshSession() (tk string, err error) {
// Clear any existing session token to request a new one.
kf.client.Jar.(*KiwiJar).SetCookie(kf.domain, &http.Cookie{
kf.Client.Jar.(*KiwiJar).SetCookie(kf.domain, &http.Cookie{
Name: "xf_session",
Value: "",
})
@@ -87,7 +87,7 @@ func (kf *KF) RefreshSession() (tk string, err error) {
}
func (kf *KF) solveKiwiFlare() error {
c, err := firebird.NewChallenge(kf.client, kf.domain.String())
c, err := firebird.NewChallenge(kf.Client, kf.domain.String())
if err != nil {
return err
}
@@ -95,7 +95,7 @@ func (kf *KF) solveKiwiFlare() error {
if err != nil {
return err
}
_, err = firebird.Submit(kf.client, kf.domain.String(), s)
_, err = firebird.Submit(kf.Client, kf.domain.String(), s)
if err != nil {
return err
}

View File

@@ -3,15 +3,16 @@ package libkiwi
import (
"log"
"net/http"
"net/url"
"os"
"testing"
)
const HOST string = "kiwifarms.st"
const TEST_HOST = "kiwifarms.st"
func TestGetPage(t *testing.T) {
cookies := os.Getenv("TEST_COOKIES")
kf, err := NewKF(http.Client{}, HOST, cookies)
kf, err := NewKF(http.Client{}, TEST_HOST, cookies)
if err != nil {
t.Error(err)
}
@@ -32,7 +33,7 @@ func TestGetPage(t *testing.T) {
func TestRefreshSession(t *testing.T) {
cookies := os.Getenv("TEST_COOKIES")
kf, err := NewKF(http.Client{}, HOST, cookies)
kf, err := NewKF(http.Client{}, TEST_HOST, cookies)
if err != nil {
t.Error(err)
}
@@ -43,3 +44,16 @@ func TestRefreshSession(t *testing.T) {
}
log.Println("New xf_session token: " + tk)
}
func TestCookieString(t *testing.T) {
cookies := os.Getenv("TEST_COOKIES")
kf, err := NewKF(http.Client{}, TEST_HOST, cookies)
if err != nil {
t.Error(err)
}
u, err := url.Parse("https://" + TEST_HOST)
if err != nil {
t.Error(err)
}
log.Println("Cookies from jar: " + kf.Client.Jar.(*KiwiJar).CookieString(u))
}