NetBurner 3.5.6
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
43
58
67
68enum WebsocketFlags
69{
70 WS_UPGRADE = 0x1,
71 WS_CONNECT = 0x2,
72 WS_VER_13 = 0x4,
73 WS_KEY_CONFIRM = 0x8
74};
75
83{
84 PSTR pURL;
87 PSTR pData;
88 PSTR pSep;
89 PSTR pHost;
91 PSTR wsKey;
93 uint16_t sep_len;
94 uint32_t content_length;
95 uint32_t rx_length;
99
100 void ProcessLine(PSTR startofline);
101 int Respond(int socket);
102 bool ExtractAuthentication(char **pPassword, char **pUser);
104
110 const char * GetBoundaryMarker();
123 int ReadSimpleBody(int socket, const char *boundary = NULL);
124} __attribute__((packed));
125
126
132
133
134
135
136struct HTTP_Socket
137{
138 int FileDescriptor;
139 uint32_t StartTime;
140 IPADDR client_IPaddr;
141} __attribute__((packed));
142
143
144enum eWEB_ERROR
145{
146 eTCP_CONN_OPEN_NO_DATA, // WEB client open TCP port#80 conn, but no data request was sent
147 eTCP_CONN_OPEN_TO_LONG, // WEB client open TCP connect and send endless data ( for example - never ending POST).
148};
149
156{
157 protected:
159 const char *m_pUrlName;
162
165 void Remove();
166 virtual bool Match(HTTP_Request &req);
167
168 public:
177 HtmlPageHandler(const char *url, HTTP_RequestTypes rt = tGet, int accessGroup = 0, bool Before_Files = false);
179
180 static HtmlPageHandler *FindHandler(HTTP_Request &req, bool bBeforeFiles);
181
182 void MoveHandlerToLast(); //Make the handdler functionally the 404 handler.
183
189 virtual int ProcessRaw(int sock, HTTP_Request &pd) = 0;
190
196 inline int GetGroup() { return m_access_group; };
197};
198
208typedef int(http_gethandlerfunc)(int sock, HTTP_Request &pd);
209
210
211typedef bool(http_matchhandlerfunc)(HTTP_Request &pd);
212
218{
219 protected:
221 http_matchhandlerfunc *m_mhf;
222
223 public:
224 inline virtual int ProcessRaw(int sock, HTTP_Request &pdt) { return m_pf(sock, pdt); };
225
238 inline CallBackFunctionPageHandler(const char *pUrl,
239 http_gethandlerfunc *pFunction,
240 HTTP_RequestTypes reqType = tGet,
241 int accessGroup = 0,
242 bool beforeFiles = false)
243 : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), m_pf(pFunction)
244 {
245 m_mhf = 0;
246 };
247
260 inline CallBackFunctionPageHandler(const char *pUrl,
261 http_gethandlerfunc *pFunction,
262 http_matchhandlerfunc *pMatchFunction,
263 HTTP_RequestTypes reqType = tGet,
264 int accessGroup = 0,
265 bool beforeFiles = false)
266 : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), m_pf(pFunction)
267 {
268 m_mhf = pMatchFunction;
269 };
270
271 virtual bool Match(HTTP_Request &req)
272 {
273 if (m_mhf)
274 return m_mhf(req);
275 else
276 return HtmlPageHandler::Match(req);
277 }
278};
279
280
281
282
283class HtmlConfigExposer : public HtmlPageHandler
284{
285protected:
286 config_leaf * pLeaf;
287public:
288 inline HtmlConfigExposer(const char *pUrl, config_leaf & leaf,
289 HTTP_RequestTypes reqType = tGet,
290 int accessGroup = 0,
291 bool beforeFiles = false
292 )
293 : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), pLeaf(&leaf)
294 {
295 }
296
297virtual int ProcessRaw(int sock, HTTP_Request &pd);
298};
299
300
301typedef void (ws_createhandlerfunc)(int web_socket_fd);
302
303class CallBackWSEndPoint : public HtmlPageHandler
304{
305 ws_createhandlerfunc *m_pf;
306public:
307 int ProcessRaw(int sock, HTTP_Request &pdt);
308 inline CallBackWSEndPoint(const char *pUrl,
309 ws_createhandlerfunc *pFunction,
310 int accessGroup = 0,
311 bool beforeFiles = false)
312 : HtmlPageHandler(pUrl, tGet, accessGroup, beforeFiles), m_pf(pFunction){};
313
314
315};
316
317
318
319
320extern const char *pHttpRealm; // The realm used for password requests
321
335HTTP_ACCESS CheckHttpAccess(int sock, int access_level, HTTP_Request &Req);
336
348void StartHttp(uint16_t port /* = 80 */, bool RunConfigMirror /* = true */);
349
350typedef int(http_errorhandler)(IPADDR ip, enum eWEB_ERROR Err, void *prm);
351
352/*Setup a custom WEB Error Report Handler */
353http_errorhandler *SetNewErrorHandler(http_errorhandler *newhandler);
354
360void StopHttp();
361
371void SendHTMLHeader(int sock);
372
383void SendHTMLHeaderWCookie(int sock, char *cookie);
384
394void SendTextHeader(int sock);
395
406void SendGifHeader(int sock);
407
416void EmptyResponse(int sock);
417
426void NoContentResponse(int sock);
427
437void RedirectResponse(int sock, PCSTR new_page);
438
448void NotFoundResponse(int sock, PCSTR new_page);
449
459void ForbiddenResponse(int sock, PCSTR new_page);
460
471void NotAvailableResponse(int sock, PCSTR new_page);
472
484void BadRequestResponse(int sock, PCSTR url, PCSTR data);
485
486void writesafestring(int fd, PCSTR str);
487
500int writeallsafestring(int fd, PCSTR str);
501
515int writesafestring(int fd, PCSTR str, size_t strLength);
516
517// Encode a uri component
518int decodeURI(char *str);
519
542int httpstricmp(PCSTR str1, PCSTR strIsUpper2);
543
544void append(char *&cpto, const char *cpfrm);
545
546//Add extra headers to html responses.
547extern const char * pExtraHeaders;
548
549
550
551#endif
552
Implements the HtmlPageHandler class as a function pointer callback for GET requests.
Definition http.h:218
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:238
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:224
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:260
Base class for all GET handlers. To handle GET requests for a specific URL in your application,...
Definition http.h:156
HtmlPageHandler * m_pNextHandler
Pointer to next page handle object.
Definition http.h:158
HTTP_RequestTypes m_requestTypes
Type of request, HTTP_RequestTypes.
Definition http.h:161
const char * m_pUrlName
Pointer to URL. Performs a length match, an empty string matches everything.
Definition http.h:159
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:196
void InsertSort(HtmlPageHandler *&ph)
Insert sort.
int m_access_group
The access group for this request see CheckHttpAccess.
Definition http.h:160
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:60
HTTP_ACCESS CheckHttpAccess(int sock, int access_level, HTTP_Request &Req)
All HTTP requests go though this function.
Definition JSON/DemoNetBurner/src/main.cpp:142
void NotAvailableResponse(int sock, PCSTR new_page)
Send a response indicating that the requested resource is not available at this time.
HTTP_REQ_RD_RESULT
Definition http.h:60
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:208
HTTP_ACCESS
HTTP page access return values.
Definition http.h:51
@ tGet
GET request.
Definition http.h:37
@ tPost
POST request.
Definition http.h:38
@ tPut
Put request.
Definition http.h:40
@ tUnknown
The type of request is not yet known.
Definition http.h:36
@ tDelete
Delete Request.
Definition http.h:39
@ tHead
Header request, does not include content.
Definition http.h:41
@ HTTP_RRR_BUF_FULL
Buffer full, more data remains to be read.
Definition http.h:61
@ HTTP_RRR_FINISHED
Read read all content as indicated by headers.
Definition http.h:62
@ HTTP_RRR_SOCK_ERR
Socket returned error while having no data, probably closed.
Definition http.h:65
@ HTTP_RRR_END_FOUND
Read found the trailing '\r ' and terminated.
Definition http.h:63
@ HTTP_FORBIDEN
Report the page forbiden.
Definition http.h:55
@ HTTP_OK_TO_SERVE
Ok to serve the page.
Definition http.h:52
@ HTTP_NOTFOUND
Report the page is not found.
Definition http.h:54
@ HTTP_NEED_PASSWORD
Either the password in the HTTP request is wrong or missing.
Definition http.h:53
@ HTTP_RESPONSE_HANDLED
This means this function handled the full response, including closing the socket.
Definition http.h:56
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.
HTTP Request Structure.
Definition http.h:83
void ProcessLine(PSTR startofline)
internal function
uint32_t content_length
Content length field from HTML header.
Definition http.h:94
uint16_t sep_len
Length of separator for multipart forms.
Definition http.h:93
int Respond(int socket)
internal function
PSTR pSep
Separator for multipart forms, null if not present.
Definition http.h:88
PSTR last_datarx
Pointer to last data read, internal use only.
Definition http.h:90
PSTR pHost
Pointer to host record null if not present.
Definition http.h:89
PSTR wsKey
Web socket ket, internal use only.
Definition http.h:91
PSTR wsProtocol
Web socket prototocl, internal use only.
Definition http.h:92
PSTR pFirstCookie
First cookie if present, otherwise null. More may follow.
Definition http.h:86
PSTR pData
Pointer to entire data set. Note: not null terminated.
Definition http.h:87
HTTP_RequestTypes req
Type of request HTTP_RequestTypes.
Definition http.h:98
PSTR pAuthorization
Authorization header if present, otherwise null.
Definition http.h:85
uint8_t websocketFlags
Web socket flags.
Definition http.h:97
PSTR pURL
Request URL.
Definition http.h:84
const char * GetBoundaryMarker()
Search the headers and return the multipart boundary marker if it exists.
int ReadSimpleBody(int socket, const char *boundary=NULL)
Read the remainder of the basic Request Body from the socket into the buffer backing the request obje...
uint32_t rx_length
Total bytes receive from request, internal use only.
Definition http.h:95
IPADDR client_IPaddr
IP address of client.
Definition http.h:96
bool ExtractAuthentication(char **pPassword, char **pUser)