-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathwebserver.cpp
More file actions
153 lines (131 loc) · 3.43 KB
/
Copy pathwebserver.cpp
File metadata and controls
153 lines (131 loc) · 3.43 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
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
//
// webserver.cpp
//
// Circle - A C++ bare metal environment for Raspberry Pi
// Copyright (C) 2015-2025 R. Stange <rsta2@gmx.net>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "webserver.h"
#include <circle/logger.h>
#include <circle/string.h>
#include <circle/util.h>
#include <assert.h>
#define TIMEOUT_SECONDS 10 // receive timeout
#define MAX_CONTENT_SIZE 4000
// our content
static const char s_Index[] =
#include "index.h"
;
static const u8 s_Style[] =
#include "style.h"
;
static const u8 s_LEDOff[] =
{
#include "ledoff.h"
};
static const u8 s_LEDOn[] =
{
#include "ledon.h"
};
static const u8 s_Favicon[] =
{
#include "favicon.h"
};
static const char FromWebServer[] = "webserver";
CWebServer::CWebServer (CNetSubSystem *pNetSubSystem, CActLED *pActLED, CSocket *pSocket)
: CHTTPDaemon (pNetSubSystem, pSocket, MAX_CONTENT_SIZE, HTTP_PORT, 0, TIMEOUT_SECONDS),
m_pActLED (pActLED)
{
}
CWebServer::~CWebServer (void)
{
m_pActLED = 0;
}
CHTTPDaemon *CWebServer::CreateWorker (CNetSubSystem *pNetSubSystem, CSocket *pSocket)
{
return new CWebServer (pNetSubSystem, m_pActLED, pSocket);
}
THTTPStatus CWebServer::GetContent (const char *pPath,
const char *pParams,
const char *pFormData,
u8 *pBuffer,
unsigned *pLength,
const char **ppContentType)
{
assert (pPath != 0);
assert (ppContentType != 0);
assert (m_pActLED != 0);
CString String;
const u8 *pContent = 0;
unsigned nLength = 0;
if ( strcmp (pPath, "/") == 0
|| strcmp (pPath, "/index.html") == 0)
{
assert (pFormData != 0);
if (strcmp (pFormData, "actled=1") == 0)
{
m_pActLED->On ();
String.Format (s_Index, "", "checked", "ledon.png"); // must match index.html
}
else
{
m_pActLED->Off ();
String.Format (s_Index, "checked", "", "ledoff.png");
}
pContent = (const u8 *) (const char *) String;
nLength = String.GetLength ();
*ppContentType = "text/html; charset=iso-8859-1";
}
else if (strcmp (pPath, "/style.css") == 0)
{
pContent = s_Style;
nLength = sizeof s_Style-1;
*ppContentType = "text/css";
}
else if (strcmp (pPath, "/ledoff.png") == 0)
{
pContent = s_LEDOff;
nLength = sizeof s_LEDOff;
*ppContentType = "image/png";
}
else if (strcmp (pPath, "/ledon.png") == 0)
{
pContent = s_LEDOn;
nLength = sizeof s_LEDOn;
*ppContentType = "image/png";
}
else if (strcmp (pPath, "/favicon.ico") == 0)
{
pContent = s_Favicon;
nLength = sizeof s_Favicon;
*ppContentType = "image/x-icon";
}
else
{
return HTTPNotFound;
}
assert (pLength != 0);
if (*pLength < nLength)
{
CLogger::Get ()->Write (FromWebServer, LogError, "Increase MAX_CONTENT_SIZE to at least %u", nLength);
return HTTPInternalServerError;
}
assert (pBuffer != 0);
assert (pContent != 0);
assert (nLength > 0);
memcpy (pBuffer, pContent, nLength);
*pLength = nLength;
return HTTPOK;
}