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; } } }
//------------------------------------------------------------------------------ void ofxIpVideoServerRouteHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { if(isValidRequest(settings.route,request,response)) { MediaType mediaType("multipart/x-mixed-replace"); mediaType.setParameter("boundary", settings.boundary); string expires = DateTimeFormatter::format(Timestamp(0), // the beginning of time DateTimeFormat::HTTP_FORMAT); response.set("Cache-control","no-cache"); response.setContentType(mediaType); response.set("Expires",expires); response.set("Pragma","no-cache"); std::ostream& ostr = response.send(); // get output stream bool stopStreaming = false; while(ostr.good() && !stopStreaming) { if(!queue.empty()) { ofxIpVideoServerFramePtr frame = queue.pop(); if(frame != NULL) { ofBuffer* buffer = &frame.get()->buffer; ostr << settings.boundary; ostr << "\r\n"; ostr << "Content-Type: image/jpeg"; ostr << "\r\n"; ostr << "Content-Length: " << ofToString(buffer->size()); ostr << "\r\n"; ostr << "\r\n"; ostr << *buffer; } else { ofLogVerbose("ofxIpVideoServerRouteHandler::handleRequest") << "Null buffer."; } } else { ofLogVerbose("ofxIpVideoServerRouteHandler::handleRequest") << "Queue empty."; } Thread::sleep(50); } queue.setActive(false); // a desperate move ofLogNotice("ofxIpVideoServerRouteHandler::handleRequest") << "Client disconneted."; //sendErrorResponse(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) { 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()); } }