8000 webserver hook: allow to handle external http protocol by d-a-v · Pull Request #7459 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

webserver hook: allow to handle external http protocol #7459

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 14 commits into from
Jul 28, 2020
Prev Previous commit
Next Next commit
Hook examples in HelloServer example
  • Loading branch information
d-a-v committed Jul 17, 2020
commit bfc12fbc9d432da37e35bb2ad2bf944385cd7c68
69 changes: 67 additions & 2 deletions libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const int led = 13;

void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp8266!");
server.send(200, "text/plain", "hello from esp8266!\r\n");
digitalWrite(led, 0);
}

Expand Down Expand Up @@ -86,11 +86,76 @@ void setup(void) {

server.onNotFound(handleNotFound);

/////////////////////////////////////////////////////////
// Hook examples

server.addHook([](const String & method, const String & url, WiFiClient * client, ESP8266WebServer::ContentTypeFunction contentType) {
(void)method;
(void)url;
(void)client;
(void)contentType;
Copy link
Collaborator
@mcspr mcspr Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor style nitpick - by omitting argument name in the signature, there would be no need for these 4 lines.
There is also [[maybe_unused]] / [[gnu::unused]] for local declaration, since now we use c++17. But that one is mostly for variables

Serial.printf("A useless web hook has passed\n");
return ESP8266WebServer::CLIENT_REQUEST_CAN_CONTINUE;
});

server.addHook([](const String & method, const String & url, WiFiClient * client, ESP8266WebServer::ContentTypeFunction contentType) {
(void)method;
(void)client;
(void)contentType;
if (url.startsWith("/fail")) {
Serial.printf("An always failing web hook has been triggered\n");
return ESP8266WebServer::CLIENT_MUST_STOP;
}
return ESP8266WebServer::CLIENT_REQUEST_CAN_CONTINUE;
});

server.addHook([](const String & method, const String & url, WiFiClient * client, ESP8266WebServer::ContentTypeFunction contentType) {
(void)method;
(void)client;
(void)contentType;
if (url.startsWith("/dump")) {
Serial.printf("The dumper web hook is on the run\n");

// Here the request is not interpreted, so we cannot for sure
// swallow the exact amount matching the full request+content,
// hence the tcp connection cannot be handled anymore by the
// webserver.
#ifdef STREAMTO_API
// we are lucky
client->streamToWithTimeout(Serial, 500);
#else
auto last = millis();
while ((millis() - last) < 500) {
char buf[32];
size_t len = client->read((uint8_t*)buf, sizeof(buf));
if (len > 0) {
Serial.printf("(<%d> chars)", (int)len);
Serial.write(buf, len);
last = millis();
}
}
#endif
// Two choices: return MUST STOP and webserver will close it
// (we already have the example with '/fail' hook)
// or IS GIVEN and webserver will forget it
// trying with IS GIVEN and storing it on a dumb WiFiClient.
// check the client connection: it should not immediately be closed
// (make another '/dump' one to close the first)
Serial.printf("\nTelling server to forget this connection\n");
static WiFiClient forgetme = *client; // stop previous one if present and transfer client refcounter
return ESP8266WebServer::CLIENT_IS_GIVEN;
}
return ESP8266WebServer::CLIENT_REQUEST_CAN_CONTINUE;
});

// Hook examples
/////////////////////////////////////////////////////////

server.begin();
Serial.println("HTTP server started");
}

void loop(void) {
server.handleClient();
MDNS.update();
//MDNS.update();
}
2 changes: 1 addition & 1 deletion libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "detail/mimetable.h"
#include "Uri.h"

#define DEBUG_ESP_HTTP_SERVER
//#define DEBUG_ESP_HTTP_SERVER

#ifdef DEBUG_ESP_HTTP_SERVER
#ifdef DEBUG_ESP_PORT
Expand Down
0