18#define HTTP_JSON_NOTFOUND -10
19#define HTTP_JSON_DICSTRUC -11
64 entry (
const std::string& n,
void* a,
jb_type t,
size_t s,
size_t c)
78 template <
typename T,
size_t C>
79 void add_var (T (&var)[C],
const std::string& name)
81 using U =
typename std::remove_extent<T>::type;
82 constexpr size_t sz =
sizeof (U) * (std::extent_v<T> ? std::extent_v<T> : 1);
84 : std::is_same_v<U, unsigned short> ?
JT_USHORT
85 : std::is_same_v<U, int> ?
JT_INT
86 : std::is_same_v<U, unsigned int> ?
JT_UINT
87 : std::is_same_v<U, long> ?
JT_LONG
88 : std::is_same_v<U, unsigned long> ?
JT_ULONG
89 : std::is_same_v<U, float> ?
JT_FLT
90 : std::is_same_v<U, double> ?
JT_DBL
91 : std::is_same_v<U, bool> ?
JT_BOOL
92 : std::is_same_v<U, char> ?
JT_CSTR
93 : std::is_same_v<U, std::string> ?
JT_STR
94 : std::is_same_v<U, char*> ?
JT_PSTR
95 : std::is_same_v<U, const char*> ?
JT_PSTR
97 static_assert (t != JT_UNKNOWN,
"Invalid array type");
99 children.emplace_back (name, var, t, C, 1);
101 children.emplace_back (name, var, t, sz, C);
110 template <
typename T>
111 std::enable_if_t<std::is_same_v<T, std::string> || std::is_same_v<T, short>
112 || std::is_same_v<T, unsigned short> || std::is_same_v<T, int>
113 || std::is_same_v<T, unsigned int> || std::is_same_v<T, long>
114 || std::is_same_v<T, unsigned long> || std::is_same_v<T, float>
115 || std::is_same_v<T, double> || std::is_same_v<T, bool>>
119 : std::is_same_v<T, unsigned short> ?
JT_USHORT
120 : std::is_same_v<T, int> ?
JT_INT
121 : std::is_same_v<T, unsigned int> ?
JT_UINT
122 : std::is_same_v<T, long> ?
JT_LONG
123 : std::is_same_v<T, unsigned long> ?
JT_ULONG
124 : std::is_same_v<T, float> ?
JT_FLT
125 : std::is_same_v<T, double> ?
JT_DBL
126 : std::is_same_v<T, bool> ?
JT_BOOL
127 : std::is_same_v<T, char> ?
JT_CSTR
128 : std::is_same_v<T, std::string> ?
JT_STR
129 : std::is_same_v<T, char*> ?
JT_PSTR
130 : std::is_same_v<T, const char*> ?
JT_PSTR
132 static_assert (t != JT_UNKNOWN,
"Invalid variable type");
133 children.emplace_back (name, &var, t,
sizeof (T), 1);
138 children.emplace_back (name,
nullptr,
JT_OBJECT, 0, 1);
139 return *(--children.end ());
158 const std::string&
path ()
const;
167 template <
typename T,
size_t C>
168 void add_var (T (&var)[C],
const std::string& name)
170 using U =
typename std::remove_extent<T>::type;
171 constexpr size_t sz =
sizeof (U) * (std::extent_v<T> ? std::extent_v<T> : 1);
173 : std::is_same_v<U, unsigned short> ?
JT_USHORT
174 : std::is_same_v<U, int> ?
JT_INT
175 : std::is_same_v<U, unsigned int> ?
JT_UINT
176 : std::is_same_v<U, long> ?
JT_LONG
177 : std::is_same_v<U, unsigned long> ?
JT_ULONG
178 : std::is_same_v<U, float> ?
JT_FLT
179 : std::is_same_v<U, double> ?
JT_DBL
180 : std::is_same_v<U, bool> ?
JT_BOOL
181 : std::is_same_v<U, char> ?
JT_CSTR
182 : std::is_same_v<U, char*> ?
JT_PSTR
183 : std::is_same_v<U, const char*> ?
JT_PSTR
185 static_assert (t != JT_UNKNOWN,
"Invalid array type");
187 dict_.emplace_back (name, var, t, C, 1);
189 dict_.emplace_back (name, var, t, sz, C);
193 template <
typename T>
194 std::enable_if_t<std::is_same_v<T, std::string> || std::is_same_v<T, short>
195 || std::is_same_v<T, unsigned short> || std::is_same_v<T, int>
196 || std::is_same_v<T, unsigned int> || std::is_same_v<T, long>
197 || std::is_same_v<T, unsigned long> || std::is_same_v<T, float>
198 || std::is_same_v<T, double> || std::is_same_v<T, bool>>
202 : std::is_same_v<T, unsigned short> ?
JT_USHORT
203 : std::is_same_v<T, int> ?
JT_INT
204 : std::is_same_v<T, unsigned int> ?
JT_UINT
205 : std::is_same_v<T, long> ?
JT_LONG
206 : std::is_same_v<T, unsigned long> ?
JT_ULONG
207 : std::is_same_v<T, float> ?
JT_FLT
208 : std::is_same_v<T, double> ?
JT_DBL
209 : std::is_same_v<T, bool> ?
JT_BOOL
210 : std::is_same_v<T, char> ?
JT_CSTR
211 : std::is_same_v<T, std::string> ?
JT_STR
212 : std::is_same_v<T, char*> ?
JT_PSTR
213 : std::is_same_v<T, const char*> ?
JT_PSTR
215 static_assert (t != JT_UNKNOWN,
"Invalid variable type");
216 dict_.emplace_back (name, &var, t,
sizeof (T), 1);
226 bool find (
const std::string& name,
dict_cptr& found,
size_t* idx = 0)
const;
230 void process_request ();
240 std::string redirect_uri;
241 std::map<std::string, uri_handler> post_handlers;
244 static bool deep_search (
const std::string& var,
const dictionary& dict,
dict_cptr& found);
279 throw std::logic_error (
"Missing client connection");
300 dict_.emplace_back (name,
nullptr,
JT_OBJECT, 0, 1);
301 return *(--dict_.end ());
315 post_handlers[qparam] = pfn;
Lightweight inter-thread synchronization.
Definition critsect.h:27
objects returned as a function result or thrown directly.
Definition errorcode.h:73
Representation of a HTTP client connection request.
JSON data dictionary entry.
Definition jbridge.h:62
std::enable_if_t< std::is_same_v< T, std::string >||std::is_same_v< T, short >||std::is_same_v< T, unsigned short >||std::is_same_v< T, int >||std::is_same_v< T, unsigned int >||std::is_same_v< T, long >||std::is_same_v< T, unsigned long >||std::is_same_v< T, float >||std::is_same_v< T, double >||std::is_same_v< T, bool > > add_var(T &var, const std::string &name)
Add a variable to data dictionary.
Definition jbridge.h:116
void add_var(T(&var)[C], const std::string &name)
Add an array to data dictionary.
Definition jbridge.h:79
void unlock()
Leave the critical section associated with this bridge.
Definition jbridge.h:256
entry & add_object(const std::string &name)
Add an object to data dictionary.
Definition jbridge.h:298
dictionary::const_iterator dict_cptr
Dictionary iterator.
Definition jbridge.h:57
erc jsonify(json::node &n, dict_cptr entry)
Serializes a variable to JSON format.
Definition jbridge.cpp:110
dictionary::iterator dict_ptr
Dictionary iterator.
Definition jbridge.h:56
void set_post_action(uri_handler pfn)
Set the function invoked after successful processing of a POST request.
Definition jbridge.h:288
connection & client()
Return currently connected client (if any)
Definition jbridge.h:274
void redirect_to(const std::string &uri)
Set default redirection target for POST requests.
Definition jbridge.h:268
void not_found(const char *varname)
Generate 410 response if a variable was not found in JSON dictionary.
Definition jbridge.cpp:249
std::list< entry > dictionary
Data dictionary structure.
Definition jbridge.h:53
bool parse_urlencoded() const
Parse the URL-encoded body of a POST request assigning new values to all variables.
Definition jbridge.cpp:338
const std::string & path() const
Return the root path where bridge is attached.
Definition jbridge.h:262
jbridge(const char *path)
Creates a jbridge object for the given path.
Definition jbridge.cpp:54
void lock()
Enter the critical section associated with this bridge.
Definition jbridge.h:250
void attach_to(server &server)
Attach jbridge object to a HTTP server.
Definition jbridge.cpp:66
void add_var(T(&var)[C], const std::string &name)
add array
Definition jbridge.h:168
bool find(const std::string &name, dict_cptr &found, size_t *idx=0) const
Search a variable in JSON dictionary.
Definition jbridge.cpp:263
bool deep_find(const std::string &name, dict_cptr &found, size_t *idx=0) const
Search for a variable name in JSON dictionary.
Definition jbridge.cpp:312
std::enable_if_t< std::is_same_v< T, std::string >||std::is_same_v< T, short >||std::is_same_v< T, unsigned short >||std::is_same_v< T, int >||std::is_same_v< T, unsigned int >||std::is_same_v< T, long >||std::is_same_v< T, unsigned long >||std::is_same_v< T, float >||std::is_same_v< T, double >||std::is_same_v< T, bool > > add_var(T &var, const std::string &name)
add variable of one of basic types
Definition jbridge.h:199
jb_type
Dictionary entry types.
Definition jbridge.h:32
@ JT_FLT
float
Definition jbridge.h:40
@ JT_ULONG
unsigned long
Definition jbridge.h:39
@ JT_POSTFUN
POST function call.
Definition jbridge.h:47
@ JT_INT
int
Definition jbridge.h:36
@ JT_STR
std::string
Definition jbridge.h:44
@ JT_LONG
long
Definition jbridge.h:38
@ JT_CSTR
char[]
Definition jbridge.h:43
@ JT_OBJECT
composite object
Definition jbridge.h:46
@ JT_SHORT
short int
Definition jbridge.h:34
@ JT_DBL
double
Definition jbridge.h:41
@ JT_UINT
unsigned int
Definition jbridge.h:37
@ JT_PSTR
char*
Definition jbridge.h:42
@ JT_USHORT
unsigned short int
Definition jbridge.h:35
@ JT_BOOL
bool
Definition jbridge.h:45
bool parse_jsonencoded() const
Parse a JSON-encoded body of a POST message.
Definition jbridge.cpp:411
void add_postfun(const std::string &name, uri_handler pfn)
Add a handler function invoked in response to a POST request with an additional parameter.
Definition jbridge.h:313
Representation of a JSON node.
Definition json.h:61
Definition of mlib::http::server and mlib::http::connection classes.
std::function< int(connection &client, void *info)> uri_handler
User defined URL handler function.
Definition http.h:50
std::function< int(jbridge &bridge, void *var)> var_handler
User defined variable handler function.
Definition jbridge.h:24
Definition of mlib::json::node class.