mirror of
https://codeberg.org/scip/kleingebaeck.git
synced 2025-12-17 20:41:01 +01:00
first step in fixing #49:
fetch cookies from 1st response and use them in subsequent requests.
This commit is contained in:
27
fetch.go
27
fetch.go
@@ -20,8 +20,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/cookiejar"
|
||||||
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
// convenient wrapper to fetch some web content
|
// convenient wrapper to fetch some web content
|
||||||
@@ -29,13 +32,24 @@ type Fetcher struct {
|
|||||||
Config *Config
|
Config *Config
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
Useragent string // FIXME: make configurable
|
Useragent string // FIXME: make configurable
|
||||||
|
Cookies []*http.Cookie
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFetcher(c *Config) *Fetcher {
|
func NewFetcher(c *Config) *Fetcher {
|
||||||
|
jar, err := cookiejar.New(nil)
|
||||||
|
if err != nil {
|
||||||
|
// cannot return error here, FIXME
|
||||||
|
log.Fatalf("Got error while creating cookie jar %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
return &Fetcher{
|
return &Fetcher{
|
||||||
Client: &http.Client{Transport: &loggingTransport{}}, // implemented in http.go
|
Client: &http.Client{
|
||||||
|
Transport: &loggingTransport{}, // implemented in http.go
|
||||||
|
Jar: jar,
|
||||||
|
},
|
||||||
Useragent: Useragent, // default in config.go
|
Useragent: Useragent, // default in config.go
|
||||||
Config: c,
|
Config: c,
|
||||||
|
Cookies: []*http.Cookie{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,6 +61,15 @@ func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
|
|||||||
|
|
||||||
req.Header.Set("User-Agent", f.Useragent)
|
req.Header.Set("User-Agent", f.Useragent)
|
||||||
|
|
||||||
|
if len(f.Cookies) > 0 {
|
||||||
|
uriobj, _ := url.Parse(Baseuri)
|
||||||
|
slog.Debug("have cookies, sending them",
|
||||||
|
"sample-cookie-name", f.Cookies[0].Name,
|
||||||
|
"sample-cookie-expire", f.Cookies[0].Expires,
|
||||||
|
)
|
||||||
|
f.Client.Jar.SetCookies(uriobj, f.Cookies)
|
||||||
|
}
|
||||||
|
|
||||||
res, err := f.Client.Do(req)
|
res, err := f.Client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -56,6 +79,8 @@ func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
|
|||||||
return nil, errors.New("could not get page via HTTP")
|
return nil, errors.New("could not get page via HTTP")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f.Cookies = res.Cookies()
|
||||||
|
|
||||||
return res.Body, nil
|
return res.Body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user