handler.go 1.99 KB
Newer Older
1 2 3
package http

import (
4
	"errors"
5 6 7 8
	"io"
	"net/http"

	cmds "github.com/jbenet/go-ipfs/commands"
9
	u "github.com/jbenet/go-ipfs/util"
10 11
)

12 13
var log = u.Logger("commands/http")

14
type Handler struct {
15 16
	ctx  cmds.Context
	root *cmds.Command
17
}
18

19
var ErrNotFound = errors.New("404 page not found")
20

21 22
const streamHeader = "X-Stream-Output"

23 24 25 26 27 28
var mimeTypes = map[string]string{
	cmds.JSON: "application/json",
	cmds.XML:  "application/xml",
	cmds.Text: "text/plain",
}

29 30 31 32
func NewHandler(ctx cmds.Context, root *cmds.Command) *Handler {
	return &Handler{ctx, root}
}

33
func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
34 35
	log.Debug("Incoming API request: ", r.URL)

36
	req, err := Parse(r, i.root)
37
	if err != nil {
38 39 40 41 42 43
		if err == ErrNotFound {
			w.WriteHeader(http.StatusNotFound)
		} else {
			w.WriteHeader(http.StatusBadRequest)
		}
		w.Write([]byte(err.Error()))
44 45
		return
	}
46
	req.SetContext(i.ctx)
47 48

	// call the command
49
	res := i.root.Call(req)
50 51

	// set the Content-Type based on res output
52
	if _, ok := res.Output().(io.Reader); ok {
53 54 55 56 57 58
		// we don't set the Content-Type for streams, so that browsers can MIME-sniff the type themselves
		// we set this header so clients have a way to know this is an output stream
		// (not marshalled command output)
		// TODO: set a specific Content-Type if the command response needs it to be a certain type
		w.Header().Set(streamHeader, "1")

59 60
	} else {
		enc, _ := req.Option(cmds.EncShort)
61 62 63 64 65 66
		encStr, ok := enc.(string)
		if !ok {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		mime := mimeTypes[encStr]
67
		w.Header().Set("Content-Type", mime)
68 69 70 71 72 73 74 75 76 77 78
	}

	// if response contains an error, write an HTTP error status code
	if e := res.Error(); e != nil {
		if e.Code == cmds.ErrClient {
			w.WriteHeader(http.StatusBadRequest)
		} else {
			w.WriteHeader(http.StatusInternalServerError)
		}
	}

79
	out, err := res.Reader()
80 81 82 83
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Header().Set("Content-Type", "text/plain")
		w.Write([]byte(err.Error()))
84
		return
85
	}
86 87

	io.Copy(w, out)
88
}