summaryrefslogtreecommitdiff
path: root/plugins/http/plugin.go
blob: 770ca8cab4fd7f3ecf6d9f9c5a1ae00c0c20690f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package http

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"sync"

	endure "github.com/spiral/endure/pkg/container"
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/pkg/pool"
	"github.com/spiral/roadrunner/v2/pkg/process"
	"github.com/spiral/roadrunner/v2/pkg/worker"
	handler "github.com/spiral/roadrunner/v2/pkg/worker_handler"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/http/attributes"
	httpConfig "github.com/spiral/roadrunner/v2/plugins/http/config"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	"github.com/spiral/roadrunner/v2/plugins/server"
	"github.com/spiral/roadrunner/v2/plugins/status"
	"golang.org/x/net/http2"
	"golang.org/x/net/http2/h2c"
)

const (
	// PluginName declares plugin name.
	PluginName = "http"

	// RrMode RR_HTTP env variable key (internal) if the HTTP presents
	RrMode = "RR_MODE"

	HTTPSScheme = "https"
)

// Middleware interface
type Middleware interface {
	Middleware(f http.Handler) http.Handler
}

type middleware map[string]Middleware

// Plugin manages pool, http servers. The main http plugin structure
type Plugin struct {
	sync.RWMutex

	// plugins
	server server.Server
	log    logger.Logger
	// stdlog passed to the http/https/fcgi servers to log their internal messages
	stdLog *log.Logger

	// http configuration
	cfg *httpConfig.HTTP `mapstructure:"http"`

	// middlewares to chain
	mdwr middleware

	// Pool which attached to all servers
	pool pool.Pool

	// servers RR handler
	handler *handler.Handler

	// servers
	http  *http.Server
	https *http.Server
	fcgi  *http.Server
}

// Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
func (p *Plugin) Init(cfg config.Configurer, rrLogger logger.Logger, server server.Server) error {
	const op = errors.Op("http_plugin_init")
	if !cfg.Has(PluginName) {
		return errors.E(op, errors.Disabled)
	}

	err := cfg.UnmarshalKey(PluginName, &p.cfg)
	if err != nil {
		return errors.E(op, err)
	}

	err = p.cfg.InitDefaults()
	if err != nil {
		return errors.E(op, err)
	}

	// rr logger (via plugin)
	p.log = rrLogger
	// use time and date in UTC format
	p.stdLog = log.New(logger.NewStdAdapter(p.log), "http_plugin: ", log.Ldate|log.Ltime|log.LUTC)

	p.mdwr = make(map[string]Middleware)

	if !p.cfg.EnableHTTP() && !p.cfg.EnableTLS() && !p.cfg.EnableFCGI() {
		return errors.E(op, errors.Disabled)
	}

	// init if nil
	if p.cfg.Env == nil {
		p.cfg.Env = make(map[string]string)
	}

	p.cfg.Env[RrMode] = "http"
	p.server = server

	return nil
}

func (p *Plugin) logCallback(event interface{}) {
	if ev, ok := event.(handler.ResponseEvent); ok {
		p.log.Debug(fmt.Sprintf("%d %s %s", ev.Response.Status, ev.Request.Method, ev.Request.URI),
			"remote", ev.Request.RemoteAddr,
			"elapsed", ev.Elapsed().String(),
		)
	}
}

// Serve serves the svc.
func (p *Plugin) Serve() chan error {
	errCh := make(chan error, 2)
	// run whole process in the goroutine
	go func() {
		// protect http initialization
		p.Lock()
		p.serve(errCh)
		p.Unlock()
	}()

	return errCh
}

func (p *Plugin) serve(errCh chan error) {
	var err error
	const op = errors.Op("http_plugin_serve")
	p.pool, err = p.server.NewWorkerPool(context.Background(), pool.Config{
		Debug:           p.cfg.Pool.Debug,
		NumWorkers:      p.cfg.Pool.NumWorkers,
		MaxJobs:         p.cfg.Pool.MaxJobs,
		AllocateTimeout: p.cfg.Pool.AllocateTimeout,
		DestroyTimeout:  p.cfg.Pool.DestroyTimeout,
		Supervisor:      p.cfg.Pool.Supervisor,
	}, p.cfg.Env, p.logCallback)
	if err != nil {
		errCh <- errors.E(op, err)
		return
	}

	p.handler, err = handler.NewHandler(
		p.cfg.MaxRequestSize,
		*p.cfg.Uploads,
		p.cfg.Cidrs,
		p.pool,
	)
	if err != nil {
		errCh <- errors.E(op, err)
		return
	}

	p.handler.AddListener(p.logCallback)

	if p.cfg.EnableHTTP() {
		if p.cfg.EnableH2C() {
			p.http = &http.Server{
				Handler:  h2c.NewHandler(p, &http2.Server{}),
				ErrorLog: p.stdLog,
			}
		} else {
			p.http = &http.Server{
				Handler:  p,
				ErrorLog: p.stdLog,
			}
		}
	}

	if p.cfg.EnableTLS() {
		p.https = p.initSSL()
		if p.cfg.SSLConfig.RootCA != "" {
			err = p.appendRootCa()
			if err != nil {
				errCh <- errors.E(op, err)
				return
			}
		}

		// if HTTP2Config not nil
		if p.cfg.HTTP2Config != nil {
			if err := p.initHTTP2(); err != nil {
				errCh <- errors.E(op, err)
				return
			}
		}
	}

	if p.cfg.EnableFCGI() {
		p.fcgi = &http.Server{Handler: p, ErrorLog: p.stdLog}
	}

	// start http, https and fcgi servers if requested in the config
	go func() {
		p.serveHTTP(errCh)
	}()

	go func() {
		p.serveHTTPS(errCh)
	}()

	go func() {
		p.serveFCGI(errCh)
	}()
}

