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

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

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

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

15
type Handler struct {
16 17 18
	ctx    cmds.Context
	root   *cmds.Command
	origin string
19
}
20

21
var ErrNotFound = errors.New("404 page not found")
22

23
const (
24 25 26 27 28
	streamHeader           = "X-Stream-Output"
	channelHeader          = "X-Chunked-Output"
	contentTypeHeader      = "Content-Type"
	contentLengthHeader    = "Content-Length"
	transferEncodingHeader = "Transfer-Encoding"
29
)
30

31 32 33 34 35 36
var mimeTypes = map[string]string{
	cmds.JSON: "application/json",
	cmds.XML:  "application/xml",
	cmds.Text: "text/plain",
}

37 38 39 40 41 42 43
func NewHandler(ctx cmds.Context, root *cmds.Command, origin string) *Handler {
	// allow whitelisted origins (so we can make API requests from the browser)
	if len(origin) > 0 {
		log.Info("Allowing API requests from origin: " + origin)
	}

	return &Handler{ctx, root, origin}
44 45
}

46
func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
47 48
	log.Debug("Incoming API request: ", r.URL)

49 50 51 52 53
	if len(i.origin) > 0 {
		w.Header().Set("Access-Control-Allow-Origin", i.origin)
	}
	w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

54
	req, err := Parse(r, i.root)
55
	if err != nil {
56 57 58 59 60 61
		if err == ErrNotFound {
			w.WriteHeader(http.StatusNotFound)
		} else {
			w.WriteHeader(http.StatusBadRequest)
		}
		w.Write([]byte(err.Error()))
62 63
		return
	}
64
	req.SetContext(i.ctx)
65 66

	// call the command
67
	res := i.root.Call(req)
68 69

	// set the Content-Type based on res output
70
	if _, ok := res.Output().(io.Reader); ok {
71 72 73 74 75 76
		// 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")

77
	} else {
78 79
		enc, found, err := req.Option(cmds.EncShort).String()
		if err != nil || !found {
80 81 82
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
83
		mime := mimeTypes[enc]
84
		w.Header().Set(contentTypeHeader, mime)
85 86 87 88 89 90 91 92 93 94 95
	}

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

96
	out, err := res.Reader()
97 98
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
99
		w.Header().Set(contentTypeHeader, "text/plain")
100
		w.Write([]byte(err.Error()))
101
		return
102
	}
103

104 105 106 107 108
	// if output is a channel and user requested streaming channels,
	// use chunk copier for the output
	_, isChan := res.Output().(chan interface{})
	streamChans, _, _ := req.Option("stream-channels").Bool()
	if isChan && streamChans {
109 110 111 112 113
		err = copyChunks(w, out)
		if err != nil {
			log.Error(err)
		}
		return
114
	}
115 116

	io.Copy(w, out)
117 118 119 120 121
}

// Copies from an io.Reader to a http.ResponseWriter.
// Flushes chunks over HTTP stream as they are read (if supported by transport).
func copyChunks(w http.ResponseWriter, out io.Reader) error {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	hijacker, ok := w.(http.Hijacker)
	if !ok {
		return errors.New("Could not create hijacker")
	}
	conn, writer, err := hijacker.Hijack()
	if err != nil {
		return err
	}
	defer conn.Close()

	writer.WriteString("HTTP/1.1 200 OK\r\n")
	writer.WriteString(contentTypeHeader + ": application/json\r\n")
	writer.WriteString(transferEncodingHeader + ": chunked\r\n")
	writer.WriteString(channelHeader + ": 1\r\n\r\n")

137 138 139 140 141 142
	buf := make([]byte, 32*1024)

	for {
		n, err := out.Read(buf)

		if n > 0 {
143 144 145 146
			length := fmt.Sprintf("%x\r\n", n)
			writer.WriteString(length)

			_, err := writer.Write(buf[0:n])
147 148 149 150
			if err != nil {
				return err
			}

151 152
			writer.WriteString("\r\n")
			writer.Flush()
153 154
		}

155
		if err != nil && err != io.EOF {
156 157
			return err
		}
158 159 160
		if err == io.EOF {
			break
		}
161
	}
162 163 164 165

	writer.WriteString("0\r\n\r\n")
	writer.Flush()

166
	return nil
167
}