Skip to content

Commit

Permalink
Added UpgradeAsClient func
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrr committed Nov 21, 2020
1 parent 2fe12b9 commit dc27611
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func ClientWithHeaders(c net.Conn, url string, req *fasthttp.Request) (*Conn, er
return client(c, url, req)
}

func client(c net.Conn, url string, r *fasthttp.Request) (conn *Conn, err error) {
// UpgradeAsClient will upgrade the connection as a client
//
// r can be nil.
func UpgradeAsClient(c net.Conn, url string, r *fasthttp.Request) error {
req := fasthttp.AcquireRequest()
res := fasthttp.AcquireResponse()
uri := fasthttp.AcquireURI()
Expand Down Expand Up @@ -64,16 +67,24 @@ func client(c net.Conn, url string, r *fasthttp.Request) (conn *Conn, err error)
bw := bufio.NewWriter(c)
req.Write(bw)
bw.Flush()
err = res.Read(br)
err := res.Read(br)
if err == nil {
if res.StatusCode() != 101 ||
!equalsFold(res.Header.PeekBytes(upgradeString), websocketString) {
err = ErrCannotUpgrade
} else {
conn = acquireConn(c)
conn.server = false
}
}

return err
}

func client(c net.Conn, url string, r *fasthttp.Request) (conn *Conn, err error) {
err = UpgradeAsClient(c, url, r)
if err == nil {
conn = acquireConn(c)
conn.server = false
}

return conn, err
}

Expand Down

0 comments on commit dc27611

Please sign in to comment.