Skip to content

Commit 9faecc2

Browse files
committed
feat(header): 添加对报头的定义
1 parent 2cd4eb0 commit 9faecc2

File tree

11 files changed

+209
-31
lines changed

11 files changed

+209
-31
lines changed

examples/ctx/ctx.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/issue9/mux/v7"
1212
"github.com/issue9/mux/v7/group"
13+
"github.com/issue9/mux/v7/header"
1314
"github.com/issue9/mux/v7/types"
1415
)
1516

@@ -39,13 +40,13 @@ func call(w http.ResponseWriter, r *http.Request, ps types.Route, h Handler) {
3940

4041
func optionsHandlerBuilder(p types.Node) Handler {
4142
return HandlerFunc(func(ctx *CTX) {
42-
ctx.W.Header().Set("allow", p.AllowHeader())
43+
ctx.W.Header().Set(header.Allow, p.AllowHeader())
4344
})
4445
}
4546

4647
func methodNotAllowedBuilder(p types.Node) Handler {
4748
return HandlerFunc(func(ctx *CTX) {
48-
ctx.W.Header().Set("allow", p.AllowHeader())
49+
ctx.W.Header().Set(header.Allow, p.AllowHeader())
4950
ctx.W.WriteHeader(http.StatusMethodNotAllowed)
5051
})
5152
}

examples/std/std.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/issue9/mux/v7"
1313
"github.com/issue9/mux/v7/group"
14+
"github.com/issue9/mux/v7/header"
1415
"github.com/issue9/mux/v7/types"
1516
)
1617

@@ -34,14 +35,14 @@ func call(w http.ResponseWriter, r *http.Request, ps types.Route, h http.Handler
3435

3536
func methodNotAllowedBuilder(p types.Node) http.Handler {
3637
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37-
w.Header().Set("Allow", p.AllowHeader())
38+
w.Header().Set(header.Allow, p.AllowHeader())
3839
w.WriteHeader(http.StatusMethodNotAllowed)
3940
})
4041
}
4142

4243
func optionsHandlerBuilder(p types.Node) http.Handler {
4344
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
44-
w.Header().Set("Allow", p.AllowHeader())
45+
w.Header().Set(header.Allow, p.AllowHeader())
4546
})
4647
}
4748

group/version.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"strings"
1212

13+
"github.com/issue9/mux/v7/header"
1314
"github.com/issue9/mux/v7/types"
1415
)
1516

@@ -83,7 +84,7 @@ func NewHeaderVersion(param, key string, errlog *log.Logger, version ...string)
8384
}
8485

8586
func (v *HeaderVersion) Match(r *http.Request, ctx *types.Context) (ok bool) {
86-
header := r.Header.Get("Accept")
87+
header := r.Header.Get(header.Accept)
8788
if header == "" {
8889
return false
8990
}

header/header.go

+169
Large diffs are not rendered by default.

internal/options/cors.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strconv"
1212
"strings"
1313

14+
"github.com/issue9/mux/v7/header"
1415
"github.com/issue9/mux/v7/types"
1516
)
1617

@@ -77,7 +78,7 @@ func (c *CORS) Handle(node types.Node, wh http.Header, r *http.Request) {
7778
}
7879

7980
// Origin 是可以为空的,所以采用 Access-Control-Request-Method 判断是否为预检。
80-
reqMethod := r.Header.Get("Access-Control-Request-Method")
81+
reqMethod := r.Header.Get(header.AccessControlRequestMethod)
8182
preflight := r.Method == http.MethodOptions &&
8283
reqMethod != "" &&
8384
r.URL.Path != "*" // OPTIONS * 不算预检,也不存在其它的请求方法处理方式。
@@ -87,44 +88,44 @@ func (c *CORS) Handle(node types.Node, wh http.Header, r *http.Request) {
8788
if slices.Index(node.Methods(), reqMethod) < 0 {
8889
return
8990
}
90-
wh.Set("Access-Control-Allow-Methods", node.AllowHeader())
91-
wh.Add("Vary", "Access-Control-Request-Method")
91+
wh.Set(header.AccessControlAllowMethods, node.AllowHeader())
92+
wh.Add(header.Vary, header.AccessControlRequestMethod)
9293

9394
// Access-Control-Allow-Headers
9495
if !c.headerIsAllowed(r) {
9596
return
9697
}
9798
if c.allowHeadersString != "" {
98-
wh.Set("Access-Control-Allow-Headers", c.allowHeadersString)
99-
wh.Add("Vary", "Access-Control-Request-Headers")
99+
wh.Set(header.AccessControlAllowHeaders, c.allowHeadersString)
100+
wh.Add(header.Vary, header.AccessControlAllowHeaders)
100101
}
101102

102103
// Access-Control-Max-Age
103104
if c.maxAgeString != "" {
104-
wh.Set("Access-Control-Max-Age", c.maxAgeString)
105+
wh.Set(header.AccessControlMaxAge, c.maxAgeString)
105106
}
106107
}
107108

