11 #include <restinio/asio_include.hpp> 13 #include <http_parser.h> 15 #include <fmt/format.h> 17 #include <restinio/exception.hpp> 18 #include <restinio/http_headers.hpp> 19 #include <restinio/request_handler.hpp> 20 #include <restinio/impl/connection_base.hpp> 21 #include <restinio/impl/header_helpers.hpp> 22 #include <restinio/impl/response_coordinator.hpp> 23 #include <restinio/impl/connection_settings.hpp> 24 #include <restinio/impl/fixed_buffer.hpp> 25 #include <restinio/impl/write_group_output_ctx.hpp> 26 #include <restinio/impl/executor_wrapper.hpp> 27 #include <restinio/impl/sendfile_operation.hpp> 29 #include <restinio/utils/impl/safe_uint_truncate.hpp> 67 m_header = http_request_header_t{};
69 m_current_field_name.clear();
86 template<
typename Http_Methods >
178 http_parser_init( &m_parser, HTTP_REQUEST);
181 m_parser_ctx.reset();
182 m_parser.data = &m_parser_ctx;
186 template <
typename Connection,
typename Start_Read_CB,
typename Failed_CB >
191 Start_Read_CB start_read_cb,
217 template <
typename Traits >
225 using timer_manager_t =
typename Traits::timer_manager_t;
226 using timer_guard_t =
typename timer_manager_t::timer_guard_t;
227 using request_handler_t =
typename Traits::request_handler_t;
228 using logger_t =
typename Traits::logger_t;
229 using strand_t =
typename Traits::strand_t;
230 using stream_socket_t =
typename Traits::stream_socket_t;
234 connection_id_t conn_id,
236 stream_socket_t && socket,
238 connection_settings_handle_t< Traits > settings,
240 endpoint_t remote_endpoint )
255 "[connection:{}] start connection with {}",
273 "[connection:{}] destructor called",
306 "[connection:{}] prepare connection error: {}",
319 "[connection:{}] start waiting for request",
353 connection_settings_handle_t< Traits > settings,
354 stream_socket_t socket )
381 "[connection:{}] continue reading request",
402 "[connection:{}] skip read operation: already running",
410 after_read(
const asio_ns::error_code & ec, std::size_t length )
416 "[connection:{}] received {} bytes",
434 "[connection:{}] read socket error: {}; " 448 "[connection:{}] EOF and no request, " 489 "[connection:{}] parser error {}: {}",
535 "[connection:{}] request received (#{}): {} {}",
581 "[connection:{}] upgrade request received: {} {}; " 604 "[connection:{}] upgrade request happened to be a pipelined one, " 605 "and will be handled after previous requests are handled",
618 "[connection:{}] error while handling request: {}",
643 "[connection:{}] handle upgrade request (#{}): {} {}",
688 "[connection:{}] upgrade request handler rejects " 689 "request, but socket was moved out from connection",
712 request_id_t request_id,
714 response_output_flags_t response_output_flags,
716 write_group_t wg )
override 737 "[connection:{}] unable to handle response: {}",
749 request_id_t request_id,
751 response_output_flags_t response_output_flags,
766 "[connection:{}] notificator error: {}",
796 "[connection:{}] append response (#{}), " 797 "flags: {}, write group size: {}",
815 "[connection:{}] receive response parts for " 816 "request (#{}), but response with connection-close " 817 "attribute happened before",
828 "[connection:{}] try to write response, " 829 "while socket is closed",
866 "[connection:{}] start next write group for response (#{}), " 899 "[connection:{}] start response (#{}): {}",
958 "[connection:{}] handle_current_write_ctx failed: {}",
976 "[connection:{}] sending resp data with " 977 "connection-close attribute " 993 "[connection:{}] sending resp data, " 1015 "[connection:{}] outgoing data was sent: {} bytes",
1035 "[connection:{}] sending resp file data with " 1036 "connection-close attribute, " 1050 "[connection:{}] sending resp file data, total size: {}",
1079 "[connection:{}] file data was sent: {} bytes",
1088 "[connection:{}] send file data error: {} ({}) bytes",
1107 "[connection:{}] finishing current write group",
1118 "[connection:{}] should keep alive",
1178 "[connection:{}] last sent response was marked " 1215 "[connection:{}] unable to write: {}",
1231 "[connection:{}] notificator error: {}",
1248 "[connection:{}] close",
1260 "[connection:{}] close: close socket",
1269 "[connection:{}] close: timer canceled",
1277 "[connection:{}] close: reset responses data",
1295 template<
typename Message_Builder >
1349 using timout_cb_t =
void (connection_t::* )(
void );
1394 std::chrono::steady_clock::time_point timeout_after,
1395 timout_cb_t timout_cb )
1403 std::chrono::steady_clock::duration timeout,
1404 timout_cb_t timout_cb )
1416 "[connection:{}] {} timed out",
1505 template <
typename Traits >
1509 using logger_t =
typename Traits::logger_t;
1510 using stream_socket_t =
typename Traits::stream_socket_t;
1513 connection_settings_handle_t< Traits > connection_settings,
1514 std::unique_ptr< socket_options_setter_t > socket_options_setter )
1522 stream_socket_t socket,
1523 endpoint_t remote_endpoint )
1526 std::shared_ptr< connection_type_t > result;
1530 socket_options_t options{ socket.lowest_layer() };
1531 (*m_socket_options_setter)( options );
1534 result = std::make_shared< connection_type_t >(
1535 m_connection_id_counter++,
1536 std::move( socket ),
1537 m_connection_settings,
1538 std::move( remote_endpoint ) );
1540 catch(
const std::exception & ex )
1544 "failed to create connection: {}",
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.
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...
write_group_output_ctx_t m_write_output_ctx
Write to socket operation context.
void after_write(const asio_ns::error_code &ec)
Handle write response finished.
void guard_sendfile_operation(std::chrono::steady_clock::duration timelimit)
timout_cb_t m_current_timeout_cb
Callback to all if timeout happened.
void handle_current_write_ctx()
Start/continue/continue handling output data of current write group.
upgrade_internals_t(upgrade_internals_t &&)=default
connection_t & operator=(connection_t &&)=delete
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 after_read(const asio_ns::error_code &ec, std::size_t length)
Handle read operation result.
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.
upgrade_internals_t(connection_settings_handle_t< Traits > settings, stream_socket_t socket)
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.
connection_input_t m_input
Input routine.
void reset()
Prepare context to handle new request.
void handle_xxx_timeout(const char *operation_name)
connection_settings_handle_t< Traits > m_settings
connection_t(connection_id_t conn_id, stream_socket_t &&socket, connection_settings_handle_t< Traits > settings, endpoint_t remote_endpoint)
auto create_new_connection(stream_socket_t socket, endpoint_t remote_endpoint)
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
void cancel_timeout_checking()
Stop timout guarding.
void on_request_message_complete()
Handle a given request message.
Internals that are necessary for upgrade.
void handle_nothing_to_write()
void trigger_error_and_close(Message_Builder msg_builder)
Trigger an error.
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.
void handle_read_timeout()
void handle_file_write_operation(file_write_operation_t &op)
Run sendfile write operation.
void close()
Close connection functions.
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.
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.
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.