bool HTTPWSTest::isDocumentLoaded(Poco::Net::WebSocket& ws) { bool isLoaded = false; try { int flags; int bytes; int retries = 30; const Poco::Timespan waitTime(1000000); ws.setReceiveTimeout(0); std::cout << "==> isDocumentLoaded\n"; do { char buffer[READ_BUFFER_SIZE]; if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ)) { bytes = ws.receiveFrame(buffer, sizeof(buffer), flags); std::cout << "Got " << bytes << " bytes, flags: " << std::hex << flags << std::dec << '\n'; if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) { std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, bytes) << '\n'; const std::string line = LOOLProtocol::getFirstLine(buffer, bytes); const std::string prefixIndicator = "statusindicatorfinish:"; const std::string prefixStatus = "status:"; if (line.find(prefixIndicator) == 0 || line.find(prefixStatus) == 0) { isLoaded = true; break; } } retries = 10; } else { std::cout << "Timeout\n"; --retries; } } while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); } catch (const Poco::Net::WebSocketException& exc) { std::cout << exc.message(); } return isLoaded; }
void HTTPWSTest::getResponseMessage(Poco::Net::WebSocket& ws, const std::string& prefix, std::string& response, const bool isLine) { try { int flags; int bytes; int retries = 20; const Poco::Timespan waitTime(1000000); response.clear(); ws.setReceiveTimeout(0); std::cout << "==> getResponseMessage(" << prefix << ")\n"; do { char buffer[READ_BUFFER_SIZE]; if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ)) { bytes = ws.receiveFrame(buffer, sizeof(buffer), flags); std::cout << "Got " << bytes << " bytes, flags: " << std::hex << flags << std::dec << '\n'; if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) { std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, bytes) << '\n'; const std::string message = isLine ? LOOLProtocol::getFirstLine(buffer, bytes) : std::string(buffer, bytes); if (message.find(prefix) == 0) { response = message.substr(prefix.length()); break; } } retries = 10; } else { std::cout << "Timeout\n"; --retries; } } while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); } catch (const Poco::Net::WebSocketException& exc) { std::cout << exc.message(); } }
void HTTPWSTest::testCloseAfterClose() { try { int bytes; int flags; char buffer[READ_BUFFER_SIZE]; // Load a document and get its status. const std::string documentPath = Util::getTempFilePath(TDOC, "hello.odt"); const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); Poco::Net::WebSocket socket = *connectLOKit(request, _response); sendTextFrame(socket, "load url=" + documentURL); sendTextFrame(socket, "status"); CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket)); // send normal socket shutdown socket.shutdown(); // 5 seconds timeout socket.setReceiveTimeout(5000000); // receive close frame handshake do { bytes = socket.receiveFrame(buffer, sizeof(buffer), flags); } while ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); // no more messages is received. bytes = socket.receiveFrame(buffer, sizeof(buffer), flags); std::string received(buffer); std::cout << received << "received " << bytes << " flags "<< flags << std::endl; CPPUNIT_ASSERT_EQUAL(0, bytes); CPPUNIT_ASSERT_EQUAL(0, flags); } catch (const Poco::Exception& exc) { CPPUNIT_FAIL(exc.displayText()); } }