108109
// Access-Control-Allow-Origin
109110
allowOrigin := "*"
110111
if !c.anyOrigins {
111-
origin := r.Header.Get("Origin")
112+
origin := r.Header.Get(header.Origin)
112113
if slices.Index(c.Origins, origin) < 0 {
113114
return
114115
}
115116
allowOrigin = origin
116117
}
117-
wh.Set("Access-Control-Allow-Origin", allowOrigin)
118-
wh.Add("Vary", "Origin")
118+
wh.Set(header.AccessControlAllowOrigin, allowOrigin)
119+
wh.Add(header.Vary, header.Origin)
119120

120121
// Access-Control-Allow-Credentials
121122
if c.AllowCredentials {
122-
wh.Set("Access-Control-Allow-Credentials", "true")
123+
wh.Set(header.AccessControlAllowCredentials, "true")
123124
}
124125

125126
// Access-Control-Expose-Headers
126127
if c.exposedHeadersString != "" {
127-
wh.Set("Access-Control-Expose-Headers", c.exposedHeadersString)
128+
wh.Set(header.AccessControlExposeHeaders, c.exposedHeadersString)
128129
}
129130
}
130131

@@ -133,7 +134,7 @@ func (c *CORS) headerIsAllowed(r *http.Request) bool {
133134
return true
134135
}
135136

136-
h := strings.TrimSpace(r.Header.Get("Access-Control-Request-Headers"))
137+
h := strings.TrimSpace(r.Header.Get(header.AccessControlRequestHeaders))
137138
if h == "" {
138139
return true
139140
}

internal/tree/test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/issue9/mux/v7/internal/syntax"
1616
"github.com/issue9/mux/v7/types"
17+
"github.com/issue9/mux/v7/header"
1718
)
1819

1920
// NewTestTree 返回以 http.Handler 作为参数实例化的 Tree
@@ -36,7 +37,7 @@ func BuildTestMiddleware(a *assert.Assertion, text string) types.MiddlewareOf[ht
3637
func BuildTestNodeHandlerFunc(status int) types.BuildNodeHandleOf[http.Handler] {
3738
return func(n types.Node) http.Handler {
3839
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39-
w.Header().Set("Allow", n.AllowHeader())
40+
w.Header().Set(header.Allow, n.AllowHeader())
4041
w.WriteHeader(status)
4142
})
4243
}

internal/tree/tree_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/issue9/assert/v4/rest"
1616
"github.com/issue9/errwrap"
1717

18+
"github.com/issue9/mux/v7/header"
1819
"github.com/issue9/mux/v7/internal/syntax"
1920
"github.com/issue9/mux/v7/types"
2021
)
@@ -133,7 +134,7 @@ func (t *tester) optionsTrue(path, options string) {
133134
h.ServeHTTP(w, r)
134135
t.a.Equal(w.Code, http.StatusOK)
135136

136-
t.a.Equal(w.Header().Get("Allow"), options)
137+
t.a.Equal(w.Header().Get(header.Allow), options)
137138
}
138139

