diff --git a/examples/echo_server/Makefile b/examples/echo_server/Makefile index e79b687288..f7be740eca 100644 --- a/examples/echo_server/Makefile +++ b/examples/echo_server/Makefile @@ -1,4 +1,4 @@ -CFLAGS = -O2 +CFLAGS = -O2 -std=c++0x LDFLAGS = CXX ?= c++ @@ -7,10 +7,10 @@ SHARED ?= "1" ifeq ($(SHARED), 1) LDFLAGS := $(LDFLAGS) -lboost_system -lboost_date_time -lboost_program_options -lwebsocketpp else - LDFLAGS := $(LDFLAGS) -lboost_system -lboost_date_time -lboost_regex -lboost_random -lboost_program_options ../../libwebsocketpp.a + LDFLAGS := $(LDFLAGS) -lboost_system -lboost_date_time -lboost_regex -lcrypto -lssl ../../libwebsocketpp.a endif -echo_server: echo_server.o echo.o +echo_server: echo_server.o $(CXX) $(CFLAGS) $^ -o $@ $(LDFLAGS) %.o: %.cpp diff --git a/examples/echo_server/echo_server.cpp b/examples/echo_server/echo_server.cpp index 1b849c0744..d9b389d864 100644 --- a/examples/echo_server/echo_server.cpp +++ b/examples/echo_server/echo_server.cpp @@ -41,7 +41,7 @@ class echo_server_handler : public T::handler { public: typedef typename websocketpp::role::server::handler handler_type; typedef typename websocketpp::role::server::handler_ptr handler_ptr; - typedef typename handler_type::connection_ptr connection_ptr; + typedef typename handler_type::foo connection_ptr; void validate(connection_ptr connection) { //std::cout << "state: " << connection->get_state() << std::endl; @@ -74,18 +74,18 @@ public: }; int main(int argc, char* argv[]) { - short port = 9002; + unsigned short port = 9002; bool ssl = false; - + if (argc == 2) { // TODO: input validation? - port = atoi(argv[2]); + port = atoi(argv[1]); } if (argc == 3) { // TODO: input validation? - port = atoi(argv[2]); - ssl = (strcmp(argv[3],"-ssl")); + port = atoi(argv[1]); + ssl = !strcmp(argv[2],"-ssl"); } try { @@ -93,9 +93,14 @@ int main(int argc, char* argv[]) { plain_handler_ptr h(new echo_server_handler()); plain_endpoint_type e(h); - e.alog().set_level(websocketpp::log::alevel::CONNECT & websocketpp::log::alevel::DISCONNECT); + e.alog().set_level(websocketpp::log::alevel::CONNECT); + e.alog().set_level(websocketpp::log::alevel::DISCONNECT); + + // TODO: fix + //e.alog().set_level(websocketpp::log::alevel::CONNECT & websocketpp::log::alevel::DISCONNECT); e.elog().set_levels(websocketpp::log::elevel::ERROR,websocketpp::log::elevel::FATAL); + std::cout << "Starting WebSocket echo server on port " << port << std::endl; e.listen(port); } else { ssl_handler_ptr h(new echo_server_handler()); @@ -104,6 +109,7 @@ int main(int argc, char* argv[]) { e.alog().set_level(websocketpp::log::alevel::CONNECT & websocketpp::log::alevel::DISCONNECT); e.elog().set_levels(websocketpp::log::elevel::ERROR,websocketpp::log::elevel::FATAL); + std::cout << "Starting Secure WebSocket echo server on port " << port << std::endl; e.listen(port); } diff --git a/src/connection.hpp b/src/connection.hpp index 7ba42eb5e7..61816ed064 100644 --- a/src/connection.hpp +++ b/src/connection.hpp @@ -30,7 +30,7 @@ #include "common.hpp" #include "http/parser.hpp" -#include "logger.hpp" +#include "logger/logger.hpp" #include "roles/server.hpp" @@ -72,12 +72,13 @@ public: // friends (would require C++11) this would enable connection::start to be // protected instead of public. - // friend typename endpoint_traits::role_type; + friend typename endpoint_traits::role_type; + friend typename endpoint_traits::socket_type; //friend class role; //friend class socket; - friend class role:: template connection; - friend class socket:: template connection; + friend class role::template connection; + friend class socket::template connection; enum write_state { IDLE = 0, diff --git a/src/endpoint.hpp b/src/endpoint.hpp index d9322e4714..3ed747d74a 100644 --- a/src/endpoint.hpp +++ b/src/endpoint.hpp @@ -45,7 +45,7 @@ namespace websocketpp { #include "connection.hpp" #include "sockets/plain.hpp" // should this be here? -#include "logger.hpp" +#include "logger/logger.hpp" diff --git a/src/policy.cpp b/src/policy.cpp index be081418cc..dd34320642 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -53,24 +53,51 @@ public: } }; -/*class application_client_handler : public client_handler { - void on_action() { - std::cout << "application_client_handler::on_action()" << std::endl; +typedef websocketpp::endpoint basic_client; +typedef websocketpp::role::client::handler client_handler_type; +typedef websocketpp::role::client::handler_ptr client_handler_ptr; + +class application_client_handler : public client_handler_type { + void validate(handler_type::connection_ptr connection) { + std::cout << "state: " << connection->get_state() << std::endl; } -};*/ + + void on_open(handler_type::connection_ptr connection) { + std::cout << "connection opened" << std::endl; + } + + void on_close(handler_type::connection_ptr connection) { + std::cout << "connection closed" << std::endl; + } + + void on_message(connection_ptr connection,websocketpp::utf8_string_ptr msg) { + std::cout << "got message: " << *msg << std::endl; + connection->send(*msg); + } + void on_message(connection_ptr connection,websocketpp::binary_string_ptr data) { + std::cout << "got binary message of length: " << data->size() << std::endl; + connection->send(*data); + } + + void http(handler_type::connection_ptr connection) { + connection->set_body("HTTP Response!!"); + } + + void on_fail(handler_type::connection_ptr connection) { + std::cout << "connection failed" << std::endl; + } +}; int main () { - std::cout << "Endpoint 0" << std::endl; - handler_ptr h(new application_server_handler()); + handler_ptr h(new application_client_handler()); endpoint_type e(h); e.alog().set_level(websocketpp::log::alevel::ALL); e.elog().set_level(websocketpp::log::elevel::ALL); - e.listen(9002); - std::cout << std::endl; + e.connect("ws://localhost:9002"); return 0; } \ No newline at end of file diff --git a/src/processors/hybi.hpp b/src/processors/hybi.hpp index a235af6c73..f071836e8d 100644 --- a/src/processors/hybi.hpp +++ b/src/processors/hybi.hpp @@ -150,8 +150,6 @@ public: // TODO: check if get_uri is a full uri connection_uri.resource = request.uri(); - std::cout << "parsed uri: " << connection_uri.str() << std::endl; - return connection_uri; } diff --git a/src/roles/server.hpp b/src/roles/server.hpp index 001cc0b4dd..d8ebcbecf1 100644 --- a/src/roles/server.hpp +++ b/src/roles/server.hpp @@ -217,6 +217,30 @@ public: // continue as HTTP? m_endpoint.get_handler()->http(m_connection.shared_from_this()); + // should there be a more encapsulated http processor here? + m_origin = m_request.header("Origin"); + m_uri.secure = m_endpoint.is_secure(); + + std::string h = m_request.header("Host"); + + size_t found = h.find(":"); + if (found == std::string::npos) { + m_uri.host = h; + m_uri.port = (m_uri.secure ? DEFAULT_SECURE_PORT : DEFAULT_PORT); + } else { + uint16_t p = atoi(h.substr(found+1).c_str()); + + if (p == 0) { + throw(http::exception("Could not determine request uri. Check host header.",http::status_code::BAD_REQUEST)); + } else { + m_uri.host = h.substr(0,found); + m_uri.port = p; + } + } + + // TODO: check if get_uri is a full uri + m_uri.resource = m_request.uri(); + m_response.set_status(http::status_code::OK); } } catch (const http::exception& e) { @@ -337,21 +361,21 @@ public: // handler interface callback class class handler { public: - typedef connection_ptr connection_ptr; + typedef connection_ptr foo; // Required - virtual void validate(connection_ptr connection) = 0; - virtual void on_open(connection_ptr connection) = 0; - virtual void on_close(connection_ptr connection) = 0; + virtual void validate(foo connection) = 0; + virtual void on_open(foo connection) = 0; + virtual void on_close(foo connection) = 0; - virtual void on_message(connection_ptr connection,utf8_string_ptr) = 0; - virtual void on_message(connection_ptr connection,binary_string_ptr) = 0; + virtual void on_message(foo connection,utf8_string_ptr) = 0; + virtual void on_message(foo connection,binary_string_ptr) = 0; // Optional - virtual bool on_ping(connection_ptr connection,binary_string_ptr) {return true;} - virtual void on_pong(connection_ptr connection,binary_string_ptr) {} - virtual void http(connection_ptr connection) {} - virtual void on_fail(connection_ptr connection) {} + virtual bool on_ping(foo connection,binary_string_ptr) {return true;} + virtual void on_pong(foo connection,binary_string_ptr) {} + virtual void http(foo connection) {} + virtual void on_fail(foo connection) {} }; server(boost::asio::io_service& m,handler_ptr h) diff --git a/src/sockets/plain.hpp b/src/sockets/plain.hpp index f6576e42ac..846db38185 100644 --- a/src/sockets/plain.hpp +++ b/src/sockets/plain.hpp @@ -45,6 +45,10 @@ public: return m_io_service; } + bool is_secure() { + return false; + } + // Connection specific details template class connection { diff --git a/src/sockets/ssl.hpp b/src/sockets/ssl.hpp index 7b02dfaa01..8b69e4b007 100644 --- a/src/sockets/ssl.hpp +++ b/src/sockets/ssl.hpp @@ -68,6 +68,10 @@ public: } } + bool is_secure() { + return true; + } + // Connection specific details template class connection { diff --git a/src/websocket_frame.hpp b/src/websocket_frame.hpp index 45d06a9f51..1bb2545dcb 100644 --- a/src/websocket_frame.hpp +++ b/src/websocket_frame.hpp @@ -75,7 +75,7 @@ namespace frame { } // namespace websocketpp #include "network_utilities.hpp" -#include "processor.hpp" +#include "processors/processor.hpp" #include "utf8_validator/utf8_validator.hpp" #if defined(WIN32)