mirror of
https://codeberg.org/scip/kleingebaeck.git
synced 2025-12-16 20:11:01 +01:00
behavior changes: UserAgent configurable, test cookies, check errors
This commit is contained in:
30
fetch.go
30
fetch.go
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user