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

import (
4
	"html/template"
5 6 7
	"io"
	"net/http"

8
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9 10 11 12
	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
13
	chunk "github.com/jbenet/go-ipfs/importer/chunk"
14
	dag "github.com/jbenet/go-ipfs/merkledag"
15
	"github.com/jbenet/go-ipfs/routing"
16 17 18 19
	uio "github.com/jbenet/go-ipfs/unixfs/io"
	u "github.com/jbenet/go-ipfs/util"
)

20
type gateway interface {
21 22 23 24 25 26
	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)
}

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

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

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

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

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

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

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

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

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

81
func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
82 83 84 85
	path := r.URL.Path[5:]

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

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

	dr, err := i.NewDagReader(nd)
100

101
	if err != nil {
102 103 104 105
		if err == uio.ErrIsDir {
			log.Debug("is directory %s", path)

			if path[len(path)-1:] != "/" {
Simon Kirkby's avatar
Simon Kirkby committed
106
				log.Debug("missing trailing slash, redirect")
107 108 109 110 111 112 113 114 115 116 117 118 119
				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
120
						internalWebError(w, err)
121 122 123 124
						return
					}
					dr, err := i.NewDagReader(nd)
					if err != nil {
Simon Kirkby's avatar
Simon Kirkby committed
125
						internalWebError(w, err)
126 127 128 129 130 131 132 133 134
						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
135
			err := i.dirList.Execute(w, webHandler{"listing": dirListing, "path": path})
136
			if err != nil {
Simon Kirkby's avatar
Simon Kirkby committed
137
				internalWebError(w, err)
138 139 140 141
				return
			}
			return
		}
Simon Kirkby's avatar
Simon Kirkby committed
142 143
		// not a directory and still an error
		internalWebError(w, err)
144 145
		return
	}
Simon Kirkby's avatar
Simon Kirkby committed
146
	// return data file
147 148 149
	io.Copy(w, dr)
}

150
func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	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()))
}
171

Simon Kirkby's avatar
Simon Kirkby committed
172 173 174 175 176 177 178
// 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)
}

179 180 181 182 183 184 185 186 187 188 189
// Directory listing template
var listingTemplate = `
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>{{ .path }}</title>
	</head>
	<body>
	<h2>Index of {{ .path }}</h2>
	<ul>
190 191 192
	<li><a href="./..">..</a></li>	
  {{ range $item := .listing }}
	<li><a href="./{{ $item.Name }}">{{ $item.Name }}</a> - {{ $item.Size }} bytes</li>	
193 194 195 196 197
	{{ end }}
	</ul>
	</body>
</html>
`