forked from jomaora/schapiuino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerHandler.cpp
225 lines (206 loc) · 5.83 KB
/
ServerHandler.cpp
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
/*
ServerHandler.cpp - Library for receive Http Requests and send Http responses.
Created by Joan Ortega, [email protected] Feb. 29, 2016.
Released into the public domain.
*/
#include "Arduino.h"
#include <Ethernet.h>
#include "ServerHandler.h"
void ServerHandler::init() {
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Ethernet.begin(_mac, _ip);
_server->begin();
if (_activeLogs) {
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
}
bool ServerHandler::listenRequest() {
_client = _server->available();
if (!_client) {
return false;
} else {
if (_activeLogs) {
Serial.println("incomming request");
}
resetRequest();
readRequest();
return true;
}
}
void ServerHandler::sendHTMLBasicResponse(String message) {
if (_requestReceived) {
_client.println("HTTP/1.1 200 OK");
_client.println("Content-Type: text/html");
_client.println("Connection: close");
_client.println();
_client.println("<!DOCTYPE HTML>");
_client.println("<html>");
_client.println(message);
_client.println("</html>");
delay(1);
_client.stop();
if (_activeLogs) {
Serial.println("client disconnected");
}
}
}
ServerHandler & ServerHandler::buildResponse(int statusCode, String message) {
if (_requestReceived) {
parseStatusCode(statusCode, message);
}
return *this;
}
ServerHandler & ServerHandler::appendHeaderResponse(String header) {
if (_requestReceived) {
_client.println(header);
}
return *this;
}
ServerHandler & ServerHandler::appendBodyResponse(String body) {
if (_requestReceived) {
_client.println();
_client.println(body);
}
return *this;
}
void ServerHandler::send() {
if (_requestReceived) {
delay(1);
_client.stop();
if (_activeLogs) {
Serial.println("client disconnected");
}
}
}
String ServerHandler::getHttpMethod() {
if (_requestReceived) {
int blankSpaceIndex = _requestLine.indexOf(" ");
String httpMethod = _requestLine.substring(0, blankSpaceIndex);
return _requestLine.substring(0, blankSpaceIndex);
} else {
return "";
}
}
String ServerHandler::getRequestPath() {
if (_requestReceived) {
int firstBlankSpaceIndex = _requestLine.indexOf(" ") + 1;
int secondBlankSpaceIndex = _requestLine.indexOf(" ", firstBlankSpaceIndex);
String path = _requestLine.substring(firstBlankSpaceIndex, secondBlankSpaceIndex);
return _requestLine.substring(firstBlankSpaceIndex, secondBlankSpaceIndex);
} else {
return "";
}
}
const char* ServerHandler::getRequestHeaders() {
return _requestHeaders.c_str();
}
const char* ServerHandler::getRequestBody() {
return _requestBody.c_str();
}
void ServerHandler::readRequest() {
boolean currentLineIsBlank = true;
int countCLRF = 0;
while (_client.connected() && !_requestReceived) {
if (_client.available()) {
char c = _client.read();
if (c == '\n' || c == '\r') {
_returnCharCount++;
} else {
_returnCharCount = 0;
}
if (_returnCharCount == 2) {
_readingStatus = 1;
}
if (_returnCharCount == 4) {
_readingStatus = 2;
}
switch (_readingStatus) {
case 0:
_requestLine.concat(c);
break;
case 1:
_requestHeaders.concat(c);
break;
case 2:
if (c != '\n') {
_requestBody.concat(c);
}
break;
}
} else {
_requestReceived = true;
}
}
}
void ServerHandler::resetRequest() {
_readingStatus = 0;
_returnCharCount = 0;
_requestLine = "";
_requestBody = "";
_requestHeaders = "";
_requestReceived = false;
}
void ServerHandler::parseStatusCode(int status, String message) {
_client.print("HTTP/1.1 ");
_client.print(status);
switch (status) {
case 200:
_client.println("OK");
break;
case 201:
_client.println("Accepted");
break;
case 204:
_client.println("No Content");
break;
case 400:
_client.println("Bad Request");
break;
case 401:
_client.println("Unauthorize");
break;
case 403:
_client.println("Forbidden");
break;
case 404:
_client.println("Not Found");
break;
case 405:
_client.println("Method Not Allowed");
break;
case 406:
_client.println("Not Acceptable");
break;
case 500:
_client.println("Internal server error");
break;
default:
_client.println(message);
break;
}
}
void ServerHandler::sendNonImplementedMethodResponse(String message) {
if (_requestReceived) {
buildResponse(405)
.appendHeaderResponse("Content-Type: text/plain")
.appendHeaderResponse("Connection: close")
.appendBodyResponse(message)
.send();
delay(1);
_client.stop();
}
}
void ServerHandler::sendNotFoundResponse(String message) {
if (_requestReceived) {
buildResponse(405)
.appendHeaderResponse("Content-Type: text/plain")
.appendHeaderResponse("Connection: close")
.appendBodyResponse(message)
.send();
delay(1);
_client.stop();
}
}