WebSocketClient implementation
| 20198 | |
| 20199 | // WebSocketClient implementation |
| 20200 | inline WebSocketClient::WebSocketClient( |
| 20201 | const std::string &scheme_host_port_path, const Headers &headers) |
| 20202 | : headers_(headers) { |
| 20203 | detail::UrlComponents uc; |
| 20204 | if (detail::parse_url(scheme_host_port_path, uc) && !uc.scheme.empty() && |
| 20205 | !uc.host.empty() && !uc.path.empty()) { |
| 20206 | auto &scheme = uc.scheme; |
| 20207 | |
| 20208 | #ifdef CPPHTTPLIB_SSL_ENABLED |
| 20209 | if (scheme != "ws" && scheme != "wss") { |
| 20210 | #else |
| 20211 | if (scheme != "ws") { |
| 20212 | #endif |
| 20213 | #ifndef CPPHTTPLIB_NO_EXCEPTIONS |
| 20214 | std::string msg = "'" + scheme + "' scheme is not supported."; |
| 20215 | throw std::invalid_argument(msg); |
| 20216 | #endif |
| 20217 | return; |
| 20218 | } |
| 20219 | |
| 20220 | auto is_ssl = scheme == "wss"; |
| 20221 | |
| 20222 | host_ = std::move(uc.host); |
| 20223 | |
| 20224 | port_ = is_ssl ? 443 : 80; |
| 20225 | if (!uc.port.empty() && !detail::parse_port(uc.port, port_)) { return; } |
| 20226 | |
| 20227 | path_ = std::move(uc.path); |
| 20228 | |
| 20229 | #ifdef CPPHTTPLIB_SSL_ENABLED |
| 20230 | is_ssl_ = is_ssl; |
| 20231 | #else |
| 20232 | if (is_ssl) { return; } |
| 20233 | #endif |
| 20234 | |
| 20235 | is_valid_ = true; |
| 20236 | } |
| 20237 | } |
| 20238 | |
| 20239 | inline WebSocketClient::~WebSocketClient() { shutdown_and_close(); } |
| 20240 | |
| 20241 | inline bool WebSocketClient::is_valid() const { return is_valid_; } |
| 20242 | |
| 20243 | inline void WebSocketClient::shutdown_and_close() { |
| 20244 | #ifdef CPPHTTPLIB_SSL_ENABLED |
| 20245 | if (is_ssl_) { |
| 20246 | if (tls_session_) { |
| 20247 | tls::shutdown(tls_session_, true); |
| 20248 | tls::free_session(tls_session_); |
| 20249 | tls_session_ = nullptr; |
| 20250 | } |
| 20251 | if (tls_ctx_) { |
| 20252 | tls::free_context(tls_ctx_); |
| 20253 | tls_ctx_ = nullptr; |
| 20254 | } |
| 20255 | } |
| 20256 | #endif |
| 20257 | if (ws_ && ws_->is_open()) { ws_->close(); } |
nothing calls this directly
no test coverage detected