TS-E+ базовая библиотека.
libTSE+
 Указатель Классы Функции Переменные Определения типов
httpd.h
1 /*
2  * httpd.h
3  *
4  * Created on: Apr 16, 2015
5  * Author: nick
6  */
7 
8 #ifndef HTTPD_H_
9 #define HTTPD_H_
10 
11 #include "net.h"
12 #include "vector.h"
13 #include "vfs.h"
14 #include <string.h>
15 
19 typedef struct {
20  char KEY[FILENAME_SIZE];
21  char VALUE[FILENAME_SIZE];
22 } KV;
23 
27 typedef enum {
28  text,
29  html,
30  css,
31  js,
32  json,
33  xml,
34  png,
35  gif,
36  jpeg,
37  binary
38 } MIME;
39 
43 typedef enum {
44  method_none = 0, post, get
45 } RequestMethod;
46 
50 typedef enum {
51  s200,
52  s301,
53  s400,
54  s401, //
55  s403,
56  s404,
57  s500
58 } HTTPStatus;
59 
63 typedef struct Realm_ {
64  char PATH[FILENAME_SIZE]; // Пути, попадающие под Realm
65  char REALM[FILENAME_SIZE]; // имя
66  char AUTHFILE[FILENAME_SIZE]; // полный путь к файлу с паролями. Может отсутствовать
67  Realm_() {
68  memset(PATH,0,FILENAME_SIZE);
69  memset(REALM,0,FILENAME_SIZE);
70  memset(AUTHFILE,0,FILENAME_SIZE);
71  }
72  Realm_(const char *name,const char *path) {
73  strcpy(REALM,name);
74  strcpy(PATH,path);
75  memset(AUTHFILE,0,FILENAME_SIZE);
76  }
77  Realm_(const char *name,const char *path, const char *auth) {
78  strcpy(REALM,name);
79  strcpy(PATH,path);
80  strcpy(AUTHFILE,auth);
81  }
82 } Realm;
83 
87 class HTTPD : public SocketListener {
88 private:
89  uint32_t _timeout;
90  Vector<Realm *> _security; // Коллекция security realm-ов
91 public:
92  HTTPD(uint16_t port,uint32_t timeout) : SocketListener(PROTO_TCP,port),_timeout(timeout),_security(Vector<Realm*>::defaultDestructor) {
93  }
94  virtual void handle(Socket *);
95  virtual uint32_t timeout() { return _timeout; }
96  virtual void release(Socket *);
103  bool sendFile(Socket *socket,VFSFile *file, const bool parseVars);
111  char *getParam(Socket *socket,const char *name);
115  Vector<KV *> *getParams(Socket *socket);
119  Vector<KV *> *getCookies(Socket *socket);
123  char *getCookie(Socket *socket,const char*name);
128  char *getParam_P(Socket *,const char*);
132  char *getCookie_P(Socket *,const char*);
139  void setCookie(Socket *socket,const char *name, const char *value);
143  void setCookie_P(Socket *,const char *, const char *);
149  void deleteCookie(Socket *socket, const char *name);
155  void deleteCookie_P(Socket *socket, const char *name);
156 
162  void setHeaderField(Socket *socket, const char *field);
169  void sendHeader(Socket *socket,const HTTPStatus status, const MIME mime);
175  void sendError(Socket *socket,const HTTPStatus status);
179  uint8_t *getContent(Socket *);
183  size_t getContentSize(Socket *);
184 
188  RequestMethod getRequestType(Socket *);
189 
190  virtual void onRegister() {
193  }
194 protected:
200  virtual void handleRequest(Socket *socket,char *path);
206  virtual void handleVariable(Socket *socket,const char *var);
214  virtual bool handleAuth(Realm *realm,const char *user, const char *password);
218  virtual void readSecuritySettings();
222  void setTimeout(uint32_t timeout) { _timeout = timeout; }
229  VFSFile *openFile(const char *name, bool &isGZipped);
234  void addRealm(Realm *realm);
235 };
236 
237 
238 #endif /* HTTPD_H_ */
size_t getContentSize(Socket *)
const uint16_t port()
Definition: net.h:275
char * getParam(Socket *socket, const char *name)
Definition: httpd.h:87
void setHeaderField(Socket *socket, const char *field)
virtual void handle(Socket *)
char * getParam_P(Socket *, const char *)
Definition: httpd.h:19
char * getCookie(Socket *socket, const char *name)
char * getCookie_P(Socket *, const char *)
virtual bool handleAuth(Realm *realm, const char *user, const char *password)
virtual void readSecuritySettings()
VFSFile * openFile(const char *name, bool &isGZipped)
uint8_t * getContent(Socket *)
virtual uint32_t timeout()
Definition: httpd.h:95
virtual void onRegister()
Definition: net.h:293
RequestMethod getRequestType(Socket *)
virtual void handleVariable(Socket *socket, const char *var)
void setCookie(Socket *socket, const char *name, const char *value)
virtual void handleRequest(Socket *socket, char *path)
void setTimeout(uint32_t timeout)
Definition: httpd.h:222
Definition: net.h:247
void setCookie_P(Socket *, const char *, const char *)
virtual void onRegister()
Definition: httpd.h:190
Definition: net.h:155
void deleteCookie(Socket *socket, const char *name)
void addRealm(Realm *realm)
void deleteCookie_P(Socket *socket, const char *name)
Vector< KV * > * getCookies(Socket *socket)
void sendError(Socket *socket, const HTTPStatus status)
SocketListener(const uint8_t protocol, const uint16_t port)
Definition: net.h:259
Definition: vector.h:18
Definition: vfs.h:28
Vector< KV * > * getParams(Socket *socket)
bool sendFile(Socket *socket, VFSFile *file, const bool parseVars)
virtual void release(Socket *)
Definition: httpd.h:63
void sendHeader(Socket *socket, const HTTPStatus status, const MIME mime)