NetBurner 3.1
http.h
Go to the documentation of this file.
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
13 #ifndef _NB_HTTP_H
14 #define _NB_HTTP_H
15 
16 #include <nettypes.h>
17 
18 
19 const int CONFIG_ACCESS_GROUP =99;
20 
29 {
31  tGet,
34 };
36 
37 enum WebsocketFlags
38 {
39  WS_UPGRADE = 0x1,
40  WS_CONNECT = 0x2,
41  WS_VER_13 = 0x4,
42  WS_KEY_CONFIRM = 0x8
43 };
44 
48 struct HTTP_Request
49 {
50  PSTR pURL;
52  PSTR pFirstCookie;
53  PSTR pData;
54  PSTR pSep;
55  PSTR last_datarx;
56  PSTR wsKey;
57  PSTR wsProtocol;
58  uint16_t sep_len;
59  uint32_t content_length;
60  uint32_t rx_length;
62  uint8_t websocketFlags;
64 
65  void ProcessLine(PSTR startofline);
66  int Respond(int socket);
67  bool ExtractAuthentication(char **pPassword, char **pUser);
68 } __attribute__((packed));
70 
71 struct HTTP_Socket
72 {
73  int FileDescriptor;
74  uint32_t StartTime;
75  IPADDR client_IPaddr;
76 } __attribute__((packed));
77 
78 typedef int(http_wshandler)(HTTP_Request *req, int sock, PSTR url, PSTR rxb);
79 
80 enum eWEB_ERROR
81 {
82  eTCP_CONN_OPEN_NO_DATA, // WEB client open TCP port#80 conn, but no data request was sent
83  eTCP_CONN_OPEN_TO_LONG, // WEB client open TCP connect and send endless data ( for example - never ending POST).
84 };
85 
86 /*
87  * To handle GET requests for a specific URL, build a GET handler object for that url
88  * This is the base class for all GET handlers. A NULL name will be a catch all for all GET requests.
89  */
90 class HtmlPageHandler
91 {
92  protected:
93  HtmlPageHandler *m_pNextHandler;
94  const char *m_pUrlName; // Does a length match. So an empty string matches EVERYTHING...
95  int m_access_group; // The access group for this request see @ref CheckkHttpAccess
96  HTTP_RequestTypes m_requestTypes;
97  void InsertSort(HtmlPageHandler *&ph);
98  int SortValue(HtmlPageHandler *pv); // Returns -1,0,1 depding on if the passe din value sort compare.
99  void Remove();
100  virtual bool Match(HTTP_Request &req);
101 
102  public:
103  /*
104  * Register things to handle.
105  * url: The name to register for. "" empty matches everything.
106  * rt: The request type to respond to.
107  * Before_Files indicates if the response should be before or after the compiled in files.
108  */
109  HtmlPageHandler(const char *url, HTTP_RequestTypes rt = tGet, int accessGroup = 0, bool Before_Files = false);
110  ~HtmlPageHandler();
111 
112  static HtmlPageHandler *FindHandler(HTTP_Request &req, bool bBeforeFiles);
113 
114  // This class will do a call back with data for each request to the specified url
115  // Returns 0 if it did not process the request.
116  virtual int ProcessRaw(int sock, HTTP_Request &pd) = 0;
117 
118  inline int GetGroup() { return m_access_group; };
119 };
120 
121 typedef int(http_gethandlerfunc)(int sock, HTTP_Request &pd);
122 typedef bool(http_matchhandlerfunc)(HTTP_Request &pd);
123 
124 /*
125  * This impliments the above as a function pointer callback
126  *
127  * Return values:
128  * 0 = not handled (but it changes nothing)
129  * 1 = handled
130  * 2- Kept socket, it will get closed by some other method...
131  * (Say you want to send out a huge generated filbe on a differnt task)
132  * So if you return anything except a 2, things process normally....
133  */
134 
138 class CallBackFunctionPageHandler : public HtmlPageHandler
139 {
140  protected:
141  http_gethandlerfunc *m_pf;
142  http_matchhandlerfunc *m_mhf;
143 
144  public:
145  inline virtual int ProcessRaw(int sock, HTTP_Request &pdt) { return m_pf(sock, pdt); };
146 
161  inline CallBackFunctionPageHandler(const char *pUrl,
162  http_gethandlerfunc *pFunction,
163  HTTP_RequestTypes reqType = tGet,
164  int accessGroup = 0,
165  bool beforeFiles = false)
166  : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), m_pf(pFunction)
167  {
168  m_mhf = 0;
169  };
170 
171  inline CallBackFunctionPageHandler(const char *pUrl,
172  http_gethandlerfunc *pFunction,
173  http_matchhandlerfunc *pMatchFunction,
174  HTTP_RequestTypes reqType = tGet,
175  int accessGroup = 0,
176  bool beforeFiles = false)
177  : HtmlPageHandler(pUrl, reqType, accessGroup, beforeFiles), m_pf(pFunction)
178  {
179  m_mhf = pMatchFunction;
180  };
181 
182  virtual bool Match(HTTP_Request &req)
183  {
184  if (m_mhf)
185  return m_mhf(req);
186  else
187  return HtmlPageHandler::Match(req);
188  }
189 };
191 
192 enum HTTP_ACCESS
193 {
194  HTTP_OK_TO_SERVE, //< Ok to serve the page
195  HTTP_NEED_PASSWORD, //< Either the password in the HTTP request is wrong or missing
196  HTTP_NOTFOUND, //< Report the page is not found
197  HTTP_FORBIDEN, //< Report the page forbiden
198  HTTP_RESPONSE_HANDLED //< This means this function handled the full response, including closing the socket.
199 };
200 
201 extern const char *pHttpRealm; // The realm used for password requests
202 
216 HTTP_ACCESS CheckHttpAccess(int sock, int accessGroup, HTTP_Request &Req);
217 
230 void StartHttp(uint16_t port /* = 80 */);
231 
232 typedef int(http_errorhandler)(IPADDR ip, enum eWEB_ERROR Err, void *prm);
233 typedef int(http_wshandler)(HTTP_Request *req, int sock, PSTR url, PSTR rxb);
234 
235 /*Setup a custom WEB Error Report Handler */
236 http_errorhandler *SetNewErrorHandler(http_errorhandler *newhandler);
237 
238 /*Setup a custom Websocket Request Handler */
239 http_wshandler *SetNewWSHandler(http_wshandler *newhandler);
240 
248 void StopHttp();
249 
261 void SendHTMLHeader(int sock);
262 
275 void SendHTMLHeaderWCookie(int sock, char *cookie);
276 
288 void SendTextHeader(int sock);
289 
302 void SendGifHeader(int sock);
303 
315 void RedirectResponse(int sock, PCSTR new_page);
316 
328 void NotFoundResponse(int sock, PCSTR new_page);
329 
341 void ForbiddenResponse(int sock, PCSTR url);
342 
355 void NotAvailableResponse(int sock, PCSTR url);
356 
371 void BadRequestResponse(int sock, PCSTR url, PCSTR data);
372 
373 void writesafestring(int fd, PCSTR str);
374 
387 int writeallsafestring(int fd, PCSTR str);
388 
389 // Encode a uri component
390 int decodeURI(char *str);
391 
414 int httpstricmp(PCSTR str1, PCSTR strIsUpper2);
415 
416 void append(char *&cpto, const char *cpfrm);
417 
418 #endif
PSTR pURL
Request URL.
Definition: http.h:50
void StopHttp()
Stop the HTTP web server.
Definition: httpinternal.cpp:350
PSTR pData
Pointer to entire data set. Note: not null terminated.
Definition: http.h:53
uint16_t sep_len
Length of separator for multipart forms.
Definition: http.h:58
PSTR pSep
Separator for multipart forms, null if not present.
Definition: http.h:54
void ForbiddenResponse(int sock, PCSTR url)
Send a page is forbidden response.
Definition: httpinternal.cpp:477
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition: ipv6_addr.h:28
void NotAvailableResponse(int sock, PCSTR url)
Send a response indicating that the requested resource is not available at this time.
Definition: httpinternal.cpp:487
GET request.
Definition: http.h:31
void SendTextHeader(int sock)
Send a HTML plain text header.
Definition: httpinternal.cpp:273
HTTP_ACCESS CheckHttpAccess(int sock, int accessGroup, HTTP_Request &Req)
All HTTP requests go though this function.
Definition: http_weak_auth.cpp:5
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:161
uint8_t websocketFlags
Web socket flags.
Definition: http.h:62
PSTR pAuthorization
Authorization header if present, otherwise null.
Definition: http.h:51
Header request, does not include content.
Definition: http.h:33
PSTR wsKey
Web socket ket, internal use only.
Definition: http.h:56
uint32_t rx_length
Total bytes receive from request, internal use only.
Definition: http.h:60
void BadRequestResponse(int sock, PCSTR url, PCSTR data)
Send a response indicating that the client request itself is faulty in some manner.
Definition: httpinternal.cpp:497
PSTR wsProtocol
Web socket prototocl, internal use only.
Definition: http.h:57
PSTR pFirstCookie
First cookie if present, othewise null. More may follow.
Definition: http.h:52
void RedirectResponse(int sock, PCSTR new_page)
Redirect a HTTP request to a different page.
Definition: httpinternal.cpp:462
void SendHTMLHeaderWCookie(int sock, char *cookie)
Send a HTML response header and cookie.
Definition: httpinternal.cpp:266
int httpstricmp(PCSTR str1, PCSTR strIsUpper2)
Special string compare. Returns 1 if the strings match until one string ends with a null (0)...
Definition: httpstricmp.cpp:10
void StartHttp(uint16_t port)
Start the HTTP web server. Further documentation in the Initialization section Initialization - Syste...
Definition: http.cpp:296
void SendHTMLHeader(int sock)
Send a HTML response header.
Definition: httpinternal.cpp:261
HTTP_RequestTypes
Definition: http.h:28
The type of request in not yet known.
Definition: http.h:30
PSTR last_datarx
Pointer to last data read, internal use only.
Definition: http.h:55
HTTP_RequestTypes req
Type of request HTTP Request Types.
Definition: http.h:63
uint32_t content_length
Content length field from HTML header.
Definition: http.h:59
int writeallsafestring(int fd, PCSTR str)
Send a string and escape all special characters.
Definition: httpinternal.cpp:388
void SendGifHeader(int sock)
Send a HTML GIF header.
Definition: httpinternal.cpp:278
void NotFoundResponse(int sock, PCSTR new_page)
Send a page not found response.
Definition: httpinternal.cpp:469
POST request.
Definition: http.h:32
IPADDR client_IPaddr
IP address of client.
Definition: http.h:61