Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
RequestHandler.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Framework_WebServer_RequestHandler_h_
5#define _Stroika_Framework_WebServer_RequestHandler_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
9#include "Stroika/Foundation/Containers/Sequence.h"
10
11#include "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
23
24 /**
25 * A (WebServer Route) request handler (callback function) should be understood to be stateless - as far as the connection is concerned.
26 *
27 * Each handler is assumed to take an array of strings as arguments (or none). These string arguments come from
28 * the regular expression MATCH of the URL (not from the body of the request, nor from the query string - unless that's part of the regexp matching - not recommended).
29 *
30 * \note bool& handled - handled defaults to false, and is typically set to true.
31 * For constructor overloads with no 'handled' flag argument, 'handled' is automatically set to true
32 * (this is typically what you want to do - ignore param).
33 *
34 * This allows for multiple routes to match a given url, and dynamically choosing which one to apply.
35 *
36 * \note
37 * a RequestHandler should be careful about threads, as it could be called first on one thread, and
38 * then - possibly at the same time - on another thread. The same handler can be used multiple times (multiple sessions).
39 * (meaning handler/lambda function itself required to be const or at least internally synchronized).
40 *
41 * \note Before Stroika v3.0d12 - this inherited from function<void(Message*, const Sequence<String>&)> - so not fully backward
42 * compatible change...
43 */
44 class RequestHandler : public function<void (Message& message, const Sequence<String>& matchedArgs, bool& handled)> {
45 private:
46 using inherited = function<void (Message& message, const Sequence<String>& matchedArgs, bool& handled)>;
47
48 public:
49 /**
50 * HANDLER (Message& message, Sequence<String> matchedArgs, bool& handled)
51 * This is the generic, underlying full form of the message-handler, without anything defaulted. Other APIs just trivially map back to this.
52 * HANDLER (Message& message, Sequence<String> matchedArgs)
53 * Just ignore handled flags
54 * HANDLER (Message& message)
55 * Just ignore matchedArgs, and handled flags
56 *
57 * HANDLER (Message& message, String arg1, String arg2, ...)
58 * note: for this overload, \pre count of arguments == matchedArgs.size () - cuz must match count of matches returned from Route regexp.
59 *
60 * \par See Route constructors for examples...
61 */
62 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (invocable<Message&, const Sequence<String>&, bool&>) HANDLER_FUNCTION>
63 RequestHandler (HANDLER_FUNCTION&& messageHandler);
64#if !qCompilerAndStdLib_template_ConstraintDiffersInTemplateRedeclaration_Buggy
65 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (invocable<Message&, const Sequence<String>&>) HANDLER_FUNCTION>
66 RequestHandler (HANDLER_FUNCTION&& messageHandler);
67 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (invocable<Message&>) HANDLER_FUNCTION>
68 RequestHandler (HANDLER_FUNCTION&& messageHandler);
69 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (invocable<Request&, Response&>) HANDLER_FUNCTION>
70 RequestHandler (HANDLER_FUNCTION&& messageHandler);
71 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (invocable<Request&, Response&, const Sequence<String>&>) HANDLER_FUNCTION>
72 RequestHandler (HANDLER_FUNCTION&& messageHandler);
73 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (invocable<Request&, Response&, const Sequence<String>&, bool&>) HANDLER_FUNCTION>
74 RequestHandler (HANDLER_FUNCTION&& messageHandler);
75
76 // explode Sequence<String> arg inline: caller bug/assertion of invoked with wrong # of arguments (since based solely on route regexp)
77 // @todo RequestHandler overloads taking STRING arguments should use variadic templates (but tricky)
78 // not sure (yet) how to do this
79 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (invocable<Message&, const String&>) HANDLER_FUNCTION>
80 RequestHandler (HANDLER_FUNCTION&& messageHandler);
81 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (invocable<Message&, const String&, const String&>) HANDLER_FUNCTION>
82 RequestHandler (HANDLER_FUNCTION&& messageHandler);
83 template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (invocable<Message&, const String&, const String&, const String&>) HANDLER_FUNCTION>
84 RequestHandler (HANDLER_FUNCTION&& messageHandler);
85#endif
86
87 [[deprecated ("Since Stroika v3.0d12 - use Message& overload)")]] RequestHandler (
88 const function<void (Message* message, const Sequence<String>& matchedArgs, bool* handled)>& f);
89 [[deprecated ("Since Stroika v3.0d12 - use Message& overload)")]] RequestHandler (
90 const function<void (Message* message, const Sequence<String>& matchedArgs)>& f);
91 [[deprecated ("Since Stroika v3.0d12 - use Message& overload)")]] RequestHandler (const function<void (Message* message)>& f);
92 [[deprecated ("Since Stroika v3.0d12 - use Message& overload)")]] RequestHandler (const function<void (Request* request, Response* response)>& f);
93 template <typename _Fx, enable_if_t<is_convertible_v<_Fx, function<void (Message*)>>>* = nullptr>
94 [[deprecated ("Since Stroika v3.0d12 - use Message& overload)")]] RequestHandler (_Fx _Func);
95 template <typename _Fx, enable_if_t<is_convertible_v<_Fx, function<void (Message*, const Sequence<String>&)>>>* = nullptr>
96 [[deprecated ("Since Stroika v3.0d12 - use Message& overload)")]] RequestHandler (_Fx _Func, int* = nullptr);
97 template <typename _Fx, enable_if_t<is_convertible_v<_Fx, function<void (Message*, const String& arg0)>>>* = nullptr>
98 [[deprecated ("Since Stroika v3.0d12 - use Message& overload)")]] RequestHandler (_Fx _Func, short* = nullptr);
99 template <typename _Fx, enable_if_t<is_convertible_v<_Fx, function<void (Message*, const String& arg0, const String& arg1)>>>* = nullptr>
100 [[deprecated ("Since Stroika v3.0d12 - use Message& overload)")]] RequestHandler (_Fx _Func, char* = nullptr);
101 template <typename _Fx, enable_if_t<is_convertible_v<_Fx, function<void (Request*, Response*)>>>* = nullptr>
102 [[deprecated ("Since Stroika v3.0d12 - use Message& overload)")]] RequestHandler (_Fx _Func, void* = nullptr);
103 };
104
105}
106
107/*
108 ********************************************************************************
109 ***************************** Implementation Details ***************************
110 ********************************************************************************
111 */
112#include "RequestHandler.inl"
113
114#endif /*_Stroika_Framework_WebServer_RequestHandler_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187
this represents a HTTP request object for the WebServer module