Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
HttpConnection.h
浏览该文件的文档.
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
68 virtual void writeBlock(const char *buffer, size_t length,
69 const std::function<void(const asio::error_code &code)> &callback) = 0;
70
74 virtual void asyncReadSome(const asio::mutable_buffers_1 &buffer,
75 const std::function<void(const asio::error_code &error, std::size_t bytes_transferred)> &
76 callback) = 0;
77
80 virtual void checkKeepalive() = 0;
81
84 virtual void disponse();
85
86 void writeSingleRange();
87
88 void writeRanges();
89};
90
93 using Ptr = std::shared_ptr<HttpConnectionImpl>;
94 using Socket = asio::ip::tcp::socket;
95 using SharedSocket = std::shared_ptr<Socket>;
96
97 Ptr getPtr() { return std::reinterpret_pointer_cast<HttpConnectionImpl>(shared_from_this()); } // NOLINT
98
100
101 HttpConnectionImpl(const std::shared_ptr<HttpServiceImpl> &service, asio::io_context &context,
103
104 void writeBlock(const char *buffer, size_t length,
105 const std::function<void(const asio::error_code &code)> &callback) override;
106
107 void asyncReadSome(const asio::mutable_buffers_1 &buffer,
108 const std::function<void(const asio::error_code &error, std::size_t bytes_transferred)> &
109 callback) override;
110
111 void checkKeepalive() override;
112};
113
116 using Ptr = std::shared_ptr<HttpsConnectionImpl>;
117 using Stream = asio::ssl::stream<asio::ip::tcp::socket>;
118 using SharedStream = std::shared_ptr<Stream>;
119
120 Ptr getPtr() { return std::reinterpret_pointer_cast<HttpsConnectionImpl>(shared_from_this()); } // NOLINT
121
123
124 HttpsConnectionImpl(const std::shared_ptr<HttpServiceImpl> &service, asio::io_context &context,
126
127 ~HttpsConnectionImpl() override;
128
129 void writeBlock(const char *buffer, size_t length,
130 const std::function<void(const asio::error_code &code)> &callback) override;
131
132 void asyncReadSome(const asio::mutable_buffers_1 &buffer,
133 const std::function<void(const asio::error_code &error, std::size_t bytes_transferred)> &
134 callback) override;
135
136 void checkKeepalive() override;
137};
138
139}