|
@@ -133,7 +133,7 @@ string WsHandshake::generateHttpError(int responseCode) {
|
|
size_t WsHandshake::parseHttpRequest(const byte *buffer, size_t size) {
|
|
size_t WsHandshake::parseHttpRequest(const byte *buffer, size_t size) {
|
|
std::unique_lock lock(mMutex);
|
|
std::unique_lock lock(mMutex);
|
|
std::list<string> lines;
|
|
std::list<string> lines;
|
|
- size_t length = parseHttpLines(buffer, size, lines);
|
|
|
|
|
|
+ size_t length = utils::parseHttpLines(buffer, size, lines);
|
|
if (length == 0)
|
|
if (length == 0)
|
|
return 0;
|
|
return 0;
|
|
|
|
|
|
@@ -151,7 +151,7 @@ size_t WsHandshake::parseHttpRequest(const byte *buffer, size_t size) {
|
|
|
|
|
|
mPath = std::move(path);
|
|
mPath = std::move(path);
|
|
|
|
|
|
- auto headers = parseHttpHeaders(lines);
|
|
|
|
|
|
+ auto headers = utils::parseHttpHeaders(lines);
|
|
|
|
|
|
auto h = headers.find("host");
|
|
auto h = headers.find("host");
|
|
if (h == headers.end())
|
|
if (h == headers.end())
|
|
@@ -185,7 +185,7 @@ size_t WsHandshake::parseHttpRequest(const byte *buffer, size_t size) {
|
|
size_t WsHandshake::parseHttpResponse(const byte *buffer, size_t size) {
|
|
size_t WsHandshake::parseHttpResponse(const byte *buffer, size_t size) {
|
|
std::unique_lock lock(mMutex);
|
|
std::unique_lock lock(mMutex);
|
|
std::list<string> lines;
|
|
std::list<string> lines;
|
|
- size_t length = parseHttpLines(buffer, size, lines);
|
|
|
|
|
|
+ size_t length = utils::parseHttpLines(buffer, size, lines);
|
|
if (length == 0)
|
|
if (length == 0)
|
|
return 0;
|
|
return 0;
|
|
|
|
|
|
@@ -202,7 +202,7 @@ size_t WsHandshake::parseHttpResponse(const byte *buffer, size_t size) {
|
|
if (code != 101)
|
|
if (code != 101)
|
|
throw std::runtime_error("Unexpected response code " + to_string(code) + " for WebSocket");
|
|
throw std::runtime_error("Unexpected response code " + to_string(code) + " for WebSocket");
|
|
|
|
|
|
- auto headers = parseHttpHeaders(lines);
|
|
|
|
|
|
+ auto headers = utils::parseHttpHeaders(lines);
|
|
|
|
|
|
auto h = headers.find("upgrade");
|
|
auto h = headers.find("upgrade");
|
|
if (h == headers.end())
|
|
if (h == headers.end())
|
|
@@ -238,46 +238,6 @@ string WsHandshake::computeAcceptKey(const string &key) {
|
|
return utils::base64_encode(Sha1(string(key) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
|
|
return utils::base64_encode(Sha1(string(key) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
|
|
}
|
|
}
|
|
|
|
|
|
-size_t WsHandshake::parseHttpLines(const byte *buffer, size_t size, std::list<string> &lines) {
|
|
|
|
- lines.clear();
|
|
|
|
- auto begin = reinterpret_cast<const char *>(buffer);
|
|
|
|
- auto end = begin + size;
|
|
|
|
- auto cur = begin;
|
|
|
|
- while (true) {
|
|
|
|
- auto last = cur;
|
|
|
|
- cur = std::find(cur, end, '\n');
|
|
|
|
- if (cur == end)
|
|
|
|
- return 0;
|
|
|
|
- string line(last, cur != begin && *std::prev(cur) == '\r' ? std::prev(cur++) : cur++);
|
|
|
|
- if (line.empty())
|
|
|
|
- break;
|
|
|
|
- lines.emplace_back(std::move(line));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return cur - begin;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-std::multimap<string, string> WsHandshake::parseHttpHeaders(const std::list<string> &lines) {
|
|
|
|
- std::multimap<string, string> headers;
|
|
|
|
- for (const auto &line : lines) {
|
|
|
|
- if (size_t pos = line.find_first_of(':'); pos != string::npos) {
|
|
|
|
- string key = line.substr(0, pos);
|
|
|
|
- string value = "";
|
|
|
|
- if (size_t subPos = line.find_first_not_of(' ', pos + 1); subPos != string::npos )
|
|
|
|
- {
|
|
|
|
- value = line.substr(subPos);
|
|
|
|
- }
|
|
|
|
- std::transform(key.begin(), key.end(), key.begin(),
|
|
|
|
- [](char c) { return std::tolower(c); });
|
|
|
|
- headers.emplace(std::move(key), std::move(value));
|
|
|
|
- } else {
|
|
|
|
- headers.emplace(line, "");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return headers;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
WsHandshake::Error::Error(const string &w) : std::runtime_error(w) {}
|
|
WsHandshake::Error::Error(const string &w) : std::runtime_error(w) {}
|
|
|
|
|
|
WsHandshake::RequestError::RequestError(const string &w, int responseCode)
|
|
WsHandshake::RequestError::RequestError(const string &w, int responseCode)
|