-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add readonly api to gateway #1389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
988c310
a786cf9
84aaada
b240796
7a3337f
592707d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"testing" | ||
|
||
"github.com/ipfs/go-ipfs/commands" | ||
corecommands "github.com/ipfs/go-ipfs/core/commands" | ||
) | ||
|
||
func assertHeaders(t *testing.T, resHeaders http.Header, reqHeaders map[string]string) { | ||
|
@@ -21,7 +22,13 @@ func TestDisallowedOrigin(t *testing.T) { | |
req, _ := http.NewRequest("GET", "http://example.com/foo", nil) | ||
req.Header.Add("Origin", "http://barbaz.com") | ||
|
||
handler := NewHandler(commands.Context{}, nil, "") | ||
commandList := map[*commands.Command]bool{} | ||
|
||
for _, cmd := range corecommands.Root.Subcommands { | ||
commandList[cmd] = true | ||
} | ||
|
||
handler := NewHandler(commands.Context{}, nil, "", commandList) | ||
handler.ServeHTTP(res, req) | ||
|
||
assertHeaders(t, res.Header(), map[string]string{ | ||
|
@@ -38,7 +45,13 @@ func TestWildcardOrigin(t *testing.T) { | |
req, _ := http.NewRequest("GET", "http://example.com/foo", nil) | ||
req.Header.Add("Origin", "http://foobar.com") | ||
|
||
handler := NewHandler(commands.Context{}, nil, "*") | ||
commandList := map[*commands.Command]bool{} | ||
|
||
for _, cmd := range corecommands.Root.Subcommands { | ||
commandList[cmd] = true | ||
} | ||
|
||
handler := NewHandler(commands.Context{}, nil, "*", commandList) | ||
handler.ServeHTTP(res, req) | ||
|
||
assertHeaders(t, res.Header(), map[string]string{ | ||
|
@@ -57,7 +70,13 @@ func TestAllowedMethod(t *testing.T) { | |
req.Header.Add("Origin", "http://www.foobar.com") | ||
req.Header.Add("Access-Control-Request-Method", "PUT") | ||
|
||
handler := NewHandler(commands.Context{}, nil, "http://www.foobar.com") | ||
commandList := map[*commands.Command]bool{} | ||
|
||
for _, cmd := range corecommands.Root.Subcommands { | ||
commandList[cmd] = true | ||
} | ||
|
||
handler := NewHandler(commands.Context{}, nil, "http://www.foobar.com", commandList) | ||
handler.ServeHTTP(res, req) | ||
|
||
assertHeaders(t, res.Header(), map[string]string{ | ||
|
@@ -69,3 +88,21 @@ func TestAllowedMethod(t *testing.T) { | |
"Access-Control-Expose-Headers": "", | ||
}) | ||
} | ||
|
||
func TestLimitingCommands(t *testing.T) { | ||
res := httptest.NewRecorder() | ||
req, _ := http.NewRequest("GET", "http://example.com/api/v0/cat", nil) | ||
|
||
query := req.URL.Query() | ||
query.Add("arg", "QmFooBar") | ||
req.URL.RawQuery = query.Encode() | ||
|
||
commandList := map[*commands.Command]bool{} | ||
|
||
handler := NewHandler(commands.Context{}, corecommands.Root, "*", commandList) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @travisperson another option is to define |
||
handler.ServeHTTP(res, req) | ||
|
||
if(res.Code != http.StatusForbidden) { | ||
t.Errorf("Invalid status code, expected `%d` received `%d`\nBody:\n %s", http.StatusForbidden, res.Code, res.Body) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,11 @@ import ( | |
"fmt" | ||
"net/http" | ||
"sync" | ||
"os" | ||
|
||
commands "github.com/ipfs/go-ipfs/commands" | ||
cmdsHttp "github.com/ipfs/go-ipfs/commands/http" | ||
corecommands "github.com/ipfs/go-ipfs/core/commands" | ||
core "github.com/ipfs/go-ipfs/core" | ||
id "github.com/ipfs/go-ipfs/p2p/protocol/identify" | ||
) | ||
|
@@ -25,26 +29,40 @@ func NewGateway(conf GatewayConfig) *Gateway { | |
} | ||
} | ||
|
||
func (g *Gateway) ServeOption() ServeOption { | ||
func (g *Gateway) ServeOption(cctx * commands.Context) ServeOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont think this is properly |
||
var commandList = map[*commands.Command]bool{} | ||
|
||
for _, cmd := range corecommands.Root.Subcommands { | ||
commandList[cmd] = cmd.GatewayAccess | ||
} | ||
|
||
return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) { | ||
gateway, err := newGatewayHandler(n, g.Config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
mux.Handle("/ipfs/", gateway) | ||
mux.Handle("/ipns/", gateway) | ||
|
||
if cctx != nil { | ||
origin := os.Getenv(originEnvKey) | ||
cmdHandler := cmdsHttp.NewHandler(*cctx, corecommands.Root, origin, commandList) | ||
mux.Handle(cmdsHttp.ApiPath+"/", cmdHandler) | ||
} | ||
return mux, nil | ||
} | ||
} | ||
|
||
func GatewayOption(writable bool) ServeOption { | ||
|
||
func GatewayOption(writable bool, cctx * commands.Context) ServeOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style fix: i think the |
||
g := NewGateway(GatewayConfig{ | ||
Writable: writable, | ||
BlockList: &BlockList{}, | ||
}) | ||
return g.ServeOption() | ||
return g.ServeOption(cctx) | ||
} | ||
|
||
|
||
func VersionOption() ServeOption { | ||
return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) { | ||
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http.StatusForbidden
is probably fine