Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
HttpConnection.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <asio.hpp>
18#include <asio/ssl/stream.hpp>
19
21#include <sese/net/http/Range.h>
22#include <sese/io/File.h>
23
25
27class HttpServiceImpl;
28
30struct HttpConnection : Handleable, std::enable_shared_from_this<HttpConnection> {
31 using Ptr = std::shared_ptr<HttpConnection>;
32
33 Ptr getPtr() { return shared_from_this(); } // NOLINT
34
35 HttpConnection(const std::shared_ptr<HttpServiceImpl> &service, asio::io_context &io_context, const sese::net::IPAddress::Ptr &addr);
36
37 virtual ~HttpConnection() = default;
38
39 asio::system_timer timer;
40
41 void reset();
42
46 bool is0x0a = false;
48 std::unique_ptr<iocp::IOBufNode> node;
50
51 std::weak_ptr<HttpServiceImpl> service;
52
53 void readHeader();
54
55 void readBody();
56
57 void handleRequest();
58
59 void writeHeader();
60
61 void writeBody();
62
69 virtual void writeBlock(const char *buffer, size_t length,
70 const std::function<void(const asio::error_code &code)> &callback) = 0;
71
75 virtual void asyncReadSome(const asio::mutable_buffers_1 &buffer,
76 const std::function<void(const asio::error_code &error, std::size_t bytes_transferred)> &
77 callback) = 0;
78
81 virtual void checkKeepalive() = 0;
82
85 virtual void disponse();
86
87 void writeSingleRange();
88
89 void writeRanges();
90};
91
94 using Ptr = std::shared_ptr<HttpConnectionImpl>;
95 using Socket = asio::ip::tcp::socket;
96 using SharedSocket = std::shared_ptr<Socket>;
97
98 Ptr getPtr() { return std::reinterpret_pointer_cast<HttpConnectionImpl>(shared_from_this()); } // NOLINT
99
101
102 HttpConnectionImpl(const std::shared_ptr<HttpServiceImpl> &service, asio::io_context &context,
104
105 void writeBlock(const char *buffer, size_t length,
106 const std::function<void(const asio::error_code &code)> &callback) override;
107
108 void asyncReadSome(const asio::mutable_buffers_1 &buffer,
109 const std::function<void(const asio::error_code &error, std::size_t bytes_transferred)> &
110 callback) override;
111
112 void checkKeepalive() override;
113};
114
117 using Ptr = std::shared_ptr<HttpsConnectionImpl>;
118 using Stream = asio::ssl::stream<asio::ip::tcp::socket>;
119 using SharedStream = std::shared_ptr<Stream>;
120
121 Ptr getPtr() { return std::reinterpret_pointer_cast<HttpsConnectionImpl>(shared_from_this()); } // NOLINT
122
124
125 HttpsConnectionImpl(const std::shared_ptr<HttpServiceImpl> &service, asio::io_context &context,
127
128 ~HttpsConnectionImpl() override;
129
130 void writeBlock(const char *buffer, size_t length,
131 const std::function<void(const asio::error_code &code)> &callback) override;
132
133 void asyncReadSome(const asio::mutable_buffers_1 &buffer,
134 const std::function<void(const asio::error_code &error, std::size_t bytes_transferred)> &
135 callback) override;
136
137 void checkKeepalive() override;
138};
139
140}