gateway_handler.go 15.5 KB
Newer Older
1
package corehttp
2 3

import (
4
	"context"
5
	"errors"
6
	"fmt"
7 8
	"io"
	"net/http"
Jeromy's avatar
Jeromy committed
9
	gopath "path"
10
	"runtime/debug"
11
	"strings"
12
	"time"
13

14 15 16 17
	core "github.com/ipfs/go-ipfs/core"
	"github.com/ipfs/go-ipfs/importer"
	chunk "github.com/ipfs/go-ipfs/importer/chunk"
	dag "github.com/ipfs/go-ipfs/merkledag"
18
	dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
19 20

	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
21
	path "github.com/ipfs/go-ipfs/path"
22
	ft "github.com/ipfs/go-ipfs/unixfs"
23 24

	humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
25 26 27
	routing "gx/ipfs/QmQKEgGgYCDyk8VNY6A65FpuE4YwbspvjXHco1rdb75PVc/go-libp2p-routing"
	node "gx/ipfs/QmU7bFWQ793qmvNy7outdCaMfSDNk8uqhx4VNrxYj5fj5g/go-ipld-node"
	cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid"
28 29
)

30
const (
31 32
	ipfsPathPrefix = "/ipfs/"
	ipnsPathPrefix = "/ipns/"
33 34
)

35
// gatewayHandler is a HTTP handler that serves IPFS objects (accessible by default at /ipfs/<path>)
36
// (it serves requests like GET /ipfs/QmVRzPKPzNtSrEzBFm2UZfxmPAgnaLke4DMcerbsGGSaFe/link)
37
type gatewayHandler struct {
38 39
	node   *core.IpfsNode
	config GatewayConfig
40
	api    coreiface.UnixfsAPI
41 42
}

43
func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.UnixfsAPI) *gatewayHandler {
44
	i := &gatewayHandler{
45 46 47
		node:   n,
		config: c,
		api:    api,
48
	}
49
	return i
50 51
}

52
// TODO(cryptix):  find these helpers somewhere else
53
func (i *gatewayHandler) newDagFromReader(r io.Reader) (node.Node, error) {
Henry's avatar
Henry committed
54 55
	// TODO(cryptix): change and remove this helper once PR1136 is merged
	// return ufs.AddFromReader(i.node, r.Body)
Jeromy's avatar
Jeromy committed
56
	return importer.BuildDagFromReader(
57
		i.node.DAG,
Jeromy's avatar
Jeromy committed
58
		chunk.DefaultSplitter(r))
59 60
}

61
// TODO(btc): break this apart into separate handlers using a more expressive muxer
62
func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	ctx, cancel := context.WithTimeout(i.node.Context(), time.Hour)
	// the hour is a hard fallback, we don't expect it to happen, but just in case
	defer cancel()

	if cn, ok := w.(http.CloseNotifier); ok {
		clientGone := cn.CloseNotify()
		go func() {
			select {
			case <-clientGone:
			case <-ctx.Done():
			}
			cancel()
		}()
	}

78 79 80 81 82 83 84 85
	defer func() {
		if r := recover(); r != nil {
			log.Error("A panic occurred in the gateway handler!")
			log.Error(r)
			debug.PrintStack()
		}
	}()

86 87 88
	if i.config.Writable {
		switch r.Method {
		case "POST":
89
			i.postHandler(ctx, w, r)
90 91 92 93 94 95 96 97
			return
		case "PUT":
			i.putHandler(w, r)
			return
		case "DELETE":
			i.deleteHandler(w, r)
			return
		}
98 99
	}

100
	if r.Method == "GET" || r.Method == "HEAD" {
101
		i.getOrHeadHandler(ctx, w, r)
102 103 104
		return
	}

105 106 107 108 109
	if r.Method == "OPTIONS" {
		i.optionsHandler(w, r)
		return
	}

110
	errmsg := "Method " + r.Method + " not allowed: "
111
	if !i.config.Writable {
112 113 114 115 116 117
		w.WriteHeader(http.StatusMethodNotAllowed)
		errmsg = errmsg + "read only access"
	} else {
		w.WriteHeader(http.StatusBadRequest)
		errmsg = errmsg + "bad request for " + r.URL.Path
	}