// Stop stops the http.
func (p *Plugin) Stop() error {
	p.Lock()
	defer p.Unlock()

	if p.fcgi != nil {
		err := p.fcgi.Shutdown(context.Background())
		if err != nil && err != http.ErrServerClosed {
			p.log.Error("fcgi shutdown", "error", err)
		}
	}

	if p.https != nil {
		err := p.https.Shutdown(context.Background())
		if err != nil && err != http.ErrServerClosed {
			p.log.Error("https shutdown", "error", err)
		}
	}

	if p.http != nil {
		err := p.http.Shutdown(context.Background())
		if err != nil && err != http.ErrServerClosed {
			p.log.Error("http shutdown", "error", err)
		}
	}

	// check for safety
	if p.pool != nil {
		p.pool.Destroy(context.Background())
	}

	return nil
}

// ServeHTTP handles connection using set of middleware and pool PSR-7 server.
func (p *Plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if headerContainsUpgrade(r) {
		http.Error(w, "server does not support upgrade header", http.StatusInternalServerError)
		return
	}

	if p.https != nil && r.TLS == nil && p.cfg.SSLConfig.Redirect {
		p.redirect(w, r)
		return
	}

	if p.https != nil && r.TLS != nil {
		w.Header().Add("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
	}

	r = attributes.Init(r)
	// protect the case, when user sendEvent Reset and we are replacing handler with pool
	p.RLock()
	p.handler.ServeHTTP(w, r)
	p.RUnlock()
}

// Workers returns slice with the process states for the workers
func (p *Plugin) Workers() []process.State {
	p.RLock()
	defer p.RUnlock()

	workers := p.workers()

	ps := make([]process.State, 0, len(workers))
	for i := 0; i < len(workers); i++ {
		state, err := process.WorkerProcessState(workers[i])
		if err != nil {
			return nil
		}
		ps = append(ps, state)
	}

	return ps
}

// internal
func (p *Plugin) workers() []worker.BaseProcess {
	return p.pool.Workers()
}

// Name returns endure.Named interface implementation
func (p *Plugin) Name() string {
	return PluginName
}

// Reset destroys the old pool and replaces it with new one, waiting for old pool to die
func (p *Plugin) Reset() error {
	p.Lock()
	defer p.Unlock()
	const op = errors.Op("http_plugin_reset")
	p.log.Info("HTTP plugin got restart request. Restarting...")
	p.pool.Destroy(context.Background())
	p.pool = nil

	var err error
	p.pool, err = p.server.NewWorkerPool(context.Background(), pool.Config{
		Debug:           p.cfg.Pool.Debug,
		NumWorkers:      p.cfg.Pool.NumWorkers,
		MaxJobs:         p.cfg.Pool.MaxJobs,
		AllocateTimeout: p.cfg.Pool.AllocateTimeout,
		DestroyTimeout:  p.cfg.Pool.DestroyTimeout,
		Supervisor:      p.cfg.Pool.Supervisor,
	}, p.cfg.Env, p.logCallback)
	if err != nil {
		return errors.E(op, err)
	}

	p.log.Info("HTTP workers Pool successfully restarted")

	p.handler, err = handler.NewHandler(
		p.cfg.MaxRequestSize,
		*p.cfg.Uploads,
		p.cfg.Cidrs,
		p.pool,
	)

	if err != nil {
		return errors.E(op, err)
	}

	p.log.Info("HTTP handler listeners successfully re-added")
	p.handler.AddListener(p.logCallback)

	p.log.Info("HTTP plugin successfully restarted")
	return nil
}

// Collects collecting http middlewares
func (p *Plugin) Collects() []interface{} {
	return []interface{}{
		p.AddMiddleware,
	}
}

// AddMiddleware is base requirement for the middleware (name and Middleware)
func (p *Plugin) AddMiddleware(name endure.Named, m Middleware) {
	p.mdwr[name.Name()] = m
}

// Status return status of the particular plugin
func (p *Plugin) Status() status.Status {
	p.RLock()
	defer p.RUnlock()

	workers := p.workers()
	for i := 0; i < len(workers); i++ {
		if workers[i].State().IsActive() {
			return status.Status{
				Code: http.StatusOK,
			}
		}
	}
	// if there are no workers, threat this as error
	return status.Status{
		Code: http.StatusServiceUnavailable,
	}
}

// Ready return readiness status of the particular plugin
func (p *Plugin) Ready() status.Status {
	p.RLock()
	defer p.RUnlock()

	workers := p.workers()
	for i := 0; i < len(workers); i++ {
		// If state of the worker is ready (at least 1)
		// we assume, that plugin'p worker pool is ready
		if workers[i].State().Value() == worker.StateReady {
			return status.Status{
				Code: http.StatusOK,
			}
		}
	}
	// if there are no workers, threat this as no content error
	return status.Status{
		Code: http.StatusServiceUnavailable,
	}
}

// Available interface implementation
func (p *Plugin) Available() {}