Skip to content

Commit 7036297

Browse files
earlephilhowerd-a-v
authored andcommitted
Convert ESP8266WebServer* into templatized model (#5982)
* 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... * Fix HTTPUpdate templates and examples * Fix HTTPUpdateServer library build Need to remove dot-a linkage since there are no .cpp files in the directory anymore due to templates. * Provide backward-compat names for updt template Allow existing code to use the same well known names for HTTPUpdateSecure. * Remove ClientType from all templates, auto-infer Remove the ClientType template parameter from all objects. Simplifies the code and makes it more foolproof. Add a "using" in each server to define the type of connection returned by all servers, which is then used in the above templates automatically. * Can safely include FS.h now that SD/SPIFFS unified * Move the templates/objects to their own namespaces * Fix merge issues with untemplated methods * Address review comments * Fix mock test, remove warnings inside test dir Make the simple mock test CI job pass and clean up any spurious warnings in the test directory. There still are warnings in the libraries and core, but they should be addressed in a separate PR.
1 parent 93a52f9 commit 7036297

26 files changed

+293
-603
lines changed

libraries/ESP8266HTTPUpdateServer/examples/SecureBearSSLUpdater/SecureBearSSLUpdater.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const char* update_password = "admin";
3131
const char* ssid = STASSID;
3232
const char* password = STAPSK;
3333

34-
BearSSL::ESP8266WebServerSecure httpServer(443);
35-
ESP8266HTTPUpdateServer httpUpdater;
34+
ESP8266WebServerSecure httpServer(443);
35+
ESP8266HTTPUpdateServerSecure httpUpdater;
3636

3737
static const char serverCert[] PROGMEM = R"EOF(
3838
-----BEGIN CERTIFICATE-----
@@ -106,7 +106,7 @@ void setup()
106106

107107
MDNS.begin(host);
108108

109-
httpServer.setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
109+
httpServer.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
110110
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
111111
httpServer.begin();
112112

libraries/ESP8266HTTPUpdateServer/examples/SecureHTTPSUpdater/SecureHTTPSUpdater.ino

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,21 @@
4242
This example is released into the public domain.
4343
*/
4444

45+
// AXTLS is deprecated, do not use in new code.
46+
#pragma GCC diagnostic push
47+
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
48+
4549
#include <ESP8266WiFi.h>
4650
#include <WiFiClient.h>
4751
#include <ESP8266WebServerSecure.h>
4852
#include <ESP8266mDNS.h>
4953
#include <ESP8266HTTPUpdateServer.h>
54+
#include <WiFiServerSecure.h>
55+
#include <WiFiServerSecureAxTLS.h>
56+
#include <WiFiClientSecure.h>
57+
#include <WiFiClientSecureAxTLS.h>
58+
59+
#pragma GCC diagnostic pop
5060

5161
#ifndef STASSID
5262
#define STASSID "your-ssid"
@@ -60,8 +70,8 @@ const char* update_password = "admin";
6070
const char* ssid = STASSID;
6171
const char* password = STAPSK;
6272

63-
ESP8266WebServerSecure httpServer(443);
64-
ESP8266HTTPUpdateServer httpUpdater;
73+
axTLS::ESP8266WebServerSecure httpServer(443);
74+
axTLS::ESP8266HTTPUpdateServerSecure httpUpdater;
6575

6676
// The certificate is stored in PMEM
6777
static const uint8_t x509[] PROGMEM = {
@@ -176,7 +186,7 @@ void setup() {
176186

177187
MDNS.begin(host);
178188

179-
httpServer.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
189+
httpServer.getServer().setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
180190
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
181191
httpServer.begin();
182192

libraries/ESP8266HTTPUpdateServer/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=The library accepts HTTP post requests to the /update url, and updates
77
category=Communication
88
url=
99
architectures=esp8266
10-
dot_a_linkage=true
10+
dot_a_linkage=false

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "StreamString.h"
77
#include "ESP8266HTTPUpdateServer.h"
88

9+
namespace esp8266httpupdateserver {
10+
using namespace esp8266webserver;
911

1012
static const char serverIndex[] PROGMEM =
1113
R"(<html><body><form method='POST' action='' enctype='multipart/form-data'>
@@ -16,7 +18,8 @@ static const char serverIndex[] PROGMEM =
1618
static const char successResponse[] PROGMEM =
1719
"<META http-equiv=\"refresh\" content=\"15;URL=/\">Update Success! Rebooting...\n";
1820

19-
ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug)
21+
template <typename ServerType>
22+
ESP8266HTTPUpdateServerTemplate<ServerType>::ESP8266HTTPUpdateServerTemplate(bool serial_debug)
2023
{
2124
_serial_output = serial_debug;
2225
_server = NULL;
@@ -25,7 +28,8 @@ ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug)
2528
_authenticated = false;
2629
}
2730

28-
void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const String& path, const String& username, const String& password)
31+
template <typename ServerType>
32+
void ESP8266HTTPUpdateServerTemplate<ServerType>::setup(ESP8266WebServerTemplate<ServerType> *server, const String& path, const String& username, const String& password)
2933
{
3034
_server = server;
3135
_username = username;
@@ -95,10 +99,13 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const String& path
9599
});
96100
}
97101

