-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompression.diff
More file actions
416 lines (394 loc) · 11.8 KB
/
Copy pathcompression.diff
File metadata and controls
416 lines (394 loc) · 11.8 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
--- input/input.go
+++ output/output.go
@@ -55,40 +55,8 @@
type Context struct {
writermem responseWriter
Request *http.Request
- Writer ResponseWriter
-
- Params Params
- handlers HandlersChain
- index int8
- fullPath string
-
- engine *Engine
- params *Params
- skippedNodes *[]skippedNode
-
- // This mutex protects Keys map.
- mu sync.RWMutex
-
- // Keys is a key/value pair exclusively for the context of each request.
- Keys map[string]any
-
- // Errors is a list of errors attached to all the handlers/middlewares who used this context.
- Errors errorMsgs
-
- // Accepted defines a list of manually accepted formats for content negotiation.
- Accepted []string
-
- // queryCache caches the query result from c.Request.URL.Query().
- queryCache url.Values
-
- // formCache caches c.Request.PostForm, which contains the parsed form data from POST, PATCH,
- // or PUT body parameters.
- formCache url.Values
-
- // SameSite allows a server to define a cookie attribute making it impossible for
- // the browser to send this cookie along with cross-site requests.
+ { … 32 line(s) … ⟦tj:e76a48b1c6b938fc7a52f940ea8696c8⟧ }
sameSite http.SameSite
-}
/************************************/
/********** CONTEXT CREATION ********/
@@ -97,49 +65,16 @@
func (c *Context) reset() {
c.Writer = &c.writermem
c.Params = c.Params[:0]
- c.handlers = nil
- c.index = -1
-
- c.fullPath = ""
- c.Keys = nil
- c.Errors = c.Errors[:0]
- c.Accepted = nil
- c.queryCache = nil
- c.formCache = nil
- c.sameSite = 0
- *c.params = (*c.params)[:0]
+ { … 11 line(s) … ⟦tj:5b3c24b3a9ab0063b837cb0434a02fbe⟧ }
*c.skippedNodes = (*c.skippedNodes)[:0]
-}
// Copy returns a copy of the current context that can be safely used outside the request's scope.
// This has to be used when the context has to be passed to a goroutine.
func (c *Context) Copy() *Context {
cp := Context{
writermem: c.writermem,
- Request: c.Request,
- engine: c.engine,
- }
-
- cp.writermem.ResponseWriter = nil
- cp.Writer = &cp.writermem
- cp.index = abortIndex
- cp.handlers = nil
- cp.fullPath = c.fullPath
-
- cKeys := c.Keys
- cp.Keys = make(map[string]any, len(cKeys))
- c.mu.RLock()
- for k, v := range cKeys {
- cp.Keys[k] = v
- }
- c.mu.RUnlock()
-
- cParams := c.Params
- cp.Params = make([]Param, len(cParams))
- copy(cp.Params, cParams)
-
+ { … 22 line(s) … ⟦tj:feec7b1b9368f7f5a066c814534365b2⟧ }
return &cp
-}
// HandlerName returns the main handler's name. For example if the handler is "handleGetUsers()",
// this function will return "main.handleGetUsers".
@@ -236,20 +171,8 @@
func (c *Context) Error(err error) *Error {
if err == nil {
panic("err is nil")
- }
-
- var parsedError *Error
- ok := errors.As(err, &parsedError)
- if !ok {
- parsedError = &Error{
- Err: err,
- Type: ErrorTypePrivate,
- }
- }
-
- c.Errors = append(c.Errors, parsedError)
+ { … 12 line(s) … ⟦tj:96faaac29ac615e151f028edb079862e⟧ }
return parsedError
-}
/************************************/
/******** METADATA MANAGEMENT********/
@@ -537,17 +460,7 @@
}
func (c *Context) initFormCache() {
- if c.formCache == nil {
- c.formCache = make(url.Values)
- req := c.Request
- if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
- if !errors.Is(err, http.ErrNotMultipart) {
- debugPrint("error on parse multipart form array: %v", err)
- }
- }
- c.formCache = req.PostForm
- }
-}
+ { … 11 line(s) … ⟦tj:7f2b97d4ce87767a6e5a0af8ae35916e⟧ }
// GetPostFormArray returns a slice of strings for a given form key, plus
// a boolean value whether at least one value exists for the given key.
@@ -572,33 +485,11 @@
// get is an internal method and returns a map which satisfies conditions.
func (c *Context) get(m map[string][]string, key string) (map[string]string, bool) {
- dicts := make(map[string]string)
- exist := false
- for k, v := range m {
- if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {
- if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {
- exist = true
- dicts[k[i+1:][:j]] = v[0]
- }
- }
- }
- return dicts, exist
-}
+ { … 12 line(s) … ⟦tj:b7b1f01e68248c2779fdadd87580e96e⟧ }
// FormFile returns the first file for the provided form key.
func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
- if c.Request.MultipartForm == nil {
- if err := c.Request.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
- return nil, err
- }
- }
- f, fh, err := c.Request.FormFile(name)
- if err != nil {
- return nil, err
- }
- f.Close()
- return fh, err
-}
+ { … 12 line(s) … ⟦tj:ca98264052fa4111d12d090b07e64b35⟧ }
// MultipartForm is the parsed multipart form, including file uploads.
func (c *Context) MultipartForm() (*multipart.Form, error) {
@@ -760,19 +651,8 @@
func (c *Context) ShouldBindBodyWith(obj any, bb binding.BindingBody) (err error) {
var body []byte
if cb, ok := c.Get(BodyBytesKey); ok {
- if cbb, ok := cb.([]byte); ok {
- body = cbb
- }
- }
- if body == nil {
- body, err = io.ReadAll(c.Request.Body)
- if err != nil {
- return err
- }
- c.Set(BodyBytesKey, body)
- }
+ { … 11 line(s) … ⟦tj:289091dc325bb26f65dc4faa041214f3⟧ }
return bb.BindBody(body, obj)
-}
// ShouldBindBodyWithJSON is a shortcut for c.ShouldBindBodyWith(obj, binding.JSON).
func (c *Context) ShouldBindBodyWithJSON(obj any) error {
@@ -802,39 +682,8 @@
func (c *Context) ClientIP() string {
// Check if we're running on a trusted platform, continue running backwards if error
if c.engine.TrustedPlatform != "" {
- // Developers can define their own header of Trusted Platform or use predefined constants
- if addr := c.requestHeader(c.engine.TrustedPlatform); addr != "" {
- return addr
- }
- }
-
- // Legacy "AppEngine" flag
- if c.engine.AppEngine {
- log.Println(`The AppEngine flag is going to be deprecated. Please check issues #2723 and #2739 and use 'TrustedPlatform: gin.PlatformGoogleAppEngine' instead.`)
- if addr := c.requestHeader("X-Appengine-Remote-Addr"); addr != "" {
- return addr
- }
- }
-
- // It also checks if the remoteIP is a trusted proxy or not.
- // In order to perform this validation, it will see if the IP is contained within at least one of the CIDR blocks
- // defined by Engine.SetTrustedProxies()
- remoteIP := net.ParseIP(c.RemoteIP())
- if remoteIP == nil {
- return ""
- }
- trusted := c.engine.isTrustedProxy(remoteIP)
-
- if trusted && c.engine.ForwardedByClientIP && c.engine.RemoteIPHeaders != nil {
- for _, headerName := range c.engine.RemoteIPHeaders {
- ip, valid := c.engine.validateHeader(c.requestHeader(headerName))
- if valid {
- return ip
- }
- }
- }
+ { … 31 line(s) … ⟦tj:00920d9d61dc55ad09f69fdfc4c15cb9⟧ }
return remoteIP.String()
-}
// RemoteIP parses the IP from Request.RemoteAddr, normalizes and returns the IP (without the port).
func (c *Context) RemoteIP() string {
@@ -870,16 +719,7 @@
// bodyAllowedForStatus is a copy of http.bodyAllowedForStatus non-exported function.
func bodyAllowedForStatus(status int) bool {
- switch {
- case status >= 100 && status <= 199:
- return false
- case status == http.StatusNoContent:
- return false
- case status == http.StatusNotModified:
- return false
- }
- return true
-}
+ { … 10 line(s) … ⟦tj:4690c24a1f7c2314f497818795724948⟧ }
// Status sets the HTTP response code.
func (c *Context) Status(code int) {
@@ -921,18 +761,8 @@
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {
if path == "" {
path = "/"
- }
- http.SetCookie(c.Writer, &http.Cookie{
- Name: name,
- Value: url.QueryEscape(value),
- MaxAge: maxAge,
- Path: path,
- Domain: domain,
- SameSite: c.sameSite,
- Secure: secure,
- HttpOnly: httpOnly,
+ { … 10 line(s) … ⟦tj:65865993495c54bb7283c5ead1f95e66⟧ }
})
-}
// Cookie returns the named cookie provided in the request or
// ErrNoCookie if not found. And return the named cookie is unescaped.
@@ -951,19 +781,9 @@
func (c *Context) Render(code int, r render.Render) {
c.Status(code)
- if !bodyAllowedForStatus(code) {
- r.WriteContentType(c.Writer)
- c.Writer.WriteHeaderNow()
- return
+ { … 10 line(s) … ⟦tj:e59b43446c23f1cd370ed7079acd7510⟧ }
}
- if err := r.Render(c.Writer); err != nil {
- // Pushing error to c.Errors
- _ = c.Error(err)
- c.Abort()
- }
-}
-
// HTML renders the HTTP template specified by its file name.
// It also updates the HTTP code and sets the Content-Type as "text/html".
// See http://golang.org/doc/articles/wiki/
@@ -1116,19 +936,8 @@
func (c *Context) Stream(step func(w io.Writer) bool) bool {
w := c.Writer
clientGone := w.CloseNotify()
- for {
- select {
- case <-clientGone:
- return true
- default:
- keepOpen := step(w)
- w.Flush()
- if !keepOpen {
- return false
- }
- }
+ { … 11 line(s) … ⟦tj:aad66ffd8eff2e4936619394da11d9bf⟧ }
}
-}
/************************************/
/******** CONTENT NEGOTIATION *******/
@@ -1136,74 +945,21 @@
// Negotiate contains all negotiations data.
type Negotiate struct {
- Offered []string
- HTMLName string
- HTMLData any
- JSONData any
- XMLData any
- YAMLData any
- Data any
- TOMLData any
-}
+ { … 9 line(s) … ⟦tj:ecd4ed2f9a0d9b8eed19bf7b4946810d⟧ }
// Negotiate calls different Render according to acceptable Accept format.
func (c *Context) Negotiate(code int, config Negotiate) {
switch c.NegotiateFormat(config.Offered...) {
case binding.MIMEJSON:
- data := chooseData(config.JSONData, config.Data)
- c.JSON(code, data)
-
- case binding.MIMEHTML:
- data := chooseData(config.HTMLData, config.Data)
- c.HTML(code, config.HTMLName, data)
-
- case binding.MIMEXML:
- data := chooseData(config.XMLData, config.Data)
- c.XML(code, data)
-
- case binding.MIMEYAML:
- data := chooseData(config.YAMLData, config.Data)
- c.YAML(code, data)
-
- case binding.MIMETOML:
- data := chooseData(config.TOMLData, config.Data)
- c.TOML(code, data)
-
- default:
- c.AbortWithError(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server")) //nolint: errcheck
+ { … 21 line(s) … ⟦tj:6e41d39627a69477fee326b95311cdac⟧ }
}
-}
// NegotiateFormat returns an acceptable Accept format.
func (c *Context) NegotiateFormat(offered ...string) string {
assert1(len(offered) > 0, "you must provide at least one offer")
- if c.Accepted == nil {
- c.Accepted = parseAccept(c.requestHeader("Accept"))
- }
- if len(c.Accepted) == 0 {
- return offered[0]
- }
- for _, accepted := range c.Accepted {
- for _, offer := range offered {
- // According to RFC 2616 and RFC 2396, non-ASCII characters are not allowed in headers,
- // therefore we can just iterate over the string without casting it into []rune
- i := 0
- for ; i < len(accepted) && i < len(offer); i++ {
- if accepted[i] == '*' || offer[i] == '*' {
- return offer
- }
- if accepted[i] != offer[i] {
- break
- }
- }
- if i == len(accepted) {
- return offer
- }
- }
- }
+ { … 24 line(s) … ⟦tj:889280f9babfb9d383dd346cc2f25f53⟧ }
return ""
-}
// SetAccepted sets Accept header data.
func (c *Context) SetAccepted(formats ...string) {
@@ -1251,17 +1007,8 @@
func (c *Context) Value(key any) any {
if key == ContextRequestKey {
return c.Request
- }
- if key == ContextKey {
- return c
- }
- if keyAsString, ok := key.(string); ok {
- if val, exists := c.Get(keyAsString); exists {
- return val
- }
- }
- if !c.hasRequestContext() {
- return nil
- }
+ { … 12 line(s) … ⟦tj:d0c10cfc51f0bd76a8fc036800da8319⟧ }
return c.Request.Context().Value(key)
-}
+[omitted blocks are individually retrievable: call tinyjuice_retrieve with the token inside an omission marker to expand just that block]
+
+[PARTIAL view — full original (38997 bytes): call tinyjuice_retrieve with token "6b4d7e0478a20e6ab2757ea20026c3c2"]
\ No newline at end of file