bool MainThreadWebSocketChannel::processOneItemFromBuffer() { ASSERT(!m_suspended); ASSERT(m_client); ASSERT(!m_buffer.isEmpty()); WTF_LOG(Network, "MainThreadWebSocketChannel %p processBuffer() Receive buffer has %lu bytes", this, static_cast<unsigned long>(m_buffer.size())); if (m_shouldDiscardReceivedData) return false; if (m_receivedClosingHandshake) { skipBuffer(m_buffer.size()); return false; } if (m_handshake->mode() == WebSocketHandshake::Incomplete) { int headerLength = m_handshake->readServerHandshake(m_buffer.data(), m_buffer.size()); if (headerLength <= 0) return false; if (m_handshake->mode() == WebSocketHandshake::Connected) { if (m_identifier) { TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "WebSocketReceiveHandshakeResponse", "data", InspectorWebSocketEvent::data(m_document, m_identifier)); // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing. InspectorInstrumentation::didReceiveWebSocketHandshakeResponse(m_document, m_identifier, 0, &m_handshake->serverHandshakeResponse()); } if (m_deflateFramer.enabled() && m_document) { const String message = "WebSocket extension \"x-webkit-deflate-frame\" is deprecated"; m_document->addConsoleMessage(ConsoleMessage::create(JSMessageSource, WarningMessageLevel, message, m_sourceURLAtConstruction, m_lineNumberAtConstruction)); } WTF_LOG(Network, "MainThreadWebSocketChannel %p Connected", this); skipBuffer(headerLength); String subprotocol = m_handshake->serverWebSocketProtocol(); String extensions = m_handshake->acceptedExtensions(); m_client->didConnect(subprotocol.isNull() ? "" : subprotocol, extensions.isNull() ? "" : extensions); WTF_LOG(Network, "MainThreadWebSocketChannel %p %lu bytes remaining in m_buffer", this, static_cast<unsigned long>(m_buffer.size())); return !m_buffer.isEmpty(); } ASSERT(m_handshake->mode() == WebSocketHandshake::Failed); WTF_LOG(Network, "MainThreadWebSocketChannel %p Connection failed", this); skipBuffer(headerLength); m_shouldDiscardReceivedData = true; failAsError(m_handshake->failureReason()); return false; } if (m_handshake->mode() != WebSocketHandshake::Connected) return false; return processFrame(); }
bool WebSocketChannel::processBuffer() { ASSERT(!m_suspended); ASSERT(m_client); ASSERT(!m_buffer.isEmpty()); LOG(Network, "WebSocketChannel %p processBuffer() Receive buffer has %u bytes", this, static_cast<unsigned>(m_buffer.size())); if (m_shouldDiscardReceivedData) return false; if (m_receivedClosingHandshake) { skipBuffer(m_buffer.size()); return false; } Ref<WebSocketChannel> protectedThis(*this); // The client can close the channel, potentially removing the last reference. if (m_handshake->mode() == WebSocketHandshake::Incomplete) { int headerLength = m_handshake->readServerHandshake(m_buffer.data(), m_buffer.size()); if (headerLength <= 0) return false; if (m_handshake->mode() == WebSocketHandshake::Connected) { if (m_identifier) InspectorInstrumentation::didReceiveWebSocketHandshakeResponse(m_document, m_identifier, m_handshake->serverHandshakeResponse()); if (!m_handshake->serverSetCookie().isEmpty()) { if (m_document && cookiesEnabled(*m_document)) { // Exception (for sandboxed documents) ignored. m_document->setCookie(m_handshake->serverSetCookie()); } } // FIXME: handle set-cookie2. LOG(Network, "WebSocketChannel %p Connected", this); skipBuffer(headerLength); m_client->didConnect(); LOG(Network, "WebSocketChannel %p %u bytes remaining in m_buffer", this, static_cast<unsigned>(m_buffer.size())); return !m_buffer.isEmpty(); } ASSERT(m_handshake->mode() == WebSocketHandshake::Failed); LOG(Network, "WebSocketChannel %p Connection failed", this); skipBuffer(headerLength); m_shouldDiscardReceivedData = true; fail(m_handshake->failureReason()); return false; } if (m_handshake->mode() != WebSocketHandshake::Connected) return false; return processFrame(); }
bool WebSocketChannel::processBuffer() { ASSERT(!m_suspended); ASSERT(m_client); ASSERT(m_buffer); if (m_shouldDiscardReceivedData) return false; /// M: HTML5 web socket @{ RefPtr<WebSocketChannel> protect(this); // The client can close the channel, potentially removing the last reference. if (m_handshake->mode() == WebSocketHandshake::Incomplete) { int headerLength = m_handshake->readServerHandshake(m_buffer, m_bufferSize); if (headerLength <= 0) return false; if (m_handshake->mode() == WebSocketHandshake::Connected) { if (m_identifier) InspectorInstrumentation::didReceiveWebSocketHandshakeResponse(m_context, m_identifier, m_handshake->serverHandshakeResponse()); if (!m_handshake->serverSetCookie().isEmpty()) { if (m_context->isDocument()) { Document* document = static_cast<Document*>(m_context); if (cookiesEnabled(document)) { ExceptionCode ec; // Exception (for sandboxed documents) ignored. document->setCookie(m_handshake->serverSetCookie(), ec); } } } // FIXME: handle set-cookie2. LOG(Network, "WebSocketChannel %p connected", this); skipBuffer(headerLength); m_client->didConnect(); LOG(Network, "remaining in read buf %lu", static_cast<unsigned long>(m_bufferSize)); return m_buffer; } LOG(Network, "WebSocketChannel %p connection failed", this); skipBuffer(headerLength); m_shouldDiscardReceivedData = true; if (!m_closed) m_handle->close(); return false; } if (m_handshake->mode() != WebSocketHandshake::Connected) return false; /// @} /// M: HTML5 web socket return processFrame(); }
void WebSocketChannel::fail(const String& reason) { LOG(Network, "WebSocketChannel %p fail() reason='%s'", this, reason.utf8().data()); ASSERT(!m_suspended); if (m_document) { InspectorInstrumentation::didReceiveWebSocketFrameError(m_document, m_identifier, reason); String consoleMessage; if (m_handshake) consoleMessage = makeString("WebSocket connection to '", m_handshake->url().stringCenterEllipsizedToLength(), "' failed: ", reason); else consoleMessage = makeString("WebSocket connection failed: ", reason); m_document->addConsoleMessage(MessageSource::Network, MessageLevel::Error, consoleMessage); } // Hybi-10 specification explicitly states we must not continue to handle incoming data // once the WebSocket connection is failed (section 7.1.7). Ref<WebSocketChannel> protectedThis(*this); // The client can close the channel, potentially removing the last reference. m_shouldDiscardReceivedData = true; if (!m_buffer.isEmpty()) skipBuffer(m_buffer.size()); // Save memory. m_deflateFramer.didFail(); m_hasContinuousFrame = false; m_continuousFrameData.clear(); m_client->didReceiveMessageError(); if (m_handle && !m_closed) m_handle->disconnect(); // Will call didClose(). // We should be closed by now, but if we never got a handshake then we never even opened. ASSERT(m_closed || !m_handshake); }
void MainThreadWebSocketChannel::fail(const String& reason, MessageLevel level, const String& sourceURL, unsigned lineNumber) { WTF_LOG(Network, "MainThreadWebSocketChannel %p fail() reason='%s'", this, reason.utf8().data()); if (m_document) { InspectorInstrumentation::didReceiveWebSocketFrameError(m_document, m_identifier, reason); const String message = "WebSocket connection to '" + m_handshake->url().elidedString() + "' failed: " + reason; m_document->addConsoleMessage(ConsoleMessage::create(JSMessageSource, level, message, sourceURL, lineNumber)); } // Hybi-10 specification explicitly states we must not continue to handle incoming data // once the WebSocket connection is failed (section 7.1.7). m_shouldDiscardReceivedData = true; if (!m_buffer.isEmpty()) skipBuffer(m_buffer.size()); // Save memory. m_deflateFramer.didFail(); m_perMessageDeflate.didFail(); m_hasContinuousFrame = false; m_continuousFrameData.clear(); callDidReceiveMessageError(); if (m_state != ChannelClosed) disconnectHandle(); // Will call didCloseSocketStream(). }
bool WebSocketChannel::processBuffer() { ASSERT(!m_suspended); ASSERT(m_client); ASSERT(m_buffer); if (m_shouldDiscardReceivedData) return false; if (m_handshake.mode() == WebSocketHandshake::Incomplete) { int headerLength = m_handshake.readServerHandshake(m_buffer, m_bufferSize); if (headerLength <= 0) return false; if (m_handshake.mode() == WebSocketHandshake::Connected) { if (m_identifier) InspectorInstrumentation::didReceiveWebSocketHandshakeResponse(m_context, m_identifier, m_handshake.serverHandshakeResponse()); if (!m_handshake.serverSetCookie().isEmpty()) { if (m_context->isDocument()) { Document* document = static_cast<Document*>(m_context); if (cookiesEnabled(document)) { ExceptionCode ec; // Exception (for sandboxed documents) ignored. document->setCookie(m_handshake.serverSetCookie(), ec); } } } // FIXME: handle set-cookie2. LOG(Network, "WebSocketChannel %p connected", this); skipBuffer(headerLength); m_client->didConnect(); LOG(Network, "remaining in read buf %lu", static_cast<unsigned long>(m_bufferSize)); return m_buffer; } LOG(Network, "WebSocketChannel %p connection failed", this); skipBuffer(headerLength); m_shouldDiscardReceivedData = true; if (!m_closed) m_handle->close(); return false; } if (m_handshake.mode() != WebSocketHandshake::Connected) return false; const char* nextFrame = m_buffer; const char* p = m_buffer; const char* end = p + m_bufferSize; unsigned char frameByte = static_cast<unsigned char>(*p++); if ((frameByte & 0x80) == 0x80) { size_t length = 0; bool errorFrame = false; while (p < end) { if (length > std::numeric_limits<size_t>::max() / 128) { LOG(Network, "frame length overflow %lu", static_cast<unsigned long>(length)); errorFrame = true; break; } size_t newLength = length * 128; unsigned char msgByte = static_cast<unsigned char>(*p); unsigned int lengthMsgByte = msgByte & 0x7f; if (newLength > std::numeric_limits<size_t>::max() - lengthMsgByte) { LOG(Network, "frame length overflow %lu+%u", static_cast<unsigned long>(newLength), lengthMsgByte); errorFrame = true; break; } newLength += lengthMsgByte; if (newLength < length) { // sanity check LOG(Network, "frame length integer wrap %lu->%lu", static_cast<unsigned long>(length), static_cast<unsigned long>(newLength)); errorFrame = true; break; } length = newLength; ++p; if (!(msgByte & 0x80)) break; } if (p + length < p) { LOG(Network, "frame buffer pointer wrap %p+%lu->%p", p, static_cast<unsigned long>(length), p + length); errorFrame = true; } if (errorFrame) { skipBuffer(m_bufferSize); // Save memory. m_shouldDiscardReceivedData = true; if (!m_client) return false; m_client->didReceiveMessageError(); if (!m_closed) m_handle->close(); return false; } ASSERT(p + length >= p); if (p + length < end) { p += length; nextFrame = p; ASSERT(nextFrame > m_buffer); skipBuffer(nextFrame - m_buffer); m_client->didReceiveMessageError(); return m_buffer; } return false; } const char* msgStart = p; while (p < end && *p != '\xff') ++p; if (p < end && *p == '\xff') { int msgLength = p - msgStart; ++p; nextFrame = p; if (frameByte == 0x00) { String msg = String::fromUTF8(msgStart, msgLength); skipBuffer(nextFrame - m_buffer); m_client->didReceiveMessage(msg); } else { skipBuffer(nextFrame - m_buffer); m_client->didReceiveMessageError(); } return m_buffer; } return false; }