misc fixes

This commit is contained in:
Peter Thorson
2011-11-13 20:46:54 -06:00
parent 4ed86a7c30
commit 40a42dd637
7 changed files with 115 additions and 45 deletions

View File

@@ -87,7 +87,7 @@ public:
if (response.header("Sec-WebSocket-Location") == "") {
// TODO: extract from host header rather than hard code
ws_uri uri = get_uri(request);
response.add_header("Sec-WebSocket-Location",uri.base());
response.add_header("Sec-WebSocket-Location",uri.str());
}
}
@@ -99,10 +99,30 @@ public:
ws_uri uri;
uri.secure = m_secure;
std::string h = request.header("Host");
size_t found = h.find(":");
if (found == std::string::npos) {
uri.host = h;
uri.port = (m_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 {
uri.host = h.substr(0,found);
uri.port = p;
}
}
// TODO: check if get_uri is a full uri
// TODO: host and port
uri.resource = request.uri();
std::cout << "parsed uri: " << uri.str() << std::endl;
return uri;
}

View File

@@ -124,7 +124,7 @@ public:
} else if (version == 7 || version == 8) {
return request.header("Sec-WebSocket-Origin");
} else {
throw(http::exception("Could not determine origin header.",http::status_code::BAD_REQUEST));
throw(http::exception("Could not determine origin header. Check Sec-WebSocket-Version header",http::status_code::BAD_REQUEST));
}
}
@@ -132,10 +132,30 @@ public:
ws_uri uri;
uri.secure = m_secure;
std::string h = request.header("Host");
size_t found = h.find(":");
if (found == std::string::npos) {
uri.host = h;
uri.port = (m_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 {
uri.host = h.substr(0,found);
uri.port = p;
}
}
// TODO: check if get_uri is a full uri
// TODO: host and port
uri.resource = request.uri();
std::cout << "parsed uri: " << uri.str() << std::endl;
return uri;
}

View File

@@ -78,6 +78,17 @@ public:
error::value m_code;
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Server API *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
server(uint16_t port, server_handler_ptr handler)
void run();
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Server Session API *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -114,6 +125,8 @@ public:
virtual void ping(const binary_string& payload) = 0;
virtual void pong(const binary_string& payload) = 0;
virtual uint64_t buffered_amount() const = 0;
// Valid for CLOSED state
virtual close::status::value get_local_close_code() const = 0;
virtual utf8_string get_local_close_reason() const = 0;
@@ -212,8 +225,10 @@ public:
virtual int get_version() const = 0;
virtual std::string get_origin() const = 0;
virtual const ws_uri& get_uri() const = 0;
virtual bool get_secure() const = 0;
virtual std::string get_host() const = 0;
virtual std::string get_resource() const = 0;
virtual uint16_t get_port() const = 0;
// Valid for CONNECTING state
virtual void set_origin(const std::string& origin) = 0;

View File

@@ -26,6 +26,7 @@
*/
#include "network_utilities.hpp"
#include "websocket_constants.hpp"
#include <sstream>
#include "md5/md5.h"
@@ -153,7 +154,7 @@ bool websocketpp::ws_uri::parse(const std::string& uri) {
host = what[2];
if (what[3] == "") {
port = (secure ? 443 : 80);
port = (secure ? DEFAULT_SECURE_PORT : DEFAULT_PORT);
} else {
unsigned int t_port = atoi(std::string(what[3]).substr(1).c_str());
@@ -182,7 +183,7 @@ std::string websocketpp::ws_uri::base() {
s << "ws" << (secure ? "s" : "") << "://" << host;
if (port != (secure ? 443 : 80)) {
if (port != (secure ? DEFAULT_SECURE_PORT : DEFAULT_PORT)) {
s << ":" << port;
}
@@ -190,6 +191,19 @@ std::string websocketpp::ws_uri::base() {
return s.str();
}
std::string websocketpp::ws_uri::str() {
std::stringstream s;
s << "ws" << (secure ? "s" : "") << "://" << host;
if (port != (secure ? DEFAULT_SECURE_PORT : DEFAULT_PORT)) {
s << ":" << port;
}
s << resource;
return s.str();
}
void md5_hash_string(char *string,char *hash) {
md5_state_t state;

View File

@@ -52,6 +52,7 @@ namespace websocketpp {
struct ws_uri {
bool parse(const std::string& uri);
std::string base();
std::string str();
bool secure;
std::string host;

View File

@@ -47,6 +47,13 @@ namespace websocketpp {
const uint64_t DEFAULT_MAX_MESSAGE_SIZE = 0xFFFFFF; // ~16MB
const uint16_t DEFAULT_PORT = 80;
const uint16_t DEFAULT_SECURE_PORT = 443;
inline uint16_t default_port(bool secure) {
return (secure ? DEFAULT_SECURE_PORT : DEFAULT_PORT);
}
// System logging levels
static const uint16_t LOG_ALL = 0;
static const uint16_t LOG_DEBUG = 1;

View File

@@ -48,13 +48,9 @@ namespace po = boost::program_options;
#include "hybi_legacy_processor.hpp"
#include "hybi_processor.hpp"
//#include "websocket_session.hpp"
#include "websocket_connection_handler.hpp"
//#include <boost/date_time/posix_time/posix_time.hpp>
#include "rng/blank_rng.hpp"
#include "http/parser.hpp"
#include "logger/logger.hpp"
@@ -211,6 +207,11 @@ public:
m_io_service.post(boost::bind(&connection_type::write_message,connection_type::shared_from_this(),msg));
}
uint64_t buffered_amount() const {
// TODO: syncronize this member function
return m_write_buffer;
}
// Valid for CLOSED state
close::status::value get_local_close_code() const {
return m_local_close_code;
@@ -681,9 +682,7 @@ public:
// clear the queue except for the last message
while (m_write_queue.size() > 1) {
m_write_buffer -= m_write_queue.front()->size();
std::cout << "int size before: " << m_write_queue.size() << std::endl;
m_write_queue.pop();
std::cout << "int size after: " << m_write_queue.size() << std::endl;
}
break;
default:
@@ -695,9 +694,7 @@ public:
if (m_write_state == write_state::IDLE) {
m_write_state = write_state::WRITING;
}
std::cout << "starting async write " << std::endl;
boost::asio::async_write(
m_socket,
boost::asio::buffer(*m_write_queue.front()),
@@ -733,10 +730,8 @@ public:
return;
}
std::cout << "size before: " << m_write_queue.size() << std::endl;
m_write_buffer -= m_write_queue.front()->size();
m_write_queue.pop();
std::cout << "size after: " << m_write_queue.size() << std::endl;
if (m_write_state == write_state::WRITING) {
m_write_state = write_state::IDLE;
@@ -829,8 +824,6 @@ public:
}
private:
server_ptr m_server;
boost::asio::io_service& m_io_service;
@@ -859,7 +852,7 @@ private:
// Write queue
std::queue<binary_string_ptr> m_write_queue;
size_t m_write_buffer;
uint64_t m_write_buffer;
write_state::value m_write_state;
// Close state
@@ -904,30 +897,7 @@ public:
m_io_service.run();
}
// creates a new session object and connects the next websocket
// connection to it.
void start_accept() {
// TODO: sanity check whether the session buffer size bound could be reduced
connection_ptr new_session(
new connection_type(
endpoint_type::shared_from_this(),
m_io_service,
m_handler
)
);
m_acceptor.async_accept(
new_session->get_socket(),
boost::bind(
&endpoint_type::handle_accept,
endpoint_type::shared_from_this(),
new_session,
boost::asio::placeholders::error
)
);
}
// INTERFACE FOR LOCAL APPLICATIONS
void set_max_message_size(uint64_t val) {
@@ -1007,6 +977,29 @@ public:
}
private:
// creates a new session object and connects the next websocket
// connection to it.
void start_accept() {
// TODO: sanity check whether the session buffer size bound could be reduced
connection_ptr new_session(
new connection_type(
endpoint_type::shared_from_this(),
m_io_service,
m_handler
)
);
m_acceptor.async_accept(
new_session->get_socket(),
boost::bind(
&endpoint_type::handle_accept,
endpoint_type::shared_from_this(),
new_session,
boost::asio::placeholders::error
)
);
}
// if no errors starts the session's read loop and returns to the
// start_accept phase.
void handle_accept(connection_ptr connection,const boost::system::error_code& error)