Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
WebServer/Interceptor.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Framework_WebServer_Interceptor_h_
5#define _Stroika_Framework_WebServer_Interceptor_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
10
11#include "Stroika/Frameworks/WebServer/Message.h"
12
13/*
14 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
15 */
16
18
19 using namespace Stroika::Foundation;
20
21 /**
22 * \note Inspired by, but fairly different from
23 * @see https://cxf.apache.org/javadoc/latest/org/apache/cxf/interceptor/Interceptor.html
24 *
25 * The basic idea of an interceptor, is that its a handler for web-server requests/messages. These get arranged
26 * in a chain, and each gets a crack at doing something to an incoming message, preparing its
27 * response.
28 *
29 * Router, fault handlers, and logging are all achieved via interceptors (i.e. the Router IS an Interceptor).
30 *
31 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety-For-Envelope-Letter-Internally-Synchronized">C++-Standard-Thread-Safety-For-Envelope-Letter-Internally-Synchronized</a>
32 * But note that HandleMessage() is a const method, so it can safely be called from any number of threads
33 * simultaneously.
34 */
36 protected:
37 class _IRep;
38
39 public:
40 /**
41 * \nb: avoid noexcept in function<> declaration due to https://stackoverflow.com/questions/41293025/stdfunction-with-noexcept-in-c17
42 */
43 Interceptor () = delete;
44 Interceptor (const Interceptor&) = default;
45 Interceptor (Interceptor&&) = default;
46 Interceptor (const function<void (Message&)>& handleMessage, const function<void (Message&, const exception_ptr& e)>& handleFault = nullptr);
47
48 protected:
49 Interceptor (const shared_ptr<_IRep>& rep);
50
51 public:
52 nonvirtual Interceptor& operator= (const Interceptor&) = default;
53
54 public:
55 /**
56 * Called any interceptor which HandleMessage was invoked on, when a fault prevented completion of
57 * the interceptor chain.
58 *
59 * This function should NOT throw an exception - just do what it can to cleanup.
60 */
61 nonvirtual void HandleFault (Message& m, const exception_ptr& e) const noexcept;
62
63 public:
64 /**
65 * Intercepts and handles a message. Typically this will read stuff from the Request and
66 * add stuff to the Response.
67 */
68 nonvirtual void HandleMessage (Message& m) const;
69
70 public:
71 /**
72 */
73 nonvirtual void CompleteNormally (Message& m) const;
74
75 public:
76 /**
77 * Two interceptors are equal if they have the same address, or are copies of one another by copy constructor or assignment.
78 */
79 nonvirtual bool operator== (const Interceptor& rhs) const;
80
81 public:
82 nonvirtual Characters::String ToString () const;
83
84 protected:
85 /**
86 */
87 template <typename T = _IRep>
88 nonvirtual const T& _GetRep () const;
89
90 private:
91 shared_ptr<_IRep> fRep_;
92 [[no_unique_address]] Debug::AssertExternallySynchronizedMutex fThisAssertExternallySynchronized_;
93
94 private:
95 class MyRep_;
96 };
97
98 /**
99 * \note Each Interceptor::_IRep must be internally synchronized, as it may be called concurrently from different threads,
100 * for different messages.
101 */
103 public:
104 virtual ~_IRep () = default;
105
106 /**
107 * Called any interceptor which HandleMessage was invoked on, when a fault prevented completion of
108 * the interceptor chain.
109 *
110 * This function should NOT throw an exception - just do what it can to cleanup.
111 */
112 virtual void HandleFault ([[maybe_unused]] Message& m, [[maybe_unused]] const exception_ptr& e) const noexcept;
113
114 /**
115 * Intercepts and handles a message. Typically this will read stuff from the Request and
116 * add write to the Response.
117 */
118 virtual void HandleMessage (Message& m) const = 0;
119
120 /**
121 * Rarely overriden, but can be to get a notification
122 * about message a second time. Used when you do something in Handle message going one way though the interceptor
123 * chain and need todo a follow up on the way back towards the start of the interceptor chain.
124 *
125 * EG. for logging.
126 */
127 virtual void CompleteNormally (Message& m) const;
128
129 /**
130 * For debugging purposes - dump some terse summary of the state of the interceptor
131 */
132 virtual Characters::String ToString () const;
133 };
134
135}
136
137/*
138 ********************************************************************************
139 ***************************** Implementation Details ***************************
140 ********************************************************************************
141 */
142#include "Interceptor.inl"
143
144#endif /*_Stroika_Framework_WebServer_Interceptor_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
NOT a real mutex - just a debugging infrastructure support tool so in debug builds can be assured thr...
virtual void CompleteNormally(Message &m) const
virtual void HandleMessage(Message &m) const =0
virtual Characters::String ToString() const
virtual void HandleFault(Message &m, const exception_ptr &e) const noexcept
nonvirtual bool operator==(const Interceptor &rhs) const
nonvirtual void HandleFault(Message &m, const exception_ptr &e) const noexcept
nonvirtual void HandleMessage(Message &m) const