Skip to content
This repository was archived by the owner on Jun 12, 2018. It is now read-only.

Commit 7dbd961

Browse files
committed
Can now set maximum message size in WsServer:: and WsClient::config
1 parent 2535ce9 commit 7dbd961

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

client_ws.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <atomic>
88
#include <iostream>
9+
#include <limits>
910
#include <list>
1011
#include <mutex>
1112
#include <random>
@@ -291,6 +292,9 @@ namespace SimpleWeb {
291292
long timeout_request = 0;
292293
/// Idle timeout. Defaults to no timeout.
293294
long timeout_idle = 0;
295+
/// Maximum size of incoming messages. Defaults to architecture maximum.
296+
/// Exceeding this limit will result in a message_size error code and the connection will be closed.
297+
std::size_t max_message_size = std::numeric_limits<std::size_t>::max();
294298
/// Additional header fields to send when performing WebSocket handshake.
295299
/// Use this variable to for instance set Sec-WebSocket-Protocol.
296300
CaseInsensitiveMultimap header;
@@ -525,6 +529,14 @@ namespace SimpleWeb {
525529
}
526530

527531
void read_message_content(const std::shared_ptr<Connection> &connection) {
532+
if(connection->message->length > config.max_message_size) {
533+
connection_error(connection, make_error_code::make_error_code(errc::message_size));
534+
const int status = 1009;
535+
const std::string reason = "message too big";
536+
connection->send_close(status, reason);
537+
connection_close(connection, status, reason);
538+
return;
539+
}
528540
asio::async_read(*connection->socket, connection->message->streambuf, asio::transfer_exactly(connection->message->length), [this, connection](const error_code &ec, std::size_t /*bytes_transferred*/) {
529541
auto lock = connection->handler_runner->continue_lock();
530542
if(!lock)

server_ws.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <atomic>
88
#include <iostream>
9+
#include <limits>
910
#include <list>
1011
#include <memory>
1112
#include <mutex>
@@ -17,13 +18,17 @@
1718
#include <asio/steady_timer.hpp>
1819
namespace SimpleWeb {
1920
using error_code = std::error_code;
21+
using errc = std::errc;
22+
namespace make_error_code = std;
2023
} // namespace SimpleWeb
2124
#else
2225
#include <boost/asio.hpp>
2326
#include <boost/asio/steady_timer.hpp>
2427
namespace SimpleWeb {
2528
namespace asio = boost::asio;
2629
using error_code = boost::system::error_code;
30+
namespace errc = boost::system::errc;
31+
namespace make_error_code = boost::system::errc;
2732
} // namespace SimpleWeb
2833
#endif
2934

@@ -348,6 +353,9 @@ namespace SimpleWeb {
348353
long timeout_request = 5;
349354
/// Idle timeout. Defaults to no timeout.
350355
long timeout_idle = 0;
356+
/// Maximum size of incoming messages. Defaults to architecture maximum.
357+
/// Exceeding this limit will result in a message_size error code and the connection will be closed.
358+
std::size_t max_message_size = std::numeric_limits<std::size_t>::max();
351359
/// IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation.
352360
/// If empty, the address will be any address.
353361
std::string address;
@@ -611,6 +619,14 @@ namespace SimpleWeb {
611619
}
612620

613621
void read_message_content(const std::shared_ptr<Connection> &connection, std::size_t length, Endpoint &endpoint, unsigned char fin_rsv_opcode) const {
622+
if(length > config.max_message_size) {
623+
connection_error(connection, endpoint, make_error_code::make_error_code(errc::message_size));
624+
const int status = 1009;
625+
const std::string reason = "message too big";
626+
connection->send_close(status, reason);
627+
connection_close(connection, endpoint, status, reason);
628+
return;
629+
}
614630
asio::async_read(*connection->socket, connection->read_buffer, asio::transfer_exactly(4 + length), [this, connection, length, &endpoint, fin_rsv_opcode](const error_code &ec, std::size_t /*bytes_transferred*/) {
615631
auto lock = connection->handler_runner->continue_lock();
616632
if(!lock)

0 commit comments

Comments
 (0)