NetBurner 3.1
httppost.h
Go to the documentation of this file.
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
12 #ifndef _NB_HTTPPOST_H
13 #define _NB_HTTPPOST_H
14 
15 #include <http.h>
16 #include <json_lexer.h>
17 #include <nettypes.h>
18 
19 typedef int(http_posthandler)(int sock, HTTP_Request &httpReqInfo);
20 
21 // To handle posts for a specific URL, build a posthandler object for that URL.
22 // This is the base class for all post handlers. A NULL name will be a catch all for all posts.
23 class HtmlPostHandler : public HtmlPageHandler
24 {
25  public:
26  HtmlPostHandler(const char *url, int accessGroup = 0) : HtmlPageHandler(url, tPost, accessGroup, false) {}
27  // This class will do a callback with data for each post to the specified url
28  virtual int ProcessRaw(int sock, HTTP_Request &pd) = 0;
29 };
30 
31 // This implements the above as a function pointer call back
32 class CallBackFunctionPostHandler : public HtmlPostHandler
33 {
34  protected:
35  http_posthandler *m_pf;
36 
37  public:
38  inline virtual int ProcessRaw(int sock, HTTP_Request &pdt) { return m_pf(sock, pdt); };
39  inline CallBackFunctionPostHandler(const char *pUrl, http_posthandler *pf, int accessGroup = 0) : HtmlPostHandler(pUrl, accessGroup)
40  {
41  m_pf = pf;
42  };
43 };
44 
45 struct FilePostStruct
46 {
47  char FileText[5]; //< Has the text FILE\0
48  int fd; //< File descriptor of the file, only valid during the duration of the callback
49  const char *pFileName; //< Name of file
50  const char *pType; //< Pointer to MIME type of file
51 };
52 
53 enum PostEvents
54 {
55  eStartingPost, //< Occurs one time before variables are processed
56  eVariable, //< Occurs for each variable
57  eFile, //< Occurs if a file is being processed
58  eEndOfPost //< Occurs one time at the end of POST processing
59 };
60 
61 // This class will provide a virtual function call with a list of all variable values.
62 class HtmlPostVariableListHandler : public HtmlPostHandler
63 {
64  public:
65  virtual int ProcessRaw(int sock, HTTP_Request &pd);
66  HtmlPostVariableListHandler(const char *pUrl, int accessGroup = 0) : HtmlPostHandler(pUrl, accessGroup){};
67 
68  // Called back with each name/value pair.
69  // Called back with name="Start" for start.
70  // Called back with both name and value null for last post.
71  virtual void ProcessPostVariables(int sock, PostEvents event, const char *pNames, const char *pValues) = 0;
72 };
73 
74 // This class implements the above with a function call back.
75 typedef void(postvarhandler)(int sock, PostEvents event, const char *pNames, const char *pValue);
76 
77 class HtmlPostVariableListCallback : public HtmlPostVariableListHandler
78 {
79  protected:
80  postvarhandler *m_pf;
81 
82  public:
83  inline void ProcessPostVariables(int sock, PostEvents event, const char *pName, const char *pValue)
84  {
85  m_pf(sock, event, pName, pValue);
86  };
87 
99  inline HtmlPostVariableListCallback(const char *pUrl, postvarhandler *pCallback, int accessGroup = 0)
100  : HtmlPostVariableListHandler(pUrl, accessGroup), m_pf(pCallback){};
101 };
102 
103 //----------------------------------------------------------------------------------------------------
104 // JSON post handlers
105 //----------------------------------------------------------------------------------------------------
106 
107 // Handle JSON posts, virtual function call back...
108 class JsonPostHandler : public HtmlPostHandler
109 {
110  protected:
111  virtual void HandleJson(int sock, ParsedJsonDataSet &JsonSet) = 0;
112 
113  public:
114  JsonPostHandler(const char *pUrl, int accessGroup = 0) : HtmlPostHandler(pUrl, accessGroup){};
115  virtual int ProcessRaw(int sock, HTTP_Request &pd);
116 };
117 
118 // Handle JSON posts function pointer call back...
119 typedef void(jsonpostvarhandler)(int sock, ParsedJsonDataSet &JsonSet);
120 
121 class JsonPostCallbackHandler : public JsonPostHandler
122 {
123  jsonpostvarhandler *m_pf;
124 
125  public:
126  inline void HandleJson(int sock, ParsedJsonDataSet &JsonSet)
127  {
128  m_pf(sock, JsonSet);
129  ;
130  };
131  inline JsonPostCallbackHandler(const char *pUrl, jsonpostvarhandler *pCallback, int accessGroup = 0)
132  : JsonPostHandler(pUrl, accessGroup), m_pf(pCallback){};
133 };
134 
135 #endif
HtmlPostVariableListCallback(const char *pUrl, postvarhandler *pCallback, int accessGroup=0)
Custom HTTP POST handler function.
Definition: httppost.h:99
NetBurner HTTP Web Server Header File.
POST request.
Definition: http.h:32