NetBurner 3.5.6
PDF Version
httppost.h
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 int 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);
124typedef int(postvarhandler_int)(int sock, PostEvents event, const char *pNames, const char *pValue);
125
130class HtmlPostVariableListCallback : public HtmlPostVariableListHandler
131{
132 protected:
133 enum pfType_t {
134 pf_void = 0,
135 pf_int = 1
136 };
137 union
138 {
139 postvarhandler *m_pf;
140 postvarhandler_int *m_pfi;
141 };
142 pfType_t pfType;
143
144 public:
145 inline int ProcessPostVariables(int sock, PostEvents event, const char *pName, const char *pValue)
146 {
147 switch (pfType)
148 {
149 case pf_void:
150 default:
151 m_pf(sock, event, pName, pValue);
152 return 0;
153 case pf_int:
154 return m_pfi(sock, event, pName, pValue);
155 }
156 };
157
166 inline HtmlPostVariableListCallback(const char *pUrl, postvarhandler *pCallback, int accessGroup = 0)
167 : HtmlPostVariableListHandler(pUrl, accessGroup), m_pf(pCallback), pfType(pf_void){};
168
177 inline HtmlPostVariableListCallback(const char *pUrl, postvarhandler_int *pCallback, int accessGroup = 0)
178 : HtmlPostVariableListHandler(pUrl, accessGroup), m_pfi(pCallback), pfType(pf_int){};
179};
180
181//----------------------------------------------------------------------------------------------------
182// JSON post handlers
183//----------------------------------------------------------------------------------------------------
184
185// Handle JSON posts, virtual function call back...
186class JsonPostHandler : public HtmlPostHandler
187{
188 protected:
189 virtual void HandleJson(int sock, ParsedJsonDataSet &JsonSet) = 0;
190
191 public:
192 JsonPostHandler(const char *pUrl, int accessGroup = 0) : HtmlPostHandler(pUrl, accessGroup){};
193 virtual int ProcessRaw(int sock, HTTP_Request &pd);
194};
195
196// Handle JSON posts function pointer call back...
197typedef void(jsonpostvarhandler)(int sock, ParsedJsonDataSet &JsonSet);
198
199class JsonPostCallbackHandler : public JsonPostHandler
200{
201 jsonpostvarhandler *m_pf;
202
203 public:
204 inline void HandleJson(int sock, ParsedJsonDataSet &JsonSet)
205 {
206 m_pf(sock, JsonSet);
207 ;
208 };
209 inline JsonPostCallbackHandler(const char *pUrl, jsonpostvarhandler *pCallback, int accessGroup = 0)
210 : JsonPostHandler(pUrl, accessGroup), m_pf(pCallback){};
211};
212
213// Handle form posts that might contain config items in the form from CONFIGENTRY, FULL or CONFIGTABLE in the html.
214// Retuns true if it handled the event and no farther processing is required.
215bool HandleConfigFormEvent(PostEvents event, const char *pName, const char *pValue);
216
217// This class will process post that ONLY have config variables in them.
218class HtmlPostConfigVariableHandler : public HtmlPostVariableListHandler
219{
220 const char *m_pRedirect_url;
221
222 public:
223 HtmlPostConfigVariableHandler(const char *pUrl, const char *pRedirect_Url = 0, int accessGroup = 0)
224 : HtmlPostVariableListHandler(pUrl, accessGroup)
225 {
226 m_pRedirect_url = pRedirect_Url;
227 };
228
229 // Called back with each name/value pair.
230 // Called back with name="Start" for start.
231 // Called back with both name and value null for last post.
232 virtual int ProcessPostVariables(int sock, PostEvents event, const char *pNames, const char *pValues);
233};
234
235
236class CustomConfigFormHandler
237{
238static CustomConfigFormHandler * pHead;
239CustomConfigFormHandler * pNext;
240const char * pTypeName;
241protected:
242CustomConfigFormHandler(const char * pTypeName);
243public:
244virtual void RenderValue(int fd, config_leaf *pl, int len, const char *extra)=0;
245virtual void RenderInput(int fd, config_leaf *pl, int len, const char *extra)=0;
246virtual bool ProcessValue(const char * pValue,config_leaf * pl)=0;
247static CustomConfigFormHandler * Find(const NBString &type_name);
248};
249
250
251
252#endif
253
254
255
256
257
Implements the HtmlPostHandler class as a function pointer callback for POST requests.
Definition httppost.h:59
virtual int ProcessRaw(int sock, HTTP_Request &pdt)
Pure virtual method that must be implemented to process HTTP requests.
Definition httppost.h:64
Base class for HTTP request handlers that process requests for specific URLs and HTTP methods.
Definition http.h:220
HtmlPageHandler(const char *url, HTTP_RequestTypes rt=tGet, int accessGroup=0, bool Before_Files=false)
Constructs and registers an HTTP request handler for a specific URL pattern and method.
Implements the HtmlPostVariableListHandler class as a function pointer callback for HTTP POST submiss...
Definition httppost.h:131
HtmlPostVariableListCallback(const char *pUrl, postvarhandler_int *pCallback, int accessGroup=0)
Custom HTTP POST handler callback function constructor.
Definition httppost.h:177
HtmlPostVariableListCallback(const char *pUrl, postvarhandler *pCallback, int accessGroup=0)
Custom HTTP POST handler callback function constructor.
Definition httppost.h:166
Lightweight alternative to C++ CString class.
Definition nbstring.h:118
A class to create, read, and modify a JSON object.
Definition json_lexer.h:535
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
HTTP Request Structure.
Definition http.h:87