Skip to content

Convert ESP8266WebServer* into templatized model #5982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
777e8d7
Convert ESP8266WebServer* into templatized model
earlephilhower Apr 12, 2019
1e44481
Merge branch 'master' into webserver-template
earlephilhower Apr 12, 2019
786c583
Merge branch 'master' into webserver-template
earlephilhower Apr 12, 2019
1559a3b
Fix HTTPUpdate templates and examples
earlephilhower Apr 12, 2019
9a56e85
Fix HTTPUpdateServer library build
earlephilhower Apr 12, 2019
8a3e3eb
Provide backward-compat names for updt template
earlephilhower Apr 12, 2019
a90a145
Remove ClientType from all templates, auto-infer
earlephilhower Apr 12, 2019
06aa44f
Can safely include FS.h now that SD/SPIFFS unified
earlephilhower Apr 12, 2019
3a8e1d4
Move the templates/objects to their own namespaces
earlephilhower Apr 12, 2019
20ed1cb
Merge branch 'master' into webserver-template
earlephilhower Apr 24, 2019
9705872
Merge branch 'master' into webserver-template
d-a-v May 3, 2019
410fc9e
Merge branch 'master' of https://github.com/esp8266/Arduino into webs…
earlephilhower May 26, 2019
da812db
Fix merge issues with untemplated methods
earlephilhower May 26, 2019
abd8958
Merge branch 'master' into webserver-template
earlephilhower May 27, 2019
328bd2c
Merge branch 'master' into webserver-template
d-a-v Jun 27, 2019
60a6274
Address review comments
earlephilhower Jul 2, 2019
daaf9a5
Merge branch 'master' into webserver-template
earlephilhower Jul 4, 2019
bf96e42
Merge branch 'master' into webserver-template
earlephilhower Jul 4, 2019
613d2c1
Fix mock test, remove warnings inside test dir
earlephilhower Jul 4, 2019
616411c
Merge branch 'master' into webserver-template
devyte Jul 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const char* update_password = "admin";
const char* ssid = STASSID;
const char* password = STAPSK;

BearSSL::ESP8266WebServerSecure httpServer(443);
ESP8266HTTPUpdateServer httpUpdater;
ESP8266WebServerSecure httpServer(443);
ESP8266HTTPUpdateServerSecure httpUpdater;

static const char serverCert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
Expand Down Expand Up @@ -106,7 +106,7 @@ void setup()

MDNS.begin(host);

httpServer.setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
httpServer.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@
This example is released into the public domain.
*/

// AXTLS is deprecated, do not use in new code.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServerSecure.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <WiFiServerSecure.h>
#include <WiFiServerSecureAxTLS.h>
#include <WiFiClientSecure.h>
#include <WiFiClientSecureAxTLS.h>

#pragma GCC diagnostic pop

#ifndef STASSID
#define STASSID "your-ssid"
Expand All @@ -60,8 +70,8 @@ const char* update_password = "admin";
const char* ssid = STASSID;
const char* password = STAPSK;

ESP8266WebServerSecure httpServer(443);
ESP8266HTTPUpdateServer httpUpdater;
axTLS::ESP8266WebServerSecure httpServer(443);
axTLS::ESP8266HTTPUpdateServerSecure httpUpdater;

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

MDNS.begin(host);

httpServer.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
httpServer.getServer().setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();

Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266HTTPUpdateServer/library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=The library accepts HTTP post requests to the /update url, and updates
category=Communication
url=
architectures=esp8266
dot_a_linkage=true
dot_a_linkage=false
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "StreamString.h"
#include "ESP8266HTTPUpdateServer.h"

namespace esp8266httpupdateserver {
using namespace esp8266webserver;

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

ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug)
template <typename ServerType>
ESP8266HTTPUpdateServerTemplate<ServerType>::ESP8266HTTPUpdateServerTemplate(bool serial_debug)
{
_serial_output = serial_debug;
_server = NULL;
Expand All @@ -25,7 +28,8 @@ ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug)
_authenticated = false;
}

void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const String& path, const String& username, const String& password)
template <typename ServerType>
void ESP8266HTTPUpdateServerTemplate<ServerType>::setup(ESP8266WebServerTemplate<ServerType> *server, const String& path, const String& username, const String& password)
{
_server = server;
_username = username;
Expand Down Expand Up @@ -95,10 +99,13 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const String& path
});
}

void ESP8266HTTPUpdateServer::_setUpdaterError()
template <typename ServerType>
void ESP8266HTTPUpdateServerTemplate<ServerType>::_setUpdaterError()
{
if (_serial_output) Update.printError(Serial);
StreamString str;
Update.printError(str);
_updaterError = str.c_str();
}

};
34 changes: 26 additions & 8 deletions libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
#ifndef __HTTP_UPDATE_SERVER_H
#define __HTTP_UPDATE_SERVER_H

class ESP8266WebServer;
#include <ESP8266WebServer.h>

class ESP8266HTTPUpdateServer
namespace esp8266httpupdateserver {
using namespace esp8266webserver;

template <typename ServerType>
class ESP8266HTTPUpdateServerTemplate
{
public:
ESP8266HTTPUpdateServer(bool serial_debug=false);
ESP8266HTTPUpdateServerTemplate(bool serial_debug=false);

void setup(ESP8266WebServer *server)
void setup(ESP8266WebServerTemplate<ServerType> *server)
{
setup(server, emptyString, emptyString);
}

void setup(ESP8266WebServer *server, const String& path)
void setup(ESP8266WebServerTemplate<ServerType> *server, const String& path)
{
setup(server, path, emptyString, emptyString);
}

void setup(ESP8266WebServer *server, const String& username, const String& password)
void setup(ESP8266WebServerTemplate<ServerType> *server, const String& username, const String& password)
{
setup(server, "/update", username, password);
}

void setup(ESP8266WebServer *server, const String& path, const String& username, const String& password);
void setup(ESP8266WebServerTemplate<ServerType> *server, const String& path, const String& username, const String& password);

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

private:
bool _serial_output;
ESP8266WebServer *_server;
ESP8266WebServerTemplate<ServerType> *_server;
String _username;
String _password;
bool _authenticated;
String _updaterError;
};

};

#include "ESP8266HTTPUpdateServer-impl.h"


using ESP8266HTTPUpdateServer = esp8266httpupdateserver::ESP8266HTTPUpdateServerTemplate<WiFiServer>;

namespace BearSSL {
using ESP8266HTTPUpdateServerSecure = esp8266httpupdateserver::ESP8266HTTPUpdateServerTemplate<WiFiServerSecure>;
};

namespace axTLS {
using ESP8266HTTPUpdateServerSecure = esp8266httpupdateserver::ESP8266HTTPUpdateServerTemplate<WiFiServerSecure>;
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void setup(void){
Serial.println("MDNS responder started");
}

server.setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
server.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));

server.on("/", handleRoot);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void setup(void) {
Serial.println("MDNS responder started");
}

server.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
server.getServer().setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));

server.on("/", handleRoot);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void setup() {
ESP.restart();
}

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