Skip to content

Commit

Permalink
Add go-libipni user-agent header with version to HTTTP announce (#60)
Browse files Browse the repository at this point in the history
- Replaces #58
- Fixes #59
  • Loading branch information
gammazero authored Jun 19, 2023
1 parent 5cba614 commit 52807e6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
18 changes: 15 additions & 3 deletions announce/httpsender/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"fmt"
"net/http"
"time"

"github.com/ipni/go-libipni"
)

const defaultTimeout = time.Minute

type config struct {
timeout time.Duration
client *http.Client
timeout time.Duration
client *http.Client
userAgent string
}

// Option is a function that sets a value in a config.
Expand All @@ -19,7 +22,8 @@ type Option func(*config) error
// getOpts creates a config and applies Options to it.
func getOpts(opts []Option) (config, error) {
cfg := config{
timeout: defaultTimeout,
timeout: defaultTimeout,
userAgent: "go-libipni/" + libipni.Release,
}
for i, opt := range opts {
if err := opt(&cfg); err != nil {
Expand All @@ -44,3 +48,11 @@ func WithClient(c *http.Client) Option {
return nil
}
}

// WithUserAgent sets the value used for the User-Agent header.
func WithUserAgent(userAgent string) Option {
return func(cfg *config) error {
cfg.userAgent = userAgent
return nil
}
}
3 changes: 3 additions & 0 deletions announce/httpsender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Sender struct {
announceURLs []string
client *http.Client
peerID peer.ID
userAgent string
}

// New creates a new Sender that sends announce messages over HTTP. Announce
Expand Down Expand Up @@ -69,6 +70,7 @@ func New(announceURLs []*url.URL, peerID peer.ID, options ...Option) (*Sender, e
announceURLs: urls,
client: client,
peerID: peerID,
userAgent: opts.userAgent,
}, nil
}

Expand Down Expand Up @@ -165,6 +167,7 @@ func (s *Sender) sendAnnounce(ctx context.Context, announceURL string, buf *byte
if err != nil {
return err
}
req.Header.Set("User-Agent", s.userAgent)
if js {
req.Header.Set("Content-Type", "application/json")
} else {
Expand Down
3 changes: 3 additions & 0 deletions announce/httpsender/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func init() {
func TestSend(t *testing.T) {
var count int
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check for expected User-Agent
require.True(t, strings.HasPrefix(r.UserAgent(), "go-libipni/"))

// Decode CID and originator addresses from message.
an := message.Message{}
err := an.UnmarshalCBOR(r.Body)
Expand Down
41 changes: 41 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package libipni

import (
_ "embed"
"encoding/json"
"runtime/debug"
)

var (
// Release is the release version tag value, e.g. "v1.2.3"
Release string
// Revision is the git commit hash.
Revision string
// Version is the full version string: Release-Revision.
Version string
)

//go:embed version.json
var versionJSON []byte

func init() {
// Read version from embedded JSON file.
var verMap map[string]string
json.Unmarshal(versionJSON, &verMap)
Release = verMap["version"]

// If running from a module, try to get the build info.
bi, ok := debug.ReadBuildInfo()
if !ok {
return
}

// Append the revision to the version.
for i := range bi.Settings {
if bi.Settings[i].Key == "vcs.revision" {
Revision = bi.Settings[i].Value
Version = Release + "-" + Revision
break
}
}
}

0 comments on commit 52807e6

Please sign in to comment.