2 #include <samchon/API.hpp> 4 #include <samchon/protocol/Server.hpp> 5 #include <samchon/protocol/WebClientDriver.hpp> 8 #include <samchon/WeakString.hpp> 9 #include <samchon/library/StringUtil.hpp> 10 #include <samchon/protocol/WebSocketUtil.hpp> 69 virtual void handle_connection(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
override 71 std::array<char, 1000> byte_array;
72 boost::system::error_code error;
77 size_t size = socket->read_some(boost::asio::buffer(byte_array), error);
86 std::string session_id;
92 encrypted_cert_key = header.
between(
"Sec-WebSocket-Key:",
"\n").trim();
94 if (encrypted_cert_key.
find(
"\r") != std::string::npos)
95 encrypted_cert_key = encrypted_cert_key.
between(
"",
"\r");
97 if (header.find(
"Set-Cookie: ") != std::string::npos)
99 cookie = header.between(
"Set-Cookie: ",
"\r\n");
101 size_t session_id_idx = header.find(
"SESSION_ID=");
102 if (session_id_idx == std::string::npos)
105 session_id = issue_session_id();
106 cookie +=
"; SESSION_ID=" + session_id;
111 session_id = header.substr
116 header.find(
";", session_id_idx),
117 header.find(
"\r\n", session_id_idx)
125 session_id = issue_session_id();
126 cookie =
"SESSION_ID=" + session_id;
136 "HTTP/1.1 101 Switching Protocols\r\n" +
137 "Upgrade: websocket\r\n" +
138 "Connection: Upgrade\r\n" +
139 "Set-Cookie: {1}\r\n" +
140 "Sec-WebSocket-Accept: {2}\r\n" +
144 WebSocketUtil::encode_certification_key(encrypted_cert_key)
148 socket->write_some(boost::asio::buffer(reply_header), error);
157 driver->session_id = session_id;
158 driver->path = path.str();
164 auto issue_session_id() -> std::string
166 static std::uniform_int_distribution<unsigned int> distribution(0, UINT32_MAX);
167 static std::random_device device;
169 unsigned int port = _Acceptor->local_endpoint().port();
170 size_t uid = ++sequence;
172 unsigned int rand = distribution(device);
174 std::stringstream ss;
175 ss << std::hex << port;
176 ss << std::hex << uid;
177 ss << std::hex << linux_time;
178 ss << std::hex << rand;
virtual void addClient(std::shared_ptr< ClientDriver >)=0
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.
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
Date (year, month, day in month)
auto toLinuxTime() const -> long long
Converts the Date to linux_time.
A string class only references characeters, reference only.