Repository: cloudwego/hertz
Pull request #376 (2022-11-15 11:43:53)

Revert "feat: Adapt the logic after moving the http2 logic to hertz-contrib"
Strongly associated with https://github.com/hertz-contrib/http2/pull/1, do it together for the maximum compatibility.

codecov[bot]: # [Codecov](https://codecov.io/gh/cloudwego/hertz/pull/376?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) Report
Base: **67.21**% // Head: **67.20**% // Decreases project coverage by **`-0.00%`** :warning:
> Coverage data is based on head [(`2e42f89`)](https://codecov.io/gh/cloudwego/hertz/pull/376?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) compared to base [(`efde5b6`)](https://codecov.io/gh/cloudwego/hertz/commit/efde5b605c7f41d0c65b66e40b2846254d3b321f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).
> Patch coverage: 72.72% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           develop     #376      +/-   ##
===========================================
- Coverage    67.21%   67.20%   -0.01%     
===========================================
  Files           89       89              
  Lines         8573     8583      +10     
===========================================
+ Hits          5762     5768       +6     
- Misses        2450     2454       +4     
  Partials       361      361              
```


| [Impacted Files](https://codecov.io/gh/cloudwego/hertz/pull/376?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) | Coverage Δ | |
|---|---|---|
| [pkg/common/test/mock/network.go](https://codecov.io/gh/cloudwego/hertz/pull/376/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL2NvbW1vbi90ZXN0L21vY2svbmV0d29yay5nbw==) | `0.00% <0.00%> (ø)` | |
| [pkg/protocol/http1/server.go](https://codecov.io/gh/cloudwego/hertz/pull/376/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL3Byb3RvY29sL2h0dHAxL3NlcnZlci5nbw==) | `0.00% <ø> (ø)` | |
| [pkg/route/engine.go](https://codecov.io/gh/cloudwego/hertz/pull/376/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL3JvdXRlL2VuZ2luZS5nbw==) | `62.88% <88.88%> (+0.44%)` | :arrow_up: |
| [pkg/common/timer/timer.go](https://codecov.io/gh/cloudwego/hertz/pull/376/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL2NvbW1vbi90aW1lci90aW1lci5nbw==) | `61.90% <0.00%> (-19.05%)` | :arrow_down: |
| [pkg/common/compress/compress.go](https://codecov.io/gh/cloudwego/hertz/pull/376/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL2NvbW1vbi9jb21wcmVzcy9jb21wcmVzcy5nbw==) | `83.80% <0.00%> (+2.85%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/cloudwego/hertz/pull/376?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).


Patch:
diff --git a/pkg/app/context.go b/pkg/app/context.go
index 5e15f9b..16797d7 100644
--- a/pkg/app/context.go
+++ b/pkg/app/context.go
@@ -727,6 +727,14 @@ func (ctx *RequestContext) GetInt(key string) (i int) {
 	return
 }
 
+// GetInt32 returns the value associated with the key as an integer. Return int32(0) when type is error.
+func (ctx *RequestContext) GetInt32(key string) (i32 int32) {
+	if val, ok := ctx.Get(key); ok && val != nil {
+		i32, _ = val.(int32)
+	}
+	return
+}
+
 // GetInt64 returns the value associated with the key as an integer. Return int64(0) when type is error.
 func (ctx *RequestContext) GetInt64(key string) (i64 int64) {
 	if val, ok := ctx.Get(key); ok && val != nil {
@@ -735,6 +743,38 @@ func (ctx *RequestContext) GetInt64(key string) (i64 int64) {
 	return
 }
 
+// GetUint returns the value associated with the key as an unsigned integer. Return uint(0) when type is error.
+func (ctx *RequestContext) GetUint(key string) (ui uint) {
+	if val, ok := ctx.Get(key); ok && val != nil {
+		ui, _ = val.(uint)
+	}
+	return
+}
+
+// GetUint32 returns the value associated with the key as an unsigned integer. Return uint32(0) when type is error.
+func (ctx *RequestContext) GetUint32(key string) (ui32 uint32) {
+	if val, ok := ctx.Get(key); ok && val != nil {
+		ui32, _ = val.(uint32)
+	}
+	return
+}
+
+// GetUint64 returns the value associated with the key as an unsigned integer. Return uint64(0) when type is error.
+func (ctx *RequestContext) GetUint64(key string) (ui64 uint64) {
+	if val, ok := ctx.Get(key); ok && val != nil {
+		ui64, _ = val.(uint64)
+	}
+	return
+}
+
+// GetFloat32 returns the value associated with the key as a float32. Return float32(0.0) when type is error.
+func (ctx *RequestContext) GetFloat32(key string) (f32 float32) {
+	if val, ok := ctx.Get(key); ok && val != nil {
+		f32, _ = val.(float32)
+	}
+	return
+}
+
 // GetFloat64 returns the value associated with the key as a float64. Return 0.0 when type is error.
 func (ctx *RequestContext) GetFloat64(key string) (f64 float64) {
 	if val, ok := ctx.Get(key); ok && val != nil {
diff --git a/pkg/route/engine.go b/pkg/route/engine.go
index 0096eb5..94d6d22 100644
--- a/pkg/route/engine.go
+++ b/pkg/route/engine.go
@@ -353,6 +353,10 @@ func (engine *Engine) Run() (err error) {
 }
 
 func (engine *Engine) Init() error {
+	if !h2Enable(engine.options) {
+		engine.protocolSuite.Delete(suite.HTTP2)
+	}
+
 	// add built-in http1 server by default
 	if !engine.HasServer(suite.HTTP1) {
 		engine.AddProtocol(suite.HTTP1, factory.NewServerFactory(newHttp1OptionFromEngine(engine)))
@@ -367,6 +371,7 @@ func (engine *Engine) Init() error {
 
 	if engine.alpnEnable() {
 		engine.options.TLS.NextProtos = append(engine.options.TLS.NextProtos, suite.HTTP1)
+		engine.options.TLS.NextProtos = append(engine.options.TLS.NextProtos, suite.HTTP2)
 	}
 
 	if !atomic.CompareAndSwapUint32(&engine.status, 0, statusInitialized) {
@@ -579,6 +584,10 @@ func initTrace(engine *Engine) stats.Level {
 	return traceLevel
 }
 
+func h2Enable(opt *config.Options) bool {
+	return opt.H2C || (opt.TLS != nil && opt.ALPN)
+}
+
 func debugPrintRoute(httpMethod, absolutePath string, handlers app.HandlersChain) {
 	nuHandlers := len(handlers)
 	handlerName := app.GetHandlerName(handlers.Last())


Test patch:
diff --git a/pkg/app/context_test.go b/pkg/app/context_test.go
index 138852a..2e55eeb 100644
--- a/pkg/app/context_test.go
+++ b/pkg/app/context_test.go
@@ -741,7 +741,8 @@ func TestContextSetGetValues(t *testing.T) {
 	c.Set("string", "this is a string")
 	c.Set("int32", int32(-42))
 	c.Set("int64", int64(42424242424242))
-	c.Set("uint64", uint64(42))
+	c.Set("uint32", uint32(42))
+	c.Set("uint64", uint64(42424242424242))
 	c.Set("float32", float32(4.2))
 	c.Set("float64", 4.2)
 	var a interface{} = 1
@@ -750,7 +751,8 @@ func TestContextSetGetValues(t *testing.T) {
 	assert.DeepEqual(t, c.MustGet("string").(string), "this is a string")
 	assert.DeepEqual(t, c.MustGet("int32").(int32), int32(-42))
 	assert.DeepEqual(t, c.MustGet("int64").(int64), int64(42424242424242))
-	assert.DeepEqual(t, c.MustGet("uint64").(uint64), uint64(42))
+	assert.DeepEqual(t, c.MustGet("uint32").(uint32), uint32(42))
+	assert.DeepEqual(t, c.MustGet("uint64").(uint64), uint64(42424242424242))
 	assert.DeepEqual(t, c.MustGet("float32").(float32), float32(4.2))
 	assert.DeepEqual(t, c.MustGet("float64").(float64), 4.2)
 	assert.DeepEqual(t, c.MustGet("intInterface").(int), 1)
@@ -780,6 +782,14 @@ func TestContextGetInt(t *testing.T) {
 	assert.DeepEqual(t, 0, c.GetInt("string"))
 }
 
+func TestContextGetInt32(t *testing.T) {
+	c := &RequestContext{}
+	c.Set("int32", int32(-42))
+	assert.DeepEqual(t, int32(-42), c.GetInt32("int32"))
+	c.Set("string", "this is a string")
+	assert.DeepEqual(t, int32(0), c.GetInt32("string"))
+}
+
 func TestContextGetInt64(t *testing.T) {
 	c := &RequestContext{}
 	c.Set("int64", int64(42424242424242))
@@ -788,6 +798,38 @@ func TestContextGetInt64(t *testing.T) {
 	assert.DeepEqual(t, int64(0), c.GetInt64("string"))
 }
 
+func TestContextGetUint(t *testing.T) {
+	c := &RequestContext{}
+	c.Set("uint", uint(1))
+	assert.DeepEqual(t, uint(1), c.GetUint("uint"))
+	c.Set("string", "this is a string")
+	assert.DeepEqual(t, uint(0), c.GetUint("string"))
+}
+
+func TestContextGetUint32(t *testing.T) {
+	c := &RequestContext{}
+	c.Set("uint32", uint32(42))
+	assert.DeepEqual(t, uint32(42), c.GetUint32("uint32"))
+	c.Set("string", "this is a string")
+	assert.DeepEqual(t, uint32(0), c.GetUint32("string"))
+}
+
+func TestContextGetUint64(t *testing.T) {
+	c := &RequestContext{}
+	c.Set("uint64", uint64(42424242424242))
+	assert.DeepEqual(t, uint64(42424242424242), c.GetUint64("uint64"))
+	c.Set("string", "this is a string")
+	assert.DeepEqual(t, uint64(0), c.GetUint64("string"))
+}
+
+func TestContextGetFloat32(t *testing.T) {
+	c := &RequestContext{}
+	c.Set("float32", float32(4.2))
+	assert.DeepEqual(t, float32(4.2), c.GetFloat32("float32"))
+	c.Set("string", "this is a string")
+	assert.DeepEqual(t, float32(0.0), c.GetFloat32("string"))
+}
+
 func TestContextGetFloat64(t *testing.T) {
 	c := &RequestContext{}
 	c.Set("float64", 4.2)