gateway_handler.go 15.6 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
	defer func() {
		if r := recover(); r != nil {
			log.Error("A panic occurred in the gateway handler!")
			log.Error(r)
			debug.PrintStack()
		}
	}()

71 72 73 74 75 76 77 78 79 80 81 82
	if i.config.Writable {
		switch r.Method {
		case "POST":
			i.postHandler(w, r)
			return
		case "PUT":
			i.putHandler(w, r)
			return
		case "DELETE":
			i.deleteHandler(w, r)
			return
		}
83 84
	}

85
	if r.Method == "GET" || r.Method == "HEAD" {
86
		i.getOrHeadHandler(w, r)
87 88 89
		return
	}

90 91 92 93 94
	if r.Method == "OPTIONS" {
		i.optionsHandler(w, r)
		return
	}

95
	errmsg := "Method " + r.Method + " not allowed: "
96
	if !i.config.Writable {
97 98 99 100 101 102
		w.WriteHeader(http.StatusMethodNotAllowed)
		errmsg = errmsg + "read only access"
	} else {
		w.WriteHeader(http.StatusBadRequest)
		errmsg = errmsg + "bad request for " + r.URL.Path
	}
103 104
	fmt.Fprint(w, errmsg)
	log.Error(errmsg) // TODO(cryptix): log errors until we have a better way to expose these (counter metrics maybe)
105 106
}

107 108 109 110 111 112 113 114 115
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)
}

116
func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) {
117 118
	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
119
	defer cancel()
120

121
	if cn, ok := w.(http.CloseNotifier); ok {
122
		clientGone := cn.CloseNotify()
123 124
		go func() {
			select {
125
			case <-clientGone:
126 127 128 129 130 131
			case <-ctx.Done():
			}
			cancel()
		}()
	}

132 133
	urlPath := r.URL.Path

134 135 136 137 138
	// 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 {
139 140 141 142 143 144 145
		prfx := prefixHdr[0]
		for _, p := range i.config.PathPrefixes {
			if prfx == p || strings.HasPrefix(prfx, p+"/") {
				prefix = prfx
				break
			}
		}
146 147
	}

148 149 150 151 152
	// 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
153
	originalUrlPath := prefix + urlPath
154
	ipnsHostname := false
155 156
	if hdr := r.Header["X-Ipns-Original-Path"]; len(hdr) > 0 {
		originalUrlPath = prefix + hdr[0]
157 158 159
		ipnsHostname = true
	}

160 161 162 163 164
	dr, err := i.api.Cat(ctx, urlPath)
	dir := false
	if err == coreiface.ErrIsDir {
		dir = true
	} else if err == coreiface.ErrOffline {
165 166 167 168
		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
169
		webError(w, "Path Resolve error", err, http.StatusBadRequest)
170
		return
171 172
	} else {
		defer dr.Close()
173 174
	}

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

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

Jeromy's avatar
Jeromy committed
184 185 186 187 188
	// 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")

189
	// Suborigin header, sandboxes apps from each other in the browser (even
190 191 192 193 194 195 196 197 198 199 200
	// 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)
	}
201

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

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

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

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

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

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

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

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

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

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

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

			// don't go further up than /ipfs/$hash/
rht's avatar
rht committed
274
			pathSplit := path.SplitList(backLink)
Henry's avatar
Henry committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
			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 += "/.."
				}
			}

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

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

316
func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
317
	nd, err := i.newDagFromReader(r.Body)
318
	if err != nil {
319
		internalWebError(w, err)
320 321 322
		return
	}

323
	k, err := i.node.DAG.Add(nd)
324
	if err != nil {
325 326 327 328
		internalWebError(w, err)
		return
	}

329
	i.addUserHeaders(w) // ok, _now_ write user's headers.
330 331
	w.Header().Set("IPFS-Hash", k.String())
	http.Redirect(w, r, ipfsPathPrefix+k.String(), http.StatusCreated)
332 333
}

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

339
	rootPath, err := path.ParsePath(r.URL.Path)
