Skip to content

Commit

Permalink
fix header for http dialer
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Oct 27, 2024
1 parent ba708c3 commit 0796a33
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ func main() {
Addr: endpoint.String(),
Dialer: &fastdns.HTTPDialer{
Endpoint: endpoint,
UserAgent: "fastdns/0.9",
Header: http.Header{
"content-type": {"application/dns-message"},
"user-agent": {"fastdns/1.0"},
},
Transport: &http.Transport{
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
Expand Down
15 changes: 9 additions & 6 deletions client_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ type HTTPDialer struct {
// This is the base address used for sending HTTP requests.
Endpoint *url.URL

// UserAgent defines the User-Agent header that will be included in the HTTP requests
// sent by this dialer. It can be customized for specific needs.
UserAgent string

// Transport allows for customizing the underlying transport mechanism used
// for making HTTP requests. If set, it overrides the default RoundTripper behavior.
Transport http.RoundTripper

// Header defines the request header that will be sent in the HTTP requests.
// It can be customized for specific needs, E.g. User-Agent.
Header http.Header
}

func (d *HTTPDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
Expand All @@ -176,6 +176,9 @@ func (d *HTTPDialer) DialContext(ctx context.Context, network, addr string) (net
c.req.Body = nil
c.req.URL = d.Endpoint
c.req.Host = d.Endpoint.Host
if d.Header != nil {
c.req.Header = d.Header
}
c.reader.B = nil
c.writer.B = c.writer.B[:0]
c.resp = nil
Expand Down Expand Up @@ -286,8 +289,8 @@ var httpconnpool = sync.Pool{
req: &http.Request{
Method: http.MethodPost,
Header: http.Header{
"content-type": []string{"application/dns-message"},
"user-agent": []string{"fastdns/1.0"},
"content-type": {"application/dns-message"},
"user-agent": {"fastdns/1.0"},
},
},
reader: new(bufferreader),
Expand Down
10 changes: 6 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ func TestClientLookup(t *testing.T) {
{
Addr: "https://1.1.1.1/dns-query",
Dialer: &HTTPDialer{
Endpoint: func() (u *url.URL) { u, _ = url.Parse("https://1.1.1.1/dns-query"); return }(),
UserAgent: "fastdns/0.9",
Endpoint: func() (u *url.URL) { u, _ = url.Parse("https://1.1.1.1/dns-query"); return }(),
},
},
}
Expand Down Expand Up @@ -289,8 +288,11 @@ func BenchmarkResolverFastdnsHTTP(b *testing.B) {
resolver := &Client{
Addr: server,
Dialer: &HTTPDialer{
Endpoint: func() (u *url.URL) { u, _ = url.Parse("https://" + server + "/dns-query"); return }(),
UserAgent: "fastdns/0.9",
Endpoint: func() (u *url.URL) { u, _ = url.Parse("https://" + server + "/dns-query"); return }(),
Header: http.Header{
"content-type": {"application/dns-message"},
"user-agent": {"fastdns/1.0"},
},
Transport: &http.Transport{
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
Expand Down
8 changes: 6 additions & 2 deletions cmd/fastdig/fastdig.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"fmt"
"net"
"net/http"
"net/netip"
"net/url"
"os"
Expand All @@ -29,8 +30,11 @@ func main() {
os.Exit(1)
}
client.Dialer = &fastdns.HTTPDialer{
Endpoint: endpoint,
UserAgent: "fastdig/0.9",
Endpoint: endpoint,
Header: http.Header{
"content-type": {"application/dns-message"},
"user-agent": {"fastdns/1.0"},
},
}
}

Expand Down

0 comments on commit 0796a33

Please sign in to comment.