gatewayHandler.go 4.88 KB
Newer Older
1 2 3
package main

import (
4
	"html/template"
5
	"io"
6
	"mime"
7
	"net/http"
8
	"strings"
9

10
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
11 12 13 14
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"

	core "github.com/jbenet/go-ipfs/core"
	"github.com/jbenet/go-ipfs/importer"
Jeromy's avatar
Jeromy committed
15
	chunk "github.com/jbenet/go-ipfs/importer/chunk"
16
	dag "github.com/jbenet/go-ipfs/merkledag"
17
	"github.com/jbenet/go-ipfs/routing"
18 19 20 21
	uio "github.com/jbenet/go-ipfs/unixfs/io"
	u "github.com/jbenet/go-ipfs/util"
)

22
type gateway interface {
23 24 25 26 27 28
	ResolvePath(string) (*dag.Node, error)
	NewDagFromReader(io.Reader) (*dag.Node, error)
	AddNodeToDAG(nd *dag.Node) (u.Key, error)
	NewDagReader(nd *dag.Node) (io.Reader, error)
}

29
// shortcut for templating
Simon Kirkby's avatar
Simon Kirkby committed
30
type webHandler map[string]interface{}
31 32 33 34 35 36 37

// struct for directory listing
type directoryItem struct {
	Size uint64
	Name string
}

38
// gatewayHandler is a HTTP handler that serves IPFS objects (accessible by default at /ipfs/<path>)
39
// (it serves requests like GET /ipfs/QmVRzPKPzNtSrEzBFm2UZfxmPAgnaLke4DMcerbsGGSaFe/link)
40
type gatewayHandler struct {
41 42 43 44
	node    *core.IpfsNode
	dirList *template.Template
}

45 46
func NewGatewayHandler(node *core.IpfsNode) (*gatewayHandler, error) {
	i := &gatewayHandler{
47 48 49 50 51 52 53 54 55
		node: node,
	}
	err := i.loadTemplate()
	if err != nil {
		return nil, err
	}
	return i, nil
}

56
// Load the directroy list template
57
func (i *gatewayHandler) loadTemplate() error {
58 59
	t, err := template.New("dir").Parse(listingTemplate)
	if err != nil {
60
		return err
61 62
	}
	i.dirList = t
63
	return nil
64 65
}

66
func (i *gatewayHandler) ResolvePath(path string) (*dag.Node, error) {
67 68 69
	return i.node.Resolver.ResolvePath(path)
}

70
func (i *gatewayHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
Jeromy's avatar
Jeromy committed
71 72
	return importer.BuildDagFromReader(
		r, i.node.DAG, i.node.Pinning.GetManual(), chunk.DefaultSplitter)
73 74
}

75
func (i *gatewayHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
76 77 78
	return i.node.DAG.Add(nd)
}

79
func (i *gatewayHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
80 81 82
	return uio.NewDagReader(nd, i.node.DAG)
}

83
func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
84
	path := r.URL.Path[5:]
85
	log := log.Prefix("serving %s", path)
86 87 88

	nd, err := i.ResolvePath(path)
	if err != nil {
89 90 91 92 93 94 95 96
		if err == routing.ErrNotFound {
			w.WriteHeader(http.StatusNotFound)
		} else if err == context.DeadlineExceeded {
			w.WriteHeader(http.StatusRequestTimeout)
		} else {
			w.WriteHeader(http.StatusBadRequest)
		}

97 98 99 100 101
		log.Error(err)
		w.Write([]byte(err.Error()))
		return
	}

102 103 104 105 106 107 108 109 110
	extensionIndex := strings.LastIndex(path, ".")
	if extensionIndex != -1 {
		extension := path[extensionIndex:]
		mimeType := mime.TypeByExtension(extension)
		if len(mimeType) > 0 {
			w.Header().Add("Content-Type", mimeType)
		}
	}

111
	dr, err := i.NewDagReader(nd)
112 113 114 115
	if err == nil {
		io.Copy(w, dr)
		return
	}
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	if err != uio.ErrIsDir {
		// not a directory and still an error
		internalWebError(w, err)
		return
	}

	log.Debug("listing directory")
	if path[len(path)-1:] != "/" {
		log.Debug("missing trailing slash, redirect")
		http.Redirect(w, r, "/ipfs/"+path+"/", 307)
		return
	}

	// storage for directory listing
	var dirListing []directoryItem
	// loop through files
	for _, link := range nd.Links {
		if link.Name != "index.html" {
			dirListing = append(dirListing, directoryItem{link.Size, link.Name})
			continue
		}

		log.Debug("found index")
		// return index page instead.
		nd, err := i.ResolvePath(path + "/index.html")
		if err != nil {
			internalWebError(w, err)
144 145
			return
		}
146 147 148 149 150 151 152 153 154 155 156 157
		dr, err := i.NewDagReader(nd)
		if err != nil {
			internalWebError(w, err)
			return
		}
		// write to request
		io.Copy(w, dr)
	}

	// template and return directory listing
	hndlr := webHandler{"listing": dirListing, "path": path}
	if err := i.dirList.Execute(w, hndlr); err != nil {
Simon Kirkby's avatar
Simon Kirkby committed
158
		internalWebError(w, err)
159 160 161 162
		return
	}
}

163
func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	nd, err := i.NewDagFromReader(r.Body)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Error(err)
		w.Write([]byte(err.Error()))
		return
	}

	k, err := i.AddNodeToDAG(nd)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Error(err)
		w.Write([]byte(err.Error()))
		return
	}

	//TODO: return json representation of list instead
	w.WriteHeader(http.StatusCreated)
	w.Write([]byte(mh.Multihash(k).B58String()))
}
184

Simon Kirkby's avatar
Simon Kirkby committed
185 186 187 188 189 190 191
// return a 500 error and log
func internalWebError(w http.ResponseWriter, err error) {
	w.WriteHeader(http.StatusInternalServerError)
	w.Write([]byte(err.Error()))
	log.Error("%s", err)
}

192 193 194 195 196 197 198 199 200 201 202
// Directory listing template
var listingTemplate = `
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>{{ .path }}</title>
	</head>
	<body>
	<h2>Index of {{ .path }}</h2>
	<ul>
203
	<li><a href="./..">..</a></li>
204
  {{ range $item := .listing }}
205
	<li><a href="./{{ $item.Name }}">{{ $item.Name }}</a> - {{ $item.Size }} bytes</li>
206 207 208 209 210
	{{ end }}
	</ul>
	</body>
</html>
`