From a547ee5a7ac0a309fbe4c2770b52b550f4e1845c Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sun, 17 Mar 2013 20:13:51 -0500 Subject: [PATCH] adds logging of connection opening results --- websocketpp/connection.hpp | 10 ++++++- websocketpp/impl/connection_impl.hpp | 41 ++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/websocketpp/connection.hpp b/websocketpp/connection.hpp index 292ae02e9e..51bd2f9f63 100644 --- a/websocketpp/connection.hpp +++ b/websocketpp/connection.hpp @@ -823,7 +823,15 @@ private: * @return the message_ptr at the front of the queue */ message_ptr write_pop(); - + + /// Prints information about the incoming connection to the access log + /** + * Prints information about the incoming connection to the access log. + * Includes: connection type, websocket version, remote endpoint, user agent + * path, status code. + */ + void log_open_result(); + // static settings const std::string m_user_agent; diff --git a/websocketpp/impl/connection_impl.hpp b/websocketpp/impl/connection_impl.hpp index 86e99d5f50..4c2be7c046 100644 --- a/websocketpp/impl/connection_impl.hpp +++ b/websocketpp/impl/connection_impl.hpp @@ -890,7 +890,7 @@ void connection::write(std::string msg) { m_alog.write(log::alevel::devel,"connection write"); transport_con_type::async_write( - msg.c_str(), + msg.data(), msg.size(), lib::bind( &type::handle_write, @@ -968,6 +968,8 @@ void connection::handle_send_http_response( return; } + this->log_open_result(); + if (m_response.get_status_code() != http::status_code::SWITCHING_PROTOCOLS) { if (m_processor) { @@ -985,7 +987,6 @@ void connection::handle_send_http_response( } // TODO: cancel handshake timer - // TODO: log open result this->atomic_state_change( istate::PROCESS_HTTP_REQUEST, @@ -1451,6 +1452,42 @@ typename config::message_type::ptr connection::write_pop() return msg; } +template +void connection::log_open_result() +{ + std::stringstream s; + + int version; + if (!processor::is_websocket_handshake(m_request)) { + version = -1; + } else { + version = processor::get_websocket_version(m_request); + } + + // Connection Type + s << (version == -1 ? "HTTP" : "WebSocket") << " Connection "; + + // Remote endpoint address + s << "Unknown" << " "; + + // Version string if WebSocket + if (version != -1) { + s << "v" << version << " "; + } + + // User Agent + std::string ua = m_request.get_header("User-Agent"); + s << (ua == "" ? "NULL" : ua) << " "; + + // URI + s << (m_uri ? m_uri->get_resource() : "NULL") << " "; + + // Status code + s << m_response.get_status_code(); + + m_alog.write(log::alevel::connect,s.str()); +} + } // namespace websocketpp #endif // WEBSOCKETPP_CONNECTION_IMPL_HPP