NetBurner 3.5.0
PDF Version
 
httppost.h
Go to the documentation of this file.
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
13#ifndef _NB_HTTPPOST_H
14#define _NB_HTTPPOST_H
15
16#include <http.h>
17#include <json_lexer.h>
18#include <nettypes.h>
19#include <config_obj.h>
20
21
40typedef int(http_posthandler)(int sock, HTTP_Request &httpReqInfo);
41
42// To handle posts for a specific URL, build a posthandler object for that URL.
43// This is the base class for all post handlers. A NULL name will be a catch all for all posts.
44class HtmlPostHandler : public HtmlPageHandler
45{
46 public:
47 HtmlPostHandler(const char *url, int accessGroup = 0) : HtmlPageHandler(url, tPost, accessGroup, false) {}
48 // This class will do a callback with data for each post to the specified url
49 virtual int ProcessRaw(int sock, HTTP_Request &pd) = 0;
50};
51
52// This implements the above as a function pointer call back
53
58class CallBackFunctionPostHandler : public HtmlPostHandler
59{
60 protected:
61 http_posthandler *m_pf;
62
63 public:
64 inline virtual int ProcessRaw(int sock, HTTP_Request &pdt) { return m_pf(sock, pdt); };
65 inline CallBackFunctionPostHandler(const char *pUrl, http_posthandler *pf, int accessGroup = 0) : HtmlPostHandler(pUrl, accessGroup)
66 {
67 m_pf = pf;
68 };
69};
70
71struct FilePostStruct
72{
73 char FileText[5]; //< Has the text FILE\0
74 int fd; //< File descriptor of the file, only valid during the duration of the callback
75 const char *pFileName; //< Name of file
76 const char *pType; //< Pointer to MIME type of file
77};
78
79enum PostEvents
80{
81 eStartingPost, //< Occurs one time before variables are processed
82 eVariable, //< Occurs for each variable
83 eFile, //< Occurs if a file is being processed
84 eEndOfPost //< Occurs one time at the end of POST processing
85};
86
87// This class will provide a virtual function call with a list of all variable values.
88class HtmlPostVariableListHandler : public HtmlPostHandler
89{
90 HTTP_Request *m_pCurRequest;
91
92 public:
93 HTTP_Request *GetCurRequest() { return m_pCurRequest; };
94 virtual int ProcessRaw(int sock, HTTP_Request &pd);
95
96
104 HtmlPostVariableListHandler(const char *pUrl, int accessGroup = 0) : HtmlPostHandler(pUrl, accessGroup){};
105
106 // Called back with each name/value pair.
107 // Called back with name="Start" for start.
108 // Called back with both name and value null for last post.
109
119 virtual void ProcessPostVariables(int sock, PostEvents event, const char *pNames, const char *pValues) = 0;
120};
121
122// This class implements the above with a function call back.
123typedef void(postvarhandler)(int sock, PostEvents event, const char *pNames, const char *pValue);
124
129class HtmlPostVariableListCallback : public HtmlPostVariableListHandler
130{
131 protected:
132 postvarhandler *m_pf;
133
134 public:
135 inline void ProcessPostVariables(int sock, PostEvents event, const char *pName, const char *pValue)
136 {
137 m_pf(sock, event, pName, pValue);
138 };
139
148 inline HtmlPostVariableListCallback(const char *pUrl, postvarhandler *pCallback, int accessGroup = 0)
149 : HtmlPostVariableListHandler(pUrl, accessGroup), m_pf(pCallback){};
150};
151
152//----------------------------------------------------------------------------------------------------
153// JSON post handlers
154//----------------------------------------------------------------------------------------------------
155
156// Handle JSON posts, virtual function call back...
157class JsonPostHandler : public HtmlPostHandler
158{
159 protected:
160 virtual void HandleJson(int sock, ParsedJsonDataSet &JsonSet) = 0;
161
162 public:
163 JsonPostHandler(const char *pUrl, int accessGroup = 0) : HtmlPostHandler(pUrl, accessGroup){};
164 virtual int ProcessRaw(int sock, HTTP_Request &pd);
165};
166
167// Handle JSON posts function pointer call back...
168typedef void(jsonpostvarhandler)(int sock, ParsedJsonDataSet &JsonSet);
169
170class JsonPostCallbackHandler : public JsonPostHandler
171{
172 jsonpostvarhandler *m_pf;
173
174 public:
175 inline void HandleJson(int sock, ParsedJsonDataSet &JsonSet)
176 {
177 m_pf(sock, JsonSet);
178 ;
179 };
180 inline JsonPostCallbackHandler(const char *pUrl, jsonpostvarhandler *pCallback, int accessGroup = 0)
181 : JsonPostHandler(pUrl, accessGroup), m_pf(pCallback){};
182};
183
184// Handle form posts that might contain config items in the form from CONFIGENTRY, FULL or CONFIGTABLE in the html.
185// Retuns true if it handled the event and no farther processing is required.
186bool HandleConfigFormEvent(PostEvents event, const char *pName, const char *pValue);
187
188// This class will process post that ONLY have config variables in them.
189class HtmlPostConfigVariableHandler : public HtmlPostVariableListHandler
190{
191 const char *m_pRedirect_url;
192
193 public:
194 HtmlPostConfigVariableHandler(const char *pUrl, const char *pRedirect_Url = 0, int accessGroup = 0)
195 : HtmlPostVariableListHandler(pUrl, accessGroup)
196 {
197 m_pRedirect_url = pRedirect_Url;
198 };
199
200 // Called back with each name/value pair.
201 // Called back with name="Start" for start.
202 // Called back with both name and value null for last post.
203 virtual void ProcessPostVariables(int sock, PostEvents event, const char *pNames, const char *pValues);
204};
205
206
207class CustomConfigFormHandler
208{
209static CustomConfigFormHandler * pHead;
210CustomConfigFormHandler * pNext;
211const char * pTypeName;
212protected:
213CustomConfigFormHandler(const char * pTypeName);
214public:
215virtual void RenderValue(int fd, config_leaf *pl, int len, const char *extra)=0;
216virtual void RenderInput(int fd, config_leaf *pl, int len, const char *extra)=0;
217virtual bool ProcessValue(const char * pValue,config_leaf * pl)=0;
218static CustomConfigFormHandler * Find(const NBString &type_name);
219};
220
221
222
223#endif
224
225
226
227
228
Implements the HtmlPostHandler class as a function pointer callback for POST requests.
Definition httppost.h:59
virtual int ProcessRaw(int sock, HTTP_Request &pdt)
This class will do a callback with data for each request to the specified url.
Definition httppost.h:64
Base class for all GET handlers. To handle GET requests for a specific URL in your application,...
Definition http.h:121
HtmlPageHandler(const char *url, HTTP_RequestTypes rt=tGet, int accessGroup=0, bool Before_Files=false)
Register handler.
Implements the HtmlPostVariableListHandler class as a function pointer callback for HTTP POST submiss...
Definition httppost.h:130
HtmlPostVariableListCallback(const char *pUrl, postvarhandler *pCallback, int accessGroup=0)
Custom HTTP POST handler callback function constructor.
Definition httppost.h:148
Lightweight alternative to C++ CString class.
Definition nbstring.h:118
A class to create, read, and modify a JSON object.
Definition json_lexer.h:530
Configuration object header file.
int http_posthandler(int sock, HTTP_Request &httpReqInfo)
Type definition of the HtmlPostHandler callback for POST requests.
Definition httppost.h:40
@ tPost
POST request.
Definition http.h:38
NetBurner HTTP Web Server Header File.
NetBurner JSON Lexer. See the JSON Lexer page for complete documentation.
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.
HTTP Request Structure.
Definition http.h:69