98-
void ESP8266HTTPUpdateServer::_setUpdaterError()
102+
template <typename ServerType>
103+
void ESP8266HTTPUpdateServerTemplate<ServerType>::_setUpdaterError()
99104
{
100105
if (_serial_output) Update.printError(Serial);
101106
StreamString str;
102107
Update.printError(str);
103108
_updaterError = str.c_str();
104109
}
110+
111+
};
Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
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+
namespace esp8266httpupdateserver {
7+
using namespace esp8266webserver;
8+
9+
template <typename ServerType>
10+
class ESP8266HTTPUpdateServerTemplate
711
{
812
public:
9-
ESP8266HTTPUpdateServer(bool serial_debug=false);
13+
ESP8266HTTPUpdateServerTemplate(bool serial_debug=false);
1014

11-
void setup(ESP8266WebServer *server)
15+
void setup(ESP8266WebServerTemplate<ServerType> *server)
1216
{
1317
setup(server, emptyString, emptyString);
1418
}
1519

16-
void setup(ESP8266WebServer *server, const String& path)
20+
void setup(ESP8266WebServerTemplate<ServerType> *server, const String& path)
1721
{
1822
setup(server, path, emptyString, emptyString);
1923
}
2024

21-
void setup(ESP8266WebServer *server, const String& username, const String& password)
25+
void setup(ESP8266WebServerTemplate<ServerType> *server, const String& username, const String& password)
2226
{
2327
setup(server, "/update", username, password);
2428
}
2529

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

2832
void updateCredentials(const String& username, const String& password)
2933
{
@@ -36,12 +40,26 @@ class ESP8266HTTPUpdateServer
3640

3741
private:
3842
bool _serial_output;
39-
ESP8266WebServer *_server;
43+
ESP8266WebServerTemplate<ServerType> *_server;
4044
String _username;
4145
String _password;
4246
bool _authenticated;
4347
String _updaterError;
4448
};
4549

50+
};
51+
52+
#include "ESP8266HTTPUpdateServer-impl.h"
53+
54+
55+
using ESP8266HTTPUpdateServer = esp8266httpupdateserver::ESP8266HTTPUpdateServerTemplate<WiFiServer>;
56+
57+
namespace BearSSL {
58+
using ESP8266HTTPUpdateServerSecure = esp8266httpupdateserver::ESP8266HTTPUpdateServerTemplate<WiFiServerSecure>;
59+
};
60+
61+
namespace axTLS {
62+
using ESP8266HTTPUpdateServerSecure = esp8266httpupdateserver::ESP8266HTTPUpdateServerTemplate<WiFiServerSecure>;
63+
};
4664

4765
#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

libraries/ESP8266WebServer/examples/HttpHashCredAuth/HttpHashCredAuth.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void setup() {
110110
ESP.restart();
111111
}
112112

113-
server.setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
113+
server.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
114114
server.on("/",showcredentialpage); //for this simple example, just show a simple page for changing credentials at the root
115115
server.on("/" + change_creds,handlecredentialchange); //handles submission of credentials from the client
116116
server.onNotFound(redirect);

0 commit comments

Comments
 (0)