mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-07 16:23:38 +03:00
Merge pull request #1857 from esp8266/http_client_refactoring
HTTP Client library: decouple TLS handling
This commit is contained in:
commit
f3b6ec16e6
@ -1,5 +1,5 @@
|
|||||||
name=ESP8266HTTPClient
|
name=ESP8266HTTPClient
|
||||||
version=1.0
|
version=1.1
|
||||||
author=Markus Sattler
|
author=Markus Sattler
|
||||||
maintainer=Markus Sattler
|
maintainer=Markus Sattler
|
||||||
sentence=http Client for ESP8266
|
sentence=http Client for ESP8266
|
||||||
|
@ -30,85 +30,103 @@
|
|||||||
|
|
||||||
#include "ESP8266HTTPClient.h"
|
#include "ESP8266HTTPClient.h"
|
||||||
|
|
||||||
|
class TransportTraits
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual std::unique_ptr<WiFiClient> create()
|
||||||
|
{
|
||||||
|
return std::unique_ptr<WiFiClient>(new WiFiClient());
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool verify(WiFiClient& client, const char* host)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class TLSTraits : public TransportTraits
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TLSTraits(const String& fingerprint) :
|
||||||
|
_fingerprint(fingerprint)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<WiFiClient> create() override
|
||||||
|
{
|
||||||
|
return std::unique_ptr<WiFiClient>(new WiFiClientSecure());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool verify(WiFiClient& client, const char* host) override
|
||||||
|
{
|
||||||
|
auto wcs = reinterpret_cast<WiFiClientSecure&>(client);
|
||||||
|
return wcs.verify(_fingerprint.c_str(), host);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
String _fingerprint;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* constractor
|
* constructor
|
||||||
*/
|
*/
|
||||||
HTTPClient::HTTPClient() {
|
HTTPClient::HTTPClient()
|
||||||
_tcp = NULL;
|
{
|
||||||
_tcps = NULL;
|
|
||||||
|
|
||||||
_port = 0;
|
|
||||||
|
|
||||||
_reuse = false;
|
|
||||||
_tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
|
|
||||||
_useHTTP10 = false;
|
|
||||||
|
|
||||||
_https = false;
|
|
||||||
|
|
||||||
_userAgent = "ESP8266HTTPClient";
|
|
||||||
|
|
||||||
_headerKeysCount = 0;
|
|
||||||
_currentHeaders = NULL;
|
|
||||||
|
|
||||||
_returnCode = 0;
|
|
||||||
_size = -1;
|
|
||||||
_canReuse = false;
|
|
||||||
_transferEncoding = HTTPC_TE_IDENTITY;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* deconstractor
|
* destructor
|
||||||
*/
|
*/
|
||||||
HTTPClient::~HTTPClient() {
|
HTTPClient::~HTTPClient()
|
||||||
|
{
|
||||||
if(_tcps) {
|
if(_tcp) {
|
||||||
_tcps->stop();
|
|
||||||
delete _tcps;
|
|
||||||
_tcps = NULL;
|
|
||||||
_tcp = NULL;
|
|
||||||
} else if(_tcp) {
|
|
||||||
_tcp->stop();
|
_tcp->stop();
|
||||||
delete _tcp;
|
|
||||||
_tcp = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_currentHeaders) {
|
if(_currentHeaders) {
|
||||||
delete[] _currentHeaders;
|
delete[] _currentHeaders;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* phasing the url for all needed informations
|
bool HTTPClient::begin(String url, String httpsFingerprint)
|
||||||
* @param url const char *
|
{
|
||||||
* @param httpsFingerprint const char *
|
if (httpsFingerprint.length() == 0) {
|
||||||
*/
|
return false;
|
||||||
void HTTPClient::begin(const char *url, const char * httpsFingerprint) {
|
}
|
||||||
begin(String(url), String(httpsFingerprint));
|
if (!begin(url)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_transportTraits = TransportTraitsPtr(new TLSTraits(httpsFingerprint));
|
||||||
|
DEBUG_HTTPCLIENT("[HTTP-Client][begin] httpsFingerprint: %s\n", httpsFingerprint.c_str());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void HTTPClient::clear()
|
||||||
* phasing the url for all needed informations
|
{
|
||||||
* @param url String
|
|
||||||
* @param httpsFingerprint String
|
|
||||||
*/
|
|
||||||
void HTTPClient::begin(String url, String httpsFingerprint) {
|
|
||||||
|
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][begin] url: %s\n", url.c_str());
|
|
||||||
|
|
||||||
_httpsFingerprint = httpsFingerprint;
|
|
||||||
_returnCode = 0;
|
_returnCode = 0;
|
||||||
_size = -1;
|
_size = -1;
|
||||||
|
_headers = "";
|
||||||
|
}
|
||||||
|
|
||||||
_Headers = "";
|
|
||||||
|
|
||||||
String protocol;
|
/**
|
||||||
|
* parsing the url for all needed parameters
|
||||||
|
* @param url String
|
||||||
|
*/
|
||||||
|
bool HTTPClient::begin(String url)
|
||||||
|
{
|
||||||
|
DEBUG_HTTPCLIENT("[HTTP-Client][begin] url: %s\n", url.c_str());
|
||||||
|
bool hasPort = false;
|
||||||
|
clear();
|
||||||
|
|
||||||
// check for : (http: or https:
|
// check for : (http: or https:
|
||||||
int index = url.indexOf(':');
|
int index = url.indexOf(':');
|
||||||
//int index2;
|
if(index < 0) {
|
||||||
bool hasPort = false;
|
DEBUG_HTTPCLIENT("[HTTP-Client][begin] failed to parse protocol\n");
|
||||||
if(index >= 0) {
|
return false;
|
||||||
protocol = url.substring(0, index);
|
}
|
||||||
|
|
||||||
|
_protocol = url.substring(0, index);
|
||||||
url.remove(0, (index + 3)); // remove http:// or https://
|
url.remove(0, (index + 3)); // remove http:// or https://
|
||||||
|
|
||||||
index = url.indexOf('/');
|
index = url.indexOf('/');
|
||||||
@ -134,64 +152,66 @@ void HTTPClient::begin(String url, String httpsFingerprint) {
|
|||||||
} else {
|
} else {
|
||||||
_host = host;
|
_host = host;
|
||||||
}
|
}
|
||||||
|
_uri = url;
|
||||||
|
|
||||||
_url = url;
|
if(_protocol.equalsIgnoreCase("http")) {
|
||||||
|
|
||||||
if(protocol.equalsIgnoreCase("http")) {
|
|
||||||
_https = false;
|
|
||||||
if(!hasPort) {
|
if(!hasPort) {
|
||||||
_port = 80;
|
_port = 80;
|
||||||
}
|
}
|
||||||
} else if(protocol.equalsIgnoreCase("https")) {
|
} else if(_protocol.equalsIgnoreCase("https")) {
|
||||||
_https = true;
|
|
||||||
if(!hasPort) {
|
if(!hasPort) {
|
||||||
_port = 443;
|
_port = 443;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][begin] protocol: %s unknown?!\n", protocol.c_str());
|
DEBUG_HTTPCLIENT("[HTTP-Client][begin] protocol: %s unknown?!\n", _protocol.c_str());
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
_transportTraits = TransportTraitsPtr(new TransportTraits());
|
||||||
}
|
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d url: %s\n", _host.c_str(), _port, _uri.c_str());
|
||||||
|
return true;
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d url: %s https: %d httpsFingerprint: %s\n", _host.c_str(), _port, _url.c_str(), _https, _httpsFingerprint.c_str());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
bool HTTPClient::begin(String host, uint16_t port, String uri)
|
||||||
* begin
|
{
|
||||||
* @param host const char *
|
clear();
|
||||||
* @param port uint16_t
|
|
||||||
* @param url const char *
|
|
||||||
* @param https bool
|
|
||||||
* @param httpsFingerprint const char *
|
|
||||||
*/
|
|
||||||
void HTTPClient::begin(const char *host, uint16_t port, const char * url, bool https, const char * httpsFingerprint) {
|
|
||||||
|
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port:%d url: %s https: %d httpsFingerprint: %s\n", host, port, url, https, httpsFingerprint);
|
|
||||||
|
|
||||||
_host = host;
|
_host = host;
|
||||||
_port = port;
|
_port = port;
|
||||||
_url = url;
|
_uri = uri;
|
||||||
_https = https;
|
_transportTraits = TransportTraitsPtr(new TransportTraits());
|
||||||
_httpsFingerprint = httpsFingerprint;
|
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d uri: %s\n", host.c_str(), port, uri.c_str());
|
||||||
|
return true;
|
||||||
_returnCode = 0;
|
|
||||||
_size = -1;
|
|
||||||
|
|
||||||
_Headers = "";
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTTPClient::begin(String host, uint16_t port, String url, bool https, String httpsFingerprint) {
|
bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint)
|
||||||
begin(host.c_str(), port, url.c_str(), https, httpsFingerprint.c_str());
|
{
|
||||||
|
if (https) {
|
||||||
|
return begin(host, port, uri, httpsFingerprint);
|
||||||
|
} else {
|
||||||
|
return begin(host, port, uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFingerprint)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
_host = host;
|
||||||
|
_port = port;
|
||||||
|
_uri = uri;
|
||||||
|
|
||||||
|
if (httpsFingerprint.length() == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_transportTraits = TransportTraitsPtr(new TLSTraits(httpsFingerprint));
|
||||||
|
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d url: %s httpsFingerprint: %s\n", host.c_str(), port, uri.c_str(), httpsFingerprint.c_str());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* end
|
* end
|
||||||
* called after the payload is handled
|
* called after the payload is handled
|
||||||
*/
|
*/
|
||||||
void HTTPClient::end(void) {
|
void HTTPClient::end(void)
|
||||||
|
{
|
||||||
if(connected()) {
|
if(connected()) {
|
||||||
if(_tcp->available() > 0) {
|
if(_tcp->available() > 0) {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _tcp->available());
|
DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _tcp->available());
|
||||||
@ -214,7 +234,8 @@ void HTTPClient::end(void) {
|
|||||||
* connected
|
* connected
|
||||||
* @return connected status
|
* @return connected status
|
||||||
*/
|
*/
|
||||||
bool HTTPClient::connected() {
|
bool HTTPClient::connected()
|
||||||
|
{
|
||||||
if(_tcp) {
|
if(_tcp) {
|
||||||
return (_tcp->connected() || (_tcp->available() > 0));
|
return (_tcp->connected() || (_tcp->available() > 0));
|
||||||
}
|
}
|
||||||
@ -226,7 +247,8 @@ bool HTTPClient::connected() {
|
|||||||
* keep-alive
|
* keep-alive
|
||||||
* @param reuse bool
|
* @param reuse bool
|
||||||
*/
|
*/
|
||||||
void HTTPClient::setReuse(bool reuse) {
|
void HTTPClient::setReuse(bool reuse)
|
||||||
|
{
|
||||||
_reuse = reuse;
|
_reuse = reuse;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,7 +256,8 @@ void HTTPClient::setReuse(bool reuse) {
|
|||||||
* set User Agent
|
* set User Agent
|
||||||
* @param userAgent const char *
|
* @param userAgent const char *
|
||||||
*/
|
*/
|
||||||
void HTTPClient::setUserAgent(const char * userAgent) {
|
void HTTPClient::setUserAgent(const String& userAgent)
|
||||||
|
{
|
||||||
_userAgent = userAgent;
|
_userAgent = userAgent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,7 +266,8 @@ void HTTPClient::setUserAgent(const char * userAgent) {
|
|||||||
* @param user const char *
|
* @param user const char *
|
||||||
* @param password const char *
|
* @param password const char *
|
||||||
*/
|
*/
|
||||||
void HTTPClient::setAuthorization(const char * user, const char * password) {
|
void HTTPClient::setAuthorization(const char * user, const char * password)
|
||||||
|
{
|
||||||
if(user && password) {
|
if(user && password) {
|
||||||
String auth = user;
|
String auth = user;
|
||||||
auth += ":";
|
auth += ":";
|
||||||
@ -256,7 +280,8 @@ void HTTPClient::setAuthorization(const char * user, const char * password) {
|
|||||||
* set the Authorizatio for the http request
|
* set the Authorizatio for the http request
|
||||||
* @param auth const char * base64
|
* @param auth const char * base64
|
||||||
*/
|
*/
|
||||||
void HTTPClient::setAuthorization(const char * auth) {
|
void HTTPClient::setAuthorization(const char * auth)
|
||||||
|
{
|
||||||
if(auth) {
|
if(auth) {
|
||||||
_base64Authorization = auth;
|
_base64Authorization = auth;
|
||||||
}
|
}
|
||||||
@ -266,7 +291,8 @@ void HTTPClient::setAuthorization(const char * auth) {
|
|||||||
* set the timeout for the TCP connection
|
* set the timeout for the TCP connection
|
||||||
* @param timeout unsigned int
|
* @param timeout unsigned int
|
||||||
*/
|
*/
|
||||||
void HTTPClient::setTimeout(uint16_t timeout) {
|
void HTTPClient::setTimeout(uint16_t timeout)
|
||||||
|
{
|
||||||
_tcpTimeout = timeout;
|
_tcpTimeout = timeout;
|
||||||
if(connected()) {
|
if(connected()) {
|
||||||
_tcp->setTimeout(timeout);
|
_tcp->setTimeout(timeout);
|
||||||
@ -277,7 +303,8 @@ void HTTPClient::setTimeout(uint16_t timeout) {
|
|||||||
* use HTTP1.0
|
* use HTTP1.0
|
||||||
* @param timeout
|
* @param timeout
|
||||||
*/
|
*/
|
||||||
void HTTPClient::useHTTP10(bool useHTTP10) {
|
void HTTPClient::useHTTP10(bool useHTTP10)
|
||||||
|
{
|
||||||
_useHTTP10 = useHTTP10;
|
_useHTTP10 = useHTTP10;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,7 +312,8 @@ void HTTPClient::useHTTP10(bool useHTTP10) {
|
|||||||
* send a GET request
|
* send a GET request
|
||||||
* @return http code
|
* @return http code
|
||||||
*/
|
*/
|
||||||
int HTTPClient::GET() {
|
int HTTPClient::GET()
|
||||||
|
{
|
||||||
return sendRequest("GET");
|
return sendRequest("GET");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,11 +323,13 @@ int HTTPClient::GET() {
|
|||||||
* @param size size_t
|
* @param size size_t
|
||||||
* @return http code
|
* @return http code
|
||||||
*/
|
*/
|
||||||
int HTTPClient::POST(uint8_t * payload, size_t size) {
|
int HTTPClient::POST(uint8_t * payload, size_t size)
|
||||||
|
{
|
||||||
return sendRequest("POST", payload, size);
|
return sendRequest("POST", payload, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
int HTTPClient::POST(String payload) {
|
int HTTPClient::POST(String payload)
|
||||||
|
{
|
||||||
return POST((uint8_t *) payload.c_str(), payload.length());
|
return POST((uint8_t *) payload.c_str(), payload.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,7 +339,8 @@ int HTTPClient::POST(String payload) {
|
|||||||
* @param payload String data for the message body
|
* @param payload String data for the message body
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
int HTTPClient::sendRequest(const char * type, String payload) {
|
int HTTPClient::sendRequest(const char * type, String payload)
|
||||||
|
{
|
||||||
return sendRequest(type, (uint8_t *) payload.c_str(), payload.length());
|
return sendRequest(type, (uint8_t *) payload.c_str(), payload.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,14 +351,15 @@ int HTTPClient::sendRequest(const char * type, String payload) {
|
|||||||
* @param size size_t size for the message body if 0 not send
|
* @param size size_t size for the message body if 0 not send
|
||||||
* @return -1 if no info or > 0 when Content-Length is set by server
|
* @return -1 if no info or > 0 when Content-Length is set by server
|
||||||
*/
|
*/
|
||||||
int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
|
int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size)
|
||||||
|
{
|
||||||
// connect to server
|
// connect to server
|
||||||
if(!connect()) {
|
if(!connect()) {
|
||||||
return returnError(HTTPC_ERROR_CONNECTION_REFUSED);
|
return returnError(HTTPC_ERROR_CONNECTION_REFUSED);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(payload && size > 0) {
|
if(payload && size > 0) {
|
||||||
addHeader("Content-Length", String(size));
|
addHeader(F("Content-Length"), String(size));
|
||||||
}
|
}
|
||||||
|
|
||||||
// send Header
|
// send Header
|
||||||
@ -353,7 +385,8 @@ int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
|
|||||||
* @param size size_t size for the message body if 0 not Content-Length is send
|
* @param size size_t size for the message body if 0 not Content-Length is send
|
||||||
* @return -1 if no info or > 0 when Content-Length is set by server
|
* @return -1 if no info or > 0 when Content-Length is set by server
|
||||||
*/
|
*/
|
||||||
int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
|
int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
|
||||||
|
{
|
||||||
|
|
||||||
if(!stream) {
|
if(!stream) {
|
||||||
return returnError(HTTPC_ERROR_NO_STREAM);
|
return returnError(HTTPC_ERROR_NO_STREAM);
|
||||||
@ -468,7 +501,8 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
|
|||||||
free(buff);
|
free(buff);
|
||||||
|
|
||||||
if(size && (int) size != bytesWritten) {
|
if(size && (int) size != bytesWritten) {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, size); DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
|
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, size);
|
||||||
|
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
|
||||||
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
|
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
|
||||||
} else {
|
} else {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
|
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
|
||||||
@ -487,36 +521,38 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
|
|||||||
* size of message body / payload
|
* size of message body / payload
|
||||||
* @return -1 if no info or > 0 when Content-Length is set by server
|
* @return -1 if no info or > 0 when Content-Length is set by server
|
||||||
*/
|
*/
|
||||||
int HTTPClient::getSize(void) {
|
int HTTPClient::getSize(void)
|
||||||
|
{
|
||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* deprecated Note: this is not working with https!
|
|
||||||
* returns the stream of the tcp connection
|
* returns the stream of the tcp connection
|
||||||
* @return WiFiClient
|
* @return WiFiClient
|
||||||
*/
|
*/
|
||||||
WiFiClient & HTTPClient::getStream(void) {
|
WiFiClient& HTTPClient::getStream(void)
|
||||||
|
{
|
||||||
if(connected()) {
|
if(connected()) {
|
||||||
return *_tcp;
|
return *_tcp;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client] no stream to return!?\n");
|
DEBUG_HTTPCLIENT("[HTTP-Client] getStream: not connected\n");
|
||||||
|
static WiFiClient empty;
|
||||||
// todo return error?
|
return empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns the stream of the tcp connection
|
* returns the stream of the tcp connection
|
||||||
* @return WiFiClient *
|
* @return WiFiClient *
|
||||||
*/
|
*/
|
||||||
WiFiClient * HTTPClient::getStreamPtr(void) {
|
WiFiClient* HTTPClient::getStreamPtr(void)
|
||||||
|
{
|
||||||
if(connected()) {
|
if(connected()) {
|
||||||
return _tcp;
|
return _tcp.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client] no stream to return!?\n");
|
DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n");
|
||||||
return NULL;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -524,7 +560,8 @@ WiFiClient * HTTPClient::getStreamPtr(void) {
|
|||||||
* @param stream Stream *
|
* @param stream Stream *
|
||||||
* @return bytes written ( negative values are error codes )
|
* @return bytes written ( negative values are error codes )
|
||||||
*/
|
*/
|
||||||
int HTTPClient::writeToStream(Stream * stream) {
|
int HTTPClient::writeToStream(Stream * stream)
|
||||||
|
{
|
||||||
|
|
||||||
if(!stream) {
|
if(!stream) {
|
||||||
return returnError(HTTPC_ERROR_NO_STREAM);
|
return returnError(HTTPC_ERROR_NO_STREAM);
|
||||||
@ -600,14 +637,15 @@ int HTTPClient::writeToStream(Stream * stream) {
|
|||||||
* return all payload as String (may need lot of ram or trigger out of memory!)
|
* return all payload as String (may need lot of ram or trigger out of memory!)
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
String HTTPClient::getString(void) {
|
String HTTPClient::getString(void)
|
||||||
|
{
|
||||||
StreamString sstring;
|
StreamString sstring;
|
||||||
|
|
||||||
if(_size) {
|
if(_size) {
|
||||||
// try to reserve needed memmory
|
// try to reserve needed memmory
|
||||||
if(!sstring.reserve((_size + 1))) {
|
if(!sstring.reserve((_size + 1))) {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][getString] too less memory to reserve as string! need: %d\n", (_size + 1));
|
DEBUG_HTTPCLIENT("[HTTP-Client][getString] not enough memory to reserve a string! need: %d\n", (_size + 1));
|
||||||
return String("--too less memory--");
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -620,30 +658,31 @@ String HTTPClient::getString(void) {
|
|||||||
* @param error int
|
* @param error int
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
String HTTPClient::errorToString(int error) {
|
String HTTPClient::errorToString(int error)
|
||||||
|
{
|
||||||
switch(error) {
|
switch(error) {
|
||||||
case HTTPC_ERROR_CONNECTION_REFUSED:
|
case HTTPC_ERROR_CONNECTION_REFUSED:
|
||||||
return String("connection refused");
|
return F("connection refused");
|
||||||
case HTTPC_ERROR_SEND_HEADER_FAILED:
|
case HTTPC_ERROR_SEND_HEADER_FAILED:
|
||||||
return String("send header failed");
|
return F("send header failed");
|
||||||
case HTTPC_ERROR_SEND_PAYLOAD_FAILED:
|
case HTTPC_ERROR_SEND_PAYLOAD_FAILED:
|
||||||
return String("send payload failed");
|
return F("send payload failed");
|
||||||
case HTTPC_ERROR_NOT_CONNECTED:
|
case HTTPC_ERROR_NOT_CONNECTED:
|
||||||
return String("not connected");
|
return F("not connected");
|
||||||
case HTTPC_ERROR_CONNECTION_LOST:
|
case HTTPC_ERROR_CONNECTION_LOST:
|
||||||
return String("connection lost");
|
return F("connection lost");
|
||||||
case HTTPC_ERROR_NO_STREAM:
|
case HTTPC_ERROR_NO_STREAM:
|
||||||
return String("no stream");
|
return F("no stream");
|
||||||
case HTTPC_ERROR_NO_HTTP_SERVER:
|
case HTTPC_ERROR_NO_HTTP_SERVER:
|
||||||
return String("no HTTP server");
|
return F("no HTTP server");
|
||||||
case HTTPC_ERROR_TOO_LESS_RAM:
|
case HTTPC_ERROR_TOO_LESS_RAM:
|
||||||
return String("too less ram");
|
return F("too less ram");
|
||||||
case HTTPC_ERROR_ENCODING:
|
case HTTPC_ERROR_ENCODING:
|
||||||
return String("Transfer-Encoding not supported");
|
return F("Transfer-Encoding not supported");
|
||||||
case HTTPC_ERROR_STREAM_WRITE:
|
case HTTPC_ERROR_STREAM_WRITE:
|
||||||
return String("Stream write error");
|
return F("Stream write error");
|
||||||
case HTTPC_ERROR_READ_TIMEOUT:
|
case HTTPC_ERROR_READ_TIMEOUT:
|
||||||
return String("read Timeout");
|
return F("read Timeout");
|
||||||
default:
|
default:
|
||||||
return String();
|
return String();
|
||||||
}
|
}
|
||||||
@ -655,63 +694,78 @@ String HTTPClient::errorToString(int error) {
|
|||||||
* @param value
|
* @param value
|
||||||
* @param first
|
* @param first
|
||||||
*/
|
*/
|
||||||
void HTTPClient::addHeader(const String& name, const String& value, bool first) {
|
void HTTPClient::addHeader(const String& name, const String& value, bool first)
|
||||||
|
{
|
||||||
|
|
||||||
// not allow set of Header handled by code
|
// not allow set of Header handled by code
|
||||||
if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host") && !(_base64Authorization.length() && name.equalsIgnoreCase("Authorization"))) {
|
if(!name.equalsIgnoreCase(F("Connection")) &&
|
||||||
|
!name.equalsIgnoreCase(F("User-Agent")) &&
|
||||||
|
!name.equalsIgnoreCase(F("Host")) &&
|
||||||
|
!(name.equalsIgnoreCase(F("Authorization")) && _base64Authorization.length())){
|
||||||
String headerLine = name;
|
String headerLine = name;
|
||||||
headerLine += ": ";
|
headerLine += ": ";
|
||||||
headerLine += value;
|
headerLine += value;
|
||||||
headerLine += "\r\n";
|
headerLine += "\r\n";
|
||||||
|
|
||||||
if(first) {
|
if(first) {
|
||||||
_Headers = headerLine + _Headers;
|
_headers = headerLine + _headers;
|
||||||
} else {
|
} else {
|
||||||
_Headers += headerLine;
|
_headers += headerLine;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTTPClient::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
|
void HTTPClient::collectHeaders(const char* headerKeys[], const size_t headerKeysCount)
|
||||||
|
{
|
||||||
_headerKeysCount = headerKeysCount;
|
_headerKeysCount = headerKeysCount;
|
||||||
if(_currentHeaders)
|
if(_currentHeaders) {
|
||||||
delete[] _currentHeaders;
|
delete[] _currentHeaders;
|
||||||
|
}
|
||||||
_currentHeaders = new RequestArgument[_headerKeysCount];
|
_currentHeaders = new RequestArgument[_headerKeysCount];
|
||||||
for(size_t i = 0; i < _headerKeysCount; i++) {
|
for(size_t i = 0; i < _headerKeysCount; i++) {
|
||||||
_currentHeaders[i].key = headerKeys[i];
|
_currentHeaders[i].key = headerKeys[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String HTTPClient::header(const char* name) {
|
String HTTPClient::header(const char* name)
|
||||||
|
{
|
||||||
for(size_t i = 0; i < _headerKeysCount; ++i) {
|
for(size_t i = 0; i < _headerKeysCount; ++i) {
|
||||||
if(_currentHeaders[i].key == name)
|
if(_currentHeaders[i].key == name) {
|
||||||
|
return _currentHeaders[i].value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return String();
|
||||||
|
}
|
||||||
|
|
||||||
|
String HTTPClient::header(size_t i)
|
||||||
|
{
|
||||||
|
if(i < _headerKeysCount) {
|
||||||
return _currentHeaders[i].value;
|
return _currentHeaders[i].value;
|
||||||
}
|
}
|
||||||
return String();
|
return String();
|
||||||
}
|
}
|
||||||
|
|
||||||
String HTTPClient::header(size_t i) {
|
String HTTPClient::headerName(size_t i)
|
||||||
if(i < _headerKeysCount)
|
{
|
||||||
return _currentHeaders[i].value;
|
if(i < _headerKeysCount) {
|
||||||
return String();
|
|
||||||
}
|
|
||||||
|
|
||||||
String HTTPClient::headerName(size_t i) {
|
|
||||||
if(i < _headerKeysCount)
|
|
||||||
return _currentHeaders[i].key;
|
return _currentHeaders[i].key;
|
||||||
|
}
|
||||||
return String();
|
return String();
|
||||||
}
|
}
|
||||||
|
|
||||||
int HTTPClient::headers() {
|
int HTTPClient::headers()
|
||||||
|
{
|
||||||
return _headerKeysCount;
|
return _headerKeysCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HTTPClient::hasHeader(const char* name) {
|
bool HTTPClient::hasHeader(const char* name)
|
||||||
|
{
|
||||||
for(size_t i = 0; i < _headerKeysCount; ++i) {
|
for(size_t i = 0; i < _headerKeysCount; ++i) {
|
||||||
if((_currentHeaders[i].key == name) && (_currentHeaders[i].value.length() > 0))
|
if((_currentHeaders[i].key == name) && (_currentHeaders[i].value.length() > 0)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -719,7 +773,8 @@ bool HTTPClient::hasHeader(const char* name) {
|
|||||||
* init TCP connection and handle ssl verify if needed
|
* init TCP connection and handle ssl verify if needed
|
||||||
* @return true if connection is ok
|
* @return true if connection is ok
|
||||||
*/
|
*/
|
||||||
bool HTTPClient::connect(void) {
|
bool HTTPClient::connect(void)
|
||||||
|
{
|
||||||
|
|
||||||
if(connected()) {
|
if(connected()) {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected, try reuse!\n");
|
DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected, try reuse!\n");
|
||||||
@ -729,24 +784,13 @@ bool HTTPClient::connect(void) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_https) {
|
if (!_transportTraits) {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client] connect https...\n");
|
DEBUG_HTTPCLIENT("[HTTP-Client] _transportTraits is null (HTTPClient::begin not called?)\n");
|
||||||
if(_tcps) {
|
return false;
|
||||||
delete _tcps;
|
|
||||||
_tcps = NULL;
|
|
||||||
_tcp = NULL;
|
|
||||||
}
|
|
||||||
_tcps = new WiFiClientSecure();
|
|
||||||
_tcp = _tcps;
|
|
||||||
} else {
|
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client] connect http...\n");
|
|
||||||
if(_tcp) {
|
|
||||||
delete _tcp;
|
|
||||||
_tcp = NULL;
|
|
||||||
}
|
|
||||||
_tcp = new WiFiClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_tcp = _transportTraits->create();
|
||||||
|
|
||||||
if(!_tcp->connect(_host.c_str(), _port)) {
|
if(!_tcp->connect(_host.c_str(), _port)) {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u\n", _host.c_str(), _port);
|
DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u\n", _host.c_str(), _port);
|
||||||
return false;
|
return false;
|
||||||
@ -754,15 +798,11 @@ bool HTTPClient::connect(void) {
|
|||||||
|
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port);
|
DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port);
|
||||||
|
|
||||||
if(_https && _httpsFingerprint.length() > 0) {
|
if (!_transportTraits->verify(*_tcp, _host.c_str())) {
|
||||||
if(_tcps->verify(_httpsFingerprint.c_str(), _host.c_str())) {
|
DEBUG_HTTPCLIENT("[HTTP-Client] transport level verify failed\n");
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client] https certificate matches\n");
|
|
||||||
} else {
|
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client] https certificate doesn't match!\n");
|
|
||||||
_tcp->stop();
|
_tcp->stop();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// set Timeout for readBytesUntil and readStringUntil
|
// set Timeout for readBytesUntil and readStringUntil
|
||||||
_tcp->setTimeout(_tcpTimeout);
|
_tcp->setTimeout(_tcpTimeout);
|
||||||
@ -778,12 +818,13 @@ bool HTTPClient::connect(void) {
|
|||||||
* @param type (GET, POST, ...)
|
* @param type (GET, POST, ...)
|
||||||
* @return status
|
* @return status
|
||||||
*/
|
*/
|
||||||
bool HTTPClient::sendHeader(const char * type) {
|
bool HTTPClient::sendHeader(const char * type)
|
||||||
|
{
|
||||||
if(!connected()) {
|
if(!connected()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String header = String(type) + " " + _url + " HTTP/1.";
|
String header = String(type) + " " + _uri + F(" HTTP/1.");
|
||||||
|
|
||||||
if(_useHTTP10) {
|
if(_useHTTP10) {
|
||||||
header += "0";
|
header += "0";
|
||||||
@ -791,36 +832,38 @@ bool HTTPClient::sendHeader(const char * type) {
|
|||||||
header += "1";
|
header += "1";
|
||||||
}
|
}
|
||||||
|
|
||||||
header += "\r\n"
|
header += String(F("\r\nHost: ")) + _host +
|
||||||
"Host: " + _host + "\r\n"
|
F("\r\nUser-Agent: ") + _userAgent +
|
||||||
"User-Agent: " + _userAgent + "\r\n"
|
F("\r\nConnection: ");
|
||||||
"Connection: ";
|
|
||||||
|
|
||||||
if(_reuse) {
|
if(_reuse) {
|
||||||
header += "keep-alive";
|
header += F("keep-alive");
|
||||||
} else {
|
} else {
|
||||||
header += "close";
|
header += F("close");
|
||||||
}
|
}
|
||||||
header += "\r\n";
|
header += "\r\n";
|
||||||
|
|
||||||
if(!_useHTTP10) {
|
if(!_useHTTP10) {
|
||||||
header += "Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0\r\n";
|
header += F("Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_base64Authorization.length()) {
|
if(_base64Authorization.length()) {
|
||||||
header += "Authorization: Basic " + _base64Authorization + "\r\n";
|
header += F("Authorization: Basic ");
|
||||||
|
header += _base64Authorization;
|
||||||
|
header += "\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
header += _Headers + "\r\n";
|
header += _headers + "\r\n";
|
||||||
|
|
||||||
return (_tcp->write(header.c_str(), header.length()) == header.length());
|
return (_tcp->write((const uint8_t *) header.c_str(), header.length()) == header.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* reads the response from the server
|
* reads the response from the server
|
||||||
* @return int http code
|
* @return int http code
|
||||||
*/
|
*/
|
||||||
int HTTPClient::handleHeaderResponse() {
|
int HTTPClient::handleHeaderResponse()
|
||||||
|
{
|
||||||
|
|
||||||
if(!connected()) {
|
if(!connected()) {
|
||||||
return HTTPC_ERROR_NOT_CONNECTED;
|
return HTTPC_ERROR_NOT_CONNECTED;
|
||||||
@ -911,7 +954,8 @@ int HTTPClient::handleHeaderResponse() {
|
|||||||
* @param size int
|
* @param size int
|
||||||
* @return < 0 = error >= 0 = size written
|
* @return < 0 = error >= 0 = size written
|
||||||
*/
|
*/
|
||||||
int HTTPClient::writeToStreamDataBlock(Stream * stream, int size) {
|
int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
|
||||||
|
{
|
||||||
int buff_size = HTTP_TCP_BUFFER_SIZE;
|
int buff_size = HTTP_TCP_BUFFER_SIZE;
|
||||||
int len = size;
|
int len = size;
|
||||||
int bytesWritten = 0;
|
int bytesWritten = 0;
|
||||||
@ -1021,7 +1065,8 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size) {
|
|||||||
* @param error
|
* @param error
|
||||||
* @return error
|
* @return error
|
||||||
*/
|
*/
|
||||||
int HTTPClient::returnError(int error) {
|
int HTTPClient::returnError(int error)
|
||||||
|
{
|
||||||
if(error < 0) {
|
if(error < 0) {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][returnError] error(%d): %s\n", error, errorToString(error).c_str());
|
DEBUG_HTTPCLIENT("[HTTP-Client][returnError] error(%d): %s\n", error, errorToString(error).c_str());
|
||||||
if(connected()) {
|
if(connected()) {
|
||||||
|
@ -25,6 +25,10 @@
|
|||||||
#ifndef ESP8266HTTPClient_H_
|
#ifndef ESP8266HTTPClient_H_
|
||||||
#define ESP8266HTTPClient_H_
|
#define ESP8266HTTPClient_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <WiFiClient.h>
|
||||||
|
|
||||||
#ifdef DEBUG_ESP_HTTP_CLIENT
|
#ifdef DEBUG_ESP_HTTP_CLIENT
|
||||||
#ifdef DEBUG_ESP_PORT
|
#ifdef DEBUG_ESP_PORT
|
||||||
#define DEBUG_HTTPCLIENT(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
|
#define DEBUG_HTTPCLIENT(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
|
||||||
@ -120,23 +124,28 @@ typedef enum {
|
|||||||
HTTPC_TE_CHUNKED
|
HTTPC_TE_CHUNKED
|
||||||
} transferEncoding_t;
|
} transferEncoding_t;
|
||||||
|
|
||||||
class HTTPClient {
|
class TransportTraits;
|
||||||
public:
|
typedef std::unique_ptr<TransportTraits> TransportTraitsPtr;
|
||||||
|
|
||||||
|
class HTTPClient
|
||||||
|
{
|
||||||
|
public:
|
||||||
HTTPClient();
|
HTTPClient();
|
||||||
~HTTPClient();
|
~HTTPClient();
|
||||||
|
|
||||||
void begin(const char *url, const char * httpsFingerprint = "");
|
bool begin(String url);
|
||||||
void begin(String url, String httpsFingerprint = "");
|
bool begin(String url, String httpsFingerprint);
|
||||||
|
bool begin(String host, uint16_t port, String uri = "/");
|
||||||
void begin(const char *host, uint16_t port, const char * url = "/", bool https = false, const char * httpsFingerprint = "");
|
bool begin(String host, uint16_t port, String uri, String httpsFingerprint);
|
||||||
void begin(String host, uint16_t port, String url = "/", bool https = false, String httpsFingerprint = "");
|
// deprecated, use the overload above instead
|
||||||
|
bool begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) __attribute__ ((deprecated));
|
||||||
|
|
||||||
void end(void);
|
void end(void);
|
||||||
|
|
||||||
bool connected(void);
|
bool connected(void);
|
||||||
|
|
||||||
void setReuse(bool reuse); /// keep-alive
|
void setReuse(bool reuse); /// keep-alive
|
||||||
void setUserAgent(const char * userAgent);
|
void setUserAgent(const String& userAgent);
|
||||||
void setAuthorization(const char * user, const char * password);
|
void setAuthorization(const char * user, const char * password);
|
||||||
void setAuthorization(const char * auth);
|
void setAuthorization(const char * auth);
|
||||||
void setTimeout(uint16_t timeout);
|
void setTimeout(uint16_t timeout);
|
||||||
@ -164,53 +173,51 @@ class HTTPClient {
|
|||||||
|
|
||||||
int getSize(void);
|
int getSize(void);
|
||||||
|
|
||||||
WiFiClient & getStream(void) __attribute__ ((deprecated)) ;
|
WiFiClient& getStream(void);
|
||||||
WiFiClient * getStreamPtr(void);
|
WiFiClient* getStreamPtr(void);
|
||||||
int writeToStream(Stream * stream);
|
int writeToStream(Stream* stream);
|
||||||
String getString(void);
|
String getString(void);
|
||||||
|
|
||||||
static String errorToString(int error);
|
static String errorToString(int error);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
struct RequestArgument {
|
struct RequestArgument {
|
||||||
String key;
|
String key;
|
||||||
String value;
|
String value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void clear();
|
||||||
WiFiClient * _tcp;
|
|
||||||
WiFiClientSecure * _tcps;
|
|
||||||
|
|
||||||
/// request handling
|
|
||||||
String _host;
|
|
||||||
uint16_t _port;
|
|
||||||
bool _reuse;
|
|
||||||
uint16_t _tcpTimeout;
|
|
||||||
bool _useHTTP10;
|
|
||||||
|
|
||||||
String _url;
|
|
||||||
bool _https;
|
|
||||||
String _httpsFingerprint;
|
|
||||||
|
|
||||||
String _Headers;
|
|
||||||
String _userAgent;
|
|
||||||
String _base64Authorization;
|
|
||||||
|
|
||||||
/// Response handling
|
|
||||||
RequestArgument* _currentHeaders;
|
|
||||||
size_t _headerKeysCount;
|
|
||||||
|
|
||||||
int _returnCode;
|
|
||||||
int _size;
|
|
||||||
bool _canReuse;
|
|
||||||
transferEncoding_t _transferEncoding;
|
|
||||||
|
|
||||||
int returnError(int error);
|
int returnError(int error);
|
||||||
bool connect(void);
|
bool connect(void);
|
||||||
bool sendHeader(const char * type);
|
bool sendHeader(const char * type);
|
||||||
int handleHeaderResponse();
|
int handleHeaderResponse();
|
||||||
int writeToStreamDataBlock(Stream * stream, int len);
|
int writeToStreamDataBlock(Stream * stream, int len);
|
||||||
|
|
||||||
|
|
||||||
|
TransportTraitsPtr _transportTraits;
|
||||||
|
std::unique_ptr<WiFiClient> _tcp;
|
||||||
|
|
||||||
|
/// request handling
|
||||||
|
String _host;
|
||||||
|
uint16_t _port = 0;
|
||||||
|
bool _reuse = false;
|
||||||
|
uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
|
||||||
|
bool _useHTTP10 = false;
|
||||||
|
|
||||||
|
String _uri;
|
||||||
|
String _protocol;
|
||||||
|
String _headers;
|
||||||
|
String _userAgent = "ESP8266HTTPClient";
|
||||||
|
String _base64Authorization;
|
||||||
|
|
||||||
|
/// Response handling
|
||||||
|
RequestArgument* _currentHeaders = nullptr;
|
||||||
|
size_t _headerKeysCount = 0;
|
||||||
|
|
||||||
|
int _returnCode = 0;
|
||||||
|
int _size = -1;
|
||||||
|
bool _canReuse = false;
|
||||||
|
transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=ESP8266httpUpdate
|
name=ESP8266httpUpdate
|
||||||
version=1.0
|
version=1.1
|
||||||
author=Markus Sattler
|
author=Markus Sattler
|
||||||
maintainer=Markus Sattler
|
maintainer=Markus Sattler
|
||||||
sentence=Http Update for ESP8266
|
sentence=Http Update for ESP8266
|
||||||
|
@ -29,107 +29,127 @@
|
|||||||
extern "C" uint32_t _SPIFFS_start;
|
extern "C" uint32_t _SPIFFS_start;
|
||||||
extern "C" uint32_t _SPIFFS_end;
|
extern "C" uint32_t _SPIFFS_end;
|
||||||
|
|
||||||
ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) {
|
ESP8266HTTPUpdate::ESP8266HTTPUpdate(void)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) {
|
ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion,
|
||||||
*
|
const String& httpsFingerprint, bool reboot)
|
||||||
* @param url const char *
|
{
|
||||||
* @param current_version const char *
|
rebootOnUpdate(reboot);
|
||||||
* @param httpsFingerprint const char *
|
return update(url, currentVersion, httpsFingerprint);
|
||||||
* @return t_httpUpdate_return
|
}
|
||||||
*/
|
|
||||||
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * current_version, const char * httpsFingerprint, bool reboot) {
|
HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion)
|
||||||
|
{
|
||||||
|
HTTPClient http;
|
||||||
|
http.begin(url);
|
||||||
|
return handleUpdate(http, currentVersion, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion,
|
||||||
|
const String& httpsFingerprint)
|
||||||
|
{
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
http.begin(url, httpsFingerprint);
|
http.begin(url, httpsFingerprint);
|
||||||
return handleUpdate(&http, current_version, reboot, false);
|
return handleUpdate(http, currentVersion, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint)
|
||||||
*
|
{
|
||||||
* @param url const char *
|
|
||||||
* @param current_version const char *
|
|
||||||
* @param httpsFingerprint const char *
|
|
||||||
* @return t_httpUpdate_return
|
|
||||||
*/
|
|
||||||
t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint, bool reboot) {
|
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
http.begin(url, httpsFingerprint);
|
http.begin(url, httpsFingerprint);
|
||||||
return handleUpdate(&http, current_version, reboot, true);
|
return handleUpdate(http, currentVersion, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion)
|
||||||
*
|
{
|
||||||
* @param host const char *
|
|
||||||
* @param port uint16_t
|
|
||||||
* @param url const char *
|
|
||||||
* @param current_version const char *
|
|
||||||
* @param httpsFingerprint const char *
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port, const char * url, const char * current_version, bool https, const char * httpsFingerprint, bool reboot) {
|
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
http.begin(host, port, url, https, httpsFingerprint);
|
http.begin(url);
|
||||||
return handleUpdate(&http, current_version, reboot, false);
|
return handleUpdate(http, currentVersion, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String url, String current_version, bool https, String httpsFingerprint, bool reboot) {
|
HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& uri, const String& currentVersion,
|
||||||
|
bool https, const String& httpsFingerprint, bool reboot)
|
||||||
|
{
|
||||||
|
rebootOnUpdate(reboot);
|
||||||
|
if (httpsFingerprint.length() == 0) {
|
||||||
|
return update(host, port, uri, currentVersion);
|
||||||
|
} else {
|
||||||
|
return update(host, port, uri, currentVersion, httpsFingerprint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& uri,
|
||||||
|
const String& currentVersion)
|
||||||
|
{
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
http.begin(host, port, url, https, httpsFingerprint);
|
http.begin(host, port, uri);
|
||||||
return handleUpdate(&http, current_version.c_str(), reboot, false);
|
return handleUpdate(http, currentVersion, false);
|
||||||
|
}
|
||||||
|
HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& url,
|
||||||
|
const String& currentVersion, const String& httpsFingerprint)
|
||||||
|
{
|
||||||
|
HTTPClient http;
|
||||||
|
http.begin(host, port, url, httpsFingerprint);
|
||||||
|
return handleUpdate(http, currentVersion, false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return error code as int
|
* return error code as int
|
||||||
* @return int error code
|
* @return int error code
|
||||||
*/
|
*/
|
||||||
int ESP8266HTTPUpdate::getLastError(void){
|
int ESP8266HTTPUpdate::getLastError(void)
|
||||||
return lastError;
|
{
|
||||||
|
return _lastError;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return error code as String
|
* return error code as String
|
||||||
* @return String error
|
* @return String error
|
||||||
*/
|
*/
|
||||||
String ESP8266HTTPUpdate::getLastErrorString(void) {
|
String ESP8266HTTPUpdate::getLastErrorString(void)
|
||||||
|
{
|
||||||
|
|
||||||
if(lastError == 0) {
|
if(_lastError == 0) {
|
||||||
return String(); // no error
|
return String(); // no error
|
||||||
}
|
}
|
||||||
|
|
||||||
// error from Update class
|
// error from Update class
|
||||||
if(lastError > 0) {
|
if(_lastError > 0) {
|
||||||
StreamString error;
|
StreamString error;
|
||||||
Update.printError(error);
|
Update.printError(error);
|
||||||
error.trim(); // remove line ending
|
error.trim(); // remove line ending
|
||||||
return "Update error: " + error;
|
return String(F("Update error: ")) + error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// error from http client
|
// error from http client
|
||||||
if(lastError > -100) {
|
if(_lastError > -100) {
|
||||||
return "HTTP error: " + HTTPClient::errorToString(lastError);
|
return String(F("HTTP error: ")) + HTTPClient::errorToString(_lastError);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(lastError) {
|
switch(_lastError) {
|
||||||
case HTTP_UE_TOO_LESS_SPACE:
|
case HTTP_UE_TOO_LESS_SPACE:
|
||||||
return String("To less space");
|
return F("To less space");
|
||||||
case HTTP_UE_SERVER_NOT_REPORT_SIZE:
|
case HTTP_UE_SERVER_NOT_REPORT_SIZE:
|
||||||
return String("Server not Report Size");
|
return F("Server not Report Size");
|
||||||
case HTTP_UE_SERVER_FILE_NOT_FOUND:
|
case HTTP_UE_SERVER_FILE_NOT_FOUND:
|
||||||
return String("File not Found (404)");
|
return F("File not Found (404)");
|
||||||
case HTTP_UE_SERVER_FORBIDDEN:
|
case HTTP_UE_SERVER_FORBIDDEN:
|
||||||
return String("Forbidden (403)");
|
return F("Forbidden (403)");
|
||||||
case HTTP_UE_SERVER_WRONG_HTTP_CODE:
|
case HTTP_UE_SERVER_WRONG_HTTP_CODE:
|
||||||
return String("Wrong HTTP code");
|
return F("Wrong HTTP code");
|
||||||
case HTTP_UE_SERVER_FAULTY_MD5:
|
case HTTP_UE_SERVER_FAULTY_MD5:
|
||||||
return String("Faulty MD5");
|
return F("Faulty MD5");
|
||||||
case HTTP_UE_BIN_VERIFY_HEADER_FAILED:
|
case HTTP_UE_BIN_VERIFY_HEADER_FAILED:
|
||||||
return String("Verify bin header failed");
|
return F("Verify bin header failed");
|
||||||
case HTTP_UE_BIN_FOR_WRONG_FLASH:
|
case HTTP_UE_BIN_FOR_WRONG_FLASH:
|
||||||
return String("bin for wrong flash size");
|
return F("bin for wrong flash size");
|
||||||
}
|
}
|
||||||
|
|
||||||
return String();
|
return String();
|
||||||
@ -139,48 +159,49 @@ String ESP8266HTTPUpdate::getLastErrorString(void) {
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param http HTTPClient *
|
* @param http HTTPClient *
|
||||||
* @param current_version const char *
|
* @param currentVersion const char *
|
||||||
* @return t_httpUpdate_return
|
* @return HTTPUpdateResult
|
||||||
*/
|
*/
|
||||||
t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version, bool reboot, bool spiffs) {
|
HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs)
|
||||||
|
{
|
||||||
|
|
||||||
t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
|
HTTPUpdateResult ret = HTTP_UPDATE_FAILED;
|
||||||
|
|
||||||
// use HTTP/1.0 for update since the update handler not support any transfer Encoding
|
// use HTTP/1.0 for update since the update handler not support any transfer Encoding
|
||||||
http->useHTTP10(true);
|
http.useHTTP10(true);
|
||||||
http->setTimeout(8000);
|
http.setTimeout(8000);
|
||||||
http->setUserAgent("ESP8266-http-Update");
|
http.setUserAgent(F("ESP8266-http-Update"));
|
||||||
http->addHeader("x-ESP8266-STA-MAC", WiFi.macAddress());
|
http.addHeader(F("x-ESP8266-STA-MAC"), WiFi.macAddress());
|
||||||
http->addHeader("x-ESP8266-AP-MAC", WiFi.softAPmacAddress());
|
http.addHeader(F("x-ESP8266-AP-MAC"), WiFi.softAPmacAddress());
|
||||||
http->addHeader("x-ESP8266-free-space", String(ESP.getFreeSketchSpace()));
|
http.addHeader(F("x-ESP8266-free-space"), String(ESP.getFreeSketchSpace()));
|
||||||
http->addHeader("x-ESP8266-sketch-size", String(ESP.getSketchSize()));
|
http.addHeader(F("x-ESP8266-sketch-size"), String(ESP.getSketchSize()));
|
||||||
http->addHeader("x-ESP8266-chip-size", String(ESP.getFlashChipRealSize()));
|
http.addHeader(F("x-ESP8266-chip-size"), String(ESP.getFlashChipRealSize()));
|
||||||
http->addHeader("x-ESP8266-sdk-version", ESP.getSdkVersion());
|
http.addHeader(F("x-ESP8266-sdk-version"), ESP.getSdkVersion());
|
||||||
|
|
||||||
if(spiffs) {
|
if(spiffs) {
|
||||||
http->addHeader("x-ESP8266-mode", "spiffs");
|
http.addHeader(F("x-ESP8266-mode"), F("spiffs"));
|
||||||
} else {
|
} else {
|
||||||
http->addHeader("x-ESP8266-mode", "sketch");
|
http.addHeader(F("x-ESP8266-mode"), F("sketch"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(current_version && current_version[0] != 0x00) {
|
if(currentVersion && currentVersion[0] != 0x00) {
|
||||||
http->addHeader("x-ESP8266-version", current_version);
|
http.addHeader(F("x-ESP8266-version"), currentVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * headerkeys[] = { "x-MD5" };
|
const char * headerkeys[] = { "x-MD5" };
|
||||||
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
|
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
|
||||||
|
|
||||||
// track these headers
|
// track these headers
|
||||||
http->collectHeaders(headerkeys, headerkeyssize);
|
http.collectHeaders(headerkeys, headerkeyssize);
|
||||||
|
|
||||||
|
|
||||||
int code = http->GET();
|
int code = http.GET();
|
||||||
int len = http->getSize();
|
int len = http.getSize();
|
||||||
|
|
||||||
if(code <= 0) {
|
if(code <= 0) {
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http->errorToString(code).c_str());
|
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http.errorToString(code).c_str());
|
||||||
lastError = code;
|
_lastError = code;
|
||||||
http->end();
|
http.end();
|
||||||
return HTTP_UPDATE_FAILED;
|
return HTTP_UPDATE_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,16 +211,16 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
|
|||||||
DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code);
|
DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code);
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len);
|
DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len);
|
||||||
|
|
||||||
if(http->hasHeader("x-MD5")) {
|
if(http.hasHeader("x-MD5")) {
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] - MD5: %s\n", http->header("x-MD5").c_str());
|
DEBUG_HTTP_UPDATE("[httpUpdate] - MD5: %s\n", http.header("x-MD5").c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n");
|
DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n");
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace());
|
DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace());
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize());
|
DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize());
|
||||||
|
|
||||||
if(current_version && current_version[0] != 0x00) {
|
if(currentVersion && currentVersion[0] != 0x00) {
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", current_version);
|
DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", currentVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(code) {
|
switch(code) {
|
||||||
@ -220,11 +241,11 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!startUpdate) {
|
if(!startUpdate) {
|
||||||
lastError = HTTP_UE_TOO_LESS_SPACE;
|
_lastError = HTTP_UE_TOO_LESS_SPACE;
|
||||||
ret = HTTP_UPDATE_FAILED;
|
ret = HTTP_UPDATE_FAILED;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
WiFiClient * tcp = http->getStreamPtr();
|
WiFiClient * tcp = http.getStreamPtr();
|
||||||
|
|
||||||
WiFiUDP::stopAll();
|
WiFiUDP::stopAll();
|
||||||
WiFiClient::stopAllExcept(tcp);
|
WiFiClient::stopAllExcept(tcp);
|
||||||
@ -245,16 +266,16 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
|
|||||||
uint8_t buf[4];
|
uint8_t buf[4];
|
||||||
if(tcp->peekBytes(&buf[0], 4) != 4) {
|
if(tcp->peekBytes(&buf[0], 4) != 4) {
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] peekBytes magic header failed\n");
|
DEBUG_HTTP_UPDATE("[httpUpdate] peekBytes magic header failed\n");
|
||||||
lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
|
_lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
|
||||||
http->end();
|
http.end();
|
||||||
return HTTP_UPDATE_FAILED;
|
return HTTP_UPDATE_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for valid first magic byte
|
// check for valid first magic byte
|
||||||
if(buf[0] != 0xE9) {
|
if(buf[0] != 0xE9) {
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] magic header not starts with 0xE9\n");
|
DEBUG_HTTP_UPDATE("[httpUpdate] magic header not starts with 0xE9\n");
|
||||||
lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
|
_lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
|
||||||
http->end();
|
http.end();
|
||||||
return HTTP_UPDATE_FAILED;
|
return HTTP_UPDATE_FAILED;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -264,18 +285,18 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
|
|||||||
// check if new bin fits to SPI flash
|
// check if new bin fits to SPI flash
|
||||||
if(bin_flash_size > ESP.getFlashChipRealSize()) {
|
if(bin_flash_size > ESP.getFlashChipRealSize()) {
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] magic header, new bin not fits SPI Flash\n");
|
DEBUG_HTTP_UPDATE("[httpUpdate] magic header, new bin not fits SPI Flash\n");
|
||||||
lastError = HTTP_UE_BIN_FOR_WRONG_FLASH;
|
_lastError = HTTP_UE_BIN_FOR_WRONG_FLASH;
|
||||||
http->end();
|
http.end();
|
||||||
return HTTP_UPDATE_FAILED;
|
return HTTP_UPDATE_FAILED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(runUpdate(*tcp, len, http->header("x-MD5"), command)) {
|
if(runUpdate(*tcp, len, http.header("x-MD5"), command)) {
|
||||||
ret = HTTP_UPDATE_OK;
|
ret = HTTP_UPDATE_OK;
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
|
DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
|
||||||
http->end();
|
http.end();
|
||||||
|
|
||||||
if(reboot) {
|
if(_rebootOnUpdate) {
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,7 +306,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lastError = HTTP_UE_SERVER_NOT_REPORT_SIZE;
|
_lastError = HTTP_UE_SERVER_NOT_REPORT_SIZE;
|
||||||
ret = HTTP_UPDATE_FAILED;
|
ret = HTTP_UPDATE_FAILED;
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n");
|
DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n");
|
||||||
}
|
}
|
||||||
@ -295,22 +316,22 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
|
|||||||
ret = HTTP_UPDATE_NO_UPDATES;
|
ret = HTTP_UPDATE_NO_UPDATES;
|
||||||
break;
|
break;
|
||||||
case HTTP_CODE_NOT_FOUND:
|
case HTTP_CODE_NOT_FOUND:
|
||||||
lastError = HTTP_UE_SERVER_FILE_NOT_FOUND;
|
_lastError = HTTP_UE_SERVER_FILE_NOT_FOUND;
|
||||||
ret = HTTP_UPDATE_FAILED;
|
ret = HTTP_UPDATE_FAILED;
|
||||||
break;
|
break;
|
||||||
case HTTP_CODE_FORBIDDEN:
|
case HTTP_CODE_FORBIDDEN:
|
||||||
lastError = HTTP_UE_SERVER_FORBIDDEN;
|
_lastError = HTTP_UE_SERVER_FORBIDDEN;
|
||||||
ret = HTTP_UPDATE_FAILED;
|
ret = HTTP_UPDATE_FAILED;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
lastError = HTTP_UE_SERVER_WRONG_HTTP_CODE;
|
_lastError = HTTP_UE_SERVER_WRONG_HTTP_CODE;
|
||||||
ret = HTTP_UPDATE_FAILED;
|
ret = HTTP_UPDATE_FAILED;
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code);
|
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code);
|
||||||
//http->writeToStream(&Serial1);
|
//http.writeToStream(&Serial1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
http->end();
|
http.end();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -321,12 +342,13 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
|
|||||||
* @param md5 String
|
* @param md5 String
|
||||||
* @return true if Update ok
|
* @return true if Update ok
|
||||||
*/
|
*/
|
||||||
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command) {
|
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command)
|
||||||
|
{
|
||||||
|
|
||||||
StreamString error;
|
StreamString error;
|
||||||
|
|
||||||
if(!Update.begin(size, command)) {
|
if(!Update.begin(size, command)) {
|
||||||
lastError = Update.getError();
|
_lastError = Update.getError();
|
||||||
Update.printError(error);
|
Update.printError(error);
|
||||||
error.trim(); // remove line ending
|
error.trim(); // remove line ending
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str());
|
DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str());
|
||||||
@ -335,14 +357,14 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com
|
|||||||
|
|
||||||
if(md5.length()) {
|
if(md5.length()) {
|
||||||
if(!Update.setMD5(md5.c_str())) {
|
if(!Update.setMD5(md5.c_str())) {
|
||||||
lastError = HTTP_UE_SERVER_FAULTY_MD5;
|
_lastError = HTTP_UE_SERVER_FAULTY_MD5;
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] Update.setMD5 failed! (%s)\n", md5.c_str());
|
DEBUG_HTTP_UPDATE("[httpUpdate] Update.setMD5 failed! (%s)\n", md5.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Update.writeStream(in) != size) {
|
if(Update.writeStream(in) != size) {
|
||||||
lastError = Update.getError();
|
_lastError = Update.getError();
|
||||||
Update.printError(error);
|
Update.printError(error);
|
||||||
error.trim(); // remove line ending
|
error.trim(); // remove line ending
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str());
|
DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str());
|
||||||
@ -350,7 +372,7 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!Update.end()) {
|
if(!Update.end()) {
|
||||||
lastError = Update.getError();
|
_lastError = Update.getError();
|
||||||
Update.printError(error);
|
Update.printError(error);
|
||||||
error.trim(); // remove line ending
|
error.trim(); // remove line ending
|
||||||
DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed! (%s)\n", error.c_str());
|
DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed! (%s)\n", error.c_str());
|
||||||
|
@ -52,31 +52,57 @@
|
|||||||
#define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106)
|
#define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106)
|
||||||
#define HTTP_UE_BIN_FOR_WRONG_FLASH (-107)
|
#define HTTP_UE_BIN_FOR_WRONG_FLASH (-107)
|
||||||
|
|
||||||
typedef enum {
|
enum HTTPUpdateResult {
|
||||||
HTTP_UPDATE_FAILED,
|
HTTP_UPDATE_FAILED,
|
||||||
HTTP_UPDATE_NO_UPDATES,
|
HTTP_UPDATE_NO_UPDATES,
|
||||||
HTTP_UPDATE_OK
|
HTTP_UPDATE_OK
|
||||||
} t_httpUpdate_return;
|
};
|
||||||
|
|
||||||
class ESP8266HTTPUpdate {
|
typedef HTTPUpdateResult t_httpUpdate_return; // backward compatibility
|
||||||
public:
|
|
||||||
|
class ESP8266HTTPUpdate
|
||||||
|
{
|
||||||
|
public:
|
||||||
ESP8266HTTPUpdate(void);
|
ESP8266HTTPUpdate(void);
|
||||||
~ESP8266HTTPUpdate(void);
|
~ESP8266HTTPUpdate(void);
|
||||||
|
|
||||||
t_httpUpdate_return update(const char * url, const char * current_version = "", const char * httpsFingerprint = "", bool reboot = true);
|
void rebootOnUpdate(bool reboot)
|
||||||
t_httpUpdate_return update(const char * host, uint16_t port, const char * url = "/", const char * current_version = "", bool https = false, const char * httpsFingerprint = "", bool reboot = true);
|
{
|
||||||
t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "", bool https = false, String httpsFingerprint = "", bool reboot = true);
|
_rebootOnUpdate = reboot;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is deprecated, use rebootOnUpdate and the next one instead
|
||||||
|
t_httpUpdate_return update(const String& url, const String& currentVersion,
|
||||||
|
const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
|
||||||
|
t_httpUpdate_return update(const String& url, const String& currentVersion = "");
|
||||||
|
t_httpUpdate_return update(const String& url, const String& currentVersion,
|
||||||
|
const String& httpsFingerprint);
|
||||||
|
|
||||||
|
// This function is deprecated, use one of the overloads below along with rebootOnUpdate
|
||||||
|
t_httpUpdate_return update(const String& host, uint16_t port, const String& uri, const String& currentVersion,
|
||||||
|
bool https, const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
|
||||||
|
|
||||||
|
t_httpUpdate_return update(const String& host, uint16_t port, const String& uri = "/",
|
||||||
|
const String& currentVersion = "");
|
||||||
|
t_httpUpdate_return update(const String& host, uint16_t port, const String& url,
|
||||||
|
const String& currentVersion, const String& httpsFingerprint);
|
||||||
|
|
||||||
|
// This function is deprecated, use rebootOnUpdate and the next one instead
|
||||||
|
t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion,
|
||||||
|
const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
|
||||||
|
t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion = "");
|
||||||
|
t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint);
|
||||||
|
|
||||||
t_httpUpdate_return updateSpiffs(const char * url, const char * current_version = "", const char * httpsFingerprint = "", bool reboot = false);
|
|
||||||
|
|
||||||
int getLastError(void);
|
int getLastError(void);
|
||||||
String getLastErrorString(void);
|
String getLastErrorString(void);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version, bool reboot = true, bool spiffs = false);
|
t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false);
|
||||||
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
|
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
|
||||||
|
|
||||||
int lastError;
|
int _lastError;
|
||||||
|
bool _rebootOnUpdate = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern ESP8266HTTPUpdate ESPhttpUpdate;
|
extern ESP8266HTTPUpdate ESPhttpUpdate;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user