118 119
	fmt.Fprint(w, errmsg)
	log.Error(errmsg) // TODO(cryptix): log errors until we have a better way to expose these (counter metrics maybe)
120 121
}

122 123 124 125 126 127 128 129 130
func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request) {
	/*
		OPTIONS is a noop request that is used by the browsers to check
		if server accepts cross-site XMLHttpRequest (indicated by the presence of CORS headers)
		https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests
	*/
	i.addUserHeaders(w) // return all custom headers (including CORS ones, if set)
}

131
func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
132

133 134
	urlPath := r.URL.Path

135 136 137 138 139
	// If the gateway is behind a reverse proxy and mounted at a sub-path,
	// the prefix header can be set to signal this sub-path.
	// It will be prepended to links in directory listings and the index.html redirect.
	prefix := ""
	if prefixHdr := r.Header["X-Ipfs-Gateway-Prefix"]; len(prefixHdr) > 0 {
140 141 142 143 144 145 146
		prfx := prefixHdr[0]
		for _, p := range i.config.PathPrefixes {
			if prfx == p || strings.HasPrefix(prfx, p+"/") {
				prefix = prfx
				break
			}
		}
147 148
	}

149 150 151 152 153
	// IPNSHostnameOption might have constructed an IPNS path using the Host header.
	// In this case, we need the original path for constructing redirects
	// and links that match the requested URL.
	// For example, http://example.net would become /ipns/example.net, and
	// the redirects and links would end up as http://example.net/ipns/example.net
154
	originalUrlPath := prefix + urlPath
155
	ipnsHostname := false
156 157
	if hdr := r.Header["X-Ipns-Original-Path"]; len(hdr) > 0 {
		originalUrlPath = prefix + hdr[0]
158 159 160
		ipnsHostname = true
	}

161 162 163 164 165
	dr, err := i.api.Cat(ctx, urlPath)
	dir := false
	if err == coreiface.ErrIsDir {
		dir = true
	} else if err == coreiface.ErrOffline {
166 167 168 169
		w.WriteHeader(http.StatusServiceUnavailable)
		fmt.Fprint(w, "Could not resolve path. Node is in offline mode.")
		return
	} else if err != nil {
Andy Leap's avatar
Andy Leap committed
170
		webError(w, "Path Resolve error", err, http.StatusBadRequest)
171
		return
172 173
	} else {
		defer dr.Close()
174 175
	}

176
	etag := gopath.Base(urlPath)
177 178 179 180 181
	if r.Header.Get("If-None-Match") == etag {
		w.WriteHeader(http.StatusNotModified)
		return
	}

182
	i.addUserHeaders(w) // ok, _now_ write user's headers.
183
	w.Header().Set("X-IPFS-Path", urlPath)
184

Jeromy's avatar
Jeromy committed
185 186 187 188 189
	// set 'allowed' headers
	w.Header().Set("Access-Control-Allow-Headers", "X-Stream-Output, X-Chunked-Output")
	// expose those headers
	w.Header().Set("Access-Control-Expose-Headers", "X-Stream-Output, X-Chunked-Output")

190
	// Suborigin header, sandboxes apps from each other in the browser (even
191 192 193 194 195 196 197 198 199 200 201
	// though they are served from the same gateway domain).
	//
	// Omited if the path was treated by IPNSHostnameOption(), for example
	// a request for http://example.net/ would be changed to /ipns/example.net/,
	// which would turn into an incorrect Suborigin: example.net header.
	//
	// NOTE: This is not yet widely supported by browsers.
	if !ipnsHostname {
		pathRoot := strings.SplitN(urlPath, "/", 4)[2]
		w.Header().Set("Suborigin", pathRoot)
	}
202

203 204
	// set these headers _after_ the error, for we may just not have it
	// and dont want the client to cache a 500 response...
205 206 207
	// and only if it's /ipfs!
	// TODO: break this out when we split /ipfs /ipns routes.
	modtime := time.Now()
208
	if strings.HasPrefix(urlPath, ipfsPathPrefix) {
209
		w.Header().Set("Etag", etag)
kpcyrd's avatar
kpcyrd committed
210
		w.Header().Set("Cache-Control", "public, max-age=29030400, immutable")
211 212 213 214

		// set modtime to a really long time ago, since files are immutable and should stay cached
		modtime = time.Unix(1, 0)
	}
