Skip to content

ESP8266HTTPUpdateServer extracted from WebUpdate example. #837

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 2 commits into from
Oct 2, 2015

Conversation

mangelajo
Copy link
Contributor

It depends on issue #833 fix.

If the client connection gets closed during a form upload,
the fileUploadHandler is notified with the new
UPLOAD_FILE_ABORTED status, and the loop is ended
gracefully.
@mangelajo mangelajo changed the title Esp8266HTTPUpdateServer ESP8266HTTPUpdateServer extracted from WebUpdate example. Sep 29, 2015
@mangelajo mangelajo force-pushed the esp8266-HTTPUpdateServer branch from 9f71029 to 927d6c9 Compare September 29, 2015 12:44
#include "ESP8266HTTPUpdateServer.h"


const char* ESP8266HTTPUpdateServer::_serverIndex = "<html><body><form method='POST' action='/update' enctype='multipart/form-data'>"
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to store this HTML as a raw string literal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, sorry, I missed that comment. It seems that My C++ is outdated :) , sounds good to me, will do.

@mangelajo mangelajo force-pushed the esp8266-HTTPUpdateServer branch from 927d6c9 to 04bc0d3 Compare September 29, 2015 12:52
ESP8266HTTPUpdateServer::~ESP8266HTTPUpdateServer()
{
if (_server)
delete _server;
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't look right in case when ESP8266WebServer instance was allocated statically outside this class. Consider the following code:

void runServer() {
  ESP8266WebServer httpServer(80);
  ESP8266HTTPUpdateServer updateServer;
  updateServer.setup(&httpServer);
  while (something) {
    httpServer.handleClient();
  }
}

Upon exit from this function, ESP8266WebServer will be deleted twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, also like this, we make the dependency explicit, and make sure the user will import the ESP8266WebServer from the main .ino, otherwise, it won't get included/linked since arduino libs can't set dependencies AFAIK.

Copy link
Member

Choose a reason for hiding this comment

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

Btw, nightly builds of IDE already have library dependency tracking!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohh, nice :)

@igrr
Copy link
Member

igrr commented Sep 29, 2015

Another thought is that it would probably make sense to pass ESP8266WebServer instance into web server callbacks — this way you would not need to store a _server pointer in your class. This would allow for pluggable "middleware" classes like in node.js. I'll take a look if we can achieve this without breaking a lot of existing sketches.

Edit:
also you need to change the example file name:
examples/WebUpdater/WebUpdate.ino is not correct from IDEs point of view, it should be either
examples/WebUpdate/WebUpdate.ino or examples/WebUpdater/WebUpdater.ino.

@mangelajo
Copy link
Contributor Author

Thanks Ivan!, will do.

Yeah, the passing of server itself via the events would be nice, it's generally a good thing to provide the source of the event. I wonder if there's some way to do it without breaking existing sketches.

May be by having ::on overloaded with the different possible callbacks.

@mangelajo mangelajo force-pushed the esp8266-HTTPUpdateServer branch from 04bc0d3 to 6b9929e Compare September 29, 2015 14:50
@mangelajo
Copy link
Contributor Author

Hold on, the example is gone away

@mangelajo mangelajo force-pushed the esp8266-HTTPUpdateServer branch from 6b9929e to b7dee8b Compare September 29, 2015 15:39
@mangelajo
Copy link
Contributor Author

Any idea of why could it be failing like this:

/home/travis/build/esp8266/Arduino/build/linux/work/hardware/esp8266com/esp8266/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer.cpp:2:24: fatal error: WifiClient.h: No such file or directory
#include <WifiClient.h>

?

It does compile locally

@mangelajo
Copy link
Contributor Author

What do you think about renaming the lib to ESP8266HTTPUpdateApp or ESP8266HTTPUpdateHandler?
since now, we register to an existing webserver.

@mangelajo mangelajo force-pushed the esp8266-HTTPUpdateServer branch from b7dee8b to c3ce925 Compare September 29, 2015 18:29
@mangelajo mangelajo closed this Sep 29, 2015
@mangelajo mangelajo reopened this Sep 29, 2015
@mangelajo
Copy link
Contributor Author

@igrr , any idea of why the travis WebUpdater.ino compilation could be failing?, It executed it locally the same way, and it does provide the -I path to WifiClient :-?

@igrr
Copy link
Member

igrr commented Sep 30, 2015

In ESP8266HTTPUpdateServer.cpp, you need to observe the case in include names, i.e. WiFiClient instead of WifiClient, and others as well. Travis CI runs on Linux, so file names are case sensitive.

@mangelajo
Copy link
Contributor Author

arghhh!, damn case insensitive OSX default, thanks ...

@mangelajo mangelajo force-pushed the esp8266-HTTPUpdateServer branch from c3ce925 to bc087d1 Compare September 30, 2015 13:36
@mangelajo
Copy link
Contributor Author

@iggr I could also do something like this for the DNS_SD_Arduino_OTA if you think that's valuable, and I'd be willing in testing it for robustness, recovery, and all kind of evil stuff we could want to do to it.

Is somebody working on that, or can I take it?

@igrr
Copy link
Member

igrr commented Sep 30, 2015

Encapsulating all the update logic into a nice ArduinoOTA library would be an improvement I guess. I don't think anyone is working on it, so if you can do it, that's great.

This ESP8266HTTPUpdateServer can be instantiated and used
more cleanly, it's also able to take or create an ESP8266WebServer
to configure the events and /update handler.

It's been made more robust by handling upload abort, which
depends on fix provided for issue esp8266#833
@mangelajo mangelajo force-pushed the esp8266-HTTPUpdateServer branch from bc087d1 to 91b1bd4 Compare October 1, 2015 13:19
igrr added a commit that referenced this pull request Oct 2, 2015
ESP8266HTTPUpdateServer extracted from WebUpdate example.
@igrr igrr merged commit d8f57cb into esp8266:esp8266 Oct 2, 2015
igrr added a commit that referenced this pull request Oct 6, 2015
@igrr
Copy link
Member

igrr commented Oct 6, 2015

@mangelajo As discussed, i have made a change to ESP8266WebServer to allow writing more self-sufficient handlers.
Here's the sample: https://gist.github.com/igrr/0da0c4adc7588d9bd911
I'm still not certain on the ownership transfer. Right now user creates a handler with new and gives it to the server, which will delete the instance when required. Making the handler owned by user is also possible, but i'm not sure how to distinguish these cases better.

igrr added a commit that referenced this pull request Oct 29, 2015
ESP8266HTTPUpdateServer extracted from WebUpdate example.
igrr added a commit that referenced this pull request Oct 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants