Line data Source code
1 : #ifndef THINGER_HTTP_FRAME_HPP
2 : #define THINGER_HTTP_FRAME_HPP
3 :
4 : #include <cstdint>
5 : #include <functional>
6 : #include "../data/out_data.hpp"
7 : #include "body_logging.hpp"
8 :
9 : namespace thinger::http {
10 :
11 : // What a frame is on the wire: the head of a message, an interim response (100 Continue),
12 : // or part of a body
13 : enum class frame_kind : uint8_t { head, interim, data };
14 :
15 : class http_frame : public data::out_data {
16 : public:
17 : // Called once with whether the frame was written to its connection: false if the
18 : // write failed, or if the frame is dropped without being written
19 : using written_callback = std::function<void(bool written)>;
20 :
21 : // constructors
22 582097 : http_frame() = default;
23 : ~http_frame() override;
24 :
25 : // A copy is another frame: it is not written when this one is (no written callback)
26 : http_frame(const http_frame& other) : data::out_data(other), last_frame_(other.last_frame_) {}
27 : http_frame& operator=(const http_frame& other) {
28 : data::out_data::operator=(other);
29 : last_frame_ = other.last_frame_;
30 : return *this;
31 : }
32 :
33 : // Moved, as copied: the written callback is not moved (so a message can be moved, not
34 : // copied, into another)
35 2597 : http_frame(http_frame&& other) noexcept : data::out_data(other), last_frame_(other.last_frame_) {}
36 : http_frame& operator=(http_frame&& other) noexcept {
37 : data::out_data::operator=(other);
38 : last_frame_ = other.last_frame_;
39 : return *this;
40 : }
41 :
42 : // What the frame is (see frame_kind)
43 : virtual frame_kind kind() const = 0;
44 :
45 : // stream information
46 : void set_last_frame(bool last_frame);
47 : virtual bool end_stream();
48 :
49 : // Notify `callback` once the frame is written (see written_callback)
50 : void on_written(written_callback callback);
51 :
52 : // Report the frame written (or failed) to the callback set with on_written(), once
53 : void notify_written(bool written);
54 :
55 : // Log the frame (see body_logging): nothing by default, as for body data
56 : virtual void log(const char* scope, const body_logging& options = {}) const;
57 :
58 : private:
59 : bool last_frame_ = true;
60 : written_callback on_written_;
61 :
62 : };
63 :
64 : }
65 :
66 : #endif
|