This source file includes following definitions.
- create
- failureReason
- m_responseProcessed
- handshakeString
- processResponse
- m_succeeded
- fail
- m_succeeded
- fail
- createExtensionProcessor
- enableDeflate
- deflate
- resetDeflateContext
- inflate
- resetInflateContext
- didFail
#include "config.h"
#include "modules/websockets/WebSocketDeflateFramer.h"
#include "wtf/HashMap.h"
#include "wtf/text/StringHash.h"
#include "wtf/text/WTFString.h"
namespace WebCore {
class WebSocketExtensionDeflateFrame FINAL : public WebSocketExtensionProcessor {
WTF_MAKE_FAST_ALLOCATED;
public:
static PassOwnPtr<WebSocketExtensionDeflateFrame> create(WebSocketDeflateFramer* framer)
{
return adoptPtr(new WebSocketExtensionDeflateFrame(framer));
}
virtual ~WebSocketExtensionDeflateFrame() { }
virtual String handshakeString() OVERRIDE;
virtual bool processResponse(const HashMap<String, String>&) OVERRIDE;
virtual String failureReason() OVERRIDE { return m_failureReason; }
private:
WebSocketExtensionDeflateFrame(WebSocketDeflateFramer*);
WebSocketDeflateFramer* m_framer;
bool m_responseProcessed;
String m_failureReason;
};
WebSocketExtensionDeflateFrame::WebSocketExtensionDeflateFrame(WebSocketDeflateFramer* framer)
: WebSocketExtensionProcessor("x-webkit-deflate-frame")
, m_framer(framer)
, m_responseProcessed(false)
{
ASSERT(m_framer);
}
String WebSocketExtensionDeflateFrame::handshakeString()
{
return extensionToken();
}
bool WebSocketExtensionDeflateFrame::processResponse(const HashMap<String, String>& serverParameters)
{
if (m_responseProcessed) {
m_failureReason = "Received duplicate deflate-frame response";
return false;
}
m_responseProcessed = true;
unsigned expectedNumParameters = 0;
int windowBits = 15;
HashMap<String, String>::const_iterator parameter = serverParameters.find("max_window_bits");
if (parameter != serverParameters.end()) {
windowBits = parameter->value.toInt();
if (windowBits < 8 || windowBits > 15) {
m_failureReason = "Received invalid max_window_bits parameter";
return false;
}
expectedNumParameters++;
}
WebSocketDeflater::ContextTakeOverMode mode = WebSocketDeflater::TakeOverContext;
parameter = serverParameters.find("no_context_takeover");
if (parameter != serverParameters.end()) {
if (!parameter->value.isNull()) {
m_failureReason = "Received invalid no_context_takeover parameter";
return false;
}
mode = WebSocketDeflater::DoNotTakeOverContext;
expectedNumParameters++;
}
if (expectedNumParameters != serverParameters.size()) {
m_failureReason = "Received unexpected deflate-frame parameter";
return false;
}
m_framer->enableDeflate(windowBits, mode);
return true;
}
DeflateResultHolder::DeflateResultHolder(WebSocketDeflateFramer* framer)
: m_framer(framer)
, m_succeeded(true)
{
ASSERT(m_framer);
}
DeflateResultHolder::~DeflateResultHolder()
{
m_framer->resetDeflateContext();
}
void DeflateResultHolder::fail(const String& failureReason)
{
m_succeeded = false;
m_failureReason = failureReason;
}
InflateResultHolder::InflateResultHolder(WebSocketDeflateFramer* framer)
: m_framer(framer)
, m_succeeded(true)
{
ASSERT(m_framer);
}
InflateResultHolder::~InflateResultHolder()
{
m_framer->resetInflateContext();
}
void InflateResultHolder::fail(const String& failureReason)
{
m_succeeded = false;
m_failureReason = failureReason;
}
WebSocketDeflateFramer::WebSocketDeflateFramer()
: m_enabled(false)
{
}
PassOwnPtr<WebSocketExtensionProcessor> WebSocketDeflateFramer::createExtensionProcessor()
{
return WebSocketExtensionDeflateFrame::create(this);
}
void WebSocketDeflateFramer::enableDeflate(int windowBits, WebSocketDeflater::ContextTakeOverMode mode)
{
m_deflater = WebSocketDeflater::create(windowBits, mode);
m_inflater = WebSocketInflater::create();
if (!m_deflater->initialize() || !m_inflater->initialize()) {
m_deflater.clear();
m_inflater.clear();
return;
}
m_enabled = true;
}
PassOwnPtr<DeflateResultHolder> WebSocketDeflateFramer::deflate(WebSocketFrame& frame)
{
OwnPtr<DeflateResultHolder> result = DeflateResultHolder::create(this);
if (!enabled() || !WebSocketFrame::isNonControlOpCode(frame.opCode) || !frame.payloadLength)
return result.release();
if (!m_deflater->addBytes(frame.payload, frame.payloadLength) || !m_deflater->finish()) {
result->fail("Failed to compress frame");
return result.release();
}
frame.compress = true;
frame.payload = m_deflater->data();
frame.payloadLength = m_deflater->size();
return result.release();
}
void WebSocketDeflateFramer::resetDeflateContext()
{
if (m_deflater)
m_deflater->reset();
}
PassOwnPtr<InflateResultHolder> WebSocketDeflateFramer::inflate(WebSocketFrame& frame)
{
OwnPtr<InflateResultHolder> result = InflateResultHolder::create(this);
if (!enabled())
return result.release();
if (!frame.compress)
return result.release();
if (!WebSocketFrame::isNonControlOpCode(frame.opCode)) {
result->fail("Received unexpected compressed frame");
return result.release();
}
if (!m_inflater->addBytes(frame.payload, frame.payloadLength) || !m_inflater->finish()) {
result->fail("Failed to decompress frame");
return result.release();
}
frame.compress = false;
frame.payload = m_inflater->data();
frame.payloadLength = m_inflater->size();
return result.release();
}
void WebSocketDeflateFramer::resetInflateContext()
{
if (m_inflater)
m_inflater->reset();
}
void WebSocketDeflateFramer::didFail()
{
resetDeflateContext();
resetInflateContext();
}
}