MySQL 8.4.5
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
connection.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_HTTP_SRC_HTTP_SERVER_CONNECTION_H_
27#define ROUTER_SRC_HTTP_SRC_HTTP_SERVER_CONNECTION_H_
28
29#include <map>
30#include <utility>
31
34#include "http/server/request.h"
36#include "mysqlrouter/uri.h"
37
38namespace http {
39namespace server {
40
41template <typename Socket>
43 public:
45 using SessionId = uint32_t;
47
48 public:
51 ConnectionStatusCallbacks *connection_handler)
52 : Parent(std::move(s), allowed_method, connection_handler,
53 CNO_CONNECTION_KIND::CNO_SERVER, CNO_HTTP_VERSION::CNO_HTTP1),
54 request_handler_{rhi} {}
55
56 private:
57 int on_settings() override {
58 // Server doesn't need to synchronize to settings, it receives settings as
59 // part of the request.
60 return 0;
61 }
62
63 int on_cno_message_body(const uint32_t session_id, const char *data,
64 const size_t size) override {
65 // We can blindly use session_id with the map because
66 // the map was already initialized in `on_cno_message_head` call.
67 // The 'cno' executes callbacks in following order:
68 //
69 // * on_cno_message_head
70 // * on_cno_message_body
71 // * on_cno_message_tail
72 // * on_cno_stream_end
73 sessions_[session_id].get_data().input_body_.get().append(data, size);
74 return 0;
75 }
76
77 int on_cno_message_tail(const uint32_t session_id,
78 [[maybe_unused]] const cno_tail_t *tail) override {
79 if (request_handler_) {
80 request_handler_->route(sessions_[session_id]);
81 }
82
83 return 0;
84 }
85
86 int on_cno_stream_end(const uint32_t id) override {
87 sessions_.erase(id);
88 return 0;
89 }
90
91 int on_cno_message_head(const uint32_t session_id,
92 const cno_message_t *msg) override {
94 first_request_ = false;
95 const auto method_pos =
97
98 http::base::Headers input_headers;
99 const auto path = cno::to_string(msg->path);
100 cno::Sequence<const cno_header_t> sequence{msg->headers, msg->headers_len};
101
102 for (const auto &header : sequence) {
103 input_headers.add(cno::to_string(header.name),
104 cno::to_string(header.value));
105 }
106
107 if (!(*Parent::allowed_method_)[method_pos]) {
108 ServerRequest(this, session_id, (base::method::key_type)(1 << method_pos),
109 "", std::move(input_headers))
111 return 1;
112 }
113
114 sessions_.erase(session_id);
115 try {
116 auto pair =
117 sessions_.try_emplace(session_id, this, session_id,
118 (base::method::key_type)(1 << method_pos), path,
119 std::move(input_headers));
120
121 char buffer[90];
123 sizeof(buffer));
124 pair.first->second.get_output_headers().add("Date", buffer);
125 pair.first->second.get_output_headers().add(
126 "Content-Type", "text/html; charset=ISO-8859-1");
127 } catch (...) {
128 ServerRequest(this, session_id, (base::method::key_type)(1 << method_pos),
129 "", std::move(input_headers))
131 return 1;
132 }
133
134 return 0;
135 }
136
137 bool first_request_{true};
138 std::map<SessionId, ServerRequest> sessions_;
140};
141
142} // namespace server
143} // namespace http
144
145#endif // ROUTER_SRC_HTTP_SRC_HTTP_SERVER_CONNECTION_H_
Definition: connection_status_callbacks.h:35
Definition: connection.h:102
headers of a HTTP response/request.
Definition: headers.h:43
virtual void add(const std::string_view &key, std::string &&value)
Definition: headers.cc:48
Definition: buffer_sequence.h:39
Definition: request_handler_interface.h:34
virtual void route(http::base::Request &request)=0
Definition: connection.h:42
typename Parent::ConnectionStatusCallbacks ConnectionStatusCallbacks
Definition: connection.h:46
int on_cno_message_head(const uint32_t session_id, const cno_message_t *msg) override
Definition: connection.h:91
int on_cno_message_body(const uint32_t session_id, const char *data, const size_t size) override
Definition: connection.h:63
ServerConnection(Socket s, base::method::Bitset *allowed_method, RequestHandlerInterface *rhi, ConnectionStatusCallbacks *connection_handler)
Definition: connection.h:49
RequestHandlerInterface * request_handler_
Definition: connection.h:139
bool first_request_
Definition: connection.h:137
uint32_t SessionId
Definition: connection.h:45
int on_cno_stream_end(const uint32_t id) override
Definition: connection.h:86
int on_cno_message_tail(const uint32_t session_id, const cno_tail_t *tail) override
Definition: connection.h:77
int on_settings() override
Definition: connection.h:57
std::map< SessionId, ServerRequest > sessions_
Definition: connection.h:138
a HTTP request and response.
Definition: request.h:46
void send_error(StatusType status_code) override
Definition: request.cc:76
Definition: socket.h:1090
static char * path
Definition: mysqldump.cc:149
std::bitset< Pos::_LAST+1 > Bitset
Definition: method.h:57
HTTP_COMMON_EXPORT pos_type from_string_to_post(const std::string_view &method)
Definition: method.cc:50
int key_type
Definition: method.h:38
constexpr key_type NotImplemented
Definition: status_code.h:94
constexpr key_type BadRequest
Definition: status_code.h:64
HTTP_COMMON_EXPORT int time_to_rfc5322_fixdate(time_t ts, char *date_buf, size_t date_buf_len)
convert time_t into a Date: header value.
Definition: http_time.cc:39
std::string to_string(const T &str)
Convert CNO buffers to strings.
Definition: string.h:42
HTTP_SERVER_LIB_EXPORT std::atomic< uint64_t > http_connections_reused
Definition: server.cc:43
Definition: connection.h:55
size_t size(const char *const c)
Definition: base64.h:46
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
Definition: gcs_xcom_synode.h:64
Definition: server_struct.h:39
long long sequence(UDF_INIT *initid, UDF_ARGS *args, unsigned char *, unsigned char *)
Definition: udf_example.cc:568