Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
http2_server_stream.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include "frame.h"
8#include "hpack.h"
9#include "http2_client.h"
10#include "http2_request.h"
12
13#include <cstdint>
14#include <functional>
15#include <memory>
16#include <mutex>
17#include <string_view>
18#include <vector>
19
21{
22 // Forward declaration
23 class http2_connection;
24
51 {
52 public:
54 using frame_sender_t = std::function<VoidResult(const frame&)>;
55
66 std::shared_ptr<hpack_encoder> encoder,
67 frame_sender_t frame_sender,
68 uint32_t max_frame_size = 16384);
69
74
75 // Non-copyable, non-movable (contains mutex)
80
91 auto send_headers(int status_code,
92 const std::vector<http_header>& headers,
93 bool end_stream = false) -> VoidResult;
94
104 auto send_data(const std::vector<uint8_t>& data,
105 bool end_stream = false) -> VoidResult;
106
113 auto send_data(std::string_view data, bool end_stream = false) -> VoidResult;
114
124 auto start_response(int status_code,
125 const std::vector<http_header>& headers) -> VoidResult;
126
134 auto write(const std::vector<uint8_t>& chunk) -> VoidResult;
135
142 auto end_response() -> VoidResult;
143
151 auto reset(uint32_t err_code = static_cast<uint32_t>(error_code::cancel))
152 -> VoidResult;
153
158 [[nodiscard]] auto stream_id() const -> uint32_t;
159
164 [[nodiscard]] auto method() const -> std::string_view;
165
170 [[nodiscard]] auto path() const -> std::string_view;
171
176 [[nodiscard]] auto headers() const -> const std::vector<http_header>&;
177
182 [[nodiscard]] auto request() const -> const http2_request&;
183
188 [[nodiscard]] auto is_open() const -> bool;
189
194 [[nodiscard]] auto headers_sent() const -> bool;
195
200 [[nodiscard]] auto state() const -> stream_state;
201
206 auto update_window(int32_t increment) -> void;
207
212 [[nodiscard]] auto window_size() const -> int32_t;
213
214 private:
221 auto build_response_headers(int status_code,
222 const std::vector<http_header>& additional)
223 -> std::vector<http_header>;
224
225 uint32_t stream_id_;
227 std::shared_ptr<hpack_encoder> encoder_;
230
232 bool headers_sent_{false};
233 int32_t window_size_{65535};
234
235 mutable std::mutex mutex_;
236 };
237
238} // namespace kcenon::network::protocols::http2
Base class for HTTP/2 frames.
Definition frame.h:82
HPACK header encoder (RFC 7541)
Definition hpack.h:159
Server-side HTTP/2 stream for sending responses.
std::shared_ptr< hpack_encoder > encoder_
HPACK encoder.
auto headers_sent() const -> bool
Check if headers have been sent.
auto state() const -> stream_state
Get current stream state.
http2_server_stream(const http2_server_stream &)=delete
std::function< VoidResult(const frame &)> frame_sender_t
Function type for sending frames.
auto method() const -> std::string_view
Get request method.
auto send_headers(int status_code, const std::vector< http_header > &headers, bool end_stream=false) -> VoidResult
Send response headers.
auto is_open() const -> bool
Check if stream is open for sending.
auto start_response(int status_code, const std::vector< http_header > &headers) -> VoidResult
Start a streaming response.
auto path() const -> std::string_view
Get request path.
http2_server_stream & operator=(const http2_server_stream &)=delete
http2_server_stream(http2_server_stream &&)=delete
auto reset(uint32_t err_code=static_cast< uint32_t >(error_code::cancel)) -> VoidResult
Reset the stream with an error code.
auto build_response_headers(int status_code, const std::vector< http_header > &additional) -> std::vector< http_header >
Build response headers with :status pseudo-header.
auto send_data(const std::vector< uint8_t > &data, bool end_stream=false) -> VoidResult
Send response data.
auto end_response() -> VoidResult
End the streaming response.
auto headers() const -> const std::vector< http_header > &
Get request headers.
http2_server_stream(uint32_t stream_id, http2_request request, std::shared_ptr< hpack_encoder > encoder, frame_sender_t frame_sender, uint32_t max_frame_size=16384)
Construct server stream.
auto request() const -> const http2_request &
Get the full request.
http2_server_stream & operator=(http2_server_stream &&)=delete
auto stream_id() const -> uint32_t
Get stream identifier.
auto write(const std::vector< uint8_t > &chunk) -> VoidResult
Write data chunk for streaming response.
auto update_window(int32_t increment) -> void
Update flow control window.
auto window_size() const -> int32_t
Get available window size.
stream_state
HTTP/2 stream state (RFC 7540 Section 5.1)
Result< std::monostate > VoidResult
Network-specific error and result type definitions.
HTTP header name-value pair.
Definition hpack.h:24