handler.go 2.05 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 23 24
const (
	streamHeader      = "X-Stream-Output"
	contentTypeHeader = "Content-Type"
)
25

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

32 33 34 35
func NewHandler(ctx cmds.Context, root *cmds.Command) *Handler {
	return &Handler{ctx, root}
}

36
func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
37 38
	log.Debug("Incoming API request: ", r.URL)

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

	// call the command
52
	res := i.root.Call(req)
53 54

	// set the Content-Type based on res output
55
	if _, ok := res.Output().(io.Reader); ok {
56 57 58 59 60 61
		// 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")

62
	} else {
63 64
		enc, found, err := req.Option(cmds.EncShort).String()
		if err != nil || !found {
65 66 67
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
68
		mime := mimeTypes[enc]
69
		w.Header().Set(contentTypeHeader, mime)
70 71 72 73 74 75 76 77 78 79 80
	}

	// 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)
		}
	}

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

	io.Copy(w, out)
90
}