kubo/core/corehttp/ipns_hostname.go
Jakub Sztandera fe969d18be
mics: cleanup imports in touched files
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
2017-04-24 15:01:55 +02:00

37 lines
1004 B
Go

package corehttp
import (
"context"
"net"
"net/http"
"strings"
"github.com/ipfs/go-ipfs/core"
isd "gx/ipfs/QmZmmuAXgX73UQmX1jRKjTGmjzq24Jinqkq8vzkBtno4uX/go-is-domain"
)
// 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, _ net.Listener, 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 len(host) > 0 && isd.IsDomain(host) {
name := "/ipns/" + host
if _, err := n.Namesys.Resolve(ctx, name); err == nil {
r.Header["X-Ipns-Original-Path"] = []string{r.URL.Path}
r.URL.Path = name + r.URL.Path
}
}
childMux.ServeHTTP(w, r)
})
return childMux, nil
}
}