int SecureSocketImpl::receiveBytes(void* buffer, int length, int flags) { poco_assert (_pSocket->initialized()); poco_check_ptr (_pSSL); int rc; if (_needHandshake) { rc = completeHandshake(); if (rc == 1) verifyPeerCertificate(); else return rc; } do { rc = SSL_read(_pSSL, buffer, length); } while (rc <= 0 && _pSocket->lastError() == POCO_EINTR); if (rc <= 0) { return handleError(rc); } return rc; }
int SecureSocketImpl::sendBytes(const void* buffer, int length, int flags) { poco_assert (_pSocket->initialized()); poco_check_ptr (_pSSL); int rc; if (_needHandshake) { rc = completeHandshake(); if (rc == 1) verifyPeerCertificate(); else if (rc == 0) throw SSLConnectionUnexpectedlyClosedException(); else return rc; } do { rc = SSL_write(_pSSL, buffer, length); } while (rc <= 0 && _pSocket->lastError() == POCO_EINTR); if (rc <= 0) { rc = handleError(rc); if (rc == 0) throw SSLConnectionUnexpectedlyClosedException(); } return rc; }
WebSocketImpl* WebSocket::connect(HTTPClientSession& cs, HTTPRequest& request, HTTPResponse& response, HTTPCredentials& credentials) { if (!cs.getProxyHost().empty() && !cs.secure()) { cs.proxyTunnel(); } std::string key = createKey(); request.set("Connection", "Upgrade"); request.set("Upgrade", "websocket"); request.set("Sec-WebSocket-Version", WEBSOCKET_VERSION); request.set("Sec-WebSocket-Key", key); request.setChunkedTransferEncoding(false); cs.setKeepAlive(true); cs.sendRequest(request); std::istream& istr = cs.receiveResponse(response); if (response.getStatus() == HTTPResponse::HTTP_SWITCHING_PROTOCOLS) { return completeHandshake(cs, response, key); } else if (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED) { Poco::NullOutputStream null; Poco::StreamCopier::copyStream(istr, null); credentials.authenticate(request, response); if (!cs.getProxyHost().empty() && !cs.secure()) { cs.reset(); cs.proxyTunnel(); } cs.sendRequest(request); cs.receiveResponse(response); if (response.getStatus() == HTTPResponse::HTTP_SWITCHING_PROTOCOLS) { return completeHandshake(cs, response, key); } else if (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED) { throw WebSocketException("Not authorized", WS_ERR_UNAUTHORIZED); } } throw WebSocketException("Cannot upgrade to WebSocket connection", response.getReason(), WS_ERR_NO_HANDSHAKE); }
bool WebSocketFramer::checkHandshakeResponse(http::Response& response) { assert(_mode == ws::ClientSide); assert(_headerState == 1); if (response.getStatus() == http::StatusCode::SwitchingProtocols) { // Complete handshake or throw completeHandshake(response); // Success _headerState++; assert(handshakeComplete()); return true; } else if (response.getStatus() == http::StatusCode::Unauthorized) assert(0 && "authentication not implemented"); else throw std::runtime_error("WebSocket error: Cannot upgrade to WebSocket connection: " + response.getReason()); //, ws::ErrorNoHandshake // Need to resend request return false; }