Skip to content

Commit 3ecccd6

Browse files
lidelStebalien
authored andcommitted
feat(gateway): subdomain and proxy gateway
License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent 848d4c7 commit 3ecccd6

18 files changed

+1421
-85
lines changed

cmd/ipfs/daemon.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"sort"
1313
"sync"
1414

15+
multierror "github.com/hashicorp/go-multierror"
16+
1517
version "github.com/ipfs/go-ipfs"
1618
config "github.com/ipfs/go-ipfs-config"
1719
cserial "github.com/ipfs/go-ipfs-config/serialize"
@@ -27,7 +29,6 @@ import (
2729
migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
2830
sockets "github.com/libp2p/go-socket-activation"
2931

30-
"github.com/hashicorp/go-multierror"
3132
cmds "github.com/ipfs/go-ipfs-cmds"
3233
mprome "github.com/ipfs/go-metrics-prometheus"
3334
goprocess "github.com/jbenet/goprocess"
@@ -298,9 +299,9 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
298299

299300
// Start assembling node config
300301
ncfg := &core.BuildCfg{
301-
Repo: repo,
302-
Permanent: true, // It is temporary way to signify that node is permanent
303-
Online: !offline,
302+
Repo: repo,
303+
Permanent: true, // It is temporary way to signify that node is permanent
304+
Online: !offline,
304305
DisableEncryptedConnections: unencrypted,
305306
ExtraOpts: map[string]bool{
306307
"pubsub": pubsub,
@@ -636,7 +637,7 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
636637

637638
var opts = []corehttp.ServeOption{
638639
corehttp.MetricsCollectionOption("gateway"),
639-
corehttp.IPNSHostnameOption(),
640+
corehttp.HostnameOption(),
640641
corehttp.GatewayOption(writable, "/ipfs", "/ipns"),
641642
corehttp.VersionOption(),
642643
corehttp.CheckVersionOption(),

core/corehttp/corehttp.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ func makeHandler(n *core.IpfsNode, l net.Listener, options ...ServeOption) (http
4343
return nil, err
4444
}
4545
}
46-
return topMux, nil
46+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
47+
// ServeMux does not support requests with CONNECT method,
48+
// so we need to handle them separately
49+
// https://golang.org/src/net/http/request.go#L111
50+
if r.Method == http.MethodConnect {
51+
w.WriteHeader(http.StatusOK)
52+
return
53+
}
54+
topMux.ServeHTTP(w, r)
55+
})
56+
return handler, nil
4757
}
4858

4959
// ListenAndServe runs an HTTP server listening at |listeningMultiAddr| with
@@ -70,6 +80,8 @@ func ListenAndServe(n *core.IpfsNode, listeningMultiAddr string, options ...Serv
7080
return Serve(n, manet.NetListener(list), options...)
7181
}
7282

83+
// Serve accepts incoming HTTP connections on the listener and pass them
84+
// to ServeOption handlers.
7385
func Serve(node *core.IpfsNode, lis net.Listener, options ...ServeOption) error {
7486
// make sure we close this no matter what.
7587
defer lis.Close()

core/corehttp/gateway_handler.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import (
1414
"strings"
1515
"time"
1616

17-
"github.com/dustin/go-humanize"
17+
humanize "github.com/dustin/go-humanize"
1818
"github.com/ipfs/go-cid"
1919
files "github.com/ipfs/go-ipfs-files"
2020
dag "github.com/ipfs/go-merkledag"
21-
"github.com/ipfs/go-mfs"
22-
"github.com/ipfs/go-path"
21+
mfs "github.com/ipfs/go-mfs"
22+
path "github.com/ipfs/go-path"
2323
"github.com/ipfs/go-path/resolver"
2424
coreiface "github.com/ipfs/interface-go-ipfs-core"
2525
ipath "github.com/ipfs/interface-go-ipfs-core/path"
@@ -142,14 +142,15 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
142142
}
143143
}
144144

145-
// IPNSHostnameOption might have constructed an IPNS path using the Host header.
145+
// HostnameOption might have constructed an IPNS/IPFS path using the Host header.
146146
// In this case, we need the original path for constructing redirects
147147
// and links that match the requested URL.
148148
// For example, http://example.net would become /ipns/example.net, and
149149
// the redirects and links would end up as http://example.net/ipns/example.net
150150
requestURI, err := url.ParseRequestURI(r.RequestURI)
151151
if err != nil {
152152
webError(w, "failed to parse request path", err, http.StatusInternalServerError)
153+
return
153154
}
154155
originalUrlPath := prefix + requestURI.Path
155156

core/corehttp/gateway_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, iface
138138

139139
dh.Handler, err = makeHandler(n,
140140
ts.Listener,
141-
IPNSHostnameOption(),
141+
HostnameOption(),
142142
GatewayOption(false, "/ipfs", "/ipns"),
143143
VersionOption(),
144144
)
@@ -184,12 +184,12 @@ func TestGatewayGet(t *testing.T) {
184184
status int
185185
text string
186186
}{
187-
{"localhost:5001", "/", http.StatusNotFound, "404 page not found\n"},
188-
{"localhost:5001", "/" + k.Cid().String(), http.StatusNotFound, "404 page not found\n"},
189-
{"localhost:5001", k.String(), http.StatusOK, "fnord"},
190-
{"localhost:5001", "/ipns/nxdomain.example.com", http.StatusNotFound, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
191-
{"localhost:5001", "/ipns/%0D%0A%0D%0Ahello", http.StatusNotFound, "ipfs resolve -r /ipns/%0D%0A%0D%0Ahello: " + namesys.ErrResolveFailed.Error() + "\n"},
192-
{"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"},
187+
{"127.0.0.1:8080", "/", http.StatusNotFound, "404 page not found\n"},
188+
{"127.0.0.1:8080", "/" + k.Cid().String(), http.StatusNotFound, "404 page not found\n"},
189+
{"127.0.0.1:8080", k.String(), http.StatusOK, "fnord"},
190+
{"127.0.0.1:8080", "/ipns/nxdomain.example.com", http.StatusNotFound, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
191+
{"127.0.0.1:8080", "/ipns/%0D%0A%0D%0Ahello", http.StatusNotFound, "ipfs resolve -r /ipns/%0D%0A%0D%0Ahello: " + namesys.ErrResolveFailed.Error() + "\n"},
192+
{"127.0.0.1:8080", "/ipns/example.com", http.StatusOK, "fnord"},
193193
{"example.com", "/", http.StatusOK, "fnord"},
194194

195195
{"working.example.com", "/", http.StatusOK, "fnord"},
@@ -381,7 +381,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
381381
if !strings.Contains(s, "Index of /foo? #&lt;&#39;/") {
382382
t.Fatalf("expected a path in directory listing")
383383
}
384-
if !strings.Contains(s, "<a href=\"/\">") {
384+
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/./..\">") {
385385
t.Fatalf("expected backlink in directory listing")
386386
}
387387
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/file.txt\">") {
@@ -447,7 +447,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
447447
if !strings.Contains(s, "Index of /foo? #&lt;&#39;/bar/") {
448448
t.Fatalf("expected a path in directory listing")
449449
}
450-
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/\">") {
450+
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/bar/./..\">") {
451451
t.Fatalf("expected backlink in directory listing")
452452
}
453453
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/bar/file.txt\">") {

0 commit comments

Comments
 (0)