handler.go 4.81 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 20 21 22
var (
	ErrNotFound           = errors.New("404 page not found")
	errApiVersionMismatch = errors.New("api version mismatch")
)
23

24
const (
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

35 36 37
	applicationJson        = "application/json"
	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
}

60
func NewHandler(env cmds.Environment, root *cmds.Command, cfg *ServerConfig) http.Handler {
61
	if cfg == nil {
62
		panic("must provide a valid ServerConfig")
63
	}
64

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

	var h http.Handler
68

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

	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
81 82
}

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

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

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

98 99
	// First of all, check if we are allowed to handle the request method
	// or we are configured not to.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	//
	// 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)
119
		http.Error(w, "405 - Method Not Allowed", http.StatusMethodNotAllowed)
120
		log.Warnf("The IPFS API does not support %s requests.", r.Method)
121 122 123
		return
	}

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

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

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

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

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

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

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

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

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

193 194 195 196 197 198
func sanitizedErrStr(err error) string {
	s := err.Error()
	s = strings.Split(s, "\n")[0]
	s = strings.Split(s, "\r")[0]
	return s
}
199

200 201 202 203 204 205
func setAllowedHeaders(w http.ResponseWriter, allowGet bool) {
	w.Header().Add("Allow", http.MethodHead)
	w.Header().Add("Allow", http.MethodOptions)
	w.Header().Add("Allow", http.MethodPost)
	if allowGet {
		w.Header().Add("Allow", http.MethodGet)
206 207
	}
}