MySQL 9.0.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),
53 request_handler_{rhi} {}
54
55 private:
56 int on_cno_message_body(const uint32_t session_id, const char *data,
57 const size_t size) override {
58 // We can blindly use session_id with the map because
59 // the map was already initialized in `on_cno_message_head` call.
60 // The 'cno' executes callbacks in following order:
61 //
62 // * on_cno_message_head
63 // * on_cno_message_body
64 // * on_cno_message_tail
65 // * on_cno_stream_end
66 sessions_[session_id].get_data().input_body_.get().append(data, size);
67 return 0;
68 }
69
70 int on_cno_message_tail(const uint32_t session_id,
71 [[maybe_unused]] const cno_tail_t *tail) override {
72 if (request_handler_) {
73 request_handler_->route(sessions_[session_id]);
74 }
75
76 return 0;
77 }
78
79 int on_cno_stream_end(const uint32_t id) override {
80 sessions_.erase(id);
81 return 0;
82 }
83
84 int on_cno_message_head(const uint32_t session_id,
85 const cno_message_t *msg) override {
87 first_request_ = false;
88 const auto method_pos =
90
91 http::base::Headers input_headers;
92 const auto path = cno::to_string(msg->path);
93 cno::Sequence<const cno_header_t> sequence{msg->headers, msg->headers_len};
94
95 for (const auto &header : sequence) {
96 input_headers.add(cno::to_string(header.name),
97 cno::to_string(header.value));
98 }
99
100 if (!(*Parent::allowed_method_)[method_pos]) {
101 ServerRequest(this, session_id, (base::method::key_type)(1 << method_pos),
102 path, std::move(input_headers))
104 return 1;
105 }
106
107 sessions_.erase(session_id);
108 auto pair = sessions_.try_emplace(session_id, this, session_id,
109 (base::method::key_type)(1 << method_pos),
110 path, std::move(input_headers));
111
112 char buffer[90];
113 http::base::time_to_rfc5322_fixdate(time(nullptr), buffer, sizeof(buffer));
114 pair.first->second.get_output_headers().add("Date", buffer);
115 pair.first->second.get_output_headers().add(
116 "Content-Type", "text/html; charset=ISO-8859-1");
117
118 return 0;
119 }
120
121 bool first_request_{true};
122 std::map<SessionId, ServerRequest> sessions_;
124};
125
126} // namespace server
127} // namespace http
128
129#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:34
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:84
int on_cno_message_body(const uint32_t session_id, const char *data, const size_t size) override
Definition: connection.h:56
ServerConnection(Socket s, base::method::Bitset *allowed_method, RequestHandlerInterface *rhi, ConnectionStatusCallbacks *connection_handler)
Definition: connection.h:48
RequestHandlerInterface * request_handler_
Definition: connection.h:123
bool first_request_
Definition: connection.h:121
uint32_t SessionId
Definition: connection.h:44
int on_cno_stream_end(const uint32_t id) override
Definition: connection.h:79
int on_cno_message_tail(const uint32_t session_id, const cno_tail_t *tail) override
Definition: connection.h:70
std::map< SessionId, ServerRequest > sessions_
Definition: connection.h:122
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:40
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