mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-06-13 15:47:38 +08:00
Compare commits
5 commits
fcc23745b6
...
e5400c2da7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5400c2da7 | ||
|
|
67a7e21d81 | ||
|
|
91a330f51b | ||
|
|
a98fb27058 | ||
|
|
3be2c05f0c |
6 changed files with 140 additions and 20 deletions
6
.github/workflows/test.yml
vendored
6
.github/workflows/test.yml
vendored
|
|
@ -2,8 +2,6 @@ name: Go Test
|
|||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
|
||||
jobs:
|
||||
test:
|
||||
|
|
@ -13,9 +11,9 @@ jobs:
|
|||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
uses: actions/setup-go@v6
|
||||
with:
|
||||
go-version: '1.24'
|
||||
go-version-file: 'go.mod'
|
||||
|
||||
- name: Run tests
|
||||
run: go test -v ./...
|
||||
|
|
|
|||
15
engine.go
15
engine.go
|
|
@ -319,11 +319,16 @@ func GetDefaultProtocolsConfig() *ProtocolsConfig {
|
|||
// 设置默认Protocols
|
||||
func (engine *Engine) SetDefaultProtocols() {
|
||||
engine.useDefaultProtocols = true
|
||||
engine.SetProtocols(GetDefaultProtocolsConfig())
|
||||
engine.setProtocols(GetDefaultProtocolsConfig())
|
||||
}
|
||||
|
||||
// 设置Protocol
|
||||
func (engine *Engine) SetProtocols(config *ProtocolsConfig) {
|
||||
engine.setProtocols(config)
|
||||
engine.useDefaultProtocols = false
|
||||
}
|
||||
|
||||
func (engine *Engine) setProtocols(config *ProtocolsConfig) {
|
||||
engine.Protocols = *config
|
||||
engine.serverProtocols = &http.Protocols{} // 初始化指针
|
||||
func() {
|
||||
|
|
@ -333,7 +338,13 @@ func (engine *Engine) SetProtocols(config *ProtocolsConfig) {
|
|||
p.SetUnencryptedHTTP2(config.Http2_Cleartext)
|
||||
*engine.serverProtocols = p // 将值赋给指针指向的结构体
|
||||
}()
|
||||
engine.useDefaultProtocols = false
|
||||
}
|
||||
|
||||
// applyDefaultServerConfig 应用框架的默认配置到 http.Server
|
||||
func (engine *Engine) applyDefaultServerConfig(srv *http.Server) {
|
||||
if engine.serverProtocols != nil {
|
||||
srv.Protocols = engine.serverProtocols
|
||||
}
|
||||
}
|
||||
|
||||
// 配置全局Req Body大小限制
|
||||
|
|
|
|||
4
go.mod
4
go.mod
|
|
@ -4,7 +4,7 @@ go 1.26
|
|||
|
||||
require (
|
||||
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2
|
||||
github.com/WJQSERVER-STUDIO/httpc v0.8.3
|
||||
github.com/WJQSERVER-STUDIO/httpc v0.9.0
|
||||
github.com/WJQSERVER/wanf v0.0.8
|
||||
github.com/fenthope/reco v0.0.5
|
||||
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433
|
||||
|
|
@ -12,5 +12,5 @@ require (
|
|||
|
||||
require (
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
golang.org/x/net v0.50.0 // indirect
|
||||
golang.org/x/net v0.52.0 // indirect
|
||||
)
|
||||
|
|
|
|||
8
go.sum
8
go.sum
|
|
@ -1,7 +1,7 @@
|
|||
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2 h1:AiIHXP21LpK7pFfqUlUstgQEWzjbekZgxOuvVwiMfyM=
|
||||
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2/go.mod h1:mCLqYU32bTmEE6dpj37MKKiZgz70Jh/xyK9vVbq6pok=
|
||||
github.com/WJQSERVER-STUDIO/httpc v0.8.3 h1:g3CvOimwPonQuKDfbH8Ex35J/VSz+W1k5Q1FiHg2xn8=
|
||||
github.com/WJQSERVER-STUDIO/httpc v0.8.3/go.mod h1:/+NKun9LIUW5YFdvpOf7JbChSVsvdySOGn04FB3rTPg=
|
||||
github.com/WJQSERVER-STUDIO/httpc v0.9.0 h1:MpXcQQqukrSLHH/2tTfnXrhqD6nEDHB/gbzehXaS8o4=
|
||||
github.com/WJQSERVER-STUDIO/httpc v0.9.0/go.mod h1:filzryrl4eAtFVyl4oVHcJqx1SpNFbrCn+ddQPLlCSg=
|
||||
github.com/WJQSERVER/wanf v0.0.8 h1:1Ri9d7nKhu22hGxP8O9B9rXnYym6DYGKgi6WRVx3VF8=
|
||||
github.com/WJQSERVER/wanf v0.0.8/go.mod h1:R0Zw/1skEMVlQ9m5atbkmanlW+9h2bkdq7+wbPY+F/8=
|
||||
github.com/fenthope/reco v0.0.5 h1:Z/bOunFf4LSgYP/IxG9fe2pTrIq7bPsDflflbNR5Agw=
|
||||
|
|
@ -10,5 +10,5 @@ github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 h1:vymEbVw
|
|||
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
golang.org/x/net v0.50.0 h1:ucWh9eiCGyDR3vtzso0WMQinm2Dnt8cFMuQa9K33J60=
|
||||
golang.org/x/net v0.50.0/go.mod h1:UgoSli3F/pBgdJBHCTc+tp3gmrU4XswgGRgtnwWTfyM=
|
||||
golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0=
|
||||
golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw=
|
||||
|
|
|
|||
111
protocols_test.go
Normal file
111
protocols_test.go
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
package touka
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestApplyDefaultServerConfig(t *testing.T) {
|
||||
engine := New()
|
||||
|
||||
// 1. 测试默认协议
|
||||
srv1 := &http.Server{}
|
||||
engine.applyDefaultServerConfig(srv1)
|
||||
|
||||
if srv1.Protocols == nil {
|
||||
t.Fatal("srv1.Protocols should not be nil after applyDefaultServerConfig")
|
||||
}
|
||||
|
||||
// 默认配置是 Http1: true, Http2: false, Http2_Cleartext: false
|
||||
if !srv1.Protocols.HTTP1() {
|
||||
t.Error("Expected HTTP/1 to be enabled by default")
|
||||
}
|
||||
if srv1.Protocols.HTTP2() {
|
||||
t.Error("Expected HTTP/2 to be disabled by default")
|
||||
}
|
||||
|
||||
// 2. 测试自定义协议
|
||||
engine.SetProtocols(&ProtocolsConfig{
|
||||
Http1: true,
|
||||
Http2: true,
|
||||
Http2_Cleartext: true,
|
||||
})
|
||||
|
||||
srv2 := &http.Server{}
|
||||
engine.applyDefaultServerConfig(srv2)
|
||||
|
||||
if srv2.Protocols == nil {
|
||||
t.Fatal("srv2.Protocols should not be nil after applyDefaultServerConfig")
|
||||
}
|
||||
|
||||
if !srv2.Protocols.HTTP1() {
|
||||
t.Error("Expected HTTP/1 to be enabled after SetProtocols")
|
||||
}
|
||||
if !srv2.Protocols.HTTP2() {
|
||||
t.Error("Expected HTTP/2 to be enabled after SetProtocols")
|
||||
}
|
||||
if !srv2.Protocols.UnencryptedHTTP2() {
|
||||
t.Error("Expected Unencrypted HTTP/2 to be enabled after SetProtocols")
|
||||
}
|
||||
|
||||
// 3. 再次更改协议并验证
|
||||
engine.SetProtocols(&ProtocolsConfig{
|
||||
Http1: false,
|
||||
Http2: true,
|
||||
Http2_Cleartext: false,
|
||||
})
|
||||
|
||||
srv3 := &http.Server{}
|
||||
engine.applyDefaultServerConfig(srv3)
|
||||
|
||||
if srv3.Protocols == nil {
|
||||
t.Fatal("srv3.Protocols should not be nil")
|
||||
}
|
||||
if srv3.Protocols.HTTP1() {
|
||||
t.Error("Expected HTTP/1 to be disabled")
|
||||
}
|
||||
if !srv3.Protocols.HTTP2() {
|
||||
t.Error("Expected HTTP/2 to be enabled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunTLSProtocolInheritance(t *testing.T) {
|
||||
engine := New()
|
||||
|
||||
// 模拟 RunTLS 中的逻辑: 如果使用默认协议, 则启用 HTTP/2
|
||||
if engine.useDefaultProtocols {
|
||||
engine.setProtocols(&ProtocolsConfig{
|
||||
Http1: true,
|
||||
Http2: true,
|
||||
})
|
||||
}
|
||||
|
||||
srv := &http.Server{TLSConfig: &tls.Config{}}
|
||||
engine.applyDefaultServerConfig(srv)
|
||||
|
||||
if !srv.Protocols.HTTP2() {
|
||||
t.Error("RunTLS simulation: Expected HTTP/2 to be enabled for default config")
|
||||
}
|
||||
|
||||
// 模拟用户设置了自定义协议后调用 RunTLS
|
||||
engine = New()
|
||||
engine.SetProtocols(&ProtocolsConfig{
|
||||
Http1: true,
|
||||
Http2: false, // 用户明确不想要 HTTP/2
|
||||
})
|
||||
|
||||
if engine.useDefaultProtocols {
|
||||
engine.setProtocols(&ProtocolsConfig{
|
||||
Http1: true,
|
||||
Http2: true,
|
||||
})
|
||||
}
|
||||
|
||||
srv2 := &http.Server{TLSConfig: &tls.Config{}}
|
||||
engine.applyDefaultServerConfig(srv2)
|
||||
|
||||
if srv2.Protocols.HTTP2() {
|
||||
t.Error("RunTLS simulation: Expected HTTP/2 to be DISABLED if user set custom protocols previously")
|
||||
}
|
||||
}
|
||||
16
serve.go
16
serve.go
|
|
@ -211,7 +211,7 @@ func (engine *Engine) Run(addr ...string) error {
|
|||
srv := &http.Server{Addr: address, Handler: engine}
|
||||
|
||||
// 即使是不支持优雅关闭的 Run,也应用默认和用户配置,以保持行为一致性
|
||||
//engine.applyDefaultServerConfig(srv)
|
||||
engine.applyDefaultServerConfig(srv)
|
||||
if engine.ServerConfigurator != nil {
|
||||
engine.ServerConfigurator(srv)
|
||||
}
|
||||
|
|
@ -231,7 +231,7 @@ func (engine *Engine) RunShutdown(addr string, timeouts ...time.Duration) error
|
|||
srv.RegisterOnShutdown(engine.shutdownCancel)
|
||||
|
||||
// 应用框架的默认配置和用户提供的自定义配置
|
||||
//engine.applyDefaultServerConfig(srv)
|
||||
engine.applyDefaultServerConfig(srv)
|
||||
if engine.ServerConfigurator != nil {
|
||||
engine.ServerConfigurator(srv)
|
||||
}
|
||||
|
|
@ -252,7 +252,7 @@ func (engine *Engine) RunShutdownWithContext(addr string, ctx context.Context, t
|
|||
srv.RegisterOnShutdown(engine.shutdownCancel)
|
||||
|
||||
// 应用框架的默认配置和用户提供的自定义配置
|
||||
//engine.applyDefaultServerConfig(srv)
|
||||
engine.applyDefaultServerConfig(srv)
|
||||
if engine.ServerConfigurator != nil {
|
||||
engine.ServerConfigurator(srv)
|
||||
}
|
||||
|
|
@ -268,7 +268,7 @@ func (engine *Engine) RunTLS(addr string, tlsConfig *tls.Config, timeouts ...tim
|
|||
|
||||
// 配置 HTTP/2 支持 (如果使用默认配置)
|
||||
if engine.useDefaultProtocols {
|
||||
engine.SetProtocols(&ProtocolsConfig{
|
||||
engine.setProtocols(&ProtocolsConfig{
|
||||
Http1: true,
|
||||
Http2: true, // 默认在 TLS 上启用 HTTP/2
|
||||
})
|
||||
|
|
@ -286,7 +286,7 @@ func (engine *Engine) RunTLS(addr string, tlsConfig *tls.Config, timeouts ...tim
|
|||
|
||||
// 应用框架的默认配置和用户提供的自定义配置
|
||||
// 优先使用 TLSServerConfigurator,如果未设置,则回退到通用的 ServerConfigurator
|
||||
//engine.applyDefaultServerConfig(srv)
|
||||
engine.applyDefaultServerConfig(srv)
|
||||
if engine.TLSServerConfigurator != nil {
|
||||
engine.TLSServerConfigurator(srv)
|
||||
} else if engine.ServerConfigurator != nil {
|
||||
|
|
@ -310,7 +310,7 @@ func (engine *Engine) RunTLSRedir(httpAddr, httpsAddr string, tlsConfig *tls.Con
|
|||
|
||||
// --- HTTPS 服务器 ---
|
||||
if engine.useDefaultProtocols {
|
||||
engine.SetProtocols(&ProtocolsConfig{Http1: true, Http2: true})
|
||||
engine.setProtocols(&ProtocolsConfig{Http1: true, Http2: true})
|
||||
}
|
||||
httpsSrv := &http.Server{
|
||||
Addr: httpsAddr,
|
||||
|
|
@ -321,7 +321,7 @@ func (engine *Engine) RunTLSRedir(httpAddr, httpsAddr string, tlsConfig *tls.Con
|
|||
},
|
||||
}
|
||||
httpsSrv.RegisterOnShutdown(engine.shutdownCancel)
|
||||
//engine.applyDefaultServerConfig(httpsSrv)
|
||||
engine.applyDefaultServerConfig(httpsSrv)
|
||||
if engine.TLSServerConfigurator != nil {
|
||||
engine.TLSServerConfigurator(httpsSrv)
|
||||
} else if engine.ServerConfigurator != nil {
|
||||
|
|
@ -355,7 +355,7 @@ func (engine *Engine) RunTLSRedir(httpAddr, httpsAddr string, tlsConfig *tls.Con
|
|||
Addr: httpAddr,
|
||||
Handler: redirectHandler,
|
||||
}
|
||||
//engine.applyDefaultServerConfig(httpSrv)
|
||||
engine.applyDefaultServerConfig(httpSrv)
|
||||
if engine.ServerConfigurator != nil {
|
||||
engine.ServerConfigurator(httpSrv)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue