Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
ResponseWriter.hpp
Go to the documentation of this file.
1#pragma once
4#include <string_view>
5#include <atomic>
6#include <cstdint>
7#include <exception>
8#include <functional>
9#include <memory>
10#include <mutex>
11#include <vector>
12#include <orbit/http/json.hpp>
13
14namespace orbit::network { class Proactor; }
15namespace orbit::concurrency { class ThreadPool; }
16
17namespace orbit::http {
18
24public:
25 virtual ~ResponseWriter() = default;
26
27 using Interceptor = std::function<void(HttpResponse&)>;
32 virtual void add_interceptor(Interceptor interceptor) = 0;
33
39 virtual void set_header(const std::string& key, const std::string& value) = 0;
40
46
52
58 virtual void send(HttpResponse&& response) = 0;
59
65 virtual void send_headers(HttpResponse& response) = 0;
66
71 virtual void write_chunk(std::string_view chunk) = 0;
72
76 virtual void end() = 0;
77
84 virtual void send_sse_event(std::string_view data, std::string_view event = "", std::string_view id = "") = 0;
85
91 virtual void upgrade_to_raw_stream(std::function<void(std::string_view)> on_data, std::function<void()> on_close) = 0;
92
98 virtual void read_body_stream(std::function<void(std::string_view)> on_data, std::function<void()> on_end) = 0;
99
107 bool has_responded() const { return responded_.load(); }
108
116 using ErrorSink = std::function<void(std::exception_ptr, std::shared_ptr<ResponseWriter>)>;
118 std::lock_guard<std::mutex> lock(error_sink_mutex_);
119 error_sink_ = std::move(sink);
120 }
121
125 uint64_t request_generation() const { return generation_.load(); }
126
135 static void report_async_exception(const std::shared_ptr<ResponseWriter>& writer, uint64_t generation,
136 std::exception_ptr error) noexcept;
137
146 bool is_open() const { return !closed_.load(); }
147
156 void on_close(std::function<void()> callback);
157
158protected:
160 void mark_responded() { responded_ = true; }
164 responded_ = false;
165 ++generation_;
166 std::lock_guard<std::mutex> lock(error_sink_mutex_);
167 error_sink_ = nullptr;
168 }
171 void mark_closed() noexcept;
172
173private:
174 std::atomic<bool> responded_{false};
175 std::atomic<uint64_t> generation_{0};
178 std::atomic<bool> closed_{false};
179 std::mutex close_mutex_;
180 std::vector<std::function<void()>> close_callbacks_;
181};
182
183} // namespace http
Definition ThreadPool.hpp:13
Represents an HTTP response to be sent to a client.
Definition HttpResponse.hpp:107
Abstract interface for writing HTTP responses.
Definition ResponseWriter.hpp:23
virtual void send_sse_event(std::string_view data, std::string_view event="", std::string_view id="")=0
Sends a Server-Sent Events (SSE) message.
virtual void upgrade_to_raw_stream(std::function< void(std::string_view)> on_data, std::function< void()> on_close)=0
Upgrades the connection to a raw bi-directional byte stream.
std::mutex close_mutex_
Definition ResponseWriter.hpp:179
std::atomic< bool > closed_
Definition ResponseWriter.hpp:178
static void report_async_exception(const std::shared_ptr< ResponseWriter > &writer, uint64_t generation, std::exception_ptr error) noexcept
Reports an exception from asynchronous handler code.
std::atomic< bool > responded_
Definition ResponseWriter.hpp:174
std::vector< std::function< void()> > close_callbacks_
Definition ResponseWriter.hpp:180
virtual void write_chunk(std::string_view chunk)=0
Streams a chunk of data (for Chunked Transfer Encoding).
void mark_responded()
Implementations call this whenever they write part of a response.
Definition ResponseWriter.hpp:160
virtual ~ResponseWriter()=default
void set_error_sink(ErrorSink sink)
Definition ResponseWriter.hpp:117
virtual void send_headers(HttpResponse &response)=0
Sends only the HTTP headers.
std::function< void(std::exception_ptr, std::shared_ptr< ResponseWriter >)> ErrorSink
Receives an exception that escaped asynchronous handler code, such as a coroutine handler that threw ...
Definition ResponseWriter.hpp:116
virtual concurrency::ThreadPool & thread_pool()=0
Gets the thread pool to offload blocking tasks.
void reset_responded()
Definition ResponseWriter.hpp:163
virtual void set_header(const std::string &key, const std::string &value)=0
Adds a default header that will be included in the final response.
ErrorSink error_sink_
Definition ResponseWriter.hpp:177
std::mutex error_sink_mutex_
Definition ResponseWriter.hpp:176
virtual void add_interceptor(Interceptor interceptor)=0
Adds an interceptor to modify the response before it is sent.
uint64_t request_generation() const
Definition ResponseWriter.hpp:125
virtual network::Proactor & proactor()=0
Gets the underlying Proactor to dispatch async operations.
virtual void read_body_stream(std::function< void(std::string_view)> on_data, std::function< void()> on_end)=0
Asynchronously streams the incoming HTTP request body.
virtual void end()=0
Ends a chunked response stream.
void mark_closed() noexcept
void on_close(std::function< void()> callback)
Runs callback once when the writer closes (see is_open()), or right away if it already has.
std::atomic< uint64_t > generation_
Definition ResponseWriter.hpp:175
virtual void send(HttpResponse &&response)=0
Sends a complete HTTP response.
std::function< void(HttpResponse &)> Interceptor
Definition ResponseWriter.hpp:27
bool is_open() const
False once the client can no longer receive anything: the connection (HTTP/1.1) or stream (HTTP/2,...
Definition ResponseWriter.hpp:146
bool has_responded() const
True once anything of a response to the current request has been sent: a full response,...
Definition ResponseWriter.hpp:107
Definition Proactor.hpp:9
Definition Awaitable.hpp:10
Definition Task.hpp:10
Definition Http2Session.hpp:21
Definition json.hpp:5363