Compare commits

..

5 commits

Author SHA1 Message Date
WJQSERVER
e5400c2da7
Update test.yml
Some checks are pending
Go Test / test (push) Waiting to run
2026-03-28 01:34:45 +08:00
WJQSERVER
67a7e21d81
Merge pull request #70 from infinite-iroha/fix-protocols-propagation-4620108489799509698
fix: correctly propagate custom Protocols to http.Server
2026-03-28 01:32:33 +08:00
WJQSERVER
91a330f51b
Merge pull request #69 from infinite-iroha/dependabot/go_modules/github.com/WJQSERVER-STUDIO/httpc-0.9.0
Bump github.com/WJQSERVER-STUDIO/httpc from 0.8.3 to 0.9.0
2026-03-28 01:29:10 +08:00
WJQSERVER
a98fb27058 fix: correctly propagate custom Protocols to http.Server
- Implemented \`applyDefaultServerConfig\` in \`Engine\` to apply \`serverProtocols\` to \`http.Server\`.
- Uncommented all calls to \`applyDefaultServerConfig\` in \`serve.go\`.
- Refactored \`SetProtocols\` and added internal \`setProtocols\` to ensure user-defined protocols are not overwritten by framework defaults in \`RunTLS\`.
- Added exhaustive tests in \`protocols_test.go\` to verify protocol inheritance and persistence.
2026-03-27 17:27:22 +00:00
dependabot[bot]
3be2c05f0c
Bump github.com/WJQSERVER-STUDIO/httpc from 0.8.3 to 0.9.0
Bumps [github.com/WJQSERVER-STUDIO/httpc](https://github.com/WJQSERVER-STUDIO/httpc) from 0.8.3 to 0.9.0.
- [Release notes](https://github.com/WJQSERVER-STUDIO/httpc/releases)
- [Commits](https://github.com/WJQSERVER-STUDIO/httpc/compare/v0.8.3...v0.9.0)

---
updated-dependencies:
- dependency-name: github.com/WJQSERVER-STUDIO/httpc
  dependency-version: 0.9.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-03-23 00:35:07 +00:00
6 changed files with 140 additions and 20 deletions

View file

@ -2,8 +2,6 @@ name: Go Test
on: on:
push: push:
tags:
- '*'
jobs: jobs:
test: test:
@ -13,9 +11,9 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up Go - name: Set up Go
uses: actions/setup-go@v5 uses: actions/setup-go@v6
with: with:
go-version: '1.24' go-version-file: 'go.mod'
- name: Run tests - name: Run tests
run: go test -v ./... run: go test -v ./...

View file

@ -319,11 +319,16 @@ func GetDefaultProtocolsConfig() *ProtocolsConfig {
// 设置默认Protocols // 设置默认Protocols
func (engine *Engine) SetDefaultProtocols() { func (engine *Engine) SetDefaultProtocols() {
engine.useDefaultProtocols = true engine.useDefaultProtocols = true
engine.SetProtocols(GetDefaultProtocolsConfig()) engine.setProtocols(GetDefaultProtocolsConfig())
} }
// 设置Protocol // 设置Protocol
func (engine *Engine) SetProtocols(config *ProtocolsConfig) { func (engine *Engine) SetProtocols(config *ProtocolsConfig) {
engine.setProtocols(config)
engine.useDefaultProtocols = false
}
func (engine *Engine) setProtocols(config *ProtocolsConfig) {
engine.Protocols = *config engine.Protocols = *config
engine.serverProtocols = &http.Protocols{} // 初始化指针 engine.serverProtocols = &http.Protocols{} // 初始化指针
func() { func() {
@ -333,7 +338,13 @@ func (engine *Engine) SetProtocols(config *ProtocolsConfig) {
p.SetUnencryptedHTTP2(config.Http2_Cleartext) p.SetUnencryptedHTTP2(config.Http2_Cleartext)
*engine.serverProtocols = p // 将值赋给指针指向的结构体 *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大小限制 // 配置全局Req Body大小限制

4
go.mod
View file

@ -4,7 +4,7 @@ go 1.26
require ( require (
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2 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/WJQSERVER/wanf v0.0.8
github.com/fenthope/reco v0.0.5 github.com/fenthope/reco v0.0.5
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433
@ -12,5 +12,5 @@ require (
require ( require (
github.com/valyala/bytebufferpool v1.0.0 // indirect 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
View file

@ -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 h1:AiIHXP21LpK7pFfqUlUstgQEWzjbekZgxOuvVwiMfyM=
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2/go.mod h1:mCLqYU32bTmEE6dpj37MKKiZgz70Jh/xyK9vVbq6pok= 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.9.0 h1:MpXcQQqukrSLHH/2tTfnXrhqD6nEDHB/gbzehXaS8o4=
github.com/WJQSERVER-STUDIO/httpc v0.8.3/go.mod h1:/+NKun9LIUW5YFdvpOf7JbChSVsvdySOGn04FB3rTPg= 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 h1:1Ri9d7nKhu22hGxP8O9B9rXnYym6DYGKgi6WRVx3VF8=
github.com/WJQSERVER/wanf v0.0.8/go.mod h1:R0Zw/1skEMVlQ9m5atbkmanlW+9h2bkdq7+wbPY+F/8= 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= 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/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 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 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.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0=
golang.org/x/net v0.50.0/go.mod h1:UgoSli3F/pBgdJBHCTc+tp3gmrU4XswgGRgtnwWTfyM= golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw=

111
protocols_test.go Normal file
View 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")
}
}

View file

@ -211,7 +211,7 @@ func (engine *Engine) Run(addr ...string) error {
srv := &http.Server{Addr: address, Handler: engine} srv := &http.Server{Addr: address, Handler: engine}
// 即使是不支持优雅关闭的 Run,也应用默认和用户配置,以保持行为一致性 // 即使是不支持优雅关闭的 Run,也应用默认和用户配置,以保持行为一致性
//engine.applyDefaultServerConfig(srv) engine.applyDefaultServerConfig(srv)
if engine.ServerConfigurator != nil { if engine.ServerConfigurator != nil {
engine.ServerConfigurator(srv) engine.ServerConfigurator(srv)
} }
@ -231,7 +231,7 @@ func (engine *Engine) RunShutdown(addr string, timeouts ...time.Duration) error
srv.RegisterOnShutdown(engine.shutdownCancel) srv.RegisterOnShutdown(engine.shutdownCancel)
// 应用框架的默认配置和用户提供的自定义配置 // 应用框架的默认配置和用户提供的自定义配置
//engine.applyDefaultServerConfig(srv) engine.applyDefaultServerConfig(srv)
if engine.ServerConfigurator != nil { if engine.ServerConfigurator != nil {
engine.ServerConfigurator(srv) engine.ServerConfigurator(srv)
} }
@ -252,7 +252,7 @@ func (engine *Engine) RunShutdownWithContext(addr string, ctx context.Context, t
srv.RegisterOnShutdown(engine.shutdownCancel) srv.RegisterOnShutdown(engine.shutdownCancel)
// 应用框架的默认配置和用户提供的自定义配置 // 应用框架的默认配置和用户提供的自定义配置
//engine.applyDefaultServerConfig(srv) engine.applyDefaultServerConfig(srv)
if engine.ServerConfigurator != nil { if engine.ServerConfigurator != nil {
engine.ServerConfigurator(srv) engine.ServerConfigurator(srv)
} }
@ -268,7 +268,7 @@ func (engine *Engine) RunTLS(addr string, tlsConfig *tls.Config, timeouts ...tim
// 配置 HTTP/2 支持 (如果使用默认配置) // 配置 HTTP/2 支持 (如果使用默认配置)
if engine.useDefaultProtocols { if engine.useDefaultProtocols {
engine.SetProtocols(&ProtocolsConfig{ engine.setProtocols(&ProtocolsConfig{
Http1: true, Http1: true,
Http2: true, // 默认在 TLS 上启用 HTTP/2 Http2: true, // 默认在 TLS 上启用 HTTP/2
}) })
@ -286,7 +286,7 @@ func (engine *Engine) RunTLS(addr string, tlsConfig *tls.Config, timeouts ...tim
// 应用框架的默认配置和用户提供的自定义配置 // 应用框架的默认配置和用户提供的自定义配置
// 优先使用 TLSServerConfigurator,如果未设置,则回退到通用的 ServerConfigurator // 优先使用 TLSServerConfigurator,如果未设置,则回退到通用的 ServerConfigurator
//engine.applyDefaultServerConfig(srv) engine.applyDefaultServerConfig(srv)
if engine.TLSServerConfigurator != nil { if engine.TLSServerConfigurator != nil {
engine.TLSServerConfigurator(srv) engine.TLSServerConfigurator(srv)
} else if engine.ServerConfigurator != nil { } else if engine.ServerConfigurator != nil {
@ -310,7 +310,7 @@ func (engine *Engine) RunTLSRedir(httpAddr, httpsAddr string, tlsConfig *tls.Con
// --- HTTPS 服务器 --- // --- HTTPS 服务器 ---
if engine.useDefaultProtocols { if engine.useDefaultProtocols {
engine.SetProtocols(&ProtocolsConfig{Http1: true, Http2: true}) engine.setProtocols(&ProtocolsConfig{Http1: true, Http2: true})
} }
httpsSrv := &http.Server{ httpsSrv := &http.Server{
Addr: httpsAddr, Addr: httpsAddr,
@ -321,7 +321,7 @@ func (engine *Engine) RunTLSRedir(httpAddr, httpsAddr string, tlsConfig *tls.Con
}, },
} }
httpsSrv.RegisterOnShutdown(engine.shutdownCancel) httpsSrv.RegisterOnShutdown(engine.shutdownCancel)
//engine.applyDefaultServerConfig(httpsSrv) engine.applyDefaultServerConfig(httpsSrv)
if engine.TLSServerConfigurator != nil { if engine.TLSServerConfigurator != nil {
engine.TLSServerConfigurator(httpsSrv) engine.TLSServerConfigurator(httpsSrv)
} else if engine.ServerConfigurator != nil { } else if engine.ServerConfigurator != nil {
@ -355,7 +355,7 @@ func (engine *Engine) RunTLSRedir(httpAddr, httpsAddr string, tlsConfig *tls.Con
Addr: httpAddr, Addr: httpAddr,
Handler: redirectHandler, Handler: redirectHandler,
} }
//engine.applyDefaultServerConfig(httpSrv) engine.applyDefaultServerConfig(httpSrv)
if engine.ServerConfigurator != nil { if engine.ServerConfigurator != nil {
engine.ServerConfigurator(httpSrv) engine.ServerConfigurator(httpSrv)
} }