Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support h2c with prior knowledge #1398

Merged
merged 1 commit into from Feb 5, 2022
Merged

support h2c with prior knowledge #1398

merged 1 commit into from Feb 5, 2022

Conversation

taoso
Copy link
Contributor

@taoso taoso commented Jun 20, 2018

Support both http/1.x and h2c over the same plain tcp port.

It is common to make h2c communication on the server side environment. However, the standard net/http library does not support this feature in box (more detail). It would be a great convenient for gin to support it.

This patch is extracted from William Chang's work.

@taoso taoso force-pushed the h2c branch 3 times, most recently from 7cc9267 to feac8a6 Compare June 20, 2018 02:58
@codecov
Copy link

codecov bot commented Jun 20, 2018

Codecov Report

Merging #1398 into master will decrease coverage by 0.6%.
The diff coverage is 62.5%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #1398      +/-   ##
=========================================
- Coverage   98.21%   97.6%   -0.61%     
=========================================
  Files          35      36       +1     
  Lines        1846    1878      +32     
=========================================
+ Hits         1813    1833      +20     
- Misses         26      32       +6     
- Partials        7      13       +6
Impacted Files Coverage Δ
gin.go 93.96% <60%> (-4.11%) ⬇️
h2c.go 71.42% <71.42%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update caf3e35...3ae0ad4. Read the comment docs.

@steveum0105
Copy link

Do you have any plan to keep going this feature?
I need H2C to support HTTP2 with TLS offloading.

In addition, there is now "golang.org/x/net/http2/h2c" package. I think it is quite simple to complete this with the package.

@Mistic92
Copy link

Any update on this or maybe it's already implemented?

@taoso
Copy link
Contributor Author

taoso commented Mar 16, 2021

hi @thinkerou , it seems some people still need this feature. And I have reimplemented it based on the golang.org/x/net/http2/h2c package. A new UseH2c option has been introduced to minimize the influence to other feature.

@kishaningithub
Copy link

@appleboy @thinkerou You have any concerns? I also resonate with @epii1 on the usefulness of this feature.

@raptoria
Copy link

raptoria commented Sep 8, 2021

+1

@c3l3si4n
Copy link

+1, i need this feature also

1 similar comment
@HowardTangHw
Copy link

+1, i need this feature also

@Cavdy
Copy link

Cavdy commented Nov 5, 2021

+1

@xiekeyi98
Copy link

+1 , any update?

@appleboy appleboy modified the milestones: v1.x, v1.8 Feb 5, 2022
@appleboy
Copy link
Member

appleboy commented Feb 5, 2022

need @thinkerou approval.

Copy link
Member

@thinkerou thinkerou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@thinkerou thinkerou merged commit b40ded1 into gin-gonic:master Feb 5, 2022
@taoso taoso deleted the h2c branch February 5, 2022 13:44
daheige pushed a commit to daheige/gin that referenced this pull request Apr 18, 2022
@halegreen
Copy link

Is gin support h2c ? I don't got this work

@kishaningithub
Copy link

kishaningithub commented Jul 1, 2022

The support for this got released in version 1.8.0. What is the version of gin you are using @ZoeShaw101 ?

Release notes - https://github.com/gin-gonic/gin/releases/tag/v1.8.0

@halegreen
Copy link

The support for this got released in version 1.8.0. What is the version of gin you are using @ZoeShaw101 ?

Release notes - https://github.com/gin-gonic/gin/releases/tag/v1.8.0

I'm using gin v1.8.1

github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.8.1

@vorishirne
Copy link

doesn't supports yet.

curl --http2-prior-knowledge -i --location --request GET 'http://ncx-auth.cradlepoint.com:8000/oauth2/authorize?client_id=nca_linux_client&redirect_uri=https://www.google.com&scope=readall&state=fresh&login_hint=pkv@google_2.com&code_challenge=kp3mXDziinTfUVfUwdZVKHIB4OqQIQOBdBg41aWco30&response_type=code&code_challenge_method=SHA256' -v
Note: Unnecessary use of -X or --request, GET is already inferred.
*   Trying 127.0.0.1:8000...
* Connected to ncx-auth.cradlepoint.com (127.0.0.1) port 8000 (#0)
* Using HTTP2, server supports multiplexing
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x55a9f9d3ae80)
> GET /oauth2/authorize?client_id=nca_linux_client&redirect_uri=https://www.google.com&scope=readall&state=fresh&login_hint=pkv@google_2.com&code_challenge=kp3mXDziinTfUVfUwdZVKHIB4OqQIQOBdBg41aWco30&response_type=code&code_challenge_method=SHA256 HTTP/2
> Host: ncx-auth.cradlepoint.com:8000
> user-agent: curl/7.81.0
> accept: */*
> 
* Empty reply from server
* Closing connection 0
curl: (52) Empty reply from server

@vorishirne
Copy link

Oh needed to add these flags

	router := gin.Default()
	router.UseH2C = true

appleboy added a commit to go-training/proto-def-demo that referenced this pull request Jul 31, 2022
gin-gonic/gin#1398

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
@appleboy
Copy link
Member

Another example using http.Server package.

	r := gin.Default()
	r.POST(path+":name", giteaHandler(handler))
	r.POST(grpcPath+":name", giteaHandler(gHandler))
	r.POST(grpcAlphaPath+":name", giteaHandler(gAlphaHandler))
	r.POST(grpcHealthPath+":name", giteaHandler(gHealthHandler))

	srv := &http.Server{
		Addr: ":8080",
		Handler: h2c.NewHandler(
			r,
			&http2.Server{},
		),
		ReadHeaderTimeout: time.Second,
		ReadTimeout:       5 * time.Minute,
		WriteTimeout:      5 * time.Minute,
		MaxHeaderBytes:    8 * 1024, // 8KiB
	}

	if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatalf("HTTP listen and serve: %v", err)
	}

@ecmap
Copy link

ecmap commented Nov 17, 2022

Can Gin support custom handler in H2C?

	func (engine *Engine) Handler(customHandler ...http.Handler) http.Handler
	gin.UseH2C = true
	httpServer := &http.Server{Handler: gin.Handler(customHandler)}

@kishaningithub
Copy link

@ecmap What is the use case you have in mind?

@ecmap
Copy link

ecmap commented Dec 1, 2022

@ecmap What is the use case you have in mind?

grpc & rest on the same port

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet