Commit 52bc2982 authored by keks's avatar keks Committed by Jeromy

fix ServerNameOption

License: MIT
Signed-off-by: default avatarkeks <keks@cryptoscope.co>
parent 8294f0a3
# TODO before merge
- return an error if a http client connects with User-Agent ~ '^/go-ipfs/` but not our exact version
......@@ -433,8 +433,8 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (error, <-chan error
var opts = []corehttp.ServeOption{
corehttp.MetricsCollectionOption("api"),
corehttp.CheckVersionOption(),
corehttp.CommandsOption(*cctx),
//corehttp.CheckVersionOption(),
//corehttp.ServerNameOption("go-ipfs/" + config.CurrentVersionNumber),
corehttp.WebUIOption,
gatewayOpt,
......@@ -529,11 +529,11 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (error, <-chan e
var opts = []corehttp.ServeOption{
corehttp.MetricsCollectionOption("gateway"),
corehttp.CheckVersionOption(),
corehttp.CommandsROOption(*cctx),
corehttp.VersionOption(),
corehttp.IPNSHostnameOption(),
corehttp.GatewayOption(writable, "/ipfs", "/ipns"),
corehttp.CheckVersionOption(),
}
if len(cfg.Gateway.RootRedirect) > 0 {
......
......@@ -144,11 +144,13 @@ func CommandsROOption(cctx oldcmds.Context) ServeOption {
func CheckVersionOption() ServeOption {
daemonVersion := config.ApiVersion
return ServeOption(func(n *core.IpfsNode, l net.Listener, next *http.ServeMux) (*http.ServeMux, error) {
return ServeOption(func(n *core.IpfsNode, l net.Listener, parent *http.ServeMux) (*http.ServeMux, error) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
parent.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, APIPath) {
pth := path.SplitList(r.URL.Path[len(APIPath):])
cmdqry := r.URL.Path[len(APIPath):]
pth := path.SplitList(cmdqry)
// backwards compatibility to previous version check
if pth[1] != "version" {
clientVersion := r.UserAgent()
......@@ -160,7 +162,7 @@ func CheckVersionOption() ServeOption {
}
}
next.ServeHTTP(w, r)
mux.ServeHTTP(w, r)
})
return mux, nil
......
......@@ -41,8 +41,13 @@ func TestCheckVersionOption(t *testing.T) {
r.Header.Add("User-Agent", tc.userAgent) // old version, should fail
called := false
inner := http.NewServeMux()
inner.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
root := http.NewServeMux()
mux, err := CheckVersionOption()(nil, nil, root)
if err != nil {
t.Fatal(err)
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
called = true
if !tc.shouldHandle {
t.Error("handler was called even though version didn't match")
......@@ -51,14 +56,9 @@ func TestCheckVersionOption(t *testing.T) {
}
})
mux, err := CheckVersionOption()(nil, nil, inner)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
root.ServeHTTP(w, r)
if tc.shouldHandle && !called {
t.Error("handler wasn't called even though it should have")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment