equal
deleted
inserted
replaced
21 |
21 |
22 func (e HandshakeError) Error() string { return e.message } |
22 func (e HandshakeError) Error() string { return e.message } |
23 |
23 |
24 // Upgrader specifies parameters for upgrading an HTTP connection to a |
24 // Upgrader specifies parameters for upgrading an HTTP connection to a |
25 // WebSocket connection. |
25 // WebSocket connection. |
|
26 // |
|
27 // It is safe to call Upgrader's methods concurrently. |
26 type Upgrader struct { |
28 type Upgrader struct { |
27 // HandshakeTimeout specifies the duration for the handshake to complete. |
29 // HandshakeTimeout specifies the duration for the handshake to complete. |
28 HandshakeTimeout time.Duration |
30 HandshakeTimeout time.Duration |
29 |
31 |
30 // ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes. If a buffer |
32 // ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes. If a buffer |
113 } |
115 } |
114 |
116 |
115 // Upgrade upgrades the HTTP server connection to the WebSocket protocol. |
117 // Upgrade upgrades the HTTP server connection to the WebSocket protocol. |
116 // |
118 // |
117 // The responseHeader is included in the response to the client's upgrade |
119 // The responseHeader is included in the response to the client's upgrade |
118 // request. Use the responseHeader to specify cookies (Set-Cookie) and the |
120 // request. Use the responseHeader to specify cookies (Set-Cookie). To specify |
119 // application negotiated subprotocol (Sec-WebSocket-Protocol). |
121 // subprotocols supported by the server, set Upgrader.Subprotocols directly. |
120 // |
122 // |
121 // If the upgrade fails, then Upgrade replies to the client with an HTTP error |
123 // If the upgrade fails, then Upgrade replies to the client with an HTTP error |
122 // response. |
124 // response. |
123 func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error) { |
125 func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error) { |
124 const badHandshake = "websocket: the client is not using the websocket protocol: " |
126 const badHandshake = "websocket: the client is not using the websocket protocol: " |
129 |
131 |
130 if !tokenListContainsValue(r.Header, "Upgrade", "websocket") { |
132 if !tokenListContainsValue(r.Header, "Upgrade", "websocket") { |
131 return u.returnError(w, r, http.StatusBadRequest, badHandshake+"'websocket' token not found in 'Upgrade' header") |
133 return u.returnError(w, r, http.StatusBadRequest, badHandshake+"'websocket' token not found in 'Upgrade' header") |
132 } |
134 } |
133 |
135 |
134 if r.Method != "GET" { |
136 if r.Method != http.MethodGet { |
135 return u.returnError(w, r, http.StatusMethodNotAllowed, badHandshake+"request method is not GET") |
137 return u.returnError(w, r, http.StatusMethodNotAllowed, badHandshake+"request method is not GET") |
136 } |
138 } |
137 |
139 |
138 if !tokenListContainsValue(r.Header, "Sec-Websocket-Version", "13") { |
140 if !tokenListContainsValue(r.Header, "Sec-Websocket-Version", "13") { |
139 return u.returnError(w, r, http.StatusBadRequest, "websocket: unsupported version: 13 not found in 'Sec-Websocket-Version' header") |
141 return u.returnError(w, r, http.StatusBadRequest, "websocket: unsupported version: 13 not found in 'Sec-Websocket-Version' header") |