1
0
Fork 0

resolver: make decoders return string instead of byte slice

This commit is contained in:
Henrik Hautakoski 2026-02-19 10:23:11 +01:00
parent ae79cd131e
commit c36a83c9b8
3 changed files with 16 additions and 20 deletions

View file

@ -1,6 +1,7 @@
package http
import (
"bufio"
"context"
"io"
"net"
@ -11,7 +12,7 @@ import (
"dnsupdater/ip/internal"
)
type Decoder func(io.Reader) ([]byte, error)
type Decoder func(io.Reader) (string, error)
type Service struct {
ServiceName string
@ -31,16 +32,19 @@ func (s Service) Lookup(ctx context.Context) (net.IP, error) {
}
if s.Decoder == nil {
s.Decoder = io.ReadAll
s.Decoder = func(r io.Reader) (string, error) {
line, err := bufio.NewReader(r).ReadString('\n')
if err == nil || err == io.EOF {
line = strings.TrimRight(line, "\r\n")
line = strings.TrimSpace(line)
}
return line, err
}
}
body, err := s.Decoder(resp.Body)
if err != nil {
if err != nil && err != io.EOF {
return nil, err
}
// Trim spaces and stuff.
ip_str := strings.TrimSpace(string(body))
return internal.ParseIP(ip_str)
return internal.ParseIP(string(body))
}