215

216
	if !dir {
217
		name := gopath.Base(urlPath)
218
		http.ServeContent(w, r, name, modtime, dr)
219 220
		return
	}
221

222 223 224 225 226 227
	links, err := i.api.Ls(ctx, urlPath)
	if err != nil {
		internalWebError(w, err)
		return
	}

228 229 230
	// storage for directory listing
	var dirListing []directoryItem
	// loop through files
231
	foundIndex := false
232
	for _, link := range links {
233
		if link.Name == "index.html" {
234 235 236
			log.Debugf("found index.html link for %s", urlPath)
			foundIndex = true

237
			if urlPath[len(urlPath)-1] != '/' {
238 239 240
				// See comment above where originalUrlPath is declared.
				http.Redirect(w, r, originalUrlPath+"/", 302)
				log.Debugf("redirect to %s", originalUrlPath+"/")
241 242 243
				return
			}

244 245 246 247 248 249
			p, err := path.ParsePath(urlPath + "/index.html")
			if err != nil {
				internalWebError(w, err)
				return
			}

250
			// return index page instead.
251
			dr, err := i.api.Cat(ctx, p.String())
252 253 254 255
			if err != nil {
				internalWebError(w, err)
				return
			}
256
			defer dr.Close()
257

258
			// write to request
259
			http.ServeContent(w, r, "index.html", modtime, dr)
260
			break
261 262
		}

263
		// See comment above where originalUrlPath is declared.
rht's avatar
rht committed
264
		di := directoryItem{humanize.Bytes(link.Size), link.Name, gopath.Join(originalUrlPath, link.Name)}
265
		dirListing = append(dirListing, di)
266 267 268
	}

	if !foundIndex {
269
		if r.Method != "HEAD" {
Henry's avatar
Henry committed
270 271
			// construct the correct back link
			// https://github.com/ipfs/go-ipfs/issues/1365
272
			var backLink string = prefix + urlPath
Henry's avatar
Henry committed
273 274

			// don't go further up than /ipfs/$hash/
rht's avatar
rht committed
275
			pathSplit := path.SplitList(backLink)
Henry's avatar
Henry committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
			switch {
			// keep backlink
			case len(pathSplit) == 3: // url: /ipfs/$hash

			// keep backlink
			case len(pathSplit) == 4 && pathSplit[3] == "": // url: /ipfs/$hash/

			// add the correct link depending on wether the path ends with a slash
			default:
				if strings.HasSuffix(backLink, "/") {
					backLink += "./.."
				} else {
					backLink += "/.."
				}
			}

292 293
			// strip /ipfs/$hash from backlink if IPNSHostnameOption touched the path.
			if ipnsHostname {
294
				backLink = prefix + "/"
295 296 297
				if len(pathSplit) > 5 {
					// also strip the trailing segment, because it's a backlink
					backLinkParts := pathSplit[3 : len(pathSplit)-2]
298
					backLink += path.Join(backLinkParts) + "/"
299 300 301 302
				}
			}

			// See comment above where originalUrlPath is declared.
Henry's avatar
Henry committed
303 304
			tplData := listingTemplateData{
				Listing:  dirListing,
305
				Path:     originalUrlPath,
Henry's avatar
Henry committed
306 307 308 309
				BackLink: backLink,
			}
			err := listingTemplate.Execute(w, tplData)
			if err != nil {
310 311 312
				internalWebError(w, err)
				return
			}
313
		}
314 315 316
	}
}

317 318
func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	k, err := i.api.Add(ctx, r.Body)
319
	if err != nil {
320 321 322 323
		internalWebError(w, err)
		return
	}

324
	i.addUserHeaders(w) // ok, _now_ write user's headers.
325 326
	w.Header().Set("IPFS-Hash", k.String())
	http.Redirect(w, r, ipfsPathPrefix+k.String(), http.StatusCreated)
327 328
}

329
func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
Henry's avatar
Henry committed
330 331 332 333
	// TODO(cryptix): move me to ServeHTTP and pass into all handlers
	ctx, cancel := context.WithCancel(i.node.Context())
	defer cancel()

334
	rootPath, err := path.ParsePath(r.URL.Path)
