@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 92 lines 2.39 KB
Raw
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package timeouts
16
17 import (
18 "strconv"
19 "time"
20
21 "github.com/dustin/go-humanize"
22
23 "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
24 "github.com/caddyserver/caddy/v2/modules/caddyhttp"
25 )
26
27 func init() {
28 httpcaddyfile.RegisterHandlerDirective("timeouts", parseCaddyfile)
29 }
30
31 func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
32 h.Next() // consume directive name
33
34 t := new(Timeouts)
35
36 // configuration should be in a block
37 for h.NextBlock(0) {
38 switch h.Val() {
39 case "read_timeout":
40 args := h.RemainingArgs()
41 if len(args) < 1 || len(args) > 2 {
42 return nil, h.ArgErr()
43 }
44 timeout, err := time.ParseDuration(args[0])
45 if err != nil {
46 return nil, h.Errf("parsing read_timeout: %v", err)
47 }
48 t.ReadTimeout = timeout
49 if len(args) == 2 {
50 rate, err := strconv.ParseInt(args[1], 10, 64)
51 if err != nil {
52 return nil, h.Errf("parsing read_timeout min_rate: %v", err)
53 }
54 t.ReadMinRate = rate
55 }
56
57 case "write_timeout":
58 args := h.RemainingArgs()
59 if len(args) < 1 || len(args) > 2 {
60 return nil, h.ArgErr()
61 }
62 timeout, err := time.ParseDuration(args[0])
63 if err != nil {
64 return nil, h.Errf("parsing write_timeout: %v", err)
65 }
66 t.WriteTimeout = timeout
67 if len(args) == 2 {
68 rate, err := strconv.ParseInt(args[1], 10, 64)
69 if err != nil {
70 return nil, h.Errf("parsing write_timeout min_rate: %v", err)
71 }
72 t.WriteMinRate = rate
73 }
74
75 case "max_write_chunk":
76 var sizeStr string
77 if !h.AllArgs(&sizeStr) {
78 return nil, h.ArgErr()
79 }
80 size, err := humanize.ParseBytes(sizeStr)
81 if err != nil {
82 return nil, h.Errf("parsing max_write_chunk: %v", err)
83 }
84 t.MaxWriteChunk = int(size)
85
86 default:
87 return nil, h.Errf("unrecognized timeouts subdirective '%s'", h.Val())
88 }
89 }
90
91 return t, nil
92 }