Skip to content

Commit 777e8d7

Browse files
Convert ESP8266WebServer* into templatized model
Supercedes #4912 Refactor the three versions of ESP8266WebServer and *WebServerSecure to a single templated class. Use "using" to enable old, non-templated names to b used (so no user changes required to compile or run). Fixes #4908 and clean up the code base a lot. Basic tests run (the ones in the example code). No code changes are required in userland except for setting the SSL certificates which now use a cleaner "getServer()" accessor and lets the app use the native BearSSL calls on the WiFiClientSecure object. @devyte should be proud, it removes virtuals and even has template specialization...
1 parent 882d5ba commit 777e8d7

16 files changed

+212
-584
lines changed

libraries/ESP8266HTTPUpdateServer/examples/SecureBearSSLUpdater/SecureBearSSLUpdater.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const char* ssid = STASSID;
3232
const char* password = STAPSK;
3333

3434
BearSSL::ESP8266WebServerSecure httpServer(443);
35-
ESP8266HTTPUpdateServer httpUpdater;
35+
ESP8266HTTPUpdateServerTemplate<BearSSL::WiFiServerSecure, BearSSL::WiFiClientSecure> httpUpdater;
3636

3737
static const char serverCert[] PROGMEM = R"EOF(
3838
-----BEGIN CERTIFICATE-----

libraries/ESP8266HTTPUpdateServer/examples/SecureHTTPSUpdater/SecureHTTPSUpdater.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const char* ssid = STASSID;
6161
const char* password = STAPSK;
6262

6363
ESP8266WebServerSecure httpServer(443);
64-
ESP8266HTTPUpdateServer httpUpdater;
64+
ESP8266HTTPUpdateServerTemplate<axTLS::WiFiServerSecure, axTLS::WiFiClientSecure> httpUpdater;
6565

6666
// The certificate is stored in PMEM
6767
static const uint8_t x509[] PROGMEM = {

libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer.cpp renamed to libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ static const char serverIndex[] PROGMEM =
1616
static const char successResponse[] PROGMEM =
1717
"<META http-equiv=\"refresh\" content=\"15;URL=/\">Update Success! Rebooting...\n";
1818

19-
ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug)
19+
template <typename ServerType, typename ClientType>
20+
ESP8266HTTPUpdateServerTemplate<ServerType, ClientType>::ESP8266HTTPUpdateServer(bool serial_debug)
2021
{
2122
_serial_output = serial_debug;
2223
_server = NULL;
@@ -25,7 +26,8 @@ ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug)
2526
_authenticated = false;
2627
}
2728

28-
void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const String& path, const String& username, const String& password)
29+
template <typename ServerType, typename ClientType>
30+
void ESP8266HTTPUpdateServerTemplate<ServerType, ClientType>::setup(ESP8266WebServerTemplate<ServerType, ClientType> *server, const String& path, const String& username, const String& password)
2931
{
3032
_server = server;
3133
_username = username;
@@ -95,7 +97,8 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const String& path
9597
});
9698
}
9799

98-
void ESP8266HTTPUpdateServer::_setUpdaterError()
100+
template <typename ServerType, typename ClientType>
101+
void ESP8266HTTPUpdateServerTemplate<ServerType, ClientType>::_setUpdaterError()
99102
{
100103
if (_serial_output) Update.printError(Serial);
101104
StreamString str;
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
#ifndef __HTTP_UPDATE_SERVER_H
22
#define __HTTP_UPDATE_SERVER_H
33

4-
class ESP8266WebServer;
4+
#include <ESP8266WebServer.h>
55

6-
class ESP8266HTTPUpdateServer
6+
temnplate <typename ServerType, typename ClientType>
7+
class ESP8266HTTPUpdateServerTemplate
78
{
89
public:
9-
ESP8266HTTPUpdateServer(bool serial_debug=false);
10+
ESP8266HTTPUpdateServerTemplate(bool serial_debug=false);
1011

11-
void setup(ESP8266WebServer *server)
12+
void setup(ESP8266WebServerTemplate<ServerType, ClientType> *server)
1213
{
1314
setup(server, emptyString, emptyString);
1415
}
1516

16-
void setup(ESP8266WebServer *server, const String& path)
17+
void setup(ESP8266WebServerTemplate<ServerType, ClientType> *server, const String& path)
1718
{
1819
setup(server, path, emptyString, emptyString);
1920
}
2021

21-
void setup(ESP8266WebServer *server, const String& username, const String& password)
22+
void setup(ESP8266WebServerTemplate<ServerType, ClientType> *server, const String& username, const String& password)
2223
{
2324
setup(server, "/update", username, password);
2425
}
2526

26-
void setup(ESP8266WebServer *server, const String& path, const String& username, const String& password);
27+
void setup(ESP8266WebServerTemplate<ServerType, ClientType> *server, const String& path, const String& username, const String& password);
2728

2829
void updateCredentials(const String& username, const String& password)
2930
{
@@ -36,12 +37,16 @@ class ESP8266HTTPUpdateServer
3637

3738
private:
3839
bool _serial_output;
39-
ESP8266WebServer *_server;
40+
ESP8266WebServerTemplate<ServerType, ClientType> *_server;
4041
String _username;
4142
String _password;
4243
bool _authenticated;
4344
String _updaterError;
4445
};
4546

47+
#include "ESP8266HTTPUpdateServer-impl.h"
48+
49+
50+
using ESP8266HTTPUpdateServer = ESP8266HTTPUpdateServerTemplate<WiFiServer, WiFiClient>;
4651

4752
#endif

libraries/ESP8266WebServer/examples/HelloServerBearSSL/HelloServerBearSSL.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void setup(void){
128128
Serial.println("MDNS responder started");
129129
}
130130

131-
server.setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
131+
server.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
132132

133133
server.on("/", handleRoot);
134134

libraries/ESP8266WebServer/examples/HelloServerSecure/HelloServerSecure.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void setup(void) {
165165
Serial.println("MDNS responder started");
166166
}
167167

168-
server.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
168+
server.getServer().setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
169169

170170
server.on("/", handleRoot);
171171

0 commit comments

Comments
 (0)