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

import (
4
	"context"
5
	"errors"
6
	"net/http"
keks's avatar
keks committed
7
	"runtime/debug"
8
	"sync"
9
	"time"
10

11
	cmds "github.com/ipfs/go-ipfs-cmds"
keks's avatar
keks committed
12
	logging "github.com/ipfs/go-log"
13
	cors "github.com/rs/cors"
14 15
)

16
var log = logging.Logger("cmds/http")
17

rht's avatar
rht committed
18
var (
Hector Sanjuan's avatar
Hector Sanjuan committed
19 20
	// ErrNotFound is returned when the endpoint does not exist.
	ErrNotFound = errors.New("404 page not found")
rht's avatar
rht committed
21
)
22

23
const (
Hector Sanjuan's avatar
Hector Sanjuan committed
24
	// StreamErrHeader is used as trailer when stream errors happen.
25 26 27 28 29 30 31 32 33
	StreamErrHeader          = "X-Stream-Error"
	streamHeader             = "X-Stream-Output"
	channelHeader            = "X-Chunked-Output"
	extraContentLengthHeader = "X-Content-Length"
	uaHeader                 = "User-Agent"
	contentTypeHeader        = "Content-Type"
	contentDispHeader        = "Content-Disposition"
	transferEncodingHeader   = "Transfer-Encoding"
	originHeader             = "origin"
34

Hector Sanjuan's avatar
Hector Sanjuan committed
35
	applicationJSON        = "application/json"
36 37
	applicationOctetStream = "application/octet-stream"
	plainText              = "text/plain"
38
)
39

40 41 42
func skipAPIHeader(h string) bool {
	switch h {
	case "Access-Control-Allow-Origin":
43
		return true
44
	case "Access-Control-Allow-Methods":
45
		return true
46
	case "Access-Control-Allow-Credentials":
47 48 49
		return true
	default:
		return false
50 51 52
	}
}

53 54 55 56
// the internal handler for the API
type handler struct {
	root *cmds.Command
	cfg  *ServerConfig
57
	env  cmds.Environment
58 59
}

Hector Sanjuan's avatar
Hector Sanjuan committed
60
// NewHandler creates the http.Handler for the given commands.
61
func NewHandler(env cmds.Environment, root *cmds.Command, cfg *ServerConfig) http.Handler {
62
	if cfg == nil {
63
		panic("must provide a valid ServerConfig")
64
	}
65

66 67 68
	c := cors.New(*cfg.corsOpts)

	var h http.Handler
69

70 71
	h = &handler{
		env:  env,
72 73 74
		root: root,
		cfg:  cfg,
	}
75 76 77 78 79 80 81

	if cfg.APIPath != "" {
		h = newPrefixHandler(cfg.APIPath, h) // wrap with path prefix checker and trimmer
	}
	h = c.Handler(h) // wrap with CORS handler

	return h
82 83
}

84 85
type requestLogger interface {
	LogRequest(*cmds.Request) func()
Jeromy's avatar
Jeromy committed
86 87
}

88
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
89
	log.Debug("incoming API request: ", r.URL)
90

91 92
	defer func() {
		if r := recover(); r != nil {
93
			log.Error("a panic has occurred in the commands handler!")
94
			log.Error(r)
keks's avatar
keks committed
95
			log.Errorf("stack trace:\n%s", debug.Stack())
96 97 98
		}
	}()

99 100
	// First of all, check if we are allowed to handle the request method
	// or we are configured not to.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	//
	// Always allow OPTIONS, POST
	switch r.Method {
	case http.MethodOptions:
		// If we get here, this is a normal (non-preflight) request.
		// The CORS library handles all other requests.

		// Tell the user the allowed methods, and return.
		setAllowedHeaders(w, h.cfg.AllowGet)
		w.WriteHeader(http.StatusNoContent)
		return
	case http.MethodPost:
	case http.MethodGet, http.MethodHead:
		if h.cfg.AllowGet {
			break
		}
		fallthrough
	default:
		setAllowedHeaders(w, h.cfg.AllowGet)
120
		http.Error(w, "405 - Method Not Allowed", http.StatusMethodNotAllowed)
121
		log.Warnf("The IPFS API does not support %s requests.", r.Method)
122 123 124
		return
	}

125
	if !allowOrigin(r, h.cfg) || !allowReferer(r, h.cfg) || !allowUserAgent(r, h.cfg) {
126
		http.Error(w, "403 - Forbidden", http.StatusForbidden)
127
		log.Warnf("API blocked request to %s. (possible CSRF)", r.URL)
128 129 130
		return
	}

131 132 133 134 135
	// If we have a request body, make sure the preamble
	// knows that it should close the body if it wants to
	// write before completing reading.
	// FIXME: https://github.com/ipfs/go-ipfs/issues/5168
	// FIXME: https://github.com/golang/go/issues/15527
136
	var bodyEOFChan chan struct{}
137
	if r.Body != http.NoBody {
138 139
		bodyEOFChan = make(chan struct{})
		var once sync.Once
140 141
		bw := bodyWrapper{
			ReadCloser: r.Body,
142 143
			onEOF: func() {
				once.Do(func() { close(bodyEOFChan) })
144 145 146 147 148
			},
		}
		r.Body = bw
	}

Steven Allen's avatar
Steven Allen committed
149
	req, err := parseRequest(r, h.root)
150
	if err != nil {
151
		status := http.StatusBadRequest
152
		if err == ErrNotFound {
153
			status = http.StatusNotFound
154
		}
155 156

		http.Error(w, err.Error(), status)
157 158
		return
	}
159

160 161 162 163 164 165 166
	// set user's headers first.
	for k, v := range h.cfg.Headers {
		if !skipAPIHeader(k) {
			w.Header()[k] = v
		}
	}

167 168 169 170 171 172 173 174 175 176 177 178 179
	// Handle the timeout up front.
	var cancel func()
	if timeoutStr, ok := req.Options[cmds.TimeoutOpt]; ok {
		timeout, err := time.ParseDuration(timeoutStr.(string))
		if err != nil {
			return
		}
		req.Context, cancel = context.WithTimeout(req.Context, timeout)
	} else {
		req.Context, cancel = context.WithCancel(req.Context)
	}
	defer cancel()

180
	re, err := NewResponseEmitter(w, r.Method, req, withRequestBodyEOFChan(bodyEOFChan))
181
	if err != nil {
182
		http.Error(w, err.Error(), http.StatusBadRequest)
183 184 185
		return
	}

186 187 188
	if reqLogger, ok := h.env.(requestLogger); ok {
		done := reqLogger.LogRequest(req)
		defer done()
189
	}
190

191
	h.root.Call(req, re, h.env)
192 193
}

194 195 196 197
func setAllowedHeaders(w http.ResponseWriter, allowGet bool) {
	w.Header().Add("Allow", http.MethodOptions)
	w.Header().Add("Allow", http.MethodPost)
	if allowGet {
198
		w.Header().Add("Allow", http.MethodHead)
199
		w.Header().Add("Allow", http.MethodGet)
200 201
	}
}