|
6 | 6 |
|
7 | 7 | #include <atomic> |
8 | 8 | #include <iostream> |
| 9 | +#include <limits> |
9 | 10 | #include <list> |
10 | 11 | #include <memory> |
11 | 12 | #include <mutex> |
|
17 | 18 | #include <asio/steady_timer.hpp> |
18 | 19 | namespace SimpleWeb { |
19 | 20 | using error_code = std::error_code; |
| 21 | + using errc = std::errc; |
| 22 | + namespace make_error_code = std; |
20 | 23 | } // namespace SimpleWeb |
21 | 24 | #else |
22 | 25 | #include <boost/asio.hpp> |
23 | 26 | #include <boost/asio/steady_timer.hpp> |
24 | 27 | namespace SimpleWeb { |
25 | 28 | namespace asio = boost::asio; |
26 | 29 | using error_code = boost::system::error_code; |
| 30 | + namespace errc = boost::system::errc; |
| 31 | + namespace make_error_code = boost::system::errc; |
27 | 32 | } // namespace SimpleWeb |
28 | 33 | #endif |
29 | 34 |
|
@@ -348,6 +353,9 @@ namespace SimpleWeb { |
348 | 353 | long timeout_request = 5; |
349 | 354 | /// Idle timeout. Defaults to no timeout. |
350 | 355 | 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(); |
351 | 359 | /// IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation. |
352 | 360 | /// If empty, the address will be any address. |
353 | 361 | std::string address; |
@@ -611,6 +619,14 @@ namespace SimpleWeb { |
611 | 619 | } |
612 | 620 |
|
613 | 621 | 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 | + } |
614 | 630 | 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*/) { |
615 | 631 | auto lock = connection->handler_runner->continue_lock(); |
616 | 632 | if(!lock) |
|
0 commit comments