handler.go 4.82 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
	"strings"
9
	"sync"
10
	"time"
11

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

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

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

24
const (
Hector Sanjuan's avatar
Hector Sanjuan committed
25
	// StreamErrHeader is used as trailer when stream errors happen.
26 27 28 29 30 31 32 33 34
	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"
35

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

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

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

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

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

	var h http.Handler
70

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

	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
83 84
}

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

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

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

100 101
	// First of all, check if we are allowed to handle the request method
	// or we are configured not to.
102 103 104 105 106 107 108 109
	//
	// 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.
110
		setAllowHeader(w, h.cfg.AllowGet)
111 112 113 114 115 116 117 118 119
		w.WriteHeader(http.StatusNoContent)
		return
	case http.MethodPost:
	case http.MethodGet, http.MethodHead:
		if h.cfg.AllowGet {
			break
		}
		fallthrough
	default:
120
		setAllowHeader(w, h.cfg.AllowGet)
121
		http.Error(w, "405 - Method Not Allowed", http.StatusMethodNotAllowed)
122
		log.Warnf("The IPFS API does not support %s requests.", r.Method)
123 124 125
		return
	}

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

132 133 134 135 136
	// 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
137
	var bodyEOFChan chan struct{}
138
	if r.Body != http.NoBody {
139 140
		bodyEOFChan = make(chan struct{})
		var once sync.Once
141 142
		bw := bodyWrapper{
			ReadCloser: r.Body,
143 144
			onEOF: func() {
				once.Do(func() { close(bodyEOFChan) })
145 146 147 148 149
			},
		}
		r.Body = bw
	}

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

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

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

168 169 170 171 172 173 174 175 176 177 178 179 180
	// 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()

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

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

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

195
func setAllowHeader(w http.ResponseWriter, allowGet bool) {
196
	allowedMethods := []string{http.MethodOptions, http.MethodPost}
197
	if allowGet {
198
		allowedMethods = append(allowedMethods, http.MethodHead, http.MethodGet)
199
	}
200
	w.Header().Set("Allow", strings.Join(allowedMethods, ", "))
201
}