@cryptotaxi247 / kubo / commits / 3f1cbe2f4

corehttp: add net.Listener to ServeOption

ServeOptions take the node and muxer, they should get the listener too as sometimes they need to operate on the listener address. License: MIT Signed-off-by: Juan Batiz-Benet <juan@benet.ai>

Juan Batiz-Benet committed Jul 31, 2015 at 17:34 UTC 3f1cbe2f437c9aad8b972b40283d2dc69a2bd6b9
9 files changed +36 -17
cmd/ipfs/daemon.go
+3 -2
@@ -3,6 +3,7 @@ package main
3 import (
4 _ "expvar"
5 "fmt"
6 + "net"
7 "net/http"
8 _ "net/http/pprof"
9 "os"
@@ -126,8 +127,8 @@ future version, along with this notice. Please move to setting the HTTP Headers.
127 // mostly useful to hook up things that register in the default muxer,
128 // and don't provide a convenient http.Handler entry point, such as
129 // expvar and http/pprof.
129 -func defaultMux(path string) func(node *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
130 - return func(node *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
130 +func defaultMux(path string) corehttp.ServeOption {
131 + return func(node *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
132 mux.Handle(path, http.DefaultServeMux)
133 return mux, nil
134 }
core/corehttp/commands.go
+2 -1
@@ -1,6 +1,7 @@
1 package corehttp
2
3 import (
4 + "net"
5 "net/http"
6 "os"
7 "strings"
@@ -58,7 +59,7 @@ func addHeadersFromConfig(c *cmdsHttp.ServerConfig, nc *config.Config) {
59 }
60
61 func CommandsOption(cctx commands.Context) ServeOption {
61 - return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
62 + return func(n *core.IpfsNode, l net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
63
64 cfg := &cmdsHttp.ServerConfig{
65 CORSOpts: &cors.Options{
core/corehttp/corehttp.go
+4 -4
@@ -23,16 +23,16 @@ var log = eventlog.Logger("core/server")
23 // It returns the mux to expose to future options, which may be a new mux if it
24 // is interested in mediating requests to future options, or the same mux
25 // initially passed in if not.
26 -type ServeOption func(*core.IpfsNode, *http.ServeMux) (*http.ServeMux, error)
26 +type ServeOption func(*core.IpfsNode, net.Listener, *http.ServeMux) (*http.ServeMux, error)
27
28 // makeHandler turns a list of ServeOptions into a http.Handler that implements
29 // all of the given options, in order.
30 -func makeHandler(n *core.IpfsNode, options ...ServeOption) (http.Handler, error) {
30 +func makeHandler(n *core.IpfsNode, l net.Listener, options ...ServeOption) (http.Handler, error) {
31 topMux := http.NewServeMux()
32 mux := topMux
33 for _, option := range options {
34 var err error
35 - mux, err = option(n, mux)
35 + mux, err = option(n, l, mux)
36 if err != nil {
37 return nil, err
38 }
@@ -65,7 +65,7 @@ func ListenAndServe(n *core.IpfsNode, listeningMultiAddr string, options ...Serv
65 }
66
67 func Serve(node *core.IpfsNode, lis net.Listener, options ...ServeOption) error {
68 - handler, err := makeHandler(node, options...)
68 + handler, err := makeHandler(node, lis, options...)
69 if err != nil {
70 return err
71 }
core/corehttp/gateway.go
+3 -2
@@ -2,6 +2,7 @@ package corehttp
2
3 import (
4 "fmt"
5 + "net"
6 "net/http"
7 "sync"
8
@@ -27,7 +28,7 @@ func NewGateway(conf GatewayConfig) *Gateway {
28 }
29
30 func (g *Gateway) ServeOption() ServeOption {
30 - return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
31 + return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
32 // pass user's HTTP headers
33 g.Config.Headers = n.Repo.Config().Gateway.HTTPHeaders
34
@@ -50,7 +51,7 @@ func GatewayOption(writable bool) ServeOption {
51 }
52
53 func VersionOption() ServeOption {
53 - return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
54 + return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
55 mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
56 fmt.Fprintf(w, "Client Version: %s\n", id.ClientVersion)
57 fmt.Fprintf(w, "Protocol Version: %s\n", id.IpfsVersion)
core/corehttp/gateway_test.go
+16 -4
@@ -55,6 +55,14 @@ func newNodeWithMockNamesys(t *testing.T, ns mockNamesys) *core.IpfsNode {
55 return n
56 }
57
58 +type delegatedHandler struct {
59 + http.Handler
60 +}
61 +
62 +func (dh *delegatedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
63 + dh.Handler.ServeHTTP(w, r)
64 +}
65 +
66 func TestGatewayGet(t *testing.T) {
67 t.Skip("not sure whats going on here")
68 ns := mockNamesys{}
@@ -65,7 +73,14 @@ func TestGatewayGet(t *testing.T) {
73 }
74 ns["example.com"] = path.FromString("/ipfs/" + k)
75
68 - h, err := makeHandler(n,
76 + // need this variable here since we need to construct handler with
77 + // listener, and server with handler. yay cycles.
78 + dh := &delegatedHandler{}
79 + ts := httptest.NewServer(dh)
80 + defer ts.Close()
81 +
82 + dh.Handler, err = makeHandler(n,
83 + ts.Listener,
84 IPNSHostnameOption(),
85 GatewayOption(false),
86 )
@@ -73,9 +88,6 @@ func TestGatewayGet(t *testing.T) {
88 t.Fatal(err)
89 }
90
76 - ts := httptest.NewServer(h)
77 - defer ts.Close()
78 -
91 t.Log(ts.URL)
92 for _, test := range []struct {
93 host string
core/corehttp/ipns_hostname.go
+2 -1
@@ -1,6 +1,7 @@
1 package corehttp
2
3 import (
4 + "net"
5 "net/http"
6 "strings"
7
@@ -13,7 +14,7 @@ import (
14 // an IPNS name.
15 // The rewritten request points at the resolved name on the gateway handler.
16 func IPNSHostnameOption() ServeOption {
16 - return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
17 + return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
18 childMux := http.NewServeMux()
19 mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
20 ctx, cancel := context.WithCancel(n.Context())
core/corehttp/logs.go
+2 -1
@@ -2,6 +2,7 @@ package corehttp
2
3 import (
4 "io"
5 + "net"
6 "net/http"
7
8 core "github.com/ipfs/go-ipfs/core"
@@ -36,7 +37,7 @@ func (w *writeErrNotifier) Write(b []byte) (int, error) {
37 }
38
39 func LogOption() ServeOption {
39 - return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
40 + return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
41 mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) {
42 w.WriteHeader(200)
43 wnf, errs := newWriteErrNotifier(w)
core/corehttp/prometheus.go
+2 -1
@@ -1,6 +1,7 @@
1 package corehttp
2
3 import (
4 + "net"
5 "net/http"
6
7 prom "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
@@ -9,7 +10,7 @@ import (
10 )
11
12 func PrometheusOption(path string) ServeOption {
12 - return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
13 + return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
14 mux.Handle(path, prom.Handler())
15 return mux, nil
16 }
core/corehttp/redirect.go
+2 -1
@@ -1,6 +1,7 @@
1 package corehttp
2
3 import (
4 + "net"
5 "net/http"
6
7 core "github.com/ipfs/go-ipfs/core"
@@ -8,7 +9,7 @@ import (
9
10 func RedirectOption(path string, redirect string) ServeOption {
11 handler := &redirectHandler{redirect}
11 - return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
12 + return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
13 mux.Handle("/"+path+"/", handler)
14 return mux, nil
15 }