Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions transport/internet/httpupgrade/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@ func (s *server) Addr() net.Addr {
return nil
}

func (s *server) Handle(conn net.Conn) (stat.Connection, error) {
func (s *server) Handle(conn net.Conn) {
upgradedConn, err := s.upgrade(conn)
if err != nil {
common.CloseIfExists(conn)
errors.LogInfoInner(context.Background(), err, "failed to handle request")
return
}
s.addConn(upgradedConn)
}

// upgrade execute a fake websocket upgrade process and return the available connection
func (s *server) upgrade(conn net.Conn) (stat.Connection, error) {
connReader := bufio.NewReader(conn)
req, err := http.ReadRequest(connReader)
if err != nil {
Expand All @@ -52,7 +63,6 @@ func (s *server) Handle(conn net.Conn) (stat.Connection, error) {
connection := strings.ToLower(req.Header.Get("Connection"))
upgrade := strings.ToLower(req.Header.Get("Upgrade"))
if connection != "upgrade" || upgrade != "websocket" {
_ = conn.Close()
return nil, errors.New("unrecognized request")
}
resp := &http.Response{
Expand All @@ -67,7 +77,6 @@ func (s *server) Handle(conn net.Conn) (stat.Connection, error) {
resp.Header.Set("Upgrade", "websocket")
err = resp.Write(conn)
if err != nil {
_ = conn.Close()
return nil, err
}

Expand Down Expand Up @@ -99,12 +108,7 @@ func (s *server) keepAccepting() {
if err != nil {
return
}
handledConn, err := s.Handle(conn)
if err != nil {
errors.LogInfoInner(context.Background(), err, "failed to handle request")
continue
}
s.addConn(handledConn)
go s.Handle(conn)
}
}

Expand Down