NetBurner 3.5.0
PDF Version
 
http.h
Go to the documentation of this file.
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
21#ifndef _NB_HTTP_H
22#define _NB_HTTP_H
23
24#include <nettypes.h>
25#include <stddef.h>
26#include <config_obj.h>
27
28const int CONFIG_ACCESS_GROUP = 99;
29
41
46{
47 HTTP_OK_TO_SERVE, //< Ok to serve the page
48 HTTP_NEED_PASSWORD, //< Either the password in the HTTP request is wrong or missing
49 HTTP_NOTFOUND, //< Report the page is not found
50 HTTP_FORBIDEN, //< Report the page forbiden
51 HTTP_RESPONSE_HANDLED //< This means this function handled the full response, including closing the socket.
52};
53
54enum WebsocketFlags
55{
56 WS_UPGRADE = 0x1,
57 WS_CONNECT = 0x2,
58 WS_VER_13 = 0x4,
59 WS_KEY_CONFIRM = 0x8
60};
61
69{
70 PSTR pURL;
73 PSTR pData;
74 PSTR pSep;
75 PSTR pHost;
77 PSTR wsKey;
79 uint16_t sep_len;
80 uint32_t content_length;
81 uint32_t rx_length;
85
86 void ProcessLine(PSTR startofline);
87 int Respond(int socket);
88 bool ExtractAuthentication(char **pPassword, char **pUser);
89} __attribute__((packed));
90
91
97
98
99
100
101struct HTTP_Socket
102{
103 int FileDescriptor;
104 uint32_t StartTime;
105 IPADDR client_IPaddr;
106} __attribute__((packed));
107
108
109enum eWEB_ERROR
110{
111 eTCP_CONN_OPEN_NO_DATA, // WEB client open TCP port#80 conn, but no data request was sent
112 eTCP_CONN_OPEN_TO_LONG, // WEB client open TCP connect and send endless data ( for example - never ending POST).
113};
114
121{
122 protected:
124 const char *m_pUrlName;
127
130 void Remove();
131 virtual bool Match(HTTP_Request &req);
132
133 public:
142 HtmlPageHandler(const char *url, HTTP_RequestTypes rt = tGet, int accessGroup = 0, bool Before_Files = false);
144
145 static HtmlPageHandler *FindHandler(HTTP_Request &req, bool bBeforeFiles);
146
152 virtual int ProcessRaw(int sock, HTTP_Request &pd) = 0;
153
159 inline int GetGroup() { return m_access_group; };
160};
161
171typedef int(http_gethandlerfunc)(int sock, HTTP_Request &pd);
172
173
174typedef bool(http_matchhandlerfunc)(HTTP_Request &pd);
175
181{
182 protected:
184 http_matchhandlerfunc *m_mhf;
185
186 public:
187 inline virtual int ProcessRaw(int sock, HTTP_Request &pdt) { return m_pf(sock, pdt); };
188
201 inline CallBackFunctionPageHandler(const char *pUrl,
202 http_gethandlerfunc *pFunction,
203 HTTP_RequestTypes reqType = tGet,
204 int accessGroup = 0,
205 bool beforeFiles = false)
206 : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), m_pf(pFunction)
207 {
208 m_mhf = 0;
209 };
210
223 inline CallBackFunctionPageHandler(const char *pUrl,
224 http_gethandlerfunc *pFunction,
225 http_matchhandlerfunc *pMatchFunction,
226 HTTP_RequestTypes reqType = tGet,
227 int accessGroup = 0,
228 bool beforeFiles = false)
229 : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), m_pf(pFunction)
230 {
231 m_mhf = pMatchFunction;
232 };
233
234 virtual bool Match(HTTP_Request &req)
235 {
236 if (m_mhf)
237 return m_mhf(req);
238 else
239 return HtmlPageHandler::Match(req);
240 }
241};
242
243
244
245
246class HtmlConfigExposer : public HtmlPageHandler
247{
248protected:
249 config_leaf * pLeaf;
250public:
251 inline HtmlConfigExposer(const char *pUrl, config_leaf & leaf,
252 HTTP_RequestTypes reqType = tGet,
253 int accessGroup = 0,
254 bool beforeFiles = false
255 )
256 : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), pLeaf(&leaf)
257 {
258 }
259
260virtual int ProcessRaw(int sock, HTTP_Request &pd);
261};
262
263
264typedef void (ws_createhandlerfunc)(int web_socket_fd);
265
266class CallBackWSEndPoint : public HtmlPageHandler
267{
268 ws_createhandlerfunc *m_pf;
269public:
270 int ProcessRaw(int sock, HTTP_Request &pdt);
271 inline CallBackWSEndPoint(const char *pUrl,
272 ws_createhandlerfunc *pFunction,
273 int accessGroup = 0,
274 bool beforeFiles = false)
275 : HtmlPageHandler(pUrl, tGet, accessGroup, beforeFiles), m_pf(pFunction){};
276
277
278};
279
280
281
282
283extern const char *pHttpRealm; // The realm used for password requests
284
297HTTP_ACCESS CheckHttpAccess(int sock, int access_level, HTTP_Request &Req);
298
310void StartHttp(uint16_t port /* = 80 */, bool RunConfigMirror /* = true */);
311
312typedef int(http_errorhandler)(IPADDR ip, enum eWEB_ERROR Err, void *prm);
313
314/*Setup a custom WEB Error Report Handler */
315http_errorhandler *SetNewErrorHandler(http_errorhandler *newhandler);
316
322void StopHttp();
323
333void SendHTMLHeader(int sock);
334
345void SendHTMLHeaderWCookie(int sock, char *cookie);
346
356void SendTextHeader(int sock);
357
368void SendGifHeader(int sock);
369
378void EmptyResponse(int sock);
379
388void NoContentResponse(int sock);
389
399void RedirectResponse(int sock, PCSTR new_page);
400
410void NotFoundResponse(int sock, PCSTR new_page);
411
421void ForbiddenResponse(int sock, PCSTR new_page);
422
433void NotAvailableResponse(int sock, PCSTR new_page);
434
446void BadRequestResponse(int sock, PCSTR url, PCSTR data);
447
448void writesafestring(int fd, PCSTR str);
449
462int writeallsafestring(int fd, PCSTR str);
463
477int writesafestring(int fd, PCSTR str, size_t strLength);
478
479// Encode a uri component
480int decodeURI(char *str);
481
504int httpstricmp(PCSTR str1, PCSTR strIsUpper2);
505
506void append(char *&cpto, const char *cpfrm);
507
508#endif
509
Implements the HtmlPageHandler class as a function pointer callback for GET requests.
Definition http.h:181
CallBackFunctionPageHandler(const char *pUrl, http_gethandlerfunc *pFunction, HTTP_RequestTypes reqType=tGet, int accessGroup=0, bool beforeFiles=false)
Constructor for HTTP GET callback function.
Definition http.h:201
virtual int ProcessRaw(int sock, HTTP_Request &pdt)
This class will do a callback with data for each request to the specified url.
Definition http.h:187
CallBackFunctionPageHandler(const char *pUrl, http_gethandlerfunc *pFunction, http_matchhandlerfunc *pMatchFunction, HTTP_RequestTypes reqType=tGet, int accessGroup=0, bool beforeFiles=false)
Constructor for HTTP GET callback function, includes option for function to match the requested name.
Definition http.h:223
Base class for all GET handlers. To handle GET requests for a specific URL in your application,...
Definition http.h:121
HtmlPageHandler * m_pNextHandler
Pointer to next page handle object.
Definition http.h:123
HTTP_RequestTypes m_requestTypes
Type of request, HTTP_RequestTypes.
Definition http.h:126
const char * m_pUrlName
Pointer to URL. Performs a length match, an empty string matches everything.
Definition http.h:124
HtmlPageHandler(const char *url, HTTP_RequestTypes rt=tGet, int accessGroup=0, bool Before_Files=false)
Register handler.
virtual int ProcessRaw(int sock, HTTP_Request &pd)=0
This class will do a callback with data for each request to the specified url.
int SortValue(HtmlPageHandler *pv)
Returns the value of the sort compare: -1, 0, 1.
int GetGroup()
Returns access group setting.
Definition http.h:159
void InsertSort(HtmlPageHandler *&ph)
Insert sort.
int m_access_group
The access group for this request see CheckHttpAccess.
Definition http.h:125
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
Configuration object header file.
void ForbiddenResponse(int sock, PCSTR new_page)
Send a page is forbidden response.
HTTP_Request * GetActiveHttpRequest()
Get thc curent active running http request. Only valid fromwithing http get or post handeling....
void NotFoundResponse(int sock, PCSTR new_page)
Send a page not found response.
void SendHTMLHeader(int sock)
Send a HTML response header.
void BadRequestResponse(int sock, PCSTR url, PCSTR data)
Send a response indicating that the client request itself is faulty in some manner.
int writeallsafestring(int fd, PCSTR str)
Send a string and escape all special characters.
void SendHTMLHeaderWCookie(int sock, char *cookie)
Send a HTML response header and cookie.
HTTP_RequestTypes
HTTP request types for HTTP page handler callback functions.
Definition http.h:35
void EmptyResponse(int sock)
Send an empty response back.
void SendTextHeader(int sock)
Send a HTML plain text header.
void RedirectResponse(int sock, PCSTR new_page)
Redirect a HTTP request to a different page.
void NoContentResponse(int sock)
Send a no content response back.
int httpstricmp(PCSTR str1, PCSTR strIsUpper2)
Special string compare. Returns 1 if the strings match until one string ends with a null (0).
Definition WebSockets/Console/src/main.cpp:69
HTTP_ACCESS CheckHttpAccess(int sock, int access_level, HTTP_Request &Req)
All HTTP requests go though this function.
Definition JSON/DemoNetBurner/src/main.cpp:165
void NotAvailableResponse(int sock, PCSTR new_page)
Send a response indicating that the requested resource is not available at this time.
void StopHttp()
Stop the HTTP web server.
void SendGifHeader(int sock)
Send a HTML GIF header.
void StartHttp(uint16_t port, bool RunConfigMirror)
Start the HTTP web server. Further documentation in the Initialization section Initialization - Syste...
int http_gethandlerfunc(int sock, HTTP_Request &pd)
Implements the HtmlPageHandler class as a function pointer callback for GET requests.
Definition http.h:171
HTTP_ACCESS
HTTP page access return values.
Definition http.h:46
@ tGet
GET request.
Definition http.h:37
@ tPost
POST request.
Definition http.h:38
@ tUnknown
The type of request is not yet known.
Definition http.h:36
@ tHead
Header request, does not include content.
Definition http.h:39
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.
HTTP Request Structure.
Definition http.h:69
void ProcessLine(PSTR startofline)
internal function
uint32_t content_length
Content length field from HTML header.
Definition http.h:80
uint16_t sep_len
Length of separator for multipart forms.
Definition http.h:79
int Respond(int socket)
internal function
PSTR pSep
Separator for multipart forms, null if not present.
Definition http.h:74
PSTR last_datarx
Pointer to last data read, internal use only.
Definition http.h:76
PSTR pHost
Pointer to host record null if not present.
Definition http.h:75
PSTR wsKey
Web socket ket, internal use only.
Definition http.h:77
PSTR wsProtocol
Web socket prototocl, internal use only.
Definition http.h:78
PSTR pFirstCookie
First cookie if present, otherwise null. More may follow.
Definition http.h:72
PSTR pData
Pointer to entire data set. Note: not null terminated.
Definition http.h:73
HTTP_RequestTypes req
Type of request HTTP_RequestTypes.
Definition http.h:84
PSTR pAuthorization
Authorization header if present, otherwise null.
Definition http.h:71
uint8_t websocketFlags
Web socket flags.
Definition http.h:83
PSTR pURL
Request URL.
Definition http.h:70
uint32_t rx_length
Total bytes receive from request, internal use only.
Definition http.h:81
IPADDR client_IPaddr
IP address of client.
Definition http.h:82
bool ExtractAuthentication(char **pPassword, char **pUser)
used to extract user name and password in authentication test