Skip to content

Commit

Permalink
WebSocket: Check keyword string in comma separeted values in HTTP
Browse files Browse the repository at this point in the history
header field.
  • Loading branch information
tatsuhiro-t committed Apr 8, 2012
1 parent c648ca0 commit 1e0068e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/HttpHeader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,28 @@ const std::string& HttpHeader::getRequestPath() const
return requestPath_;
}

bool HttpHeader::fieldContains(const std::string& name,
const std::string& value)
{
std::pair<std::multimap<std::string, std::string>::const_iterator,
std::multimap<std::string, std::string>::const_iterator> range =
equalRange(name);
for(std::multimap<std::string, std::string>::const_iterator i = range.first;
i != range.second; ++i) {
std::vector<Scip> values;
util::splitIter((*i).second.begin(), (*i).second.end(),
std::back_inserter(values),
',',
true // doStrip
);
for(std::vector<Scip>::const_iterator j = values.begin(),
eoj = values.end(); j != eoj; ++j) {
if(util::strieq((*j).first, (*j).second, value.begin(), value.end())) {
return true;
}
}
}
return false;
}

} // namespace aria2
5 changes: 5 additions & 0 deletions src/HttpHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class HttpHeader {
HttpHeader();
~HttpHeader();

// For all methods, use lowercased header field name.
void put(const std::string& name, const std::string& value);
bool defined(const std::string& name) const;
const std::string& find(const std::string& name) const;
Expand Down Expand Up @@ -121,6 +122,10 @@ class HttpHeader {
// Clears table_. responseStatus_ and version_ are unchanged.
void clearField();

// Returns true if heder field |name| contains |value|. This method
// assumes the values of the header field is delimited by ','.
bool fieldContains(const std::string& name, const std::string& value);

static const std::string LOCATION;
static const std::string TRANSFER_ENCODING;
static const std::string CONTENT_ENCODING;
Expand Down
6 changes: 2 additions & 4 deletions src/HttpServerCommand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ bool HttpServerCommand::execute()
e_->setNoWait(true);
return true;
}
const std::string& upgradeHd = header->find("upgrade");
const std::string& connectionHd = header->find("connection");
if(util::strieq(upgradeHd.begin(), upgradeHd.end(), "websocket") &&
util::strieq(connectionHd.begin(), connectionHd.end(), "upgrade")) {
if(header->fieldContains("upgrade", "websocket") &&
header->fieldContains("connection", "upgrade")) {
#ifdef ENABLE_WEBSOCKET
int status = websocketHandshake(header);
Command* command;
Expand Down
19 changes: 19 additions & 0 deletions test/HttpHeaderTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class HttpHeaderTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testFindAll);
CPPUNIT_TEST(testClearField);
CPPUNIT_TEST(testFill);
CPPUNIT_TEST(testFieldContains);
CPPUNIT_TEST_SUITE_END();

public:
void testGetRange();
void testFindAll();
void testClearField();
void testFill();
void testFieldContains();
};


Expand Down Expand Up @@ -175,4 +177,21 @@ void HttpHeaderTest::testFill()
h.findAll("duplicate")[1]);
}

void HttpHeaderTest::testFieldContains()
{
HttpHeader h;
h.put("connection", "Keep-Alive, Upgrade");
h.put("upgrade", "WebSocket");
h.put("sec-websocket-version", "13");
h.put("sec-websocket-version", "8, 7");
CPPUNIT_ASSERT(h.fieldContains("connection", "upgrade"));
CPPUNIT_ASSERT(h.fieldContains("connection", "keep-alive"));
CPPUNIT_ASSERT(!h.fieldContains("connection", "close"));
CPPUNIT_ASSERT(h.fieldContains("upgrade", "websocket"));
CPPUNIT_ASSERT(!h.fieldContains("upgrade", "spdy"));
CPPUNIT_ASSERT(h.fieldContains("sec-websocket-version", "13"));
CPPUNIT_ASSERT(h.fieldContains("sec-websocket-version", "8"));
CPPUNIT_ASSERT(!h.fieldContains("sec-websocket-version", "6"));
}

} // namespace aria2

0 comments on commit 1e0068e

Please sign in to comment.