From f7af98fc6ef283155f94f4294a430d8e792b9a7b Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Tue, 1 Dec 2020 13:01:51 +0100 Subject: removed explicit string function --- client/client.go | 12 ++++++------ handler.go | 8 ++++---- handler_test.go | 20 ++++++++++---------- instance.go | 16 ++++++---------- reqres_test.go | 6 +++--- 5 files changed, 29 insertions(+), 33 deletions(-) diff --git a/client/client.go b/client/client.go index 7c78034..21be669 100644 --- a/client/client.go +++ b/client/client.go @@ -99,7 +99,7 @@ func (c *Client) AddEntry(ctx context.Context, name, checksum []byte) (*stfe.StI } glog.V(3).Infof("created post data: %s", string(data)) - url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, stfe.EndpointAddEntry.String()}, "/") + url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, string(stfe.EndpointAddEntry)}, "/") req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) if err != nil { return nil, fmt.Errorf("failed creating http request: %v", err) @@ -126,7 +126,7 @@ func (c *Client) AddEntry(ctx context.Context, name, checksum []byte) (*stfe.StI // GetSth fetches and verifies the most recent STH. Safe to use without a // client chain and corresponding private key. func (c *Client) GetSth(ctx context.Context) (*stfe.StItem, error) { - url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, stfe.EndpointGetSth.String()}, "/") + url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, string(stfe.EndpointGetSth)}, "/") req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, fmt.Errorf("failed creating http request: %v", err) @@ -152,7 +152,7 @@ func (c *Client) GetSth(ctx context.Context) (*stfe.StItem, error) { // GetConsistencyProof fetches and verifies a consistency proof between two // STHs. Safe to use without a client chain and corresponding private key. func (c *Client) GetConsistencyProof(ctx context.Context, first, second *stfe.StItem) (*stfe.StItem, error) { - url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, stfe.EndpointGetConsistencyProof.String()}, "/") + url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, string(stfe.EndpointGetConsistencyProof)}, "/") req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, fmt.Errorf("failed creating http request: %v", err) @@ -181,7 +181,7 @@ func (c *Client) GetConsistencyProof(ctx context.Context, first, second *stfe.St // STH. Safe to use without a client chain and corresponding private key. func (c *Client) GetProofByHash(ctx context.Context, treeSize uint64, rootHash, leaf []byte) (*stfe.StItem, error) { leafHash := rfc6962.DefaultHasher.HashLeaf(leaf) - url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, stfe.EndpointGetProofByHash.String()}, "/") + url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, string(stfe.EndpointGetProofByHash)}, "/") req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, fmt.Errorf("failed creating http request: %v", err) @@ -214,7 +214,7 @@ func (c *Client) GetProofByHash(ctx context.Context, treeSize uint64, rootHash, // Note that a certificate chain is considered valid if it is chained correctly. // In other words, the caller may want to check whether the anchor is trusted. func (c *Client) GetEntries(ctx context.Context, start, end uint64) ([]*stfe.GetEntryResponse, error) { - url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, stfe.EndpointGetEntries.String()}, "/") + url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, string(stfe.EndpointGetEntries)}, "/") req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, fmt.Errorf("failed creating http request: %v", err) @@ -252,7 +252,7 @@ func (c *Client) GetEntries(ctx context.Context, start, end uint64) ([]*stfe.Get // GetAnchors fetches the log's trust anchors. Safe to use without a client // chain and corresponding private key. func (c *Client) GetAnchors(ctx context.Context) ([]*x509.Certificate, error) { - url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, stfe.EndpointGetAnchors.String()}, "/") + url := c.protocol() + strings.Join([]string{c.Log.BaseUrl, string(stfe.EndpointGetAnchors)}, "/") req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, fmt.Errorf("failed creating http request: %v", err) diff --git a/handler.go b/handler.go index d6d4224..bd1fbdb 100644 --- a/handler.go +++ b/handler.go @@ -26,16 +26,16 @@ func (a handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { var now time.Time = time.Now() var statusCode int defer func() { - rspcnt.Inc(a.instance.LogParameters.id(), a.endpoint.String(), fmt.Sprintf("%d", statusCode)) - latency.Observe(time.Now().Sub(now).Seconds(), a.instance.LogParameters.id(), a.endpoint.String(), fmt.Sprintf("%d", statusCode)) + rspcnt.Inc(a.instance.LogParameters.id(), string(a.endpoint), fmt.Sprintf("%d", statusCode)) + latency.Observe(time.Now().Sub(now).Seconds(), a.instance.LogParameters.id(), string(a.endpoint), fmt.Sprintf("%d", statusCode)) }() - reqcnt.Inc(a.instance.LogParameters.id(), a.endpoint.String()) + reqcnt.Inc(a.instance.LogParameters.id(), string(a.endpoint)) ctx, cancel := context.WithDeadline(r.Context(), now.Add(a.instance.Deadline)) defer cancel() if r.Method != a.method { - glog.Warningf("%s: got HTTP %s, wanted HTTP %s", a.instance.LogParameters.Prefix+a.endpoint.String(), r.Method, a.method) + glog.Warningf("%s/%s: got HTTP %s, wanted HTTP %s", a.instance.LogParameters.Prefix, string(a.endpoint), r.Method, a.method) a.sendHTTPError(w, http.StatusMethodNotAllowed, fmt.Errorf("method not allowed: %s", r.Method)) return } diff --git a/handler_test.go b/handler_test.go index bdf0752..0c8b1a6 100644 --- a/handler_test.go +++ b/handler_test.go @@ -82,11 +82,11 @@ func TestGetHandlersRejectPost(t *testing.T) { defer th.mockCtrl.Finish() for endpoint, handler := range th.getHandlers(t) { - t.Run(endpoint.String(), func(t *testing.T) { + t.Run(string(endpoint), func(t *testing.T) { s := httptest.NewServer(handler) defer s.Close() - url := strings.Join([]string{s.URL, th.instance.LogParameters.Prefix, endpoint.String()}, "/") + url := strings.Join([]string{s.URL, th.instance.LogParameters.Prefix, string(endpoint)}, "/") if rsp, err := http.Post(url, "application/json", nil); err != nil { t.Fatalf("http.Post(%s)=(_,%q), want (_,nil)", url, err) } else if rsp.StatusCode != http.StatusMethodNotAllowed { @@ -102,11 +102,11 @@ func TestPostHandlersRejectGet(t *testing.T) { defer th.mockCtrl.Finish() for endpoint, handler := range th.postHandlers(t) { - t.Run(endpoint.String(), func(t *testing.T) { + t.Run(string(endpoint), func(t *testing.T) { s := httptest.NewServer(handler) defer s.Close() - url := strings.Join([]string{s.URL, th.instance.LogParameters.Prefix, endpoint.String()}, "/") + url := strings.Join([]string{s.URL, th.instance.LogParameters.Prefix, string(endpoint)}, "/") if rsp, err := http.Get(url); err != nil { t.Fatalf("http.Get(%s)=(_,%q), want (_,nil)", url, err) } else if rsp.StatusCode != http.StatusMethodNotAllowed { @@ -121,7 +121,7 @@ func TestGetAnchors(t *testing.T) { th := newTestHandler(t, nil) defer th.mockCtrl.Finish() - url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, EndpointGetAnchors.String()}, "/") + url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, string(EndpointGetAnchors)}, "/") req, err := http.NewRequest("GET", url, nil) if err != nil { t.Fatalf("failed creating http request: %v", err) @@ -200,7 +200,7 @@ func TestGetEntries(t *testing.T) { th := newTestHandler(t, nil) defer th.mockCtrl.Finish() - url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, EndpointGetEntries.String()}, "/") + url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, string(EndpointGetEntries)}, "/") req, err := http.NewRequest("GET", url, nil) if err != nil { t.Fatalf("failed creating http request: %v", err) @@ -313,7 +313,7 @@ func TestAddEntry(t *testing.T) { th := newTestHandler(t, table.signer) defer th.mockCtrl.Finish() - url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, EndpointAddEntry.String()}, "/") + url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, string(EndpointAddEntry)}, "/") req, err := http.NewRequest("POST", url, table.breq) if err != nil { t.Fatalf("failed creating http request: %v", err) @@ -407,7 +407,7 @@ func TestGetSth(t *testing.T) { th := newTestHandler(t, table.signer) defer th.mockCtrl.Finish() - url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, EndpointGetSth.String()}, "/") + url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, string(EndpointGetSth)}, "/") req, err := http.NewRequest("GET", url, nil) if err != nil { t.Fatalf("failed creating http request: %v", err) @@ -511,7 +511,7 @@ func TestGetConsistencyProof(t *testing.T) { th := newTestHandler(t, nil) defer th.mockCtrl.Finish() - url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, EndpointGetConsistencyProof.String()}, "/") + url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, string(EndpointGetConsistencyProof)}, "/") req, err := http.NewRequest("GET", url, nil) if err != nil { t.Fatalf("failed creating http request: %v", err) @@ -620,7 +620,7 @@ func TestGetProofByHash(t *testing.T) { th := newTestHandler(t, nil) defer th.mockCtrl.Finish() - url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, EndpointGetProofByHash.String()}, "/") + url := strings.Join([]string{"http://example.com", th.instance.LogParameters.Prefix, string(EndpointGetProofByHash)}, "/") req, err := http.NewRequest("GET", url, nil) if err != nil { t.Fatalf("failed creating http request: %v", err) diff --git a/instance.go b/instance.go index 8d9070a..1eb2b7c 100644 --- a/instance.go +++ b/instance.go @@ -28,10 +28,6 @@ const ( EndpointGetSth = Endpoint("get-sth") ) -func (e Endpoint) String() string { - return string(e) -} - // TODO: type EndpointParam string? // Instance is an instance of a particular log front-end @@ -122,27 +118,27 @@ func (i *Instance) registerHandlers(mux *http.ServeMux) { handler handler }{ { - strings.Join([]string{"", i.LogParameters.Prefix, EndpointAddEntry.String()}, "/"), + strings.Join([]string{"", i.LogParameters.Prefix, string(EndpointAddEntry)}, "/"), handler{instance: i, handler: addEntry, endpoint: EndpointAddEntry, method: http.MethodPost}, }, { - strings.Join([]string{"", i.LogParameters.Prefix, EndpointGetEntries.String()}, "/"), + strings.Join([]string{"", i.LogParameters.Prefix, string(EndpointGetEntries)}, "/"), handler{instance: i, handler: getEntries, endpoint: EndpointGetEntries, method: http.MethodGet}, }, { - strings.Join([]string{"", i.LogParameters.Prefix, EndpointGetAnchors.String()}, "/"), + strings.Join([]string{"", i.LogParameters.Prefix, string(EndpointGetAnchors)}, "/"), handler{instance: i, handler: getAnchors, endpoint: EndpointGetAnchors, method: http.MethodGet}, }, { - strings.Join([]string{"", i.LogParameters.Prefix, EndpointGetProofByHash.String()}, "/"), + strings.Join([]string{"", i.LogParameters.Prefix, string(EndpointGetProofByHash)}, "/"), handler{instance: i, handler: getProofByHash, endpoint: EndpointGetProofByHash, method: http.MethodGet}, }, { - strings.Join([]string{"", i.LogParameters.Prefix, EndpointGetConsistencyProof.String()}, "/"), + strings.Join([]string{"", i.LogParameters.Prefix, string(EndpointGetConsistencyProof)}, "/"), handler{instance: i, handler: getConsistencyProof, endpoint: EndpointGetConsistencyProof, method: http.MethodGet}, }, { - strings.Join([]string{"", i.LogParameters.Prefix, EndpointGetSth.String()}, "/"), + strings.Join([]string{"", i.LogParameters.Prefix, string(EndpointGetSth)}, "/"), handler{instance: i, handler: getSth, endpoint: EndpointGetSth, method: http.MethodGet}, }, } { diff --git a/reqres_test.go b/reqres_test.go index 8334bd7..fadfb73 100644 --- a/reqres_test.go +++ b/reqres_test.go @@ -66,7 +66,7 @@ func TestNewGetEntriesRequest(t *testing.T) { end: fmt.Sprintf("%d", testMaxRange-1), }, } { - url := strings.Join([]string{"http://example.com/", lp.Prefix, EndpointGetEntries.String()}, "/") + url := strings.Join([]string{"http://example.com/", lp.Prefix, string(EndpointGetEntries)}, "/") r, err := http.NewRequest("GET", url, nil) if err != nil { t.Fatalf("must make http request in test %q: %v", table.description, err) @@ -137,7 +137,7 @@ func TestNewGetProofByHashRequest(t *testing.T) { hash: b64(testNodeHash), }, } { - url := strings.Join([]string{"http://example.com/", lp.Prefix, EndpointGetProofByHash.String()}, "/") + url := strings.Join([]string{"http://example.com/", lp.Prefix, string(EndpointGetProofByHash)}, "/") r, err := http.NewRequest("GET", url, nil) if err != nil { t.Fatalf("must make http request in test %q: %v", table.description, err) @@ -202,7 +202,7 @@ func TestNewGetConsistencyProofRequest(t *testing.T) { second: "2", }, } { - url := strings.Join([]string{"http://example.com/", lp.Prefix, EndpointGetConsistencyProof.String()}, "/") + url := strings.Join([]string{"http://example.com/", lp.Prefix, string(EndpointGetConsistencyProof)}, "/") r, err := http.NewRequest("GET", url, nil) if err != nil { t.Fatalf("must make http request in test %q: %v", table.description, err) -- cgit v1.2.3