Skip to content

Commit

Permalink
Send the service credentials to initial /api request
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Oct 19, 2015
1 parent 5ed3240 commit 637cb23
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
31 changes: 21 additions & 10 deletions xfer/http_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ var fastClient = http.Client{

// NewHTTPPublisher returns an HTTPPublisher ready for use.
func NewHTTPPublisher(target, token, probeID string) (string, *HTTPPublisher, error) {
targetAPI := sanitize.URL("http://", 0, "/api")(target)
resp, err := fastClient.Get(targetAPI)
p := &HTTPPublisher{
url: sanitize.URL("http://", 0, "/api/report")(target),
token: token,
probeID: probeID,
}
req, err := p.authorizedRequest("GET", sanitize.URL("http://", 0, "/api")(target), nil)
if err != nil {
return "", nil, err
}
resp, err := fastClient.Do(req)
if err != nil {
return "", nil, err
}
Expand All @@ -35,11 +43,16 @@ func NewHTTPPublisher(target, token, probeID string) (string, *HTTPPublisher, er
if err := json.NewDecoder(resp.Body).Decode(&apiResponse); err != nil {
return "", nil, err
}
return apiResponse.ID, &HTTPPublisher{
url: sanitize.URL("http://", 0, "/api/report")(target),
token: token,
probeID: probeID,
}, nil
return apiResponse.ID, p, nil
}

func (p HTTPPublisher) authorizedRequest(method string, urlStr string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, urlStr, body)
if err == nil {
req.Header.Set("Authorization", AuthorizationHeader(p.token))
req.Header.Set(ScopeProbeIDHeader, p.probeID)
}
return req, err
}

func (p HTTPPublisher) String() string {
Expand All @@ -48,12 +61,10 @@ func (p HTTPPublisher) String() string {

// Publish publishes the report to the URL.
func (p HTTPPublisher) Publish(r io.Reader) error {
req, err := http.NewRequest("POST", p.url, r)
req, err := p.authorizedRequest("POST", p.url, r)
if err != nil {
return err
}
req.Header.Set("Authorization", AuthorizationHeader(p.token))
req.Header.Set(ScopeProbeIDHeader, p.probeID)
req.Header.Set("Content-Encoding", "gzip")
// req.Header.Set("Content-Type", "application/binary") // TODO: we should use http.DetectContentType(..) on the gob'ed

Expand Down
12 changes: 7 additions & 5 deletions xfer/http_publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ func TestHTTPPublisher(t *testing.T) {
)

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api" {
_ = json.NewEncoder(w).Encode(map[string]string{"id": "irrelevant"})
return
}

if want, have := xfer.AuthorizationHeader(token), r.Header.Get("Authorization"); want != have {
t.Errorf("want %q, have %q", want, have)
}

if want, have := id, r.Header.Get(xfer.ScopeProbeIDHeader); want != have {
t.Errorf("want %q, have %q", want, have)
}

if r.URL.Path == "/api" {
_ = json.NewEncoder(w).Encode(map[string]string{"id": "irrelevant"})
return
}

var have report.Report

reader := r.Body
Expand Down

0 comments on commit 637cb23

Please sign in to comment.