11 #include <restinio/asio_include.hpp> 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> 113 incoming_http_msg_limits_t limits )
121 m_header = http_request_header_t{};
123 m_current_field_name.clear();
124 m_last_value_total_size = 0u;
128 m_total_field_count = 0u;
140 chunked_input_info_unique_ptr_t result;
142 if( !m_chunked_info_block.m_chunks.empty() ||
143 0u != m_chunked_info_block.m_trailing_fields.fields_count() )
145 result = std::make_unique< chunked_input_info_t >(
146 std::move( m_chunked_info_block ) );
164 template<
typename Http_Methods >
272 std::size_t buffer_size,
273 incoming_http_msg_limits_t limits,
274 const llhttp_settings_t* settings )
278 llhttp_init( &m_parser, llhttp_type_t::HTTP_REQUEST, settings );
279 m_parser.data = &m_parser_ctx;
303 llhttp_reset( &m_parser);
306 m_parser_ctx.reset();
310 template <
typename Connection,
typename Start_Read_CB,
typename Failed_CB >
315 Start_Read_CB start_read_cb,
349 template <
typename Traits >
357 using timer_manager_t =
typename Traits::timer_manager_t;
358 using timer_guard_t =
typename timer_manager_t::timer_guard_t;
361 using logger_t =
typename Traits::logger_t;
362 using strand_t =
typename Traits::strand_t;
363 using stream_socket_t =
typename Traits::stream_socket_t;
369 connection_id_t conn_id,
371 stream_socket_t && socket,
373 connection_settings_handle_t< Traits > settings,
375 endpoint_t remote_endpoint,
377 lifetime_monitor_t lifetime_monitor )
398 "[connection:{}] start connection with {}" ),
416 "[connection:{}] destructor called" ),
451 "[connection:{}] prepare connection error: {}" ),
465 "[connection:{}] start waiting for request" ),
499 connection_settings_handle_t< Traits > settings,
500 stream_socket_t socket,
501 lifetime_monitor_t lifetime_monitor )
533 "[connection:{}] continue reading request" ),
556 "[connection:{}] skip read operation: already running" ),
564 after_read(
const asio_ns::error_code & ec, std::size_t length )
noexcept 576 "[connection:{}] received {} bytes" ),
590 "[connection:{}] unexpected exception during the " 591 "handling of incoming data: {}" ),
607 "[connection:{}] read socket error: {}; " 608 "parsed bytes: {}" ),
623 "[connection:{}] EOF and no request, " 624 "close connection" ),
657 "[connection:{}] unexpected parser behavior: " 658 "llhttp_execute() reports parsed bytes number ({}) " 659 "is greater than the size of a buffer ({})" 660 "that was fed to the parser" ),
695 "[connection:{}] parser error {}: {}" ),
742 "[connection:{}] request received (#{}): {} {}" ),
799 "[connection:{}] upgrade request received: {} {}; " 823 "[connection:{}] upgrade request happened to " 824 "be a pipelined one, " 825 "and will be handled after previous requests " 840 "[connection:{}] error while handling request: {}" ),
866 "[connection:{}] handle upgrade request (#{}): {} {}" ),
916 "[connection:{}] upgrade request handler rejects " 917 "request, but socket was moved out from connection" ),
945 request_id_t request_id,
947 response_output_flags_t response_output_flags,
949 write_group_t wg )
override 974 "[connection:{}] unable to handle response: {}" ),
986 request_id_t request_id,
988 response_output_flags_t response_output_flags,
1004 "[connection:{}] notificator error: {}" ),
1035 "[connection:{}] append response (#{}), " 1036 "flags: {}, write group size: {}" ),
1055 "[connection:{}] receive response parts for " 1056 "request (#{}), but response with connection-close " 1057 "attribute happened before" ),
1069 "[connection:{}] try to write response, " 1070 "while socket is closed" ),
1108 "[connection:{}] start next write group for response (#{}), " 1140 "[connection:{}] start response (#{}): {}" ),
1203 "[connection:{}] handle_current_write_ctx failed: {}" ),
1222 "[connection:{}] sending resp data with " 1223 "connection-close attribute " 1240 "[connection:{}] sending resp data, " 1264 "[connection:{}] outgoing data was " 1286 "[connection:{}] sending resp file data with " 1287 "connection-close attribute, " 1302 "[connection:{}] sending resp file data, total size: {}" ),
1339 "[connection:{}] file data was sent: " 1351 "[connection:{}] send file data error: " 1371 "[connection:{}] finishing current write group" ),
1383 "[connection:{}] should keep alive" ),
1444 "[connection:{}] last sent response was marked " 1486 "[connection:{}] unable to write: {}" ),
1503 "[connection:{}] notificator error: {}" ),
1529 "connection.socket.shutdown",
1538 "connection.socket.close",
1547 "[connection:{}] close: close socket" ),
1558 "[connection:{}] close: timer canceled" ),
1568 "[connection:{}] close: reset responses data" ),
1588 template<
typename Message_Builder >
1654 "[connection: {}] unexpected " 1655 "error during timeout handling: {}" ),
1664 using timout_cb_t =
void (connection_t::* )(
void );
1709 std::chrono::steady_clock::time_point timeout_after,
1710 timout_cb_t timout_cb )
1718 std::chrono::steady_clock::duration timeout,
1719 timout_cb_t timout_cb )
1830 template <
typename Traits >
1834 using logger_t =
typename Traits::logger_t;
1835 using stream_socket_t =
typename Traits::stream_socket_t;
1840 connection_settings_handle_t< Traits > connection_settings,
1841 std::unique_ptr< socket_options_setter_t > socket_options_setter )
1853 stream_socket_t socket,
1854 endpoint_t remote_endpoint,
1855 lifetime_monitor_t lifetime_monitor )
1860 socket_options_t options{ socket.lowest_layer() };
1861 (*m_socket_options_setter)( options );
1864 return std::make_shared< connection_type_t >(
1865 m_connection_id_counter++,
1866 std::move( socket ),
1867 m_connection_settings,
1868 std::move( remote_endpoint ),
1869 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.
chunk_ext_params_unique_ptr_t m_chunk_ext_params
Chunk extnsion's params if any.
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
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.
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, std::optional< Value_Type > >::type opt_value(const Parameter_Container ¶ms, string_view_t key)
Gets the value of a parameter specified by key wrapped in std::optional<Value_Type> if parameter exis...
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.
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.
llhttp_settings_t create_parser_settings() noexcept
Helper for setting parser settings.
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)
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...
connection_id_t m_connection_id_counter
bool m_init_read_after_this_write
connection_t(const connection_t &)=delete
std::size_t m_bytes_parsed
How many bytes were parsed for current request.
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.
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.
Parsing result context for using in parser callbacks.
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.
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.