11 #include <restinio/asio_include.hpp> 13 #include <http_parser.h> 15 #include <restinio/impl/include_fmtlib.hpp> 17 #include <restinio/exception.hpp> 18 #include <restinio/http_headers.hpp> 19 #include <restinio/request_handler.hpp> 20 #include <restinio/connection_count_limiter.hpp> 21 #include <restinio/impl/connection_base.hpp> 22 #include <restinio/impl/header_helpers.hpp> 23 #include <restinio/impl/response_coordinator.hpp> 24 #include <restinio/impl/connection_settings.hpp> 25 #include <restinio/impl/fixed_buffer.hpp> 26 #include <restinio/impl/write_group_output_ctx.hpp> 27 #include <restinio/impl/executor_wrapper.hpp> 28 #include <restinio/impl/sendfile_operation.hpp> 30 #include <restinio/utils/impl/safe_uint_truncate.hpp> 31 #include <restinio/utils/at_scope_exit.hpp> 99 incoming_http_msg_limits_t limits )
107 m_header = http_request_header_t{};
109 m_current_field_name.clear();
110 m_last_value_total_size = 0u;
114 m_total_field_count = 0u;
123 chunked_input_info_unique_ptr_t
150 template<
typename Http_Methods >
228 std::size_t buffer_size,
229 incoming_http_msg_limits_t limits )
255 http_parser_init( &m_parser, HTTP_REQUEST);
258 m_parser_ctx.reset();
259 m_parser.data = &m_parser_ctx;
263 template <
typename Connection,
typename Start_Read_CB,
typename Failed_CB >
268 Start_Read_CB start_read_cb,
302 template <
typename Traits >
310 using timer_manager_t =
typename Traits::timer_manager_t;
311 using timer_guard_t =
typename timer_manager_t::timer_guard_t;
314 using logger_t =
typename Traits::logger_t;
315 using strand_t =
typename Traits::strand_t;
316 using stream_socket_t =
typename Traits::stream_socket_t;
322 connection_id_t conn_id,
324 stream_socket_t && socket,
326 connection_settings_handle_t< Traits > settings,
328 endpoint_t remote_endpoint,
330 lifetime_monitor_t lifetime_monitor )
349 "[connection:{}] start connection with {}",
366 "[connection:{}] destructor called",
400 "[connection:{}] prepare connection error: {}",
413 "[connection:{}] start waiting for request",
447 connection_settings_handle_t< Traits > settings,
448 stream_socket_t socket,
449 lifetime_monitor_t lifetime_monitor )
480 "[connection:{}] continue reading request",
502 "[connection:{}] skip read operation: already running",
510 after_read(
const asio_ns::error_code & ec, std::size_t length )
noexcept 521 "[connection:{}] received {} bytes",
534 "[connection:{}] unexpected exception during the " 535 "handling of incoming data: {}",
550 "[connection:{}] read socket error: {}; " 565 "[connection:{}] EOF and no request, " 606 "[connection:{}] parser error {}: {}",
652 "[connection:{}] request received (#{}): {} {}",
708 "[connection:{}] upgrade request received: {} {}; " 731 "[connection:{}] upgrade request happened to be a pipelined one, " 732 "and will be handled after previous requests are handled",
745 "[connection:{}] error while handling request: {}",
770 "[connection:{}] handle upgrade request (#{}): {} {}",
819 "[connection:{}] upgrade request handler rejects " 820 "request, but socket was moved out from connection",
848 request_id_t request_id,
850 response_output_flags_t response_output_flags,
852 write_group_t wg )
override 876 "[connection:{}] unable to handle response: {}",
888 request_id_t request_id,
890 response_output_flags_t response_output_flags,
905 "[connection:{}] notificator error: {}",
935 "[connection:{}] append response (#{}), " 936 "flags: {}, write group size: {}",
954 "[connection:{}] receive response parts for " 955 "request (#{}), but response with connection-close " 956 "attribute happened before",
967 "[connection:{}] try to write response, " 968 "while socket is closed",
1005 "[connection:{}] start next write group for response (#{}), " 1038 "[connection:{}] start response (#{}): {}",
1100 "[connection:{}] handle_current_write_ctx failed: {}",
1118 "[connection:{}] sending resp data with " 1119 "connection-close attribute " 1135 "[connection:{}] sending resp data, " 1158 "[connection:{}] outgoing data was sent: {} bytes",
1178 "[connection:{}] sending resp file data with " 1179 "connection-close attribute, " 1193 "[connection:{}] sending resp file data, total size: {}",
1229 "[connection:{}] file data was sent: {} bytes",
1239 "[connection:{}] send file data error: {} ({}) bytes",
1257 "[connection:{}] finishing current write group",
1268 "[connection:{}] should keep alive",
1328 "[connection:{}] last sent response was marked " 1369 "[connection:{}] unable to write: {}",
1385 "[connection:{}] notificator error: {}",
1403 "[connection:{}] close",
1411 "connection.socket.shutdown",
1420 "connection.socket.close",
1428 "[connection:{}] close: close socket",
1438 "[connection:{}] close: timer canceled",
1447 "[connection:{}] close: reset responses data",
1467 template<
typename Message_Builder >
1531 return fmt::
format(
"[connection: {}] unexpected " 1532 "error during timeout handling: {}",
1541 using timout_cb_t =
void (connection_t::* )(
void );
1586 std::chrono::steady_clock::time_point timeout_after,
1587 timout_cb_t timout_cb )
1595 std::chrono::steady_clock::duration timeout,
1596 timout_cb_t timout_cb )
1608 "[connection:{}] {} timed out",
1707 template <
typename Traits >
1711 using logger_t =
typename Traits::logger_t;
1712 using stream_socket_t =
typename Traits::stream_socket_t;
1717 connection_settings_handle_t< Traits > connection_settings,
1718 std::unique_ptr< socket_options_setter_t > socket_options_setter )
1730 stream_socket_t socket,
1731 endpoint_t remote_endpoint,
1732 lifetime_monitor_t lifetime_monitor )
1737 socket_options_t options{ socket.lowest_layer() };
1738 (*m_socket_options_setter)( options );
1741 return std::make_shared< connection_type_t >(
1742 m_connection_id_counter++,
1743 std::move( socket ),
1744 m_connection_settings,
1745 std::move( remote_endpoint ),
1746 std::move( lifetime_monitor ) );
http_request_header_t m_header
Request data.
void check_timeout_impl()
Check timed out operation.
upgrade_internals_t move_upgrade_internals()
Move socket out of connection.
static connection_t & cast_to_self(tcp_connection_ctx_base_t &base)
Timer to controll operations.
std::size_t m_total_field_count
Total number of parsed HTTP-fields.
void consume_message()
Start (continue) a chain of read-parse-read-... operations.
connection_settings_handle_t< Traits > m_settings
Common paramaters of a connection.
std::string m_current_field_name
Parser context temp values and flags.
response_coordinator_t m_response_coordinator
Response coordinator.
void handle_write_response_timeout()
bool m_message_complete
Flag: is http message parsed completely.
Request with connection-upgrade header came and waits for request handler to be called in non pipelin...
void after_write(const asio_ns::error_code &ec) noexcept
Handle write response finished.
write_group_output_ctx_t m_write_output_ctx
Write to socket operation context.
void guard_sendfile_operation(std::chrono::steady_clock::duration timelimit)
timout_cb_t m_current_timeout_cb
Callback to all if timeout happened.
tls_socket_t * make_tls_socket_pointer_for_state_listener(tls_socket_t &socket) noexcept
upgrade_internals_t(upgrade_internals_t &&)=default
connection_t & operator=(connection_t &&)=delete
lifetime_monitor_t m_lifetime_monitor
void handle_current_write_ctx() noexcept
Start/continue/continue handling output data of current write group.
connection_t & operator=(const connection_t &)=delete
std::unique_ptr< socket_options_setter_t > m_socket_options_setter
RESTINIO_NODISCARD chunked_input_info_unique_ptr_t make_chunked_input_info_if_necessary()
Creates an instance of chunked_input_info if there is an info about chunks in the body...
logger_t & m_logger
Logger for operation.
connection_settings_handle_t< Traits > m_connection_settings
stream_socket_t m_socket
Connection.
void cancel_timeout_checking() noexcept
Stop timout guarding.
connection_t(connection_id_t conn_id, stream_socket_t &&socket, connection_settings_handle_t< Traits > settings, endpoint_t remote_endpoint, lifetime_monitor_t lifetime_monitor)
void finish_handling_current_write_ctx()
Do post write actions for current write group.
timer_guard_t m_timer_guard
Timer guard.
void handle_sendfile_timeout()
void consume_data(const char *data, std::size_t length)
Parse some data.
chunked_input_info_block_t m_chunked_info_block
virtual void write_response_parts(request_id_t request_id, response_output_flags_t response_output_flags, write_group_t wg) override
Write parts for specified request.
void prepare_connection_and_start_read(asio_ns::ip::tcp::socket &, Connection &, Start_Read_CB start_read_cb, Failed_CB)
void guard_write_operation()
Start guard write operation if necessary.
http_parser_settings create_parser_settings() noexcept
Helper for setting parser settings.
void close() noexcept
Close connection functions.
connection_input_t m_input
Input routine.
void reset()
Prepare context to handle new request.
void handle_xxx_timeout(const char *operation_name)
void after_read(const asio_ns::error_code &ec, std::size_t length) noexcept
Handle read operation result.
lifetime_monitor_t m_lifetime_monitor
Monitor of the connection lifetime.
connection_settings_handle_t< Traits > m_settings
bool m_leading_headers_completed
Handler for request with connection-upgrade header was called so any response data comming is for tha...
connection_factory_t(connection_settings_handle_t< Traits > connection_settings, std::unique_ptr< socket_options_setter_t > socket_options_setter)
connection_id_t m_connection_id_counter
bool m_init_read_after_this_write
connection_t(const connection_t &)=delete
http_parser_ctx_t(incoming_http_msg_limits_t limits)
The main constructor.
void on_request_message_complete()
Handle a given request message.
Internals that are necessary for upgrade.
void handle_nothing_to_write()
std::size_t m_last_value_total_size
std::chrono::steady_clock::time_point m_current_timeout_after
Timeout point of a current guarded operation.
void schedule_operation_timeout_callback(std::chrono::steady_clock::duration timeout, timout_cb_t timout_cb)
connection_t(connection_t &&)=delete
void handle_request_handling_timeout()
void write_response_parts_impl(request_id_t request_id, response_output_flags_t response_output_flags, write_group_t wg)
Write parts for specified request.
No connection request in progress.
const endpoint_t m_remote_endpoint
Remote endpoint for this connection.
void init_write_if_necessary()
tcp_connection_ctx_weak_handle_t m_prepared_weak_ctx
A prepared weak handle for passing it to timer guard.
RESTINIO_NODISCARD char to_lower_case(unsigned char ch)
void init_next_timeout_checking()
Schedule next timeout checking.
upgrade_internals_t(connection_settings_handle_t< Traits > settings, stream_socket_t socket, lifetime_monitor_t lifetime_monitor)
void handle_read_timeout()
void handle_file_write_operation(file_write_operation_t &op)
Run sendfile write operation.
void trigger_error_and_close(Message_Builder msg_builder) noexcept
Trigger an error.
request_handler_t & m_request_handler
Request handler.
void guard_request_handling_operation()
Start guard request handling operation if necessary.
void handle_upgrade_request()
Calls handler for upgrade request.
const incoming_http_msg_limits_t m_limits
Limits for the incoming message.
void init_write()
Initiate write operation.
void wait_for_http_message()
Start reading next htttp-message.
virtual void check_timeout(tcp_connection_ctx_handle_t &self) override
Schedules real timedout operations check on the executer of a connection.
auto create_new_connection(stream_socket_t socket, endpoint_t remote_endpoint, lifetime_monitor_t lifetime_monitor)
Context for handling http connections.
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container ¶ms, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
void guard_read_operation()
Statr guard read operation if necessary.
void handle_trivial_write_operation(const trivial_write_operation_t &op)
Run trivial buffers write operation.
connection_upgrade_stage_t
Enum for a flag specifying that connection is going to upgrade or not.