gatewayHandler.go 4.92 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 85 86 87
	path := r.URL.Path[5:]

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

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

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

110
	dr, err := i.NewDagReader(nd)
111

112
	if err != nil {
113 114 115 116
		if err == uio.ErrIsDir {
			log.Debug("is directory %s", path)

			if path[len(path)-1:] != "/" {
Simon Kirkby's avatar
Simon Kirkby committed
117
				log.Debug("missing trailing slash, redirect")
118 119 120 121 122 123 124 125 126 127 128 129 130
				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" {
					log.Debug("found index")
					// return index page
					nd, err := i.ResolvePath(path + "/index.html")
					if err != nil {
Simon Kirkby's avatar
Simon Kirkby committed
131
						internalWebError(w, err)
132 133 134 135
						return
					}
					dr, err := i.NewDagReader(nd)
					if err != nil {
Simon Kirkby's avatar
Simon Kirkby committed
136
						internalWebError(w, err)
137 138 139 140 141 142 143 144 145
						return
					}
					// write to request
					io.Copy(w, dr)
					return
				}
				dirListing = append(dirListing, directoryItem{link.Size, link.Name})
			}
			// template and return directory listing
Simon Kirkby's avatar
Simon Kirkby committed
146
			err := i.dirList.Execute(w, webHandler{"listing": dirListing, "path": path})
147
			if err != nil {
Simon Kirkby's avatar
Simon Kirkby committed
148
				internalWebError(w, err)
149 150 151 152
				return
			}
			return
		}
Simon Kirkby's avatar
Simon Kirkby committed
153 154
		// not a directory and still an error
		internalWebError(w, err)
155 156
		return
	}
Simon Kirkby's avatar
Simon Kirkby committed
157
	// return data file
158 159 160
	io.Copy(w, dr)
}

161
func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	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()))
}
182

Simon Kirkby's avatar
Simon Kirkby committed
183 184 185 186 187 188 189
// 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)
}

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