@cryptotaxi247 / kubo / commits / 52bc29823

fix ServerNameOption

License: MIT Signed-off-by: keks <keks@cryptoscope.co>

keks committed Dec 18, 2017 at 17:29 UTC 52bc29823066fccf3c05ed72e9c01d5757d75af9
4 files changed +16 -15
TODO.md
-1
@@ -1,3 +1,2 @@
1 # TODO before merge
2
3 -- return an error if a http client connects with User-Agent ~ '^/go-ipfs/` but not our exact version
cmd/ipfs/daemon.go
+2 -2
@@ -433,8 +433,8 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (error, <-chan error
433
434 var opts = []corehttp.ServeOption{
435 corehttp.MetricsCollectionOption("api"),
436 + corehttp.CheckVersionOption(),
437 corehttp.CommandsOption(*cctx),
437 - //corehttp.CheckVersionOption(),
438 //corehttp.ServerNameOption("go-ipfs/" + config.CurrentVersionNumber),
439 corehttp.WebUIOption,
440 gatewayOpt,
@@ -529,11 +529,11 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (error, <-chan e
529
530 var opts = []corehttp.ServeOption{
531 corehttp.MetricsCollectionOption("gateway"),
532 + corehttp.CheckVersionOption(),
533 corehttp.CommandsROOption(*cctx),
534 corehttp.VersionOption(),
535 corehttp.IPNSHostnameOption(),
536 corehttp.GatewayOption(writable, "/ipfs", "/ipns"),
536 - corehttp.CheckVersionOption(),
537 }
538
539 if len(cfg.Gateway.RootRedirect) > 0 {
core/corehttp/commands.go
+6 -4
@@ -144,11 +144,13 @@ func CommandsROOption(cctx oldcmds.Context) ServeOption {
144 func CheckVersionOption() ServeOption {
145 daemonVersion := config.ApiVersion
146
147 - return ServeOption(func(n *core.IpfsNode, l net.Listener, next *http.ServeMux) (*http.ServeMux, error) {
147 + return ServeOption(func(n *core.IpfsNode, l net.Listener, parent *http.ServeMux) (*http.ServeMux, error) {
148 mux := http.NewServeMux()
149 - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
149 + parent.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
150 if strings.HasPrefix(r.URL.Path, APIPath) {
151 - pth := path.SplitList(r.URL.Path[len(APIPath):])
151 + cmdqry := r.URL.Path[len(APIPath):]
152 + pth := path.SplitList(cmdqry)
153 +
154 // backwards compatibility to previous version check
155 if pth[1] != "version" {
156 clientVersion := r.UserAgent()
@@ -160,7 +162,7 @@ func CheckVersionOption() ServeOption {
162 }
163 }
164
163 - next.ServeHTTP(w, r)
165 + mux.ServeHTTP(w, r)
166 })
167
168 return mux, nil
core/corehttp/option_test.go
+8 -8
@@ -41,8 +41,13 @@ func TestCheckVersionOption(t *testing.T) {
41 r.Header.Add("User-Agent", tc.userAgent) // old version, should fail
42
43 called := false
44 - inner := http.NewServeMux()
45 - inner.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
44 + root := http.NewServeMux()
45 + mux, err := CheckVersionOption()(nil, nil, root)
46 + if err != nil {
47 + t.Fatal(err)
48 + }
49 +
50 + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
51 called = true
52 if !tc.shouldHandle {
53 t.Error("handler was called even though version didn't match")
@@ -51,14 +56,9 @@ func TestCheckVersionOption(t *testing.T) {
56 }
57 })
58
54 - mux, err := CheckVersionOption()(nil, nil, inner)
55 - if err != nil {
56 - t.Fatal(err)
57 - }
58 -
59 w := httptest.NewRecorder()
60
61 - mux.ServeHTTP(w, r)
61 + root.ServeHTTP(w, r)
62
63 if tc.shouldHandle && !called {
64 t.Error("handler wasn't called even though it should have")