behavior changes: UserAgent configurable, test cookies, check errors

This commit is contained in:
2024-01-24 19:19:31 +01:00
committed by T.v.Dein
parent 8df3ebfa6d
commit 9cd1fc0596
8 changed files with 77 additions and 26 deletions

View File

@@ -20,7 +20,6 @@ package main
import (
"errors"
"io"
"log"
"log/slog"
"net/http"
"net/http/cookiejar"
@@ -29,28 +28,26 @@ import (
// convenient wrapper to fetch some web content
type Fetcher struct {
Config *Config
Client *http.Client
Useragent string // FIXME: make configurable
Cookies []*http.Cookie
Config *Config
Client *http.Client
Cookies []*http.Cookie
}
func NewFetcher(c *Config) *Fetcher {
func NewFetcher(c *Config) (*Fetcher, error) {
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 nil, err
}
return &Fetcher{
Client: &http.Client{
Transport: &loggingTransport{}, // implemented in http.go
Jar: jar,
Client: &http.Client{
Transport: &loggingTransport{}, // implemented in http.go
Jar: jar,
},
Config: c,
Cookies: []*http.Cookie{},
},
Useragent: Useragent, // default in config.go
Config: c,
Cookies: []*http.Cookie{},
}
nil
}
func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
@@ -59,7 +56,7 @@ func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
return nil, err
}
req.Header.Set("User-Agent", f.Useragent)
req.Header.Set("User-Agent", f.Config.UserAgent)
if len(f.Cookies) > 0 {
uriobj, _ := url.Parse(Baseuri)
@@ -79,6 +76,7 @@ func (f *Fetcher) Get(uri string) (io.ReadCloser, error) {
return nil, errors.New("could not get page via HTTP")
}
slog.Debug("got cookies?", "cookies", res.Cookies())
f.Cookies = res.Cookies()
return res.Body, nil