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

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

9 10
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"

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

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

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

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

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

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

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

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

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

57 58 59 60 61
	if len(i.origin) > 0 {
		w.Header().Set("Access-Control-Allow-Origin", i.origin)
	}
	w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

62
	req, err := Parse(r, i.root)
63
	if err != nil {
64 65 66 67 68 69
		if err == ErrNotFound {
			w.WriteHeader(http.StatusNotFound)
		} else {
			w.WriteHeader(http.StatusBadRequest)
		}
		w.Write([]byte(err.Error()))
70 71
		return
	}
72
	req.SetContext(i.ctx)
73 74

	// call the command
75
	res := i.root.Call(req)
76 77

	// set the Content-Type based on res output
78
	if _, ok := res.Output().(io.Reader); ok {
79 80 81 82 83 84
		// 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")

85
	} else {
86 87
		enc, found, err := req.Option(cmds.EncShort).String()
		if err != nil || !found {
88 89 90
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
91
		mime := mimeTypes[enc]
92
		w.Header().Set(contentTypeHeader, mime)
93 94 95 96 97 98 99 100 101 102 103
	}

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

104
	out, err := res.Reader()
105
	if err != nil {
106
		w.Header().Set(contentTypeHeader, "text/plain")
107
		w.WriteHeader(http.StatusInternalServerError)
108
		w.Write([]byte(err.Error()))
109
		return
110
	}
111

112 113 114 115 116
	// 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 {
117
		err = copyChunks(applicationJson, w, out)
118 119 120 121
		if err != nil {
			log.Error(err)
		}
		return
122
	}
123

124 125 126 127 128 129 130 131 132 133 134 135
	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
136 137 138 139
}

// Copies from an io.Reader to a http.ResponseWriter.
// Flushes chunks over HTTP stream as they are read (if supported by transport).
140
func copyChunks(contentType string, w http.ResponseWriter, out io.Reader) error {
141 142 143 144 145 146 147 148 149 150 151
	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")
152 153 154
	if contentType != "" {
		writer.WriteString(contentTypeHeader + ": " + contentType + "\r\n")
	}
155 156 157
	writer.WriteString(transferEncodingHeader + ": chunked\r\n")
	writer.WriteString(channelHeader + ": 1\r\n\r\n")

158 159 160 161 162 163
	buf := make([]byte, 32*1024)

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

		if n > 0 {
164 165 166 167
			length := fmt.Sprintf("%x\r\n", n)
			writer.WriteString(length)

			_, err := writer.Write(buf[0:n])
168 169 170 171
			if err != nil {
				return err
			}

172 173
			writer.WriteString("\r\n")
			writer.Flush()
174 175
		}

176
		if err != nil && err != io.EOF {
177 178
			return err
		}
179 180 181
		if err == io.EOF {
			break
		}
182
	}
183 184 185 186

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

187
	return nil
188
}
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

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
}