Skip to content
This repository was archived by the owner on Mar 23, 2020. It is now read-only.

Commit 17595fd

Browse files
committed
add ReadTimeout and WriteTimeout config
1 parent 00cfb86 commit 17595fd

File tree

4 files changed

+61
-20
lines changed

4 files changed

+61
-20
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ Browse <https://localhost:8081>
8181
verbose: true
8282
enablecolor: true
8383
enableupload: true
84-
## maxrequestbodysize:0 to default size
85-
#maxrequestbodysize: 4294967296
86-
#logfile: ./simplehttpserver.log
84+
## maxrequestbodysize 0 to default size
85+
maxrequestbodysize: 9223372036854775807
86+
## timeout 0s is no limit
87+
readtimeout: 0s
88+
writetimeout: 0s
89+
logfile: ./simplehttpserver.log
8790
#fallback: ./index.html
8891
#HTTP_PROXY:
8992
#HTTPS_PROXY:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module simplehttpserver
33
go 1.13
44

55
require (
6-
github.com/fatih/color v1.8.0
6+
github.com/fatih/color v1.9.0
77
github.com/valyala/fasthttp v1.7.1
88
gopkg.in/yaml.v2 v2.2.7
99
)

go.sum

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
github.com/fatih/color v1.8.0 h1:5bzFgL+oy7JITMTxUPJ00n7VxmYd/PdMp5mHFX40/RY=
2-
github.com/fatih/color v1.8.0/go.mod h1:3l45GVGkyrnYNl9HoIjnp2NnNWvh6hLAqD8yTfGjnw8=
1+
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
2+
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
33
github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs=
44
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
55
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
66
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
7-
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
8-
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
9-
github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI=
10-
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
7+
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
8+
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
9+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
10+
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
11+
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
1112
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
1213
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
1314
github.com/valyala/fasthttp v1.7.1 h1:UHtt5/7O70RSUZTR/hSu0PNWMAfWx5AtsPp9Jk+g17M=
1415
github.com/valyala/fasthttp v1.7.1/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
1516
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
1617
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1718
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
18-
golang.org/x/sys v0.0.0-20180202135801-37707fdb30a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
19-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
2019
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
20+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
21+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
22+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2123
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
2224
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2325
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=

main.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010
"os"
1111
"path/filepath"
12+
"strconv"
1213
"strings"
1314
"sync"
1415
"time"
@@ -48,7 +49,9 @@ var (
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:
699732
verbose: true
700733
enablecolor: true
701734
enableupload: true
702-
## maxrequestbodysize:0 to default size
735+
## maxrequestbodysize 0 to default size
703736
maxrequestbodysize: %d
737+
## timeout 0s is no limit
738+
readtimeout: 0s
739+
writetimeout: 0s
704740
logfile: ./simplehttpserver.log
705741
#fallback: ./index.html
706742
#HTTP_PROXY:

0 commit comments

Comments
 (0)