2 #include <samchon/API.hpp> 4 #include <samchon/protocol/ServerConnector.hpp> 5 #include <samchon/protocol/WebCommunicator.hpp> 8 #include <samchon/library/RWMutex.hpp> 9 #include <samchon/library/StringUtil.hpp> 51 static std::map<std::pair<std::string, int>, std::string> s_cookies;
81 virtual void connect(
const std::string &ip,
int port)
override 106 virtual void connect(
const std::string &ip,
int port,
const std::string &path)
110 handshake(ip, port, path);
116 void handshake(
const std::string &ip,
int port,
const std::string &path)
122 std::string &base64_key = WebSocketUtil::generate_base64_certification_key();
123 std::string &sha1_key = WebSocketUtil::encode_certification_key(base64_key);
129 auto it = s_cookies.find({ ip, port });
131 if (it != s_cookies.end())
132 cookie =
"Cookie: " + it->second +
"\r\n";
139 "GET {1} HTTP/1.1\r\n" +
141 "Upgrade: websocket\r\n" +
142 "Connection: Upgrade\r\n" +
144 "Sec-WebSocket-Key: {4}\r\n" +
145 "Sec-WebSocket-Version: 13\r\n" +
148 path.empty() ?
"/" :
"/" + path,
149 ip +
":" + std::to_string(port),
153 socket->write_some(boost::asio::buffer(query.data(), query.size()));
158 std::array<unsigned char, 1000> byte_array;
159 size_t size = socket->read_some(boost::asio::buffer(byte_array));
161 WeakString wstr((
const char*)byte_array.data(), size);
162 std::string server_sha1 = wstr.
between(
"Sec-WebSocket-Accept: ",
"\r\n").str();
165 if (sha1_key != server_sha1)
166 throw std::domain_error(
"WebSocket handshaking has failed.");
169 if (wstr.find(
"Set-Cookie: ") != std::string::npos)
174 s_cookies[{ip, port}] = set_cookie.
str();
auto str() const -> std::string
Get the string content.
virtual void connect(const std::string &ip, int port) override
static auto substitute(const std::string &format, const T &val, const _Args &...args) -> std::string
Substitutes "{n}" tokens within the specified string with the respective arguments passed in...
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
A string class only references characeters, reference only.
virtual void connect(const std::string &ip, int port, const std::string &path)