forked from revel/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-fasthttp.go
627 lines (551 loc) · 16.8 KB
/
server-fasthttp.go
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package fasthttp
import (
"bytes"
"fmt"
"github.com/revel/revel"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/reuseport"
"io"
"mime/multipart"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"path"
"sort"
"strconv"
"strings"
"time"
"github.com/revel/revel/utils"
)
// The engine
type FastHTTPServer struct {
Server *fasthttp.Server // The server
ServerInit *revel.EngineInit // The server initialization data
MaxMultipartSize int64 // The max form size
HttpMuxList revel.ServerMuxList // The list of muxers
HasAppMux bool // True if has a mux
signalChan chan os.Signal // The channel to stop the server
graceful net.Listener // The graceful listener
}
// The server log
var serverLog = revel.AppLog
// Called to initialize
func init() {
revel.RegisterServerEngine("fasthttp", func() revel.ServerEngine {
return &FastHTTPServer{}
})
revel.RegisterModuleInit(func(m *revel.Module) {
serverLog = m.Log
})
}
// Called to initialize the FastHttpServer
func (f *FastHTTPServer) Init(init *revel.EngineInit) {
f.MaxMultipartSize = int64(revel.Config.IntDefault("server.request.max.multipart.filesize", 32)) << 20 /* 32 MB */
fastHttpContextStack = utils.NewStackLock(revel.Config.IntDefault("server.context.stack", 100),
revel.Config.IntDefault("server.context.maxstack", 200),
func() interface{} { return NewFastHttpContext(f) })
fastHttpMultipartFormStack = utils.NewStackLock(revel.Config.IntDefault("server.form.stack", 100),
revel.Config.IntDefault("server.form.maxstack", 200),
func() interface{} { return &FastHttpMultipartForm{} })
requestHandler := func(ctx *fasthttp.RequestCtx) {
f.RequestHandler(ctx)
}
// Adds the mux list
f.HttpMuxList = init.HTTPMuxList
sort.Sort(f.HttpMuxList)
f.HasAppMux = len(f.HttpMuxList) > 0
f.signalChan = make(chan os.Signal)
f.ServerInit = init
f.Server = &fasthttp.Server{
ReadTimeout: time.Duration(revel.Config.IntDefault("http.timeout.read", 0)) * time.Second,
WriteTimeout: time.Duration(revel.Config.IntDefault("http.timeout.write", 0)) * time.Second,
Handler: requestHandler,
}
}
// Handler is assigned in the Init
func (f *FastHTTPServer) Start() {
go func() {
time.Sleep(100 * time.Millisecond)
fmt.Printf("\nListening on fasthttp %s...\n", f.ServerInit.Address)
}()
if f.ServerInit.Network == "tcp" {
f.ServerInit.Network = "tcp4"
}
listener, err := reuseport.Listen(f.ServerInit.Network, f.ServerInit.Address)
if err != nil {
serverLog.Fatal("Failed to listen http:", "error", err, "network", f.ServerInit.Network, "address", f.ServerInit.Address)
}
// create a graceful shutdown listener
duration := 5 * time.Second
f.graceful = NewGracefulListener(listener, duration)
if revel.HTTPSsl {
if f.ServerInit.Network != "tcp" {
// This limitation is just to reduce complexity, since it is standard
// to terminate SSL upstream when using unix domain sockets.
serverLog.Fatal("SSL is only supported for TCP sockets. Specify a port to listen on.")
}
serverLog.Fatal("Failed to listen https:", "error",
f.Server.ServeTLS(f.graceful, revel.HTTPSslCert, revel.HTTPSslKey))
} else {
serverLog.Info("Listening fasthttp ", f.ServerInit.Network, f.ServerInit.Address)
serverLog.Warn("Server exiting", "error", f.Server.Serve(f.graceful))
}
}
// The root handler
func (f *FastHTTPServer) RequestHandler(ctx *fasthttp.RequestCtx) {
// This section is called if the developer has added custom mux to the app
if f.HasAppMux && f.handleAppMux(ctx) {
return
}
f.handleMux(ctx)
}
// Handle the request and response for the servers mux
func (f *FastHTTPServer) handleAppMux(ctx *fasthttp.RequestCtx) (result bool) {
// Check the prefix and split them
cpath := string(ctx.Path())
requestPath := path.Clean(cpath)
if handler, hasHandler := f.HttpMuxList.Find(requestPath); hasHandler {
result = true
clientIP := HttpClientIP(ctx)
localLog := serverLog.New("ip", clientIP,
"path", cpath, "method", string(ctx.Method()))
defer func() {
if err := recover(); err != nil {
localLog.Error("An error was caught using the handler", "path", requestPath, "error", err)
fmt.Fprintf(ctx, "Unable to handle response for third part mux %v", err)
ctx.Response.SetStatusCode(http.StatusInternalServerError)
return
}
}()
start := time.Now()
handler.(fasthttp.RequestHandler)(ctx)
localLog.Info("Request Stats",
"start", start,
"duration_seconds", time.Since(start).Seconds(), "section", "requestlog",
)
return
}
return
}
// ClientIP method returns client IP address from HTTP request.
//
// Note: Set property "app.behind.proxy" to true only if Revel is running
// behind proxy like nginx, haproxy, apache, etc. Otherwise
// you may get inaccurate Client IP address. Revel parses the
// IP address in the order of X-Forwarded-For, X-Real-IP.
//
// By default revel will get http.Request's RemoteAddr
func HttpClientIP(ctx *fasthttp.RequestCtx) string {
if revel.Config.BoolDefault("app.behind.proxy", false) {
// Header X-Forwarded-For
if fwdFor := strings.TrimSpace(string(ctx.Request.Header.Peek(revel.HdrForwardedFor))); fwdFor != "" {
index := strings.Index(fwdFor, ",")
if index == -1 {
return fwdFor
}
return fwdFor[:index]
}
// Header X-Real-Ip
if realIP := strings.TrimSpace(string(ctx.Request.Header.Peek(revel.HdrRealIP))); realIP != "" {
return realIP
}
}
return ctx.RemoteIP().String()
}
// Handle response
func (f *FastHTTPServer) handleMux(ctx *fasthttp.RequestCtx) {
// TODO limit max size of body that can be read
//if maxRequestSize := int64(revel.Config.IntDefault("http.maxrequestsize", 0)); maxRequestSize > 0 {
// buffer := &bytes.Buffer{}
// err := ctx.Request.ReadLimitBody(buffer,maxRequestSize)
// if err!=nil {
// // Send the error back to the client
// ctx.SetStatusCode(http.StatusRequestEntityTooLarge)
// return
// }
//}
context := fastHttpContextStack.Pop().(*FastHttpContext)
defer func() {
fastHttpContextStack.Push(context)
}()
context.SetContext(ctx)
f.ServerInit.Callback(context)
}
// Handle an event generated from Revel
func (f *FastHTTPServer) Event(event revel.Event, args interface{}) revel.EventResponse {
switch event {
case revel.ENGINE_STARTED:
signal.Notify(f.signalChan, os.Interrupt, os.Kill)
go func() {
_ = <-f.signalChan
serverLog.Info("Received quit singal Please wait ... ")
revel.StopServer(nil)
}()
case revel.ENGINE_SHUTDOWN_REQUEST:
if err := f.graceful.Close(); err != nil {
serverLog.Fatal("Failed to close fasthttp server gracefully, exiting using os.exit", "error", err)
}
default:
}
return 0
}
// Return the engine name
func (f *FastHTTPServer) Name() string {
return "fasthttp"
}
// Return the engine
func (f *FastHTTPServer) Engine() interface{} {
return f
}
// Returns stats for the engine
func (g *FastHTTPServer) Stats() map[string]interface{} {
return map[string]interface{}{
"FastHTTP Engine Context": fastHttpContextStack.String(),
"FastHTTP Engine Forms": fastHttpMultipartFormStack.String(),
}
}
type (
// The context
FastHttpContext struct {
Request *FastHttpRequest // The request
Response *FastHttpResponse // The respnse
}
// The request
FastHttpRequest struct {
url *url.URL // The url of request
toQuery bool // True if converted to query
query url.Values // The translated query
Original *fasthttp.RequestCtx // The original
FormParsed bool // True if the form was parsed
form url.Values // The form values
MultiFormParsed bool // True if multipart form
// WebSocket *websocket.Conn // No websocket
ParsedForm *FastHttpMultipartForm // The parsed form
header *FastHttpHeader // The request header
Engine *FastHTTPServer // The response header
}
// The response
FastHttpResponse struct {
Original *fasthttp.RequestCtx // The original
header *FastHttpHeader // The header
Writer io.Writer // The writer
Engine *FastHTTPServer // The engine
}
// The form
FastHttpMultipartForm struct {
Form *multipart.Form // The embedded form
}
// The header
FastHttpHeader struct {
Source interface{} // The source
isResponse bool // True if this is a response header
}
// The cookie
FastHttpCookie []byte // The cookie
)
var (
fastHttpContextStack *utils.SimpleLockStack // context stack
fastHttpMultipartFormStack *utils.SimpleLockStack // form stack
)
// Create a new context
func NewFastHttpContext(instance *FastHTTPServer) *FastHttpContext {
if instance == nil {
instance = &FastHTTPServer{MaxMultipartSize: 32 << 20}
}
c := &FastHttpContext{
Request: &FastHttpRequest{header: &FastHttpHeader{isResponse: false},
Engine: instance},
Response: &FastHttpResponse{header: &FastHttpHeader{isResponse: true},
Engine: instance},
}
c.Response.header.Source = c.Response
c.Request.header.Source = c.Request
return c
}
// Called to get the request
func (c *FastHttpContext) GetRequest() revel.ServerRequest {
return c.Request
}
// Called to get the response
func (c *FastHttpContext) GetResponse() revel.ServerResponse {
return c.Response
}
// Called to set the context
func (c *FastHttpContext) SetContext(context *fasthttp.RequestCtx) {
c.Response.SetContext(context)
c.Request.SetContext(context)
}
// Called to destroy the context
func (c *FastHttpContext) Destroy() {
c.Response.Destroy()
c.Request.Destroy()
}
// Gets the value from the request
func (r *FastHttpRequest) Get(key int) (value interface{}, err error) {
switch key {
case revel.HTTP_SERVER_HEADER:
value = r.GetHeader()
case revel.HTTP_MULTIPART_FORM:
value, err = r.GetMultipartForm()
case revel.HTTP_QUERY:
value = r.GetQuery()
case revel.HTTP_FORM:
value, err = r.GetForm()
case revel.HTTP_REQUEST_URI:
value = string(r.Original.RequestURI())
case revel.HTTP_REMOTE_ADDR:
value = r.Original.RemoteAddr().String()
case revel.HTTP_METHOD:
value = string(r.Original.Method())
case revel.HTTP_PATH:
value = string(r.Original.Path())
case revel.HTTP_HOST:
value = string(r.Original.Request.Host())
case revel.HTTP_URL:
if r.url == nil {
r.url, _ = url.Parse(string(r.Original.Request.URI().FullURI()))
}
value = r.url
case revel.HTTP_BODY:
value = bytes.NewBuffer(r.Original.Request.Body())
default:
err = revel.ENGINE_UNKNOWN_GET
}
return
}
// Sets the request with the value
func (r *FastHttpRequest) Set(key int, value interface{}) bool {
return false
}
// Returns the query string
func (r *FastHttpRequest) GetQuery() url.Values {
if !r.toQuery {
// Attempt to convert to query
r.query = url.Values{}
r.Original.QueryArgs().VisitAll(func(key, value []byte) {
r.query.Set(string(key), string(value))
})
r.toQuery = true
}
return r.query
}
// Returns the form
func (r *FastHttpRequest) GetForm() (url.Values, error) {
if !r.FormParsed {
r.form = url.Values{}
r.Original.PostArgs().VisitAll(func(key, value []byte) {
r.query.Set(string(key), string(value))
})
r.FormParsed = true
}
return r.form, nil
}
// Returns the form
func (r *FastHttpRequest) GetMultipartForm() (revel.ServerMultipartForm, error) {
if !r.MultiFormParsed {
// TODO Limit size r.Engine.MaxMultipartSize
form, err := r.Original.MultipartForm()
if err != nil {
return nil, err
}
r.ParsedForm = fastHttpMultipartFormStack.Pop().(*FastHttpMultipartForm)
r.ParsedForm.Form = form
}
return r.ParsedForm, nil
}
// Returns the request header
func (r *FastHttpRequest) GetHeader() revel.ServerHeader {
return r.header
}
// Returns the raw request
func (r *FastHttpRequest) GetRaw() interface{} {
return r.Original
}
// Sets the context
func (r *FastHttpRequest) SetContext(req *fasthttp.RequestCtx) {
r.Original = req
}
// Called when request is done
func (r *FastHttpRequest) Destroy() {
r.Original = nil
r.FormParsed = false
r.MultiFormParsed = false
r.ParsedForm = nil
r.toQuery = false
}
// gets the key from the response
func (r *FastHttpResponse) Get(key int) (value interface{}, err error) {
switch key {
case revel.HTTP_SERVER_HEADER:
value = r.Header()
case revel.HTTP_STREAM_WRITER:
value = r
case revel.HTTP_WRITER:
value = r.Writer
default:
err = revel.ENGINE_UNKNOWN_GET
}
return
}
// Sets the key with the value
func (r *FastHttpResponse) Set(key int, value interface{}) (set bool) {
switch key {
case revel.ENGINE_RESPONSE_STATUS:
r.Header().SetStatus(value.(int))
set = true
case revel.HTTP_WRITER:
r.SetWriter(value.(io.Writer))
set = true
}
return
}
// Return the response writer
func (r *FastHttpResponse) GetWriter() io.Writer {
return r.Writer
}
// Return the header
func (r *FastHttpResponse) Header() revel.ServerHeader {
return r.header
}
// Returns the raw response
func (r *FastHttpResponse) GetRaw() interface{} {
return r.Original
}
// Writes a stream to the response
func (r *FastHttpResponse) WriteStream(name string, contentlen int64, modtime time.Time, reader io.Reader) error {
// do a simple io.Copy, we do it directly into the writer which may be configured to be a compressed
// writer
ius := r.Original.Request.Header.Peek("If-Unmodified-Since")
if t, err := http.ParseTime(string(ius)); ius != nil && err == nil && !modtime.IsZero() {
// The Date-Modified header truncates sub-second precision, so
// use mtime < t+1s instead of mtime <= t to check for unmodified.
if modtime.Before(t.Add(1 * time.Second)) {
h := r.Original.Response.Header
h.Del("Content-Type")
h.Del("Content-Length")
if h.Peek("Etag") != nil {
h.Del("Last-Modified")
}
h.SetStatusCode(http.StatusNotModified)
return nil
}
}
if contentlen != -1 {
r.Original.Response.Header.Set("Content-Length", strconv.FormatInt(contentlen, 10))
}
if _, err := io.Copy(r.Writer, reader); err != nil {
r.Original.Response.Header.SetStatusCode(http.StatusInternalServerError)
return err
} else {
r.Original.Response.Header.SetStatusCode(http.StatusOK)
}
return nil
}
// Called to reset this response
func (r *FastHttpResponse) Destroy() {
if c, ok := r.Writer.(io.Closer); ok {
c.Close()
}
r.Original = nil
r.Writer = nil
}
// Sets the context
func (r *FastHttpResponse) SetContext(w *fasthttp.RequestCtx) {
r.Original = w
r.Writer = w.Response.BodyWriter()
}
// Sets the writer
func (r *FastHttpResponse) SetWriter(writer io.Writer) {
r.Writer = writer
}
// Sets a cookie
func (r *FastHttpHeader) SetCookie(cookie string) {
if r.isResponse {
r.Source.(*FastHttpResponse).Original.Response.Header.Add("Set-Cookie", cookie)
}
}
// Returns a cookie
func (r *FastHttpHeader) GetCookie(key string) (value revel.ServerCookie, err error) {
if !r.isResponse {
var cookie []byte
if cookie = r.Source.(*FastHttpRequest).Original.Request.Header.Cookie(key); cookie != nil {
value = FastHttpCookie(cookie)
} else {
err = http.ErrNoCookie
}
}
return
}
// Sets (replaces) a header key
func (r *FastHttpHeader) Set(key string, value string) {
if r.isResponse {
r.Source.(*FastHttpResponse).Original.Response.Header.Set(key, value)
}
}
// Adds a header key
func (r *FastHttpHeader) Add(key string, value string) {
if r.isResponse {
r.Source.(*FastHttpResponse).Original.Response.Header.Add(key, value)
}
}
// Deletes a header key
func (r *FastHttpHeader) Del(key string) {
if r.isResponse {
r.Source.(*FastHttpResponse).Original.Response.Header.Del(key)
}
}
// Returns the header keys
func (r *FastHttpHeader) GetKeys() (value []string) {
addValue := func(k, v []byte) {
found := false
key := string(k)
for _, r := range value {
if key == r {
found = true
break
}
}
if !found {
value = append(value, key)
}
}
if !r.isResponse {
r.Source.(*FastHttpRequest).Original.Request.Header.VisitAll(addValue)
} else {
r.Source.(*FastHttpResponse).Original.Response.Header.VisitAll(addValue)
}
return
}
// returns the header value
func (r *FastHttpHeader) Get(key string) (value []string) {
if !r.isResponse {
value = strings.Split(string(r.Source.(*FastHttpRequest).Original.Request.Header.Peek(key)), ",")
} else {
value = strings.Split(string(r.Source.(*FastHttpResponse).Original.Response.Header.Peek(key)), ",")
}
return
}
// Sets the header status
func (r *FastHttpHeader) SetStatus(statusCode int) {
if r.isResponse {
r.Source.(*FastHttpResponse).Original.Response.SetStatusCode(statusCode)
}
}
// Returns the cookie value
func (r FastHttpCookie) GetValue() string {
return string(r)
}
// Returns the files for a form
func (f *FastHttpMultipartForm) GetFiles() map[string][]*multipart.FileHeader {
return f.Form.File
}
// Returns the values for a form
func (f *FastHttpMultipartForm) GetValues() url.Values {
return url.Values(f.Form.Value)
}
// Remove all the vlaues from a form
func (f *FastHttpMultipartForm) RemoveAll() error {
return f.Form.RemoveAll()
}