FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
caddy/caddyhttp/limits/handler.go at master · swordstudiox/caddy · GitHub
swordstudiox
caddy
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
caddy
/
caddyhttp
/
limits
/
handler.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
104 lines (90 loc) · 2.81 KB
Breadcrumbs
caddy
/
caddyhttp
/
limits
/
handler.go
Copy path
File metadata and controls
104 lines (90 loc) · 2.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// Copyright 2015 Light Code Labs, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package
limits
import
(
"io"
"net/http"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
// Limit is a middleware to control request body size
type
Limit
struct
{
Next
httpserver.
Handler
BodyLimits
[]httpserver.
PathLimit
}
func
(
l
Limit
)
ServeHTTP
(
w
http.
ResponseWriter
,
r
*
http.
Request
) (
int
,
error
) {
if
r
.
Body
==
nil
{
return
l
.
Next
.
ServeHTTP
(
w
,
r
)
}
// apply the path-based request body size limit.
for
_
,
bl
:=
range
l
.
BodyLimits
{
if
httpserver
.
Path
(
r
.
URL
.
Path
).
Matches
(
bl
.
Path
) {
r
.
Body
=
MaxBytesReader
(
w
,
r
.
Body
,
bl
.
Limit
)
break
}
}
return
l
.
Next
.
ServeHTTP
(
w
,
r
)
}
// MaxBytesReader and its associated methods are borrowed from the
// Go Standard library (comments intact). The only difference is that
// it returns a ErrMaxBytesExceeded error instead of a generic error message
// when the request body has exceeded the requested limit
func
MaxBytesReader
(
w
http.
ResponseWriter
,
r
io.
ReadCloser
,
n
int64
) io.
ReadCloser
{
return
&
maxBytesReader
{
w
:
w
,
r
:
r
,
n
:
n
}
}
type
maxBytesReader
struct
{
w
http.
ResponseWriter
r
io.
ReadCloser
// underlying reader
n
int64
// max bytes remaining
err
error
// sticky error
}
func
(
l
*
maxBytesReader
)
Read
(
p
[]
byte
) (
n
int
,
err
error
) {
if
l
.
err
!=
nil
{
return
0
,
l
.
err
}
if
len
(
p
)
==
0
{
return
0
,
nil
}
// If they asked for a 32KB byte read but only 5 bytes are
// remaining, no need to read 32KB. 6 bytes will answer the
// question of the whether we hit the limit or go past it.
if
int64
(
len
(
p
))
>
l
.
n
+
1
{
p
=
p
[:
l
.
n
+
1
]
}
n
,
err
=
l
.
r
.
Read
(
p
)
if
int64
(
n
)
<=
l
.
n
{
l
.
n
-=
int64
(
n
)
l
.
err
=
err
return
n
,
err
}
n
=
int
(
l
.
n
)
l
.
n
=
0
// The server code and client code both use
// maxBytesReader. This "requestTooLarge" check is
// only used by the server code. To prevent binaries
// which only using the HTTP Client code (such as
// cmd/go) from also linking in the HTTP server, don't
// use a static type assertion to the server
// "*response" type. Check this interface instead:
type
requestTooLarger
interface
{
requestTooLarge
()
}
if
res
,
ok
:=
l
.
w
.(
requestTooLarger
);
ok
{
res
.
requestTooLarge
()
}
l
.
err
=
httpserver
.
ErrMaxBytesExceeded
return
n
,
l
.
err
}
func
(
l
*
maxBytesReader
)
Close
()
error
{
return
l
.
r
.
Close
()
}
Back
|
FazBrowse Home
|
New Git URL