335
	if err != nil {
336
		webError(w, "putHandler: IPFS path not valid", err, http.StatusBadRequest)
337 338 339
		return
	}

340 341 342
	rsegs := rootPath.Segments()
	if rsegs[0] == ipnsPathPrefix {
		webError(w, "putHandler: updating named entries not supported", errors.New("WritableGateway: ipns put not supported"), http.StatusBadRequest)
343 344 345
		return
	}

346
	var newnode node.Node
347
	if rsegs[len(rsegs)-1] == "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" {
348
		newnode = ft.EmptyDirNode()
349
	} else {
350
		putNode, err := i.newDagFromReader(r.Body)
351
		if err != nil {
352
			webError(w, "putHandler: Could not create DAG from request", err, http.StatusInternalServerError)
353 354
			return
		}
355
		newnode = putNode
356 357
	}

358 359
	var newPath string
	if len(rsegs) > 1 {
360
		newPath = path.Join(rsegs[2:])
361 362
	}

Jeromy's avatar
Jeromy committed
363
	var newcid *cid.Cid
364
	rnode, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, rootPath)
365 366 367 368 369
	switch ev := err.(type) {
	case path.ErrNoLink:
		// ev.Node < node where resolve failed
		// ev.Name < new link
		// but we need to patch from the root
Jeromy's avatar
Jeromy committed
370 371 372 373 374 375 376
		c, err := cid.Decode(rsegs[1])
		if err != nil {
			webError(w, "putHandler: bad input path", err, http.StatusBadRequest)
			return
		}

		rnode, err := i.node.DAG.Get(ctx, c)
377 378 379 380
		if err != nil {
			webError(w, "putHandler: Could not create DAG from request", err, http.StatusInternalServerError)
			return
		}
381

382 383 384 385 386 387 388
		pbnd, ok := rnode.(*dag.ProtoNode)
		if !ok {
			webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest)
			return
		}

		e := dagutils.NewDagEditor(pbnd, i.node.DAG)
389
		err = e.InsertNodeAtPath(ctx, newPath, newnode, ft.EmptyDirNode)
390 391 392
		if err != nil {
			webError(w, "putHandler: InsertNodeAtPath failed", err, http.StatusInternalServerError)
			return
393 394
		}

395 396 397 398 399 400
		nnode, err := e.Finalize(i.node.DAG)
		if err != nil {
			webError(w, "putHandler: could not get node", err, http.StatusInternalServerError)
			return
		}

Jeromy's avatar
Jeromy committed
401
		newcid = nnode.Cid()
402

403
	case nil:
404 405 406 407 408 409
		pbnd, ok := rnode.(*dag.ProtoNode)
		if !ok {
			webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest)
			return
		}

410 411 412 413 414 415
		pbnewnode, ok := newnode.(*dag.ProtoNode)
		if !ok {
			webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest)
			return
		}

416
		// object set-data case
417
		pbnd.SetData(pbnewnode.Data())
418

419
		newcid, err = i.node.DAG.Add(pbnd)
420
		if err != nil {
Jeromy's avatar
Jeromy committed
421
			nnk := newnode.Cid()
422
			rk := pbnd.Cid()
Jeromy's avatar
Jeromy committed
423
			webError(w, fmt.Sprintf("putHandler: Could not add newnode(%q) to root(%q)", nnk.String(), rk.String()), err, http.StatusInternalServerError)
424 425 426 427 428
			return
		}
	default:
		log.Warningf("putHandler: unhandled resolve error %T", ev)
		webError(w, "could not resolve root DAG", ev, http.StatusInternalServerError)
429 430 431
		return
	}

432
	i.addUserHeaders(w) // ok, _now_ write user's headers.
Jeromy's avatar
Jeromy committed
433 434
	w.Header().Set("IPFS-Hash", newcid.String())
	http.Redirect(w, r, gopath.Join(ipfsPathPrefix, newcid.String(), newPath), http.StatusCreated)
435 436 437 438
}

func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
	urlPath := r.URL.Path
439 440 441
	ctx, cancel := context.WithCancel(i.node.Context())
	defer cancel()

Jeromy's avatar
Jeromy committed
442
	p, err := path.ParsePath(urlPath)
443
	if err != nil {
Jeromy's avatar
Jeromy committed
444
		webError(w, "failed to parse path", err, http.StatusBadRequest)
445 446 447
		return
	}