139140
func TestTree_AmbiguousRoute(t *testing.T) {
@@ -557,7 +558,7 @@ func TestTree_match(t *testing.T) {
557558
r := rest.NewRequest(a, http.MethodOptions, "/path1").Request()
558559
node.handlers[http.MethodOptions].ServeHTTP(w, r)
559560
a.Empty(w.Header().Get("h1")).
560-
Equal(w.Header().Get("Allow"), "GET, HEAD, OPTIONS").
561+
Equal(w.Header().Get(header.Allow), "GET, HEAD, OPTIONS").
561562
Empty(w.Body.String())
562563

563564
// path2,不主动调用 WriteHeader
@@ -580,7 +581,7 @@ func TestTree_match(t *testing.T) {
580581
r = rest.NewRequest(a, http.MethodOptions, "/path2").Request()
581582
node.handlers[http.MethodOptions].ServeHTTP(w, r)
582583
a.Empty(w.Header().Get("h1")).
583-
Equal(w.Header().Get("Allow"), "GET, HEAD, OPTIONS").
584+
Equal(w.Header().Get(header.Allow), "GET, HEAD, OPTIONS").
584585
Empty(w.Body.String())
585586
}
586587

@@ -634,7 +635,7 @@ func TestTree_ApplyMiddleware(t *testing.T) {
634635
r = rest.NewRequest(a, http.MethodOptions, "/m/path").Request()
635636
f.ServeHTTP(w, r)
636637
a.Equal(w.Body.String(), "m1m2"). // m1m2 由中间件产生
637-
Equal(w.Header().Get("Allow"), "GET, HEAD, OPTIONS")
638+
Equal(w.Header().Get(header.Allow), "GET, HEAD, OPTIONS")
638639

639640
// DELETE /m/path method not allowed
640641
_, f, exists = tree.Handler(newCtx("/m/path"), http.MethodDelete)

mux.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/issue9/errwrap"
3030

31+
"github.com/issue9/mux/v7/header"
3132
"github.com/issue9/mux/v7/internal/syntax"
3233
"github.com/issue9/mux/v7/internal/tree"
3334
)
@@ -76,7 +77,7 @@ func Trace(w http.ResponseWriter, r *http.Request, body bool) error {
7677
text, err := httputil.DumpRequest(r, body)
7778
if err == nil {
7879
w.WriteHeader(http.StatusOK)
79-
w.Header().Set("Content-Type", traceContentType)
80+
w.Header().Set(header.ContentType, traceContentType)
8081
_, err = w.Write([]byte(html.EscapeString(string(text))))
8182
}
8283

router.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/issue9/mux/v7/internal/options"
1414
"github.com/issue9/mux/v7/internal/tree"
1515
"github.com/issue9/mux/v7/types"
16+
"github.com/issue9/mux/v7/header"
1617
)
1718

1819
type (
@@ -345,6 +346,6 @@ func (resp *headResponse) Write(bs []byte) (int, error) {
345346
l := len(bs)
346347
resp.size += l
347348

348-
resp.Header().Set("Content-Length", strconv.Itoa(resp.size))
349+
resp.Header().Set(header.ContentLength, strconv.Itoa(resp.size))
349350
return l, nil
350351
}

router_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/issue9/mux/v7"
1717
"github.com/issue9/mux/v7/examples/std"
18+
"github.com/issue9/mux/v7/header"
1819
"github.com/issue9/mux/v7/internal/tree"
1920
)
2021

@@ -31,14 +32,14 @@ func TestRouterOf(t *testing.T) {
3132
rest.NewRequest(a, http.MethodHead, "/").Do(r).Status(201).BodyEmpty()
3233
rest.Get(a, "/abc").Do(r).Status(http.StatusNotFound)
3334
rest.NewRequest(a, http.MethodHead, "/200").Do(r).Status(200).BodyEmpty() // 不调用 WriteHeader
34-
rest.NewRequest(a, http.MethodOptions, "*").Do(r).Status(200).Header("Allow", "GET, OPTIONS")
35+
rest.NewRequest(a, http.MethodOptions, "*").Do(r).Status(200).Header(header.Allow, "GET, OPTIONS")
3536

3637
r.Get("/h/1", rest.BuildHandler(a, 201, "", nil))
3738
rest.Get(a, "/h/1").Do(r).Status(201)
3839

3940
r.Post("/h/1", rest.BuildHandler(a, 202, "", nil))
4041
rest.Post(a, "/h/1", nil).Do(r).Status(202)
41-
rest.NewRequest(a, http.MethodOptions, "*").Do(r).Status(200).Header("Allow", "GET, OPTIONS, POST")
42+
rest.NewRequest(a, http.MethodOptions, "*").Do(r).Status(200).Header(header.Allow, "GET, OPTIONS, POST")
4243

4344
r.Put("/h/1", rest.BuildHandler(a, 203, "", nil))
4445
rest.Put(a, "/h/1", nil).Do(r).Status(203)
@@ -48,7 +49,7 @@ func TestRouterOf(t *testing.T) {
4849

4950
r.Delete("/h/1", rest.BuildHandler(a, 205, "", nil))
5051
rest.Delete(a, "/h/1").Do(r).Status(205)
51-
rest.NewRequest(a, http.MethodOptions, "*").Do(r).Status(200).Header("Allow", "DELETE, GET, OPTIONS, PATCH, POST, PUT")
52+
rest.NewRequest(a, http.MethodOptions, "*").Do(r).Status(200).Header(header.Allow, "DELETE, GET, OPTIONS, PATCH, POST, PUT")
5253

5354
// Any
5455
r.Any("/h/any", rest.BuildHandler(a, 206, "", nil))
@@ -363,8 +364,8 @@ func TestPrefixOf(t *testing.T) {
363364
return s == http.MethodGet || s == http.MethodPut || s == http.MethodHead // 删除了 GET,HEAD 也会删除。
364365
})
365366
slices.Sort(methods)
366-
rest.Get(a, "/p/h/any").Do(r).Status(405).Header("Allow", strings.Join(methods, ", ")) // 已经删除
367-
rest.Delete(a, "/p/h/any").Do(r).Status(206) // 未删除
367+
rest.Get(a, "/p/h/any").Do(r).Status(405).Header(header.Allow, strings.Join(methods, ", ")) // 已经删除
368+
rest.Delete(a, "/p/h/any").Do(r).Status(206) // 未删除
368369

369370
// clean
370371
p.Clean()

routertest/apis.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func init() {
3131
p = p[end+1:]
3232
}
3333

34-
path := strings.Replace(api.pattern, "}", "", -1)
35-
api.test = strings.Replace(path, "{", "", -1)
34+
path := strings.ReplaceAll(api.pattern, "}", "")
35+
api.test = strings.ReplaceAll(path, "{", "")
3636
api.ps = ps
3737
}
3838
}

0 commit comments

Comments
 (0)