diff --git a/core/corehttp/commands.go b/core/corehttp/commands.go index a198ea11b35..867805ce2da 100644 --- a/core/corehttp/commands.go +++ b/core/corehttp/commands.go @@ -89,7 +89,7 @@ func addCORSDefaults(c *cmdsHttp.ServerConfig) { // by default, use GET, PUT, POST if len(c.AllowedMethods()) == 0 { - c.SetAllowedMethods("GET", "POST", "PUT") + c.SetAllowedMethods(http.MethodGet, http.MethodPost, http.MethodPut) } } @@ -121,7 +121,7 @@ func commandsOption(cctx oldcmds.Context, command *cmds.Command) ServeOption { return func(n *core.IpfsNode, l net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { cfg := cmdsHttp.NewServerConfig() - cfg.SetAllowedMethods("GET", "POST", "PUT") + cfg.SetAllowedMethods(http.MethodGet, http.MethodPost, http.MethodPut) cfg.APIPath = APIPath rcfg, err := n.Repo.Config() if err != nil { diff --git a/core/corehttp/gateway.go b/core/corehttp/gateway.go index e294cd308e7..f400c515b97 100644 --- a/core/corehttp/gateway.go +++ b/core/corehttp/gateway.go @@ -69,7 +69,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption { } if _, ok := headers[ACAMethodsName]; !ok { // Default to GET - headers[ACAMethodsName] = []string{"GET"} + headers[ACAMethodsName] = []string{http.MethodGet} } headers[ACAHeadersName] = cleanHeaderSet( diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index a501e21ddde..62d151ee4c6 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -83,24 +83,23 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if i.config.Writable { switch r.Method { - case "POST": + case http.MethodPost: i.postHandler(w, r) return - case "PUT": + case http.MethodPut: i.putHandler(w, r) return - case "DELETE": + case http.MethodDelete: i.deleteHandler(w, r) return } } - if r.Method == "GET" || r.Method == "HEAD" { + switch r.Method { + case http.MethodGet, http.MethodHead: i.getOrHeadHandler(w, r) return - } - - if r.Method == "OPTIONS" { + case http.MethodOptions: i.optionsHandler(w, r) return } @@ -298,7 +297,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return } - if r.Method == "HEAD" { + if r.Method == http.MethodHead { return } diff --git a/core/corehttp/gateway_test.go b/core/corehttp/gateway_test.go index c37f9082e3d..9128aa0175d 100644 --- a/core/corehttp/gateway_test.go +++ b/core/corehttp/gateway_test.go @@ -202,7 +202,7 @@ func TestGatewayGet(t *testing.T) { {"example.man", "/", http.StatusOK, "fnord"}, } { var c http.Client - r, err := http.NewRequest("GET", ts.URL+test.path, nil) + r, err := http.NewRequest(http.MethodGet, ts.URL+test.path, nil) if err != nil { t.Fatal(err) } @@ -259,7 +259,7 @@ func TestIPNSHostnameRedirect(t *testing.T) { ns["/ipns/example.net"] = path.FromString(k.String()) // make request to directory containing index.html - req, err := http.NewRequest("GET", ts.URL+"/foo", nil) + req, err := http.NewRequest(http.MethodGet, ts.URL+"/foo", nil) if err != nil { t.Fatal(err) } @@ -282,7 +282,7 @@ func TestIPNSHostnameRedirect(t *testing.T) { } // make request with prefix to directory containing index.html - req, err = http.NewRequest("GET", ts.URL+"/foo", nil) + req, err = http.NewRequest(http.MethodGet, ts.URL+"/foo", nil) if err != nil { t.Fatal(err) } @@ -306,7 +306,7 @@ func TestIPNSHostnameRedirect(t *testing.T) { } // make sure /version isn't exposed - req, err = http.NewRequest("GET", ts.URL+"/version", nil) + req, err = http.NewRequest(http.MethodGet, ts.URL+"/version", nil) if err != nil { t.Fatal(err) } @@ -359,7 +359,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) { ns["/ipns/example.net"] = path.FromString(k.String()) // make request to directory listing - req, err := http.NewRequest("GET", ts.URL+"/foo%3F%20%23%3C%27/", nil) + req, err := http.NewRequest(http.MethodGet, ts.URL+"/foo%3F%20%23%3C%27/", nil) if err != nil { t.Fatal(err) } @@ -392,7 +392,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) { } // make request to directory listing at root - req, err = http.NewRequest("GET", ts.URL, nil) + req, err = http.NewRequest(http.MethodGet, ts.URL, nil) if err != nil { t.Fatal(err) } @@ -425,7 +425,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) { } // make request to directory listing - req, err = http.NewRequest("GET", ts.URL+"/foo%3F%20%23%3C%27/bar/", nil) + req, err = http.NewRequest(http.MethodGet, ts.URL+"/foo%3F%20%23%3C%27/bar/", nil) if err != nil { t.Fatal(err) } @@ -458,7 +458,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) { } // make request to directory listing with prefix - req, err = http.NewRequest("GET", ts.URL, nil) + req, err = http.NewRequest(http.MethodGet, ts.URL, nil) if err != nil { t.Fatal(err) } @@ -492,7 +492,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) { } // make request to directory listing with illegal prefix - req, err = http.NewRequest("GET", ts.URL, nil) + req, err = http.NewRequest(http.MethodGet, ts.URL, nil) if err != nil { t.Fatal(err) } @@ -500,7 +500,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) { req.Header.Set("X-Ipfs-Gateway-Prefix", "/bad-prefix") // make request to directory listing with evil prefix - req, err = http.NewRequest("GET", ts.URL, nil) + req, err = http.NewRequest(http.MethodGet, ts.URL, nil) if err != nil { t.Fatal(err) } @@ -539,7 +539,7 @@ func TestCacheControlImmutable(t *testing.T) { t.Logf("test server url: %s", ts.URL) defer ts.Close() - req, err := http.NewRequest("GET", ts.URL+emptyDir+"/", nil) + req, err := http.NewRequest(http.MethodGet, ts.URL+emptyDir+"/", nil) if err != nil { t.Fatal(err) } @@ -566,7 +566,7 @@ func TestGoGetSupport(t *testing.T) { defer ts.Close() // mimic go-get - req, err := http.NewRequest("GET", ts.URL+emptyDir+"?go-get=1", nil) + req, err := http.NewRequest(http.MethodGet, ts.URL+emptyDir+"?go-get=1", nil) if err != nil { t.Fatal(err) } @@ -589,7 +589,7 @@ func TestVersion(t *testing.T) { t.Logf("test server url: %s", ts.URL) defer ts.Close() - req, err := http.NewRequest("GET", ts.URL+"/version", nil) + req, err := http.NewRequest(http.MethodGet, ts.URL+"/version", nil) if err != nil { t.Fatal(err) } diff --git a/core/corehttp/option_test.go b/core/corehttp/option_test.go index 17fcefe09ce..57f8d3b1672 100644 --- a/core/corehttp/option_test.go +++ b/core/corehttp/option_test.go @@ -37,7 +37,7 @@ func TestCheckVersionOption(t *testing.T) { for _, tc := range tcs { t.Logf("%#v", tc) - r := httptest.NewRequest("POST", tc.uri, nil) + r := httptest.NewRequest(http.MethodPost, tc.uri, nil) r.Header.Add("User-Agent", tc.userAgent) // old version, should fail called := false diff --git a/core/corehttp/proxy_test.go b/core/corehttp/proxy_test.go index 98d749e0c50..9f99463d9e7 100644 --- a/core/corehttp/proxy_test.go +++ b/core/corehttp/proxy_test.go @@ -26,7 +26,7 @@ var validtestCases = []TestCase{ func TestParseRequest(t *testing.T) { for _, tc := range validtestCases { url := tc.urlprefix + "/p2p/" + tc.target + tc.name + "/" + tc.path - req, _ := http.NewRequest("GET", url, strings.NewReader("")) + req, _ := http.NewRequest(http.MethodGet, url, strings.NewReader("")) parsed, err := parseRequest(req) if err != nil { @@ -46,7 +46,7 @@ var invalidtestCases = []string{ func TestParseRequestInvalidPath(t *testing.T) { for _, tc := range invalidtestCases { url := tc - req, _ := http.NewRequest("GET", url, strings.NewReader("")) + req, _ := http.NewRequest(http.MethodGet, url, strings.NewReader("")) _, err := parseRequest(req) if err == nil { diff --git a/repo/fsrepo/migrations/migrations.go b/repo/fsrepo/migrations/migrations.go index de71de60d9d..d6be54947ad 100644 --- a/repo/fsrepo/migrations/migrations.go +++ b/repo/fsrepo/migrations/migrations.go @@ -169,7 +169,7 @@ func GetLatestVersion(ipfspath, dist string) (string, error) { } func httpGet(url string) (*http.Response, error) { - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return nil, fmt.Errorf("http.NewRequest error: %s", err) }