void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { Application& app = Application::instance(); try { std::string proto = request.get("Sec-WebSocket-Protocol", ""); Poco::SharedPtr<Poco::Net::WebSocket> pWebSocket; if (proto == "com.appinf.webtunnel.server/1.0") { response.set("Sec-WebSocket-Protocol", proto); pWebSocket = new Poco::Net::WebSocket(request, response); _portReflector.addServerSocket(pWebSocket, "ac9667bb-6032-4267-af61-9a7aafd40479"); } else if (proto == "com.appinf.webtunnel.client/1.0") { response.set("Sec-WebSocket-Protocol", proto); std::string portStr = request.get("X-WebTunnel-RemotePort", ""); unsigned port; if (!portStr.empty() && Poco::NumberParser::tryParseUnsigned(portStr, port) && port > 0 && port < 65536) { pWebSocket = new Poco::Net::WebSocket(request, response); try { _portReflector.addClientSocket(pWebSocket, "ac9667bb-6032-4267-af61-9a7aafd40479", static_cast<Poco::UInt16>(port)); } catch (Poco::NotFoundException&) { pWebSocket->shutdown(Poco::Net::WebSocket::WS_UNEXPECTED_CONDITION, "No connection to target available"); } } else { pWebSocket = new Poco::Net::WebSocket(request, response); pWebSocket->shutdown(Poco::Net::WebSocket::WS_UNEXPECTED_CONDITION, "Missing or invalid X-WebTunnel-RemotePort header"); } } else { pWebSocket = new Poco::Net::WebSocket(request, response); pWebSocket->shutdown(Poco::Net::WebSocket::WS_PROTOCOL_ERROR); } } catch (WebSocketException& exc) { app.logger().log(exc); switch (exc.code()) { case Poco::Net::WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION: response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION); // fallthrough case Poco::Net::WebSocket::WS_ERR_NO_HANDSHAKE: case Poco::Net::WebSocket::WS_ERR_HANDSHAKE_NO_VERSION: case Poco::Net::WebSocket::WS_ERR_HANDSHAKE_NO_KEY: response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST); response.setContentLength(0); response.send(); break; } } }
//------------------------------------------------------------------------------ bool ofxWebServerBaseRouteHandler::isValidRequest(const Settings& settings, HTTPServerRequest& request, HTTPServerResponse& response) { string sessionId = ""; // extract cookie from request NameValueCollection cookies; request.getCookies(cookies); NameValueCollection::ConstIterator it = cookies.find(settings.sessionCookieName); if (it != cookies.end()) { sessionId = it->second; } else { sessionId = ofxWebServerSessionManager::generateSessionKey(request); HTTPCookie cookie(settings.sessionCookieName,sessionId); cookie.setPath("/"); // set no age, so it expires @ end of session response.addCookie(cookie); } // TODO: update session manager URI uri(request.getURI()); const string path = uri.getPath(); // just get the path if(settings.requireAuthentication) { if(request.hasCredentials()) { HTTPBasicCredentials credentials(request); const string& user = credentials.getUsername(); const string& pwd = credentials.getPassword(); if(settings.username == credentials.getUsername() && settings.password == credentials.getPassword()) { // add an authentication cookie? return true; } else { response.setStatusAndReason(HTTPResponse::HTTP_UNAUTHORIZED); sendErrorResponse(response); return false; } } else { response.requireAuthentication(settings.realm); response.setContentLength(0); response.send(); return false; } } else { return true; } }
//------------------------------------------------------------------------------ void ofxWebServerUploadRouteHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { if(isValidRequest(settings.route, request, response)) { HTMLForm form(request, request.stream(), *this); if(!settings.uploadRedirect.empty()) { response.redirect(settings.uploadRedirect); } else { response.setStatusAndReason(HTTPResponse::HTTP_OK); response.setContentLength(0); response.send(); } } else { return; // isValidRequest took care of the response } }
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { Application& app = Application::instance(); try { WebSocket ws(request, response); XDUTxtSetText(ID_CURRENT_STATUS, Convert::toQXStringRef(L"Выполнено подключение к плагину. Чтобы начать интерпретацию - кликните по соотвествующей кнопке плагина Вашего браузера.")); char buffer[MAX_BUFFER_SIZE]; int flags=0; int n=1; do { ZeroMemory(buffer, MAX_BUFFER_SIZE); n = ws.receiveFrame(buffer, sizeof(buffer), flags); wstring message = Convert::toUnicodeString(buffer); if (message == L"plugin-version") { //Следующее сообщение передаст данные о плагине n = ws.receiveBytes(buffer, sizeof(buffer), flags); XDUTxtSetText(ID_BROWSER_PLUGIN_VERSION, Convert::toQXStringRef(Convert::toUnicodeString(buffer))); } if (message == L"exportSettings") { n = ws.receiveBytes(buffer, sizeof(buffer), flags); ImportSettings::globalSettings=Convert::jsonToSettings(Convert::toUnicodeString(buffer)); } if (message == L"start-export") { WebCore::getInstance().setSocket(&ws); WebCore::startExport = true; while (WebCore::startExport == true); } } while (n > 0); } catch (WebSocketException& exc) { switch (exc.code()) { case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION: response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION); // fallthrough case WebSocket::WS_ERR_NO_HANDSHAKE: case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION: case WebSocket::WS_ERR_HANDSHAKE_NO_KEY: response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST); response.setContentLength(0); response.send(); break; } } }
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { Application& app = Application::instance(); try { WebSocket ws(request, response); app.logger().information("WebSocket connection established."); char buffer[BUFF_LEN]; int flags; int n; do { int recvLen = 0; while (recvLen < BUFF_LEN) { n = ws.receiveFrame(buffer, BUFF_LEN - recvLen, flags); recvLen += n; } app.logger().information(Poco::format("Frame received (length=%d, flags=0x%x).", n, unsigned(flags))); String resp ("aaaaaaaaaa"); ws.sendFrame(resp.getCStr(), resp.getLength(), flags); } while (n > 0 || (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE); app.logger().information("WebSocket connection closed."); } catch (WebSocketException& exc) { app.logger().log(exc); switch (exc.code()) { case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION: response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION); // fallthrough case WebSocket::WS_ERR_NO_HANDSHAKE: case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION: case WebSocket::WS_ERR_HANDSHAKE_NO_KEY: response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST); response.setContentLength(0); response.send(); break; } } }
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) /// Creates WebSocket and accepts the connection request from web client. { try { if (!_pWS) { _pWS = new WebSocket(request, response); Timespan ts(600, 0); _pWS->setReceiveTimeout(ts); _pWS->setSendTimeout(ts); } std::cout << std::endl << "WebSocket connection established." << std::endl << PROMPT; char buffer[1024]; int n, count = 0; do { n = _pWS->receiveFrame(buffer, sizeof(buffer), _flags); } while (n > 0 || (_flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE); std::cout << "WebSocket connection closed." << std::endl; } catch (WebSocketException& exc) { std::cout << exc.displayText() << std::endl; switch (exc.code()) { case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION: response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION); // fallthrough case WebSocket::WS_ERR_NO_HANDSHAKE: case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION: case WebSocket::WS_ERR_HANDSHAKE_NO_KEY: response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST); response.setContentLength(0); response.send(); break; } } }
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { response.setChunkedTransferEncoding(true); //response.setContentType("text/html"); Path path(_file); if (File(path).exists()) { std::string mime = "application/binary"; std::string ext = path.getExtension(); if (ext == "html" || ext == "htm" || ext == "js" || ext == "css" || ext == "xml") mime = "text/" + ext; response.sendFile(_file, mime); } else { response.setStatusAndReason(HTTPResponse::HTTP_NOT_FOUND); response.send(); } }
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { Application& app = Application::instance(); try { WebSocket ws(request, response); printf("getReceiveTimeout %d\n", ws.getReceiveTimeout().totalSeconds()); app.logger().information("WebSocket connection established."); char buffer[1024]; int flags; int n; do { n = ws.receiveFrame(buffer, sizeof(buffer), flags); app.logger().information(Poco::format("Frame received (length=%d, flags=0x%x).", n, unsigned(flags))); ws.sendFrame(buffer, n, flags); } while (n > 0 || (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE); app.logger().information("WebSocket connection closed."); } catch (WebSocketException& exc) { app.logger().log(exc); switch (exc.code()) { case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION: response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION); // fallthrough case WebSocket::WS_ERR_NO_HANDSHAKE: case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION: case WebSocket::WS_ERR_HANDSHAKE_NO_KEY: response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST); response.setContentLength(0); response.send(); break; } } app.logger().information("WebSocket connection terminated."); }
void client_request_handler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { try { LOG("Request for client connection"); // any exceptions thrown on WebSocket handshake or client validation // will lead to not registering client ws_t ws(new WebSocket(request, response)); client_t h(new client(ws)); h->init(); CLIENTS.add(h); } catch (const WebSocketException& exc) { LOGERROR(exc.displayText()); switch (exc.code()) { case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION: response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION); // fallthrough case WebSocket::WS_ERR_NO_HANDSHAKE: case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION: case WebSocket::WS_ERR_HANDSHAKE_NO_KEY: response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST); response.setContentLength(0); response.send(); break; } } catch (const Exception& e) { LOGERROR(e.displayText()); } catch (const exception& e) { LOGERROR(e.what()); } }
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { response.setStatusAndReason(HTTPResponse::HTTP_NOT_FOUND); response.send(); }