diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index 2dcf48e4d..0ad20c9dc 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -178,7 +178,10 @@ func daemonFunc(req cmds.Request, res cmds.Response) { if gatewayMaddr != nil { go func() { - var opts = []corehttp.ServeOption{corehttp.GatewayOption(writable)} + var opts = []corehttp.ServeOption{ + corehttp.IPNSHostnameOption(), + corehttp.GatewayOption(writable), + } if rootRedirect != nil { opts = append(opts, rootRedirect) } diff --git a/core/corehttp/ipns_hostname.go b/core/corehttp/ipns_hostname.go new file mode 100644 index 000000000..27d683250 --- /dev/null +++ b/core/corehttp/ipns_hostname.go @@ -0,0 +1,29 @@ +package corehttp + +import ( + "net/http" + "strings" + + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" + "github.com/jbenet/go-ipfs/core" +) + +// IPNSHostnameOption rewrites an incoming request if its Host: header contains +// an IPNS name. +// The rewritten request points at the resolved name on the gateway handler. +func IPNSHostnameOption() ServeOption { + return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) { + childMux := http.NewServeMux() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithCancel(n.Context()) + defer cancel() + + host := strings.SplitN(r.Host, ":", 2)[0] + if k, err := n.Namesys.Resolve(ctx, host); err == nil { + r.URL.Path = "/ipfs/" + k.Pretty() + r.URL.Path + } + childMux.ServeHTTP(w, r) + }) + return childMux, nil + } +}