Skip to content

Commit eae4239

Browse files
committed
docs: add documentation for exclusion and decompression functions
- Add documentation for `WithExcludedExtensions` function - Add documentation for `WithExcludedPaths` function - Add documentation for `WithExcludedPathsRegexs` function - Add documentation for `WithDecompressFn` function - Add documentation for `NewExcludedExtensions` function - Add documentation for `Contains` method in `ExcludedExtensions` - Add documentation for `NewExcludedPaths` function - Add documentation for `Contains` method in `ExcludedPaths` - Add documentation for `NewExcludedPathesRegexs` function - Add documentation for `Contains` method in `ExcludedPathesRegexs` - Add documentation for `DefaultDecompressHandle` function Signed-off-by: appleboy <[email protected]>
1 parent 1b4a866 commit eae4239

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

options.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,36 @@ type Options struct {
2828

2929
type Option func(*Options)
3030

31+
// WithExcludedExtensions returns an Option that sets the ExcludedExtensions field of the Options struct.
32+
// Parameters:
33+
// - args: []string - A slice of file extensions to exclude from gzip compression.
3134
func WithExcludedExtensions(args []string) Option {
3235
return func(o *Options) {
3336
o.ExcludedExtensions = NewExcludedExtensions(args)
3437
}
3538
}
3639

40+
// WithExcludedPaths returns an Option that sets the ExcludedPaths field of the Options struct.
41+
// Parameters:
42+
// - args: []string - A slice of paths to exclude from gzip compression.
3743
func WithExcludedPaths(args []string) Option {
3844
return func(o *Options) {
3945
o.ExcludedPaths = NewExcludedPaths(args)
4046
}
4147
}
4248

49+
// WithExcludedPathsRegexs returns an Option that sets the ExcludedPathesRegexs field of the Options struct.
50+
// Parameters:
51+
// - args: []string - A slice of regex patterns to exclude paths from gzip compression.
4352
func WithExcludedPathsRegexs(args []string) Option {
4453
return func(o *Options) {
4554
o.ExcludedPathesRegexs = NewExcludedPathesRegexs(args)
4655
}
4756
}
4857

58+
// WithDecompressFn returns an Option that sets the DecompressFn field of the Options struct.
59+
// Parameters:
60+
// - decompressFn: func(c *gin.Context) - A function to handle decompression of incoming requests.
4961
func WithDecompressFn(decompressFn func(c *gin.Context)) Option {
5062
return func(o *Options) {
5163
o.DecompressFn = decompressFn
@@ -65,6 +77,12 @@ func WithDecompressOnly() Option {
6577
// Using map for better lookup performance
6678
type ExcludedExtensions map[string]struct{}
6779

80+
// NewExcludedExtensions creates a new ExcludedExtensions map from a slice of file extensions.
81+
// Parameters:
82+
// - extensions: []string - A slice of file extensions to exclude from gzip compression.
83+
//
84+
// Returns:
85+
// - ExcludedExtensions - A map of excluded file extensions.
6886
func NewExcludedExtensions(extensions []string) ExcludedExtensions {
6987
res := make(ExcludedExtensions, len(extensions))
7088
for _, e := range extensions {
@@ -73,17 +91,35 @@ func NewExcludedExtensions(extensions []string) ExcludedExtensions {
7391
return res
7492
}
7593

94+
// Contains checks if a given file extension is in the ExcludedExtensions map.
95+
// Parameters:
96+
// - target: string - The file extension to check.
97+
//
98+
// Returns:
99+
// - bool - True if the extension is excluded, false otherwise.
76100
func (e ExcludedExtensions) Contains(target string) bool {
77101
_, ok := e[target]
78102
return ok
79103
}
80104

81105
type ExcludedPaths []string
82106

107+
// NewExcludedPaths creates a new ExcludedPaths slice from a slice of paths.
108+
// Parameters:
109+
// - paths: []string - A slice of paths to exclude from gzip compression.
110+
//
111+
// Returns:
112+
// - ExcludedPaths - A slice of excluded paths.
83113
func NewExcludedPaths(paths []string) ExcludedPaths {
84114
return ExcludedPaths(paths)
85115
}
86116

117+
// Contains checks if a given request URI starts with any of the excluded paths.
118+
// Parameters:
119+
// - requestURI: string - The request URI to check.
120+
//
121+
// Returns:
122+
// - bool - True if the URI starts with an excluded path, false otherwise.
87123
func (e ExcludedPaths) Contains(requestURI string) bool {
88124
for _, path := range e {
89125
if strings.HasPrefix(requestURI, path) {
@@ -95,6 +131,12 @@ func (e ExcludedPaths) Contains(requestURI string) bool {
95131

96132
type ExcludedPathesRegexs []*regexp.Regexp
97133

134+
// NewExcludedPathesRegexs creates a new ExcludedPathesRegexs slice from a slice of regex patterns.
135+
// Parameters:
136+
// - regexs: []string - A slice of regex patterns to exclude paths from gzip compression.
137+
//
138+
// Returns:
139+
// - ExcludedPathesRegexs - A slice of excluded path regex patterns.
98140
func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs {
99141
result := make(ExcludedPathesRegexs, len(regexs))
100142
for i, reg := range regexs {
@@ -103,6 +145,12 @@ func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs {
103145
return result
104146
}
105147

148+
// Contains checks if a given request URI matches any of the excluded path regex patterns.
149+
// Parameters:
150+
// - requestURI: string - The request URI to check.
151+
//
152+
// Returns:
153+
// - bool - True if the URI matches an excluded path regex pattern, false otherwise.
106154
func (e ExcludedPathesRegexs) Contains(requestURI string) bool {
107155
for _, reg := range e {
108156
if reg.MatchString(requestURI) {
@@ -112,6 +160,16 @@ func (e ExcludedPathesRegexs) Contains(requestURI string) bool {
112160
return false
113161
}
114162

163+
// DefaultDecompressHandle is a middleware function for the Gin framework that
164+
// decompresses the request body if it is gzip encoded. It checks if the request
165+
// body is nil and returns immediately if it is. Otherwise, it attempts to create
166+
// a new gzip reader from the request body. If an error occurs during this process,
167+
// it aborts the request with a 400 Bad Request status and the error. If successful,
168+
// it removes the "Content-Encoding" and "Content-Length" headers from the request
169+
// and replaces the request body with the decompressed reader.
170+
//
171+
// Parameters:
172+
// - c: *gin.Context - The Gin context for the current request.
115173
func DefaultDecompressHandle(c *gin.Context) {
116174
if c.Request.Body == nil {
117175
return

0 commit comments

Comments
 (0)