99 "log"
1010 "os"
1111 "path/filepath"
12+ "strconv"
1213 "strings"
1314 "sync"
1415 "time"
4849 fallback = flag .String ("fallback" , "" , "Fallback to some file. e.g.: If you serve a angular project, you can set it ./index.html" )
4950 enableColor = flag .String ("enablecolor" , "" , "Enable color output by http status code. e.g.: false" )
5051 enableUpload = flag .String ("enableupload" , "" , "Enable upload files" )
51- maxRequestBodySize = flag .Int ("maxrequestbodysize" , MaxInt , "Max request body size for upload big file" )
52+ maxRequestBodySize = flag .String ("maxrequestbodysize" , "" , "Max request body size for upload big file" )
53+ readTimeout = flag .String ("readtimeout" , "" , "Limit read timeout (unit: ns), 0 for unlimited" )
54+ writeTimeout = flag .String ("writetimeout" , "" , "Limit write timeout (unit: ns), 0 for unlimited" )
5255 makeconfig = flag .String ("makeconfig" , "" , "Make a config file. e.g.: config.yaml" )
5356 config = & Config {}
5457 fsMap = make (map [string ]fasthttp.RequestHandler )
@@ -73,6 +76,8 @@ type Config struct {
7376 EnableColor bool
7477 EnableUpload bool
7578 MaxRequestBodySize int
79+ ReadTimeout time.Duration
80+ WriteTimeout time.Duration
7681 HTTPProxy string `yaml:"HTTP_PROXY,omitempty"`
7782 HTTPSProxy string `yaml:"HTTPS_PROXY,omitempty"`
7883 NoProxy string `yaml:"NO_PROXY,omitempty"`
@@ -190,10 +195,26 @@ func main() {
190195 default :
191196 log .Fatalf ("error: %v" , fmt .Errorf ("argument enableupload error" ))
192197 }
193- if * maxRequestBodySize > 0 {
194- config .MaxRequestBodySize = * maxRequestBodySize
195- } else {
196- config .MaxRequestBodySize = fasthttp .DefaultMaxRequestBodySize
198+ if len (* maxRequestBodySize ) > 0 {
199+ i , err := strconv .Atoi (* maxRequestBodySize )
200+ if err != nil || i < 0 {
201+ log .Fatalf ("error: %v" , fmt .Errorf ("argument maxrequestbodysize error" ))
202+ }
203+ config .MaxRequestBodySize = i
204+ }
205+ if len (* readTimeout ) > 0 {
206+ i , err := strconv .ParseInt (* readTimeout , 10 , 64 )
207+ if err != nil || i < 0 {
208+ log .Fatalf ("error: %v" , fmt .Errorf ("argument readtimeout error" ))
209+ }
210+ config .ReadTimeout = time .Duration (i )
211+ }
212+ if len (* writeTimeout ) > 0 {
213+ i , err := strconv .ParseInt (* writeTimeout , 10 , 64 )
214+ if err != nil || i < 0 {
215+ log .Fatalf ("error: %v" , fmt .Errorf ("argument writetimeout error" ))
216+ }
217+ config .WriteTimeout = time .Duration (i )
197218 }
198219
199220 // safe warning
@@ -229,7 +250,12 @@ func main() {
229250 if len (config .Addr ) > 0 {
230251 log .Println ("Server address:" , config .Addr )
231252 go func () {
232- server := & fasthttp.Server {Handler : h , MaxRequestBodySize : config .MaxRequestBodySize }
253+ server := & fasthttp.Server {
254+ Handler : h ,
255+ MaxRequestBodySize : config .MaxRequestBodySize ,
256+ ReadTimeout : time .Duration (config .ReadTimeout ),
257+ WriteTimeout : time .Duration (config .WriteTimeout ),
258+ }
233259 if err := server .ListenAndServe (config .Addr ); err != nil {
234260 log .Fatalf ("error in ListenAndServe: %s" , err )
235261 }
@@ -240,7 +266,12 @@ func main() {
240266 log .Println ("CertFile:" , config .CertFile )
241267 log .Println ("KeyFile:" , config .KeyFile )
242268 go func () {
243- server := & fasthttp.Server {Handler : h , MaxRequestBodySize : config .MaxRequestBodySize }
269+ server := & fasthttp.Server {
270+ Handler : h ,
271+ MaxRequestBodySize : config .MaxRequestBodySize ,
272+ ReadTimeout : config .ReadTimeout ,
273+ WriteTimeout : config .WriteTimeout ,
274+ }
244275 if err := server .ListenAndServeTLS (config .AddrTLS , config .CertFile , config .KeyFile ); err != nil {
245276 log .Fatalf ("error in ListenAndServeTLS: %s" , err )
246277 }
@@ -254,6 +285,8 @@ func main() {
254285 log .Println ("EnableColor:" , config .EnableColor )
255286 log .Println ("EnableUpload:" , config .EnableUpload )
256287 log .Println ("MaxRequestBodySize:" , config .MaxRequestBodySize )
288+ log .Println ("ReadTimeout:" , config .ReadTimeout )
289+ log .Println ("WriteTimeout:" , config .WriteTimeout )
257290 indexNamesLen := len (config .IndexNames )
258291 if indexNamesLen > 0 {
259292 log .Printf ("Have %d index name(s):\n " , indexNamesLen )
@@ -699,8 +732,11 @@ indexnames:
699732verbose: true
700733enablecolor: true
701734enableupload: true
702- ## maxrequestbodysize: 0 to default size
735+ ## maxrequestbodysize 0 to default size
703736maxrequestbodysize: %d
737+ ## timeout 0s is no limit
738+ readtimeout: 0s
739+ writetimeout: 0s
704740logfile: ./simplehttpserver.log
705741#fallback: ./index.html
706742#HTTP_PROXY:
0 commit comments