diff --git a/.gitignore b/.gitignore index d1b663dcc2..3cdd635e2f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,7 @@ objs_static/ examples/chat_server/chat_server examples/echo_server/echo_server examples/echo_server/echo_server + +examples/echo_server/echo_server + +examples/chat_client/chat_client diff --git a/examples/chat_client/chat_client b/examples/chat_client/chat_client index 3763f4a732..6fd7b4357e 100755 Binary files a/examples/chat_client/chat_client and b/examples/chat_client/chat_client differ diff --git a/examples/chat_server/Makefile b/examples/chat_server/Makefile index 06150c10ab..a91db10f3e 100644 --- a/examples/chat_server/Makefile +++ b/examples/chat_server/Makefile @@ -5,7 +5,7 @@ CXX ?= c++ SHARED ?= "1" ifeq ($(SHARED), 1) - LDFLAGS := $(LDFLAGS) -lboost_system -boost_date_time -lwebsocketpp + LDFLAGS := $(LDFLAGS) -lboost_system -lboost_date_time -lwebsocketpp else LDFLAGS := $(LDFLAGS) -lboost_system -lboost_date_time ../../libwebsocketpp.a endif diff --git a/examples/echo_server/echo_server b/examples/echo_server/echo_server index ac8dfcf6ca..cacfd5b2b1 100755 Binary files a/examples/echo_server/echo_server and b/examples/echo_server/echo_server differ diff --git a/src/websocket_client_session.cpp b/src/websocket_client_session.cpp index 22eed0ef7f..dde98e5f26 100644 --- a/src/websocket_client_session.cpp +++ b/src/websocket_client_session.cpp @@ -56,10 +56,71 @@ void client_session::set_url(const std::string& url) { // TODO: impliment // TODO: input validation - m_host = "thor-websocket.zaphoyd.net"; - m_port = 9003; - m_resource = "/chat"; - + std::string::size_type start = 0; + std::string::size_type end; + + if (url.substr(0,5) == "ws://") { + m_secure = false; + start = 5; + } else if (url.substr(0,6) == "wss://") { + m_secure = true; + start = 6; + throw client_error("wss / secure connections are not supported by WebSocket++ at this time"); + } else { + throw client_error("Invalid websocket URL"); + } + + end = url.find(":",start); + + if (end == std::string::npos) { + // no port + if (m_secure) { + m_port = 443; + } else { + m_port = 80; + } + + end = url.find("/",start); + + if (end == std::string::npos) { + m_host = url.substr(start); + m_resource = "/"; + } else { + m_host = url.substr(start,end-start); + m_resource = url.substr(end); + } + } else { + m_host = url.substr(start,end-start); + + start += end-start + 1; + + end = url.find("/",start); + + std::string port_str; + if (end == std::string::npos) { + port_str = url.substr(start); + m_resource = "/"; + } else { + port_str = url.substr(start,end-start); + m_resource = url.substr(end); + } + + unsigned int port = std::atoi(port_str.c_str()); + + if (port > 65535) { + throw client_error("Invalid websocket URL"); + } + + // TODO: fix cast + m_port = (uint16_t) port; + } + + std::stringstream l; + + l << "parsed websocket url: secure: " << m_secure << " host: " << m_host + << " port (final): " << m_port << " resource " << m_resource; + + log(l.str(),LOG_DEBUG); } bool client_session::get_secure() const {