-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathroute.go
More file actions
275 lines (252 loc) · 6.72 KB
/
Copy pathroute.go
File metadata and controls
275 lines (252 loc) · 6.72 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
// MIT License
// Copyright (c) 2020 Tree Xie
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package elton
import (
"bufio"
"net"
"net/http"
"strings"
"sync"
)
// normalizeRoutePath converts limited legacy chi-style segments to net/http ServeMux patterns.
//
// - ":name" at a path segment start → "{name}"
// - trailing "/*" → "/{path...}" (catch-all; read with Param("path") or Params.Values[0])
//
// Patterns already using ServeMux syntax ({name}, {name...}, {$}) are left unchanged.
// Regexp constraints like {id:[0-9]+} are not supported and will panic at registration.
func normalizeRoutePath(path string) string {
if path == "" || path == "/" {
// Bare "/" is a prefix catch-all in ServeMux; elton treats it as exact root.
return "/{$}"
}
if base, ok := strings.CutSuffix(path, "/*"); ok {
path = base + "/{path...}"
}
if !strings.Contains(path, ":") {
return path
}
var b strings.Builder
b.Grow(len(path) + 4)
for i := 0; i < len(path); {
// Convert :name at segment start (after '/' or at beginning).
if path[i] == ':' && (i == 0 || path[i-1] == '/') {
j := i + 1
for j < len(path) && isRouteIdentByte(path[j]) {
j++
}
if j > i+1 {
b.WriteByte('{')
b.WriteString(path[i+1 : j])
b.WriteByte('}')
i = j
continue
}
}
b.WriteByte(path[i])
i++
}
return b.String()
}
// extractParamNames returns wildcard names from a ServeMux path pattern, in order.
// {$} and anonymous trailing-slash multis are omitted.
func extractParamNames(path string) []string {
var names []string
for i := 0; i < len(path); i++ {
if path[i] != '{' {
continue
}
end := strings.IndexByte(path[i:], '}')
if end < 0 {
break
}
name := path[i+1 : i+end]
i += end
if name == "" || name == "$" {
continue
}
if n, ok := strings.CutSuffix(name, "..."); ok {
name = n
}
if name != "" {
names = append(names, name)
}
}
return names
}
func isRouteIdentByte(c byte) bool {
return c == '_' ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9')
}
// edgeWriter wraps the real ResponseWriter so ServeMux is invoked once per request.
//
// Elton-registered handlers call markHandled() before writing. Mux-internal handlers
// (404 / 405 / trailing-slash redirect) leave handled=false; their headers/body stay
// buffered until ServeHTTP decides whether to flush or replace with elton's handlers.
type edgeWriter struct {
http.ResponseWriter
hdr http.Header // delayed headers while !handled
status int
handled bool
headerWrote bool
buf []byte
}
var edgeWriterPool = sync.Pool{
New: func() any {
return &edgeWriter{}
},
}
func acquireEdgeWriter(w http.ResponseWriter) *edgeWriter {
ew := edgeWriterPool.Get().(*edgeWriter)
ew.ResponseWriter = w
ew.hdr = nil
ew.status = 0
ew.handled = false
ew.headerWrote = false
ew.buf = ew.buf[:0]
return ew
}
func releaseEdgeWriter(ew *edgeWriter) {
if ew == nil {
return
}
ew.ResponseWriter = nil
ew.hdr = nil
ew.buf = ew.buf[:0]
edgeWriterPool.Put(ew)
}
// detachEdgeFromContext peels *edgeWriter off c.Response so the writer can be
// returned to the pool while Context (tests / DisableReuse) still has a usable Header().
func detachEdgeFromContext(c *Context) {
if c == nil {
return
}
if ew, ok := c.Response.(*edgeWriter); ok {
c.Response = ew.ResponseWriter
}
}
func (ew *edgeWriter) Header() http.Header {
if ew.handled {
return ew.ResponseWriter.Header()
}
if ew.hdr == nil {
ew.hdr = make(http.Header)
}
return ew.hdr
}
func (ew *edgeWriter) markHandled() {
if ew.handled {
return
}
ew.handled = true
if ew.hdr != nil {
dst := ew.ResponseWriter.Header()
for k, vv := range ew.hdr {
dst[k] = vv
}
ew.hdr = nil
}
// Route handlers should not have written before markHandled.
ew.buf = ew.buf[:0]
ew.status = 0
ew.headerWrote = false
}
func (ew *edgeWriter) WriteHeader(statusCode int) {
if ew.headerWrote {
return
}
ew.status = statusCode
if ew.handled {
ew.headerWrote = true
ew.ResponseWriter.WriteHeader(statusCode)
}
}
func (ew *edgeWriter) Write(b []byte) (int, error) {
if ew.handled {
if !ew.headerWrote {
if ew.status == 0 {
ew.status = http.StatusOK
}
ew.headerWrote = true
ew.ResponseWriter.WriteHeader(ew.status)
}
return ew.ResponseWriter.Write(b)
}
if ew.status == 0 {
ew.status = http.StatusOK
}
ew.buf = append(ew.buf, b...)
return len(b), nil
}
// flush writes buffered mux-internal response (e.g. trailing-slash redirect).
func (ew *edgeWriter) flush() {
if ew.hdr != nil {
dst := ew.ResponseWriter.Header()
for k, vv := range ew.hdr {
dst[k] = vv
}
ew.hdr = nil
}
if !ew.headerWrote {
code := ew.status
if code == 0 {
code = http.StatusOK
}
ew.headerWrote = true
ew.ResponseWriter.WriteHeader(code)
}
if len(ew.buf) > 0 {
_, _ = ew.ResponseWriter.Write(ew.buf)
}
}
// Unwrap supports http.ResponseController and middleware that peel wrappers.
func (ew *edgeWriter) Unwrap() http.ResponseWriter {
return ew.ResponseWriter
}
func (ew *edgeWriter) Flush() {
if f, ok := ew.ResponseWriter.(http.Flusher); ok {
if ew.handled && !ew.headerWrote {
ew.WriteHeader(http.StatusOK)
}
f.Flush()
}
}
func (ew *edgeWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hj, ok := ew.ResponseWriter.(http.Hijacker); ok {
ew.markHandled()
return hj.Hijack()
}
return nil, nil, http.ErrNotSupported
}
// markRouteHandled marks the ResponseWriter chain as an elton-registered route.
// Walks Unwrap() so Server/test wrappers do not hide *edgeWriter.
func markRouteHandled(w http.ResponseWriter) {
for w != nil {
if ew, ok := w.(*edgeWriter); ok {
ew.markHandled()
return
}
uw, ok := w.(interface{ Unwrap() http.ResponseWriter })
if !ok {
return
}
w = uw.Unwrap()
}
}