-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml510.cpp
More file actions
89 lines (89 loc) · 2.19 KB
/
Copy pathhtml510.cpp
File metadata and controls
89 lines (89 loc) · 2.19 KB
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
#include "html510.h"
void HTML510Server::begin(int port)
{
server.begin(port);
}
void HTML510Server::attachHandler(String key, void (*handler)())
{
char lastchar = key[key.length() - 1];
if (lastchar != ' ' && lastchar != '=')
key = key + " ";
handlerptrs[numHandler] = handler;
handlerpars[numHandler] = "GET " + key;
numHandler++;
}
//*** HTML text handling routines ****
void HTML510Server::sendhtml(String data)
{
String s = HTMLtext;
s = s + data;
client.print(s);
}
void HTML510Server::sendplain(String data)
{
String s = plaintext;
s = s + data;
client.print(s);
}
String HTML510Server::getText()
{
String txt = "";
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (c <= ' ')
break;
txt += c;
}
}
return txt;
}
int HTML510Server::getVal()
{
String num = "";
while (client.connected())
{
if (client.available())
{
char d = client.read();
num += d;
if (d != '-' && (d < '0' || d > '9'))
break;
}
}
return num.toInt();
}
//*** web server hack ***
// Could use Webserver.h routines, but they are slower, so use a faster hack
// looks for key parameter at beginning of header, ignores everything else
void HTML510Server::serve()
{
client = server.accept();
if (client)
{
String currentLine = "";
while (client.connected())
{
if (client.available())
{
char c = client.read();
// Serial.write(c);
if (c == '\n')
break; // only read 1st line, break & close connection
currentLine += c;
for (int i = 0; i < numHandler; i++)
{
if (currentLine.startsWith(handlerpars[i]))
{
(*handlerptrs[i])();
client.stop(); // found one, so close the connection
return;
}
}
}
}
client.stop(); // Close the connection
}
}