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

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

11
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
12
	cmds "github.com/jbenet/go-ipfs/commands"
13
	u "github.com/jbenet/go-ipfs/util"
14 15
)

16 17
var log = u.Logger("commands/http")

18
type Handler struct {
19 20 21
	ctx    cmds.Context
	root   *cmds.Command
	origin string
22
}
23

24
var ErrNotFound = errors.New("404 page not found")
25

26
const (
27 28 29 30 31
	streamHeader           = "X-Stream-Output"
	channelHeader          = "X-Chunked-Output"
	contentTypeHeader      = "Content-Type"
	contentLengthHeader    = "Content-Length"
	transferEncodingHeader = "Transfer-Encoding"
32
	applicationJson        = "application/json"
33
)
34

35 36 37 38 39 40
var mimeTypes = map[string]string{
	cmds.JSON: "application/json",
	cmds.XML:  "application/xml",
	cmds.Text: "text/plain",
}

41 42 43 44 45 46 47
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}
48 49
}

50
func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
51 52 53 54 55
	// create a context.Context to pass into the commands.
	ctx, cancel := context.WithCancel(context.TODO())
	defer cancel()
	i.ctx.Context = ctx

56 57
	log.Debug("Incoming API request: ", r.URL)

58 59 60 61 62 63 64 65 66 67 68 69 70 71
	// error on external referers (to prevent CSRF attacks)
	referer := r.Referer()
	scheme := r.URL.Scheme
	if len(scheme) == 0 {
		scheme = "http"
	}
	host := fmt.Sprintf("%s://%s/", scheme, r.Host)
	// empty string means the user isn't following a link (they are directly typing in the url)
	if referer != "" && !strings.HasPrefix(referer, host) {
		w.WriteHeader(http.StatusForbidden)
		w.Write([]byte("403 - Forbidden"))
		return
	}

72 73 74 75 76
	if len(i.origin) > 0 {
		w.Header().Set("Access-Control-Allow-Origin", i.origin)
	}
	w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

77
	req, err := Parse(r, i.root)
78
	if err != nil {
79 80 81 82 83 84
		if err == ErrNotFound {
			w.WriteHeader(http.StatusNotFound)
		} else {
			w.WriteHeader(http.StatusBadRequest)
		}
		w.Write([]byte(err.Error()))
85 86
		return
	}
87
	req.SetContext(i.ctx)
88 89

	// call the command
90
	res := i.root.Call(req)
91 92

	// set the Content-Type based on res output
93
	if _, ok := res.Output().(io.Reader); ok {
94 95 96 97 98 99
		// 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")

100
	} else {
101 102
		enc, found, err := req.Option(cmds.EncShort).String()
		if err != nil || !found {
103 104 105
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
106
		mime := mimeTypes[enc]
107
		w.Header().Set(contentTypeHeader, mime)
108 109
	}

110 111 112 113 114
	// set the Content-Length from the response length
	if res.Length() > 0 {
		w.Header().Set(contentLengthHeader, strconv.FormatUint(res.Length(), 10))
	}

115 116 117 118 119 120 121 122 123
	// 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)
		}
	}

124
	out, err := res.Reader()
125
	if err != nil {
126
		w.Header().Set(contentTypeHeader, "text/plain")
127
		w.WriteHeader(http.StatusInternalServerError)
128
		w.Write([]byte(err.Error()))
129
		return
130
	}
131

132 133 134
	// if output is a channel and user requested streaming channels,
	// use chunk copier for the output
	_, isChan := res.Output().(chan interface{})
135 136 137 138
	if !isChan {
		_, isChan = res.Output().(<-chan interface{})
	}

139 140
	streamChans, _, _ := req.Option("stream-channels").Bool()
	if isChan && streamChans {
141 142 143
		// w.WriteString(transferEncodingHeader + ": chunked\r\n")
		// w.Header().Set(channelHeader, "1")
		// w.WriteHeader(200)
144
		err = copyChunks(applicationJson, w, out)
145
		if err != nil {
146
			log.Debug(err)
147 148
		}
		return
149
	}
150

151 152 153 154 155 156 157 158 159 160 161 162
	flushCopy(w, out)
}

// flushCopy Copies from an io.Reader to a http.ResponseWriter.
// Flushes chunks over HTTP stream as they are read (if supported by transport).
func flushCopy(w http.ResponseWriter, out io.Reader) error {
	if _, ok := w.(http.Flusher); !ok {
		return copyChunks("", w, out)
	}

	io.Copy(&flushResponse{w}, out)
	return nil
163 164 165 166
}

// Copies from an io.Reader to a http.ResponseWriter.
// Flushes chunks over HTTP stream as they are read (if supported by transport).
167
func copyChunks(contentType string, w http.ResponseWriter, out io.Reader) error {
168 169 170 171 172 173 174 175 176 177 178
	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")
179 180 181
	if contentType != "" {
		writer.WriteString(contentTypeHeader + ": " + contentType + "\r\n")
	}
182 183 184
	writer.WriteString(transferEncodingHeader + ": chunked\r\n")
	writer.WriteString(channelHeader + ": 1\r\n\r\n")

185 186 187 188 189 190
	buf := make([]byte, 32*1024)

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

		if n > 0 {
191 192 193 194
			length := fmt.Sprintf("%x\r\n", n)
			writer.WriteString(length)

			_, err := writer.Write(buf[0:n])
195 196 197 198
			if err != nil {
				return err
			}

199 200
			writer.WriteString("\r\n")
			writer.Flush()
201 202
		}

203
		if err != nil && err != io.EOF {
204 205
			return err
		}
206 207 208
		if err == io.EOF {
			break
		}
209
	}
210 211 212 213

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

214
	return nil
215
}
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

type flushResponse struct {
	W http.ResponseWriter
}

func (fr *flushResponse) Write(buf []byte) (int, error) {
	n, err := fr.W.Write(buf)
	if err != nil {
		return n, err
	}

	if flusher, ok := fr.W.(http.Flusher); ok {
		flusher.Flush()
	}
	return n, err
}