This source file includes following definitions.
- reset
- addProcessor
- createHeaderValue
- appendAcceptedExtension
- fail
- processHeaderValue
- acceptedExtensions
- failureReason
#include "config.h"
#include "modules/websockets/WebSocketExtensionDispatcher.h"
#include "modules/websockets/WebSocketExtensionParser.h"
#include "wtf/ASCIICType.h"
#include "wtf/HashMap.h"
#include "wtf/text/CString.h"
#include "wtf/text/StringHash.h"
namespace WebCore {
void WebSocketExtensionDispatcher::reset()
{
m_processors.clear();
}
void WebSocketExtensionDispatcher::addProcessor(PassOwnPtr<WebSocketExtensionProcessor> processor)
{
for (size_t i = 0; i < m_processors.size(); ++i) {
if (m_processors[i]->extensionToken() == processor->extensionToken())
return;
}
ASSERT(processor->handshakeString().length());
ASSERT(!processor->handshakeString().contains('\n'));
ASSERT(!processor->handshakeString().contains(static_cast<UChar>('\0')));
m_processors.append(processor);
}
const String WebSocketExtensionDispatcher::createHeaderValue() const
{
size_t numProcessors = m_processors.size();
if (!numProcessors)
return String();
StringBuilder builder;
builder.append(m_processors[0]->handshakeString());
for (size_t i = 1; i < numProcessors; ++i) {
builder.append(", ");
builder.append(m_processors[i]->handshakeString());
}
return builder.toString();
}
void WebSocketExtensionDispatcher::appendAcceptedExtension(const String& extensionToken, HashMap<String, String>& extensionParameters)
{
if (!m_acceptedExtensionsBuilder.isEmpty())
m_acceptedExtensionsBuilder.append(", ");
m_acceptedExtensionsBuilder.append(extensionToken);
for (HashMap<String, String>::const_iterator iterator = extensionParameters.begin(); iterator != extensionParameters.end(); ++iterator) {
m_acceptedExtensionsBuilder.append("; ");
m_acceptedExtensionsBuilder.append(iterator->key);
if (!iterator->value.isNull()) {
m_acceptedExtensionsBuilder.append("=");
m_acceptedExtensionsBuilder.append(iterator->value);
}
}
}
void WebSocketExtensionDispatcher::fail(const String& reason)
{
m_failureReason = reason;
m_acceptedExtensionsBuilder.clear();
}
bool WebSocketExtensionDispatcher::processHeaderValue(const String& headerValue)
{
if (!headerValue.length())
return true;
if (!m_processors.size()) {
fail("Response must not include 'Sec-WebSocket-Extensions' header if not present in request: " + headerValue);
return false;
}
const CString headerValueData = headerValue.utf8();
WebSocketExtensionParser parser(headerValueData.data(), headerValueData.data() + headerValueData.length());
while (!parser.finished()) {
String extensionToken;
HashMap<String, String> extensionParameters;
if (!parser.parseExtension(extensionToken, extensionParameters)) {
fail("Invalid 'Sec-WebSocket-Extensions' header");
return false;
}
size_t index;
for (index = 0; index < m_processors.size(); ++index) {
WebSocketExtensionProcessor* processor = m_processors[index].get();
if (extensionToken == processor->extensionToken()) {
if (processor->processResponse(extensionParameters)) {
appendAcceptedExtension(extensionToken, extensionParameters);
break;
}
fail("Error in " + extensionToken + ": " + processor->failureReason());
return false;
}
}
if (index == m_processors.size()) {
fail("Found an unsupported extension '" + extensionToken + "' in 'Sec-WebSocket-Extensions' header");
return false;
}
}
return parser.parsedSuccessfully();
}
String WebSocketExtensionDispatcher::acceptedExtensions() const
{
if (m_acceptedExtensionsBuilder.isEmpty())
return String();
return m_acceptedExtensionsBuilder.toString();
}
String WebSocketExtensionDispatcher::failureReason() const
{
return m_failureReason;
}
}