MySQL 9.1.0
Source Code Documentation
connection.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, 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
37namespace http {
38namespace server {
39
40template <typename Socket>
42 public:
44 using SessionId = uint32_t;
46
47 public:
50 ConnectionStatusCallbacks *connection_handler)
51 : Parent(std::move(s), allowed_method, connection_handler,
52 CNO_CONNECTION_KIND::CNO_SERVER, CNO_HTTP_VERSION::CNO_HTTP1),
53 request_handler_{rhi} {}
54
55 private:
56 int on_settings() override {
57 // Server doesn't need to synchronize to settings, it receives settings as
58 // part of the request.
59 return 0;
60 }
61
62 int on_cno_message_body(const uint32_t session_id, const char *data,
63 const size_t size) override {
64 // We can blindly use session_id with the map because
65 // the map was already initialized in `on_cno_message_head` call.
66 // The 'cno' executes callbacks in following order:
67 //
68 // * on_cno_message_head
69 // * on_cno_message_body
70 // * on_cno_message_tail
71 // * on_cno_stream_end
72 sessions_[session_id].get_data().input_body_.get().append(data, size);
73 return 0;
74 }
75
76 int on_cno_message_tail(const uint32_t session_id,
77 [[maybe_unused]] const cno_tail_t *tail) override {
78 if (request_handler_) {
79 request_handler_->route(sessions_[session_id]);
80 }
81
82 return 0;
83 }
84
85 int on_cno_stream_end(const uint32_t id) override {
86 sessions_.erase(id);
87 return 0;
88 }
89
90 int on_cno_message_head(const uint32_t session_id,
91 const cno_message_t *msg) override {
93 first_request_ = false;
94 const auto method_pos =
96
97 http::base::Headers input_headers;
98 const auto path = cno::to_string(msg->path);
99 cno::Sequence<const cno_header_t> sequence{msg->headers, msg->headers_len};
100
101 for (const auto &header : sequence) {
102 input_headers.add(cno::to_string(header.name),
103 cno::to_string(header.value));
104 }
105
106 if (!(*Parent::allowed_method_)[method_pos]) {
107 ServerRequest(this, session_id, (base::method::key_type)(1 << method_pos),
108 path, std::move(input_headers))
110 return 1;
111 }
112
113 sessions_.erase(session_id);
114 auto pair = sessions_.try_emplace(session_id, this, session_id,
115 (base::method::key_type)(1 << method_pos),
116 path, std::move(input_headers));
117
118 char buffer[90];
119 http::base::time_to_rfc5322_fixdate(time(nullptr), buffer, sizeof(buffer));
120 pair.first->second.get_output_headers().add("Date", buffer);
121 pair.first->second.get_output_headers().add(
122 "Content-Type", "text/html; charset=ISO-8859-1");
123
124 return 0;
125 }
126
127 bool first_request_{true};
128 std::map<SessionId, ServerRequest> sessions_;
130};
131
132} // namespace server
133} // namespace http
134
135#endif // ROUTER_SRC_HTTP_SRC_HTTP_SERVER_CONNECTION_H_
Definition: connection_status_callbacks.h:35
Definition: connection.h:103
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:36
virtual void route(http::base::Request &request)=0
Definition: connection.h:41
typename Parent::ConnectionStatusCallbacks ConnectionStatusCallbacks
Definition: connection.h:45
int on_cno_message_head(const uint32_t session_id, const cno_message_t *msg) override
Definition: connection.h:90
int on_cno_message_body(const uint32_t session_id, const char *data, const size_t size) override
Definition: connection.h:62
ServerConnection(Socket s, base::method::Bitset *allowed_method, RequestHandlerInterface *rhi, ConnectionStatusCallbacks *connection_handler)
Definition: connection.h:48
RequestHandlerInterface * request_handler_
Definition: connection.h:129
bool first_request_
Definition: connection.h:127
uint32_t SessionId
Definition: connection.h:44
int on_cno_stream_end(const uint32_t id) override
Definition: connection.h:85
int on_cno_message_tail(const uint32_t session_id, const cno_tail_t *tail) override
Definition: connection.h:76
int on_settings() override
Definition: connection.h:56
std::map< SessionId, ServerRequest > sessions_
Definition: connection.h:128
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
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:56
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