@@ -28,24 +28,36 @@ type Options struct {
28
28
29
29
type Option func (* Options )
30
30
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.
31
34
func WithExcludedExtensions (args []string ) Option {
32
35
return func (o * Options ) {
33
36
o .ExcludedExtensions = NewExcludedExtensions (args )
34
37
}
35
38
}
36
39
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.
37
43
func WithExcludedPaths (args []string ) Option {
38
44
return func (o * Options ) {
39
45
o .ExcludedPaths = NewExcludedPaths (args )
40
46
}
41
47
}
42
48
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.
43
52
func WithExcludedPathsRegexs (args []string ) Option {
44
53
return func (o * Options ) {
45
54
o .ExcludedPathesRegexs = NewExcludedPathesRegexs (args )
46
55
}
47
56
}
48
57
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.
49
61
func WithDecompressFn (decompressFn func (c * gin.Context )) Option {
50
62
return func (o * Options ) {
51
63
o .DecompressFn = decompressFn
@@ -65,6 +77,12 @@ func WithDecompressOnly() Option {
65
77
// Using map for better lookup performance
66
78
type ExcludedExtensions map [string ]struct {}
67
79
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.
68
86
func NewExcludedExtensions (extensions []string ) ExcludedExtensions {
69
87
res := make (ExcludedExtensions , len (extensions ))
70
88
for _ , e := range extensions {
@@ -73,17 +91,35 @@ func NewExcludedExtensions(extensions []string) ExcludedExtensions {
73
91
return res
74
92
}
75
93
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.
76
100
func (e ExcludedExtensions ) Contains (target string ) bool {
77
101
_ , ok := e [target ]
78
102
return ok
79
103
}
80
104
81
105
type ExcludedPaths []string
82
106
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.
83
113
func NewExcludedPaths (paths []string ) ExcludedPaths {
84
114
return ExcludedPaths (paths )
85
115
}
86
116
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.
87
123
func (e ExcludedPaths ) Contains (requestURI string ) bool {
88
124
for _ , path := range e {
89
125
if strings .HasPrefix (requestURI , path ) {
@@ -95,6 +131,12 @@ func (e ExcludedPaths) Contains(requestURI string) bool {
95
131
96
132
type ExcludedPathesRegexs []* regexp.Regexp
97
133
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.
98
140
func NewExcludedPathesRegexs (regexs []string ) ExcludedPathesRegexs {
99
141
result := make (ExcludedPathesRegexs , len (regexs ))
100
142
for i , reg := range regexs {
@@ -103,6 +145,12 @@ func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs {
103
145
return result
104
146
}
105
147
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.
106
154
func (e ExcludedPathesRegexs ) Contains (requestURI string ) bool {
107
155
for _ , reg := range e {
108
156
if reg .MatchString (requestURI ) {
@@ -112,6 +160,16 @@ func (e ExcludedPathesRegexs) Contains(requestURI string) bool {
112
160
return false
113
161
}
114
162
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.
115
173
func DefaultDecompressHandle (c * gin.Context ) {
116
174
if c .Request .Body == nil {
117
175
return
0 commit comments