@@ -50,24 +50,27 @@ type (
50
50
ServerHeader string `default:""`
51
51
// Enables handler values to be immutable even if you return from handler
52
52
Immutable bool `default:"false"`
53
+ // Enables GZip / Deflate compression for all responses
54
+ Compression bool `default:"false"`
55
+ // CompressionLevel int `default:"1"`
53
56
// fasthttp settings
54
- GETOnly bool `default:"false"`
55
- IdleTimeout time.Duration `default:"0"`
56
- Concurrency int `default:"256 * 1024"`
57
- ReadTimeout time.Duration `default:"0"`
58
- WriteTimeout time.Duration `default:"0"`
59
- TCPKeepalive bool `default:"false"`
60
- MaxConnsPerIP int `default:"0"`
61
- ReadBufferSize int `default:"4096"`
62
- WriteBufferSize int `default:"4096"`
63
- ConcurrencySleep time.Duration `default:"0"`
64
- DisableKeepAlive bool `default:"false"`
65
- ReduceMemoryUsage bool `default:"false"`
66
- MaxRequestsPerConn int `default:"0"`
67
- TCPKeepalivePeriod time.Duration `default:"0"`
68
- MaxRequestBodySize int `default:"4 * 1024 * 1024"`
69
- NoHeaderNormalizing bool `default:"false"`
70
- NoDefaultContentType bool `default:"false"`
57
+ // GETOnly bool `default:"false"`
58
+ // IdleTimeout time.Duration `default:"0"`
59
+ // Concurrency int `default:"256 * 1024"`
60
+ // ReadTimeout time.Duration `default:"0"`
61
+ // WriteTimeout time.Duration `default:"0"`
62
+ // TCPKeepalive bool `default:"false"`
63
+ // MaxConnsPerIP int `default:"0"`
64
+ // ReadBufferSize int `default:"4096"`
65
+ // WriteBufferSize int `default:"4096"`
66
+ // ConcurrencySleep time.Duration `default:"0"`
67
+ // DisableKeepAlive bool `default:"false"`
68
+ // ReduceMemoryUsage bool `default:"false"`
69
+ // MaxRequestsPerConn int `default:"0"`
70
+ // TCPKeepalivePeriod time.Duration `default:"0"`
71
+ BodyLimit int `default:"4 * 1024 * 1024"`
72
+ // NoHeaderNormalizing bool `default:"false"`
73
+ // NoDefaultContentType bool `default:"false"`
71
74
// template settings
72
75
TemplateFolder string `default:""`
73
76
TemplateEngine string `default:""`
@@ -104,27 +107,30 @@ func New(settings ...*Settings) (app *App) {
104
107
return string (b )
105
108
}
106
109
}
107
- if opt .Concurrency == 0 {
108
- opt .Concurrency = 256 * 1024
109
- }
110
- if opt .ReadBufferSize == 0 {
111
- opt .ReadBufferSize = 4096
112
- }
113
- if opt .WriteBufferSize == 0 {
114
- opt .WriteBufferSize = 4096
115
- }
116
- if opt .MaxRequestBodySize == 0 {
117
- opt .MaxRequestBodySize = 4 * 1024 * 1024
110
+ // if opt.Concurrency == 0 {
111
+ // opt.Concurrency = 256 * 1024
112
+ // }
113
+ // if opt.ReadBufferSize == 0 {
114
+ // opt.ReadBufferSize = 4096
115
+ // }
116
+ // if opt.WriteBufferSize == 0 {
117
+ // opt.WriteBufferSize = 4096
118
+ // }
119
+ if opt .BodyLimit == 0 {
120
+ opt .BodyLimit = 4 * 1024 * 1024
118
121
}
122
+ // if opt.CompressionLevel == 0 {
123
+ // opt.CompressionLevel = 1
124
+ // }
119
125
app .Settings = opt
120
126
return
121
127
}
122
128
app .Settings = & Settings {
123
- Prefork : prefork ,
124
- Concurrency : 256 * 1024 ,
125
- ReadBufferSize : 4096 ,
126
- WriteBufferSize : 4096 ,
127
- MaxRequestBodySize : 4 * 1024 * 1024 ,
129
+ Prefork : prefork ,
130
+ // Concurrency: 256 * 1024,
131
+ // ReadBufferSize: 4096,
132
+ // WriteBufferSize: 4096,
133
+ BodyLimit : 4 * 1024 * 1024 ,
128
134
}
129
135
return
130
136
}
@@ -335,11 +341,11 @@ func (app *App) Test(request *http.Request) (*http.Response, error) {
335
341
func (app * App ) prefork (address string ) (ln net.Listener , err error ) {
336
342
// Master proc
337
343
if ! app .child {
338
- addr , err := net .ResolveTCPAddr ("tcp4 " , address )
344
+ addr , err := net .ResolveTCPAddr ("tcp " , address )
339
345
if err != nil {
340
346
return ln , err
341
347
}
342
- tcplistener , err := net .ListenTCP ("tcp4 " , addr )
348
+ tcplistener , err := net .ListenTCP ("tcp " , addr )
343
349
if err != nil {
344
350
return ln , err
345
351
}
@@ -373,31 +379,44 @@ func (app *App) prefork(address string) (ln net.Listener, err error) {
373
379
return ln , err
374
380
}
375
381
382
+ type disableLogger struct {}
383
+
384
+ func (dl * disableLogger ) Printf (format string , args ... interface {}) {
385
+ // fmt.Println(fmt.Sprintf(format, args...))
386
+ }
387
+
376
388
func (app * App ) newServer () * fasthttp.Server {
377
389
return & fasthttp.Server {
378
390
Handler : app .handler ,
391
+ Name : app .Settings .ServerHeader ,
392
+ // Concurrency: app.Settings.Concurrency,
393
+ // SleepWhenConcurrencyLimitsExceeded: app.Settings.ConcurrencySleep,
394
+ // DisableKeepalive: app.Settings.DisableKeepAlive,
395
+ // ReadBufferSize: app.Settings.ReadBufferSize,
396
+ // WriteBufferSize: app.Settings.WriteBufferSize,
397
+ // ReadTimeout: app.Settings.ReadTimeout,
398
+ // WriteTimeout: app.Settings.WriteTimeout,
399
+ // IdleTimeout: app.Settings.IdleTimeout,
400
+ // MaxConnsPerIP: app.Settings.MaxConnsPerIP,
401
+ // MaxRequestsPerConn: app.Settings.MaxRequestsPerConn,
402
+ // TCPKeepalive: app.Settings.TCPKeepalive,
403
+ // TCPKeepalivePeriod: app.Settings.TCPKeepalivePeriod,
404
+ MaxRequestBodySize : app .Settings .BodyLimit ,
405
+ // ReduceMemoryUsage: app.Settings.ReduceMemoryUsage,
406
+ // GetOnly: app.Settings.GETOnly,
407
+ // DisableHeaderNamesNormalizing: app.Settings.NoHeaderNormalizing,
408
+ NoDefaultServerHeader : app .Settings .ServerHeader == "" ,
409
+ // NoDefaultContentType: app.Settings.NoDefaultContentType,
410
+ Logger : & disableLogger {},
411
+ LogAllErrors : false ,
379
412
ErrorHandler : func (ctx * fasthttp.RequestCtx , err error ) {
380
- ctx .Response .SetStatusCode (400 )
381
- ctx .Response .SetBodyString ("Bad Request" )
413
+ if err .Error () == "body size exceeds the given limit" {
414
+ ctx .Response .SetStatusCode (413 )
415
+ ctx .Response .SetBodyString ("Request Entity Too Large" )
416
+ } else {
417
+ ctx .Response .SetStatusCode (400 )
418
+ ctx .Response .SetBodyString ("Bad Request" )
419
+ }
382
420
},
383
- Name : app .Settings .ServerHeader ,
384
- Concurrency : app .Settings .Concurrency ,
385
- SleepWhenConcurrencyLimitsExceeded : app .Settings .ConcurrencySleep ,
386
- DisableKeepalive : app .Settings .DisableKeepAlive ,
387
- ReadBufferSize : app .Settings .ReadBufferSize ,
388
- WriteBufferSize : app .Settings .WriteBufferSize ,
389
- ReadTimeout : app .Settings .ReadTimeout ,
390
- WriteTimeout : app .Settings .WriteTimeout ,
391
- IdleTimeout : app .Settings .IdleTimeout ,
392
- MaxConnsPerIP : app .Settings .MaxConnsPerIP ,
393
- MaxRequestsPerConn : app .Settings .MaxRequestsPerConn ,
394
- TCPKeepalive : app .Settings .TCPKeepalive ,
395
- TCPKeepalivePeriod : app .Settings .TCPKeepalivePeriod ,
396
- MaxRequestBodySize : app .Settings .MaxRequestBodySize ,
397
- ReduceMemoryUsage : app .Settings .ReduceMemoryUsage ,
398
- GetOnly : app .Settings .GETOnly ,
399
- DisableHeaderNamesNormalizing : app .Settings .NoHeaderNormalizing ,
400
- NoDefaultServerHeader : app .Settings .ServerHeader == "" ,
401
- NoDefaultContentType : app .Settings .NoDefaultContentType ,
402
421
}
403
422
}
0 commit comments