| 164 | |
| 165 | |
| 166 | WebSocketImpl* WebSocket::connect(HTTPClientSession& cs, HTTPRequest& request, HTTPResponse& response, HTTPCredentials& credentials) |
| 167 | { |
| 168 | if (!cs.getProxyHost().empty() && !cs.secure()) |
| 169 | { |
| 170 | cs.proxyTunnel(); |
| 171 | } |
| 172 | std::string key = createKey(); |
| 173 | request.set("Connection", "Upgrade"); |
| 174 | request.set("Upgrade", "websocket"); |
| 175 | request.set("Sec-WebSocket-Version", WEBSOCKET_VERSION); |
| 176 | request.set("Sec-WebSocket-Key", key); |
| 177 | request.setChunkedTransferEncoding(false); |
| 178 | cs.setKeepAlive(true); |
| 179 | cs.sendRequest(request); |
| 180 | std::istream& istr = cs.receiveResponse(response); |
| 181 | if (response.getStatus() == HTTPResponse::HTTP_SWITCHING_PROTOCOLS) |
| 182 | { |
| 183 | return completeHandshake(cs, response, key); |
| 184 | } |
| 185 | else if (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED) |
| 186 | { |
| 187 | if (!credentials.empty()) |
| 188 | { |
| 189 | Poco::NullOutputStream null; |
| 190 | Poco::StreamCopier::copyStream(istr, null); |
| 191 | credentials.authenticate(request, response); |
| 192 | if (!cs.getProxyHost().empty() && !cs.secure()) |
| 193 | { |
| 194 | cs.reset(); |
| 195 | cs.proxyTunnel(); |
| 196 | } |
| 197 | cs.sendRequest(request); |
| 198 | cs.receiveResponse(response); |
| 199 | if (response.getStatus() == HTTPResponse::HTTP_SWITCHING_PROTOCOLS) |
| 200 | { |
| 201 | return completeHandshake(cs, response, key); |
| 202 | } |
| 203 | else if (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED) |
| 204 | { |
| 205 | throw WebSocketException("Not authorized", WS_ERR_UNAUTHORIZED); |
| 206 | } |
| 207 | } |
| 208 | else throw WebSocketException("Not authorized", WS_ERR_UNAUTHORIZED); |
| 209 | } |
| 210 | if (response.getStatus() == HTTPResponse::HTTP_OK) |
| 211 | { |
| 212 | throw WebSocketException("The server does not understand the WebSocket protocol", WS_ERR_NO_HANDSHAKE); |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | throw WebSocketException("Cannot upgrade to WebSocket connection", response.getReason(), WS_ERR_NO_HANDSHAKE); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
| 221 | WebSocketImpl* WebSocket::completeHandshake(HTTPClientSession& cs, HTTPResponse& response, const std::string& key) |
nothing calls this directly
no test coverage detected