340
	if err != nil {
341
		webError(w, "putHandler: IPFS path not valid", err, http.StatusBadRequest)
342 343 344
		return
	}

345 346 347
	rsegs := rootPath.Segments()
	if rsegs[0] == ipnsPathPrefix {
		webError(w, "putHandler: updating named entries not supported", errors.New("WritableGateway: ipns put not supported"), http.StatusBadRequest)
348 349 350
		return
	}

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

363 364
	var newPath string
	if len(rsegs) > 1 {
365
		newPath = path.Join(rsegs[2:])
366 367
	}

Jeromy's avatar
Jeromy committed
368
	var newcid *cid.Cid
369
	rnode, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, rootPath)
370 371 372 373 374
	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
375 376 377 378 379 380 381
		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)
382 383 384 385
		if err != nil {
			webError(w, "putHandler: Could not create DAG from request", err, http.StatusInternalServerError)
			return
		}
386

387 388 389 390 391 392 393
		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)
394
		err = e.InsertNodeAtPath(ctx, newPath, newnode, ft.EmptyDirNode)
395 396 397
		if err != nil {
			webError(w, "putHandler: InsertNodeAtPath failed", err, http.StatusInternalServerError)
			return
398 399
		}

400 401 402 403 404 405
		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
406
		newcid = nnode.Cid()
407

408
	case nil:
409 410 411 412 413 414
		pbnd, ok := rnode.(*dag.ProtoNode)
		if !ok {
			webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest)
			return
		}

415 416 417 418 419 420
		pbnewnode, ok := newnode.(*dag.ProtoNode)
		if !ok {
			webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest)
			return
		}

421
		// object set-data case
422
		pbnd.SetData(pbnewnode.Data())
423

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

437
	i.addUserHeaders(w) // ok, _now_ write user's headers.
Jeromy's avatar
Jeromy committed
438 439
	w.Header().Set("IPFS-Hash", newcid.String())
	http.Redirect(w, r, gopath.Join(ipfsPathPrefix, newcid.String(), newPath), http.StatusCreated)
440 441 442 443
}

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

Jeromy's avatar
Jeromy committed
447
	p, err := path.ParsePath(urlPath)
448
	if err != nil {
Jeromy's avatar
Jeromy committed
449
		webError(w, "failed to parse path", err, http.StatusBadRequest)
450 451 452
		return
	}

Jeromy's avatar
Jeromy committed
453
	c, components, err := path.SplitAbsPath(p)
454 455 456 457 458
	if err != nil {
		webError(w, "Could not split path", err, http.StatusInternalServerError)
		return
	}

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

467
	pathNodes, err := i.node.Resolver.ResolveLinks(tctx, rootnd, components[:len(components)-1])
468 469 470 471 472
	if err != nil {
		webError(w, "Could not resolve parent object", err, http.StatusBadRequest)
		return
	}

473 474 475 476 477 478
	pbnd, ok := pathNodes[len(pathNodes)-1].(*dag.ProtoNode)
	if !ok {
		webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest)
		return
	}

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

486
	var newnode *dag.ProtoNode = pbnd
487 488 489 490 491
	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
		}
492 493 494 495 496 497 498 499

		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)
500 501 502 503 504 505
		if err != nil {
			webError(w, "Could not update node links", err, http.StatusInternalServerError)
			return
		}
	}

506 507
	if _, err := i.node.DAG.Add(newnode); err != nil {
		webError(w, "Could not add root node", err, http.StatusInternalServerError)
508 509 510 511
		return
	}

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

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

519 520 521 522 523 524
func (i *gatewayHandler) addUserHeaders(w http.ResponseWriter) {
	for k, v := range i.config.Headers {
		w.Header()[k] = v
	}
}

525 526 527 528 529 530 531 532 533 534 535 536 537 538
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)
539 540
	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)
541
}
542

Simon Kirkby's avatar
Simon Kirkby committed
543 544
// return a 500 error and log
func internalWebError(w http.ResponseWriter, err error) {
545
	webErrorWithCode(w, "internalWebError", err, http.StatusInternalServerError)
Simon Kirkby's avatar
Simon Kirkby committed
546
}