forked from lasselukkari/aWOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaWOT.h
306 lines (217 loc) · 7.36 KB
/
aWOT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
aWOT, Express.js inspired microcontreller web framework for the Web of Things
Copyright 2014 Lasse Lukkari
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef HTTPSERVER_H_
#define HTTPSERVER_H_
#include <string.h>
#include <stdlib.h>
#include <Arduino.h>
#include "Client.h"
#define CRLF "\r\n"
#ifndef SERVER_DEFAULT_REQUEST_LENGTH
#define SERVER_DEFAULT_REQUEST_LENGTH 64
#endif
#ifndef SERVER_PARAM_LENGTH
#define SERVER_PARAM_LENGTH 64
#endif
#ifndef SERVER_HEADERS_COUNT
#define SERVER_HEADERS_COUNT 5
#endif
#ifndef SERVER_READ_TIMEOUT_IN_MS
#define SERVER_READ_TIMEOUT_IN_MS 1000
#endif
#ifndef SERVER_URL_PATH_COMMAND_LENGTH
#define SERVER_URL_PATH_COMMAND_LENGTH 8
#endif
#ifndef SERVER_FAIL_MESSAGE
#define SERVER_FAIL_MESSAGE "<h1>400 Bad Request</h1>"
#endif
#ifndef SERVER_NOT_FOUND_MESSAGE
#define SERVER_NOT_FOUND_MESSAGE "<h1>404 Not Found</h1>"
#endif
#ifndef SERVER_AUTH_MESSAGE
#define SERVER_AUTH_MESSAGE "<h1>401 Unauthorized</h1>"
#endif
#ifndef SERVER_FORBIDDEN_MESSAGE
#define SERVER_FORBIDDEN_MESSAGE "<h1>403 Forbidden</h1>"
#endif
#ifndef SERVER_SERVER_ERROR_MESSAGE
#define SERVER_SERVER_ERROR_MESSAGE "<h1>500 Internal Server Error</h1>"
#endif
extern "C" unsigned long millis(void);
#define P(name) static const unsigned char name[] PROGMEM
#define SIZE(array) (sizeof(array) / sizeof(*array))
#ifdef _VARIANT_ARDUINO_DUE_X_
#define pgm_read_byte(ptr) (unsigned char)(* ptr)
#endif
class Request: public Stream {
public:
enum MethodType {
INVALID, GET, HEAD, POST, PUT, DELETE, PATCH, OPTIONS, ALL, USE
};
struct HeaderNode {
const char* name;
char* buffer;
int size;
HeaderNode* next;
};
Request();
void init(Client *client, char* buff, int bufflen);
void processRequest();
void processHeaders(HeaderNode* headerTail);
MethodType method();
int contentLeft();
bool next();
void discontinue();
char * urlPath();
int urlPathLength();
void routeString(const char * routeString);
char ** route();
char * route(const char* name);
char * route(int number);
char * query();
char * query(const char *key);
bool queryComplete();
bool postParam(char *name, int nameLen, char *value, int valueLen);
char * header(const char *name);
void slicePath(int prefixLength);
void unSlicePath(int prefixLength);
int available();
int read();
int peek();
void push(int ch);
void flush();
// dummy implementation to fulfill the Stream interface
size_t write(uint8_t ch) {return 0;}
void reset();
private:
void m_readHeader(char *value, int valueLen);
bool m_readInt(int &number);
bool m_expect(const char *expectedStr);
Client * m_clientObject;
MethodType m_methodType;
unsigned char m_pushback[32];
int m_pushbackDepth;
bool m_readingContent;
char m_paramBuffer[SERVER_PARAM_LENGTH];
int m_contentLeft;
HeaderNode* m_headerTail;
char * m_query;
bool m_queryComplete;
char * m_urlPath;
int m_urlPathLength;
char * m_urlPathParts[SERVER_URL_PATH_COMMAND_LENGTH];
int m_urlPathPartsCount;
int m_hexToInt(char *hex);
const char * m_route;
bool m_next;
};
class Response: public Stream {
public:
Response();
void init(Client *client);
void printP(const unsigned char *str);
void printP(const char *str) {printP((unsigned char*) str);}
void writeP(const unsigned char *data, size_t length);
size_t write(uint8_t ch);
size_t write(uint8_t *ch, size_t size);
void set(const char* name, const char* value);
void success(const char *contentType = "application/json; charset=utf-8");
void created(const char *contentType = "application/json; charset=utf-8");
void noContent();
void seeOther(const char *otherURL);
void fail();
void unauthorized();
void forbidden();
void notFound();
void serverError();
void reset();
//dummy implementations to fulfill Stream interface
int available() {return 0;}
int read() {return -1;}
int peek() {return -1;}
void flush() {return;}
private:
void m_printCRLF();
void m_printHeaders();
Client * m_clientObject;
struct Headers {
const char* name;
const char* value;
} m_headers[SERVER_HEADERS_COUNT];
unsigned int m_headersCount;
};
class Router {
public:
typedef void Middleware(Request& request, Response& response);
Router(const char * urlPrefix = "");
bool dispatchCommands(Request& request, Response& response);
void get(const char* urlPattern, Middleware* command);
void post(const char* urlPattern, Middleware* command);
void put(const char* urlPattern, Middleware* command);
void del(const char* urlPattern, Middleware* command);
void patch(const char* urlPattern, Middleware* command);
void options(const char* urlPattern, Middleware* command);
void all(const char* urlPattern, Middleware* command);
void use(Middleware* command);
void addCommand(Request::MethodType type, const char* urlPattern,Middleware* command);
Router * getNext();
void setNext(Router * next);
private:
struct CommandNode {
const char* urlPattern;
Middleware* command;
Request::MethodType type;
CommandNode* next;
};
CommandNode* m_tailCommand;
Router* m_next;
bool m_routeMatch(const char *str, const char *pattern);
const char * m_urlPrefix;
};
class WebApp {
public:
WebApp();
void process(Client *client);
void process(Client *client, char* buff, int bufflen);
void failCommand(Router::Middleware* command);
void notFoundCommand(Router::Middleware* command);
void get(const char* urlPattern, Router::Middleware* command);
void post(const char* urlPattern, Router::Middleware* command);
void put(const char* urlPattern, Router::Middleware* command);
void del(const char* urlPattern, Router::Middleware* command);
void patch(const char* urlPattern, Router::Middleware* command);
void options(const char* urlPattern, Router::Middleware* command);
void all(const char* urlPattern, Router::Middleware* command);
void use(Router::Middleware* command);
void use(Router * router);
void readHeader(const char* name, char* buffer, int bufflen);
private:
static void m_defaultFailCommand(Request &request, Response &response);
static void m_defaultNotFoundCommand(Request &request, Response &response);
Client * m_clientObject;
Request m_request;
Response m_response;
Router * m_routerTail;
Router m_defaultRouter;
Router::Middleware* m_failureCommand;
Router::Middleware* m_notFoundCommand;
Request::HeaderNode* m_headerTail;
};
#endif