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

http: response.Write on hijacked connection from github.com/gin-gonic/gin.(*responseWriter).Write (response_writer.go:83) #3921

Open
jimmylauu opened this issue Apr 11, 2024 · 1 comment

Comments

@jimmylauu
Copy link

  • With issues:
    • Use the search tool before opening a new issue.
    • Please provide source code and commit sha if you found a bug.
    • Review existing issues and provide feedback or react to them.

Description

I used gorilla/websocket combined with the gin framework, and an error occurred when connecting to websocket, indicating that the connection was hijacked.

How to reproduce

router.GET("/wss/push", pushMessage)
router.Run(":8080")
func pushMessage(c *gin.Context) {
	ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		c.JSON(http.StatusInternalServerError, BaseResponse{Code: CODE_INTERNAL_ERROR, Message: ""})
		log.Fatalf("upgrade http to websocket fail:%s", err)
		return
	}
	defer func(ws *websocket.Conn) {
		err := ws.Close()
		if err != nil {
			c.JSON(http.StatusInternalServerError, BaseResponse{Code: CODE_INTERNAL_ERROR, Message: ""})
			log.Fatalf("close websocket fail:%s", err)
		}
	}(ws)
	go func() {
		for {
			select {
			case msg, ok := <-receiveMsg:
				if ok {
					jsonData, err := json.Marshal(msg)
					if err != nil {
						c.JSON(http.StatusInternalServerError, BaseResponse{Code: CODE_INTERNAL_ERROR, Message: ""})
						log.Printf("recevie message convert to json fail:%s", err)
					}
					err = ws.WriteMessage(websocket.TextMessage, jsonData)
					if err != nil {
						c.JSON(http.StatusInternalServerError, BaseResponse{Code: CODE_INTERNAL_ERROR, Message: ""})
						log.Printf("websocket write message fail:%s", err)
					}
				}
			}
		}
	}()
	c.JSON(http.StatusOK, BaseResponse{Code: CODE_SUCCESS, Message: "success"})
}

Expectations

Connect to websocket normally

Actual result

[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 200 with 500
2024/04/11 19:07:59 http: response.Write on hijacked connection from github.com/gin-gonic/gin.(*responseWriter).Write (response_writer.go:83)
[GIN] 2024/04/11 - 19:07:59 | 200 |     17.8844ms |       127.0.0.1 | GET      "/wss/push"
Error #01: http: connection has been hijacked
Error #02: http: connection has been hijacked

Environment

  • go version: 1.22.2
  • gin version (or commit ref):1.9.1
  • operating system:windows 11
@RedCrazyGhost
Copy link
Contributor

The code you provided cannot be reproduced, please provide the code again

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

No branches or pull requests

2 participants