Skip to content

Commit

Permalink
Merge pull request ipfs/kubo#6820 from ipfs/fix/gateway-cleanup
Browse files Browse the repository at this point in the history
gateway cleanups

This commit was moved from ipfs/kubo@ff26934
  • Loading branch information
Stebalien committed Jan 10, 2020
2 parents 688c4b9 + ef0dc27 commit 40fe57b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
4 changes: 2 additions & 2 deletions gateway/core/corehttp/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion gateway/core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 7 additions & 8 deletions gateway/core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
26 changes: 13 additions & 13 deletions gateway/core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -492,15 +492,15 @@ 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)
}
req.Host = "example.net"
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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion gateway/core/corehttp/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gateway/core/corehttp/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 40fe57b

Please sign in to comment.