NetBurner 3.5.8
PDF Version
http.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
8
20
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
71
72enum WebsocketFlags
73{
74 WS_UPGRADE = 0x1,
75 WS_CONNECT = 0x2,
76 WS_VER_13 = 0x4,
77 WS_KEY_CONFIRM = 0x8
78};
79
87{
88 PSTR pURL;
91 PSTR pData;
92 PSTR pSep;
93 PSTR pHost;
95 PSTR wsKey;
97 uint16_t sep_len;
98 uint32_t content_length;
99 uint32_t rx_length;
104
105 void ProcessLine(PSTR startofline);
106 int Respond(int socket);
107
119 uint32_t ReadBudgetTicks() const;
120
121 bool ExtractAuthentication(char **pPassword, char **pUser);
122
123
124
125
149 const char * GetBoundaryMarker();
150
151
186 int ReadSimpleBody(int socket, const char *boundary = NULL);
187
188
189} __attribute__((packed));
190
191
197
198
199struct HTTP_Socket
200{
201 int FileDescriptor;
202 uint32_t StartTime;
203 IPADDR client_IPaddr;
204} __attribute__((packed));
205
206
207enum eWEB_ERROR
208{
209 eTCP_CONN_OPEN_NO_DATA, // WEB client open TCP port#80 conn, but no data request was sent
210 eTCP_CONN_OPEN_TO_LONG, // WEB client open TCP connect and send endless data ( for example - never ending POST).
211};
212
213
235{
236 protected:
238 const char *m_pUrlName;
241
244 void Remove();
245 virtual bool Match(HTTP_Request &req);
246
247 public:
265 HtmlPageHandler(const char *url, HTTP_RequestTypes rt = tGet, int accessGroup = 0, bool Before_Files = false);
266
271
285 static HtmlPageHandler *FindHandler(HTTP_Request &req, bool bBeforeFiles);
286
295
310 virtual int ProcessRaw(int sock, HTTP_Request &pd) = 0;
311
319 inline int GetGroup() { return m_access_group; };
320};
321
331typedef int(http_gethandlerfunc)(int sock, HTTP_Request &pd);
332
333
334typedef bool(http_matchhandlerfunc)(HTTP_Request &pd);
335
341{
342 protected:
344 http_matchhandlerfunc *m_mhf;
345
346 public:
347 inline virtual int ProcessRaw(int sock, HTTP_Request &pdt) { return m_pf(sock, pdt); };
348
361 inline CallBackFunctionPageHandler(const char *pUrl,
362 http_gethandlerfunc *pFunction,
363 HTTP_RequestTypes reqType = tGet,
364 int accessGroup = 0,
365 bool beforeFiles = false)
366 : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), m_pf(pFunction)
367 {
368 m_mhf = 0;
369 };
370
383 inline CallBackFunctionPageHandler(const char *pUrl,
384 http_gethandlerfunc *pFunction,
385 http_matchhandlerfunc *pMatchFunction,
386 HTTP_RequestTypes reqType = tGet,
387 int accessGroup = 0,
388 bool beforeFiles = false)
389 : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), m_pf(pFunction)
390 {
391 m_mhf = pMatchFunction;
392 };
393
394 virtual bool Match(HTTP_Request &req)
395 {
396 if (m_mhf)
397 return m_mhf(req);
398 else
399 return HtmlPageHandler::Match(req);
400 }
401};
402
403
404
405
406class HtmlConfigExposer : public HtmlPageHandler
407{
408protected:
409 config_leaf * pLeaf;
410public:
411 inline HtmlConfigExposer(const char *pUrl, config_leaf & leaf,
412 HTTP_RequestTypes reqType = tGet,
413 int accessGroup = 0,
414 bool beforeFiles = false
415 )
416 : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), pLeaf(&leaf)
417 {
418 }
419
420virtual int ProcessRaw(int sock, HTTP_Request &pd);
421};
422
423
424typedef void (ws_createhandlerfunc)(int web_socket_fd);
425
426class CallBackWSEndPoint : public HtmlPageHandler
427{
428 ws_createhandlerfunc *m_pf;
429public:
430 int ProcessRaw(int sock, HTTP_Request &pdt);
431 inline CallBackWSEndPoint(const char *pUrl,
432 ws_createhandlerfunc *pFunction,
433 int accessGroup = 0,
434 bool beforeFiles = false)
435 : HtmlPageHandler(pUrl, tGet, accessGroup, beforeFiles), m_pf(pFunction){};
436
437
438};
439
440
441
442
443extern const char *pHttpRealm; // The realm used for password requests
444
458HTTP_ACCESS CheckHttpAccess(int sock, int access_level, HTTP_Request &Req);
459
471void StartHttp(uint16_t port /* = 80 */, bool RunConfigMirror /* = true */);
472
473typedef int(http_errorhandler)(IPADDR ip, enum eWEB_ERROR Err, void *prm);
474
475/*Setup a custom WEB Error Report Handler */
476http_errorhandler *SetNewErrorHandler(http_errorhandler *newhandler);
477
483void StopHttp();
484
494void SendHTMLHeader(int sock);
495
506void SendHTMLHeaderWCookie(int sock, char *cookie);
507
517void SendTextHeader(int sock);
518
529void SendGifHeader(int sock);
530
539void EmptyResponse(int sock);
540
549void NoContentResponse(int sock);
550
560void RedirectResponse(int sock, PCSTR new_page);
561
571void NotFoundResponse(int sock, PCSTR new_page);
572
582void ForbiddenResponse(int sock, PCSTR new_page);
583
594void NotAvailableResponse(int sock, PCSTR new_page);
595
607void BadRequestResponse(int sock, PCSTR url, PCSTR data);
608
609void writesafestring(int fd, PCSTR str);
610
623int writeallsafestring(int fd, PCSTR str);
624
638int writesafestring(int fd, PCSTR str, size_t strLength);
639
640// Encode a uri component
641int decodeURI(char *str);
642
665int httpstricmp(PCSTR str1, PCSTR strIsUpper2);
666
667void append(char *&cpto, const char *cpfrm);
668
669//Add extra headers to html responses.
670extern const char * pExtraHeaders;
671
672
673
674#endif
675
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:361
virtual bool Match(HTTP_Request &req)
Determines if this handler matches the incoming request.
Definition http.h:394
virtual int ProcessRaw(int sock, HTTP_Request &pdt)
Pure virtual method that must be implemented to process HTTP requests.
Definition http.h:347
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:383
Base class for HTTP request handlers that process requests for specific URLs and HTTP methods.
Definition http.h:235
HtmlPageHandler * m_pNextHandler
Pointer to next handler in the linked list for efficient traversal.
Definition http.h:237
HTTP_RequestTypes m_requestTypes
HTTP method types this handler responds to (GET, POST, etc.).
Definition http.h:240
const char * m_pUrlName
URL pattern to match. Empty string or NULL matches all requests.
Definition http.h:238
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.
void MoveHandlerToLast()
Moves this handler to the end of the handler chain, making it function as a 404 handler.
virtual int ProcessRaw(int sock, HTTP_Request &pd)=0
Pure virtual method that must be implemented to process HTTP requests.
static HtmlPageHandler * FindHandler(HTTP_Request &req, bool bBeforeFiles)
Locates the appropriate handler for an incoming HTTP request.
int SortValue(HtmlPageHandler *pv)
Compares handler priorities for sorting (-1, 0, 1).
int GetGroup()
Returns the access control group identifier for this handler.
Definition http.h:319
virtual bool Match(HTTP_Request &req)
Determines if this handler matches the incoming request.
void Remove()
Removes handler from the linked list.
void InsertSort(HtmlPageHandler *&ph)
Inserts handler into sorted linked list based on priority.
int m_access_group
Access control group identifier for authorization checks.
Definition http.h:239
~HtmlPageHandler()
Destructor that automatically unregisters the handler from the global chain.
IPADDR6 IPADDR
IPADDR Object Type (either v4 or v6).
Definition nettypes.h:568
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
HTTP Request Read Result.
Definition http.h:64
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:331
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:65
@ HTTP_RRR_FINISHED
Read read all content as indicated by headers.
Definition http.h:66
@ HTTP_RRR_SOCK_ERR
Socket returned error while having no data, probably closed.
Definition http.h:69
@ HTTP_RRR_END_FOUND
Read found the trailing \r\n and terminated.
Definition http.h:67
@ 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
HTTP Request Structure.
Definition http.h:87
void ProcessLine(PSTR startofline)
internal function
uint32_t content_length
Content length field from HTML header.
Definition http.h:98
uint16_t sep_len
Length of separator for multipart forms.
Definition http.h:97
int Respond(int socket)
internal function
PSTR pSep
Separator for multipart forms, null if not present.
Definition http.h:92
PSTR last_datarx
Pointer to last data read, internal use only.
Definition http.h:94
PSTR pHost
Pointer to host record null if not present.
Definition http.h:93
PSTR wsKey
Web socket ket, internal use only.
Definition http.h:95
PSTR wsProtocol
Web socket prototocl, internal use only.
Definition http.h:96
uint32_t read_start_secs
Value of Secs when this request started, internal use only.
Definition http.h:100
PSTR pFirstCookie
First cookie if present, otherwise null. More may follow.
Definition http.h:90
PSTR pData
Pointer to entire data set. Note: not null terminated.
Definition http.h:91
HTTP_RequestTypes req
Type of request HTTP_RequestTypes.
Definition http.h:103
PSTR pAuthorization
Authorization header if present, otherwise null.
Definition http.h:89
uint8_t websocketFlags
Web socket flags.
Definition http.h:102
uint32_t ReadBudgetTicks() const
Ticks a body read may block for without exceeding this request's total read budget.
PSTR pURL
Request URL.
Definition http.h:88
const char * GetBoundaryMarker()
Extracts the boundary marker string from multipart Content-Type headers.
int ReadSimpleBody(int socket, const char *boundary=NULL)
Reads the HTTP request body from a socket into the internal buffer and processes termination conditio...
uint32_t rx_length
Total bytes receive from request, internal use only.
Definition http.h:99
IPADDR client_IPaddr
IP address of client.
Definition http.h:101
bool ExtractAuthentication(char **pPassword, char **pUser)
used to extract user name and password in authentication test