Jeromy's avatar
Jeromy committed
448
	c, components, err := path.SplitAbsPath(p)
449 450 451 452 453
	if err != nil {
		webError(w, "Could not split path", err, http.StatusInternalServerError)
		return
	}

Jeromy's avatar
Jeromy committed
454 455
	tctx, cancel := context.WithTimeout(ctx, time.Minute)
	defer cancel()
Jeromy's avatar
Jeromy committed
456
	rootnd, err := i.node.Resolver.DAG.Get(tctx, c)
457 458
	if err != nil {
		webError(w, "Could not resolve root object", err, http.StatusBadRequest)
459 460 461
		return
	}

462
	pathNodes, err := i.node.Resolver.ResolveLinks(tctx, rootnd, components[:len(components)-1])
463 464 465 466 467
	if err != nil {
		webError(w, "Could not resolve parent object", err, http.StatusBadRequest)
		return
	}

468 469 470 471 472 473
	pbnd, ok := pathNodes[len(pathNodes)-1].(*dag.ProtoNode)
	if !ok {
		webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest)
		return
	}

474
	// TODO(cyrptix): assumes len(pathNodes) > 1 - not found is an error above?
475
	err = pbnd.RemoveNodeLink(components[len(components)-1])
476 477 478 479 480
	if err != nil {
		webError(w, "Could not delete link", err, http.StatusBadRequest)
		return
	}

481
	var newnode *dag.ProtoNode = pbnd
482 483 484 485 486
	for j := len(pathNodes) - 2; j >= 0; j-- {
		if _, err := i.node.DAG.Add(newnode); err != nil {
			webError(w, "Could not add node", err, http.StatusInternalServerError)
			return
		}
487 488 489 490 491 492 493 494

		pathpb, ok := pathNodes[j].(*dag.ProtoNode)
		if !ok {
			webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest)
			return
		}

		newnode, err = pathpb.UpdateNodeLink(components[j], newnode)
495 496 497 498 499 500
		if err != nil {
			webError(w, "Could not update node links", err, http.StatusInternalServerError)
			return
		}
	}

501 502
	if _, err := i.node.DAG.Add(newnode); err != nil {
		webError(w, "Could not add root node", err, http.StatusInternalServerError)
503 504 505 506
		return
	}

	// Redirect to new path
Jeromy's avatar
Jeromy committed
507
	ncid := newnode.Cid()
508

509
	i.addUserHeaders(w) // ok, _now_ write user's headers.
Jeromy's avatar
Jeromy committed
510 511
	w.Header().Set("IPFS-Hash", ncid.String())
	http.Redirect(w, r, gopath.Join(ipfsPathPrefix+ncid.String(), path.Join(components[:len(components)-1])), http.StatusCreated)
512 513
}

514 515 516 517 518 519
func (i *gatewayHandler) addUserHeaders(w http.ResponseWriter) {
	for k, v := range i.config.Headers {
		w.Header()[k] = v
	}
}

520 521 522 523 524 525 526 527 528 529 530 531 532 533
func webError(w http.ResponseWriter, message string, err error, defaultCode int) {
	if _, ok := err.(path.ErrNoLink); ok {
		webErrorWithCode(w, message, err, http.StatusNotFound)
	} else if err == routing.ErrNotFound {
		webErrorWithCode(w, message, err, http.StatusNotFound)
	} else if err == context.DeadlineExceeded {
		webErrorWithCode(w, message, err, http.StatusRequestTimeout)
	} else {
		webErrorWithCode(w, message, err, defaultCode)
	}
}

func webErrorWithCode(w http.ResponseWriter, message string, err error, code int) {
	w.WriteHeader(code)
534 535
	log.Errorf("%s: %s", message, err) // TODO(cryptix): log errors until we have a better way to expose these (counter metrics maybe)
	fmt.Fprintf(w, "%s: %s", message, err)
536
}
537

Simon Kirkby's avatar
Simon Kirkby committed
538 539
// return a 500 error and log
func internalWebError(w http.ResponseWriter, err error) {
540
	webErrorWithCode(w, "internalWebError", err, http.StatusInternalServerError)
Simon Kirkby's avatar
Simon Kirkby committed
541
}