lots of misc fixes, mostly broadcast server related

This commit is contained in:
Peter Thorson
2011-12-21 08:23:03 -06:00
parent 3405a91e56
commit 4d03909d58
18 changed files with 898 additions and 621 deletions

View File

@@ -102,6 +102,50 @@ namespace websocketpp {
}
}
namespace frame {
// Opcodes are 4 bits
// See spec section 5.2
namespace opcode {
enum value {
CONTINUATION = 0x0,
TEXT = 0x1,
BINARY = 0x2,
RSV3 = 0x3,
RSV4 = 0x4,
RSV5 = 0x5,
RSV6 = 0x6,
RSV7 = 0x7,
CLOSE = 0x8,
PING = 0x9,
PONG = 0xA,
CONTROL_RSVB = 0xB,
CONTROL_RSVC = 0xC,
CONTROL_RSVD = 0xD,
CONTROL_RSVE = 0xE,
CONTROL_RSVF = 0xF,
};
inline bool reserved(value v) {
return (v >= RSV3 && v <= RSV7) ||
(v >= CONTROL_RSVB && v <= CONTROL_RSVF);
}
inline bool invalid(value v) {
return (v > 0xF || v < 0);
}
inline bool is_control(value v) {
return v >= 0x8;
}
}
namespace limits {
static const uint8_t PAYLOAD_SIZE_BASIC = 125;
static const uint16_t PAYLOAD_SIZE_EXTENDED = 0xFFFF; // 2^16, 65535
static const uint64_t PAYLOAD_SIZE_JUMBO = 0x7FFFFFFFFFFFFFFF;//2^63
}
} // namespace frame
}
#endif // WEBSOCKET_CONSTANTS_HPP

View File

@@ -42,7 +42,6 @@
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream> // temporary?
#include <vector>
@@ -258,10 +257,12 @@ public:
// stuff about switching handlers on the fly
// TODO: organize more
void set_handler(handler_ptr new_handler) {
m_endpoint.elog().at(log::elevel::FATAL)
<< "Tried to switch to a NULL handler." << log::endl;
if (!new_handler) {
throw "TODO";
m_endpoint.elog().at(log::elevel::FATAL)
<< "Tried to switch to a NULL handler." << log::endl;
terminate(true);
return;
}
handler_ptr old_handler = get_handler();

View File

@@ -31,8 +31,6 @@
#include "md5.h"
#include <string>
#include <iostream>
namespace websocketpp {
// could be compiled separately

116
src/messages/data.cpp Normal file
View File

@@ -0,0 +1,116 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "data.hpp"
#include "../processors/processor.hpp"
using websocketpp::message::data;
data::data() {
m_payload.reserve(PAYLOAD_SIZE_INIT);
}
websocketpp::frame::opcode::value data::get_opcode() const {
return m_opcode;
}
const std::string& data::get_payload() const {
return m_payload;
}
uint64_t data::process_payload(std::istream& input,uint64_t size) {
unsigned char c;
const uint64_t new_size = m_payload.size() + size;
uint64_t i;
if (new_size > PAYLOAD_SIZE_MAX) {
// TODO: real exception
throw processor::exception("Message too big",processor::error::MESSAGE_TOO_BIG);
}
if (new_size > m_payload.capacity()) {
m_payload.reserve(std::max(new_size,static_cast<uint64_t>(2*m_payload.capacity())));
}
for (i = 0; i < size; ++i) {
if (input.good()) {
c = input.get();
} else if (input.eof()) {
break;
} else {
// istream read error? throw?
throw processor::exception("istream read error",
processor::error::FATAL_ERROR);
}
if (input.good()) {
process_character(c);
} else if (input.eof()) {
break;
} else {
// istream read error? throw?
throw processor::exception("istream read error",
processor::error::FATAL_ERROR);
}
}
// successfully read all bytes
return i;
}
void data::process_character(unsigned char c) {
if (m_masking_index >= 0) {
c = c ^ m_masking_key[(m_masking_index++)%4];
}
if (m_opcode == frame::opcode::TEXT && !m_validator.consume(static_cast<uint32_t>((unsigned char)(c)))) {
throw processor::exception("Invalid UTF8 data",processor::error::PAYLOAD_VIOLATION);
}
// add c to payload
m_payload.push_back(c);
}
void data::reset(frame::opcode::value opcode) {
m_opcode = opcode;
m_masking_index = 0;
m_payload.resize(0);
m_validator.reset();
}
void data::complete() {
if (m_opcode == frame::opcode::TEXT) {
if (!m_validator.complete()) {
throw processor::exception("Invalid UTF8 data",processor::error::PAYLOAD_VIOLATION);
}
}
}
void data::set_masking_key(int32_t key) {
*reinterpret_cast<int32_t*>(m_masking_key) = key;
m_masking_index = (key == 0 ? -1 : 0);
}

View File

@@ -28,100 +28,27 @@
#ifndef WEBSOCKET_DATA_MESSAGE_HPP
#define WEBSOCKET_DATA_MESSAGE_HPP
#include "../processors/processor.hpp"
#include "../websocket_frame.hpp"
#include "../common.hpp"
#include "../utf8_validator/utf8_validator.hpp"
#include <algorithm>
#include <istream>
namespace websocketpp {
namespace message {
class data {
public:
data() {
m_payload.reserve(PAYLOAD_SIZE_INIT);
}
data();
frame::opcode::value get_opcode() const;
const std::string& get_payload() const;
frame::opcode::value get_opcode() const {
return m_opcode;
};
const std::string& get_payload() const {
return m_payload;
};
uint64_t process_payload(std::istream& input,uint64_t size) {
unsigned char c;
const uint64_t new_size = m_payload.size() + size;
uint64_t i;
if (new_size > PAYLOAD_SIZE_MAX) {
// TODO: real exception
throw processor::exception("Message too big",processor::error::MESSAGE_TOO_BIG);
}
if (new_size > m_payload.capacity()) {
m_payload.reserve(std::max(new_size,static_cast<uint64_t>(2*m_payload.capacity())));
}
for (i = 0; i < size; ++i) {
if (input.good()) {
c = input.get();
} else if (input.eof()) {
break;
} else {
// istream read error? throw?
throw processor::exception("istream read error",
processor::error::FATAL_ERROR);
}
if (input.good()) {
process_character(c);
} else if (input.eof()) {
break;
} else {
// istream read error? throw?
throw processor::exception("istream read error",
processor::error::FATAL_ERROR);
}
}
// successfully read all bytes
return i;
}
void process_character(unsigned char c) {
if (m_masking_index >= 0) {
c = c ^ m_masking_key[(m_masking_index++)%4];
}
if (m_opcode == frame::opcode::TEXT && !m_validator.consume(static_cast<uint32_t>((unsigned char)(c)))) {
throw processor::exception("Invalid UTF8 data",processor::error::PAYLOAD_VIOLATION);
}
// add c to payload
m_payload.push_back(c);
}
void reset(frame::opcode::value opcode) {
m_opcode = opcode;
m_masking_index = 0;
m_payload.resize(0);
m_validator.reset();
}
void complete() {
if (m_opcode == frame::opcode::TEXT) {
if (!m_validator.complete()) {
throw processor::exception("Invalid UTF8 data",processor::error::PAYLOAD_VIOLATION);
}
}
}
void set_masking_key(int32_t key) {
*reinterpret_cast<int32_t*>(m_masking_key) = key;
m_masking_index = (key == 0 ? -1 : 0);
}
uint64_t process_payload(std::istream& input,uint64_t size);
void process_character(unsigned char c);
void reset(frame::opcode::value opcode);
void complete();
void set_masking_key(int32_t key);
private:
static const uint64_t PAYLOAD_SIZE_INIT = 1000; // 1KB
static const uint64_t PAYLOAD_SIZE_MAX = 100000000;// 100MB

View File

@@ -30,7 +30,6 @@
#include "processor.hpp"
#include "../md5/md5.h"
#include "../md5/md5.hpp"
#include "../network_utilities.hpp"
@@ -74,7 +73,8 @@ public:
memcpy(&key_final[8],request.header("Sec-WebSocket-Key3").c_str(),8);
// md5
m_key3 = md5_hash_string(key_final);
m_key3 = key_final;
m_key3 = md5_hash_string(m_key3);
response.add_header("Upgrade","websocket");
response.add_header("Connection","Upgrade");

View File

@@ -158,9 +158,9 @@ public:
// handler interface callback base class
class handler_interface {
public:
virtual void validate(connection_ptr connection) {};
virtual void on_open(connection_ptr connection) {};
virtual void on_close(connection_ptr connection) {};
virtual void validate(connection_ptr connection) {}
virtual void on_open(connection_ptr connection) {}
virtual void on_close(connection_ptr connection) {}
virtual void on_fail(connection_ptr connection) {}
virtual void on_message(connection_ptr connection,message::data_ptr) {};

View File

@@ -94,7 +94,12 @@ public:
void init() {
m_context_ptr = m_connection.get_handler()->on_tls_init();
m_socket_ptr = ssl_socket_ptr(new ssl_socket(m_endpoint.get_io_service(),*m_context_ptr));
if (!m_context_ptr) {
throw "handler was unable to init tls, connection error";
}
m_socket_ptr = ssl_socket_ptr(new ssl_socket(m_endpoint.get_io_service(),*m_context_ptr));
}
void async_init(boost::function<void(const boost::system::error_code&)> callback)

View File

@@ -28,11 +28,9 @@
#ifndef WEBSOCKETPP_URI_HPP
#define WEBSOCKETPP_URI_HPP
#include <exception>
#include <stdint.h>
#include <string>
#include "common.hpp"
#include <boost/shared_ptr.hpp>
#include <exception>
namespace websocketpp {

View File

@@ -28,53 +28,10 @@
#ifndef WEBSOCKET_FRAME_HPP
#define WEBSOCKET_FRAME_HPP
namespace websocketpp {
namespace frame {
// Opcodes are 4 bits
// See spec section 5.2
namespace opcode {
enum value {
CONTINUATION = 0x0,
TEXT = 0x1,
BINARY = 0x2,
RSV3 = 0x3,
RSV4 = 0x4,
RSV5 = 0x5,
RSV6 = 0x6,
RSV7 = 0x7,
CLOSE = 0x8,
PING = 0x9,
PONG = 0xA,
CONTROL_RSVB = 0xB,
CONTROL_RSVC = 0xC,
CONTROL_RSVD = 0xD,
CONTROL_RSVE = 0xE,
CONTROL_RSVF = 0xF,
};
inline bool reserved(value v) {
return (v >= RSV3 && v <= RSV7) ||
(v >= CONTROL_RSVB && v <= CONTROL_RSVF);
}
inline bool invalid(value v) {
return (v > 0xF || v < 0);
}
inline bool is_control(value v) {
return v >= 0x8;
}
}
namespace limits {
static const uint8_t PAYLOAD_SIZE_BASIC = 125;
static const uint16_t PAYLOAD_SIZE_EXTENDED = 0xFFFF; // 2^16, 65535
static const uint64_t PAYLOAD_SIZE_JUMBO = 0x7FFFFFFFFFFFFFFF;//2^63
}
} // namespace frame
} // namespace websocketpp
#include "common.hpp"
#include "network_utilities.hpp"
#include "processors/processor.hpp"
#include "utf8_validator/utf8_validator.hpp"
@@ -85,7 +42,6 @@ namespace frame {
#include <arpa/inet.h>
#endif
#include <string>
#include <vector>
#include <cstring>
#include <iostream>