From a9ce1b4f2ebb774d69aab6573bd474d5d704ff4e Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Wed, 9 Dec 2015 13:51:55 +0100 Subject: [PATCH] add Authorization support for HTTP client --- cores/esp8266/core_esp8266_features.h | 1 + .../src/ESP8266HTTPClient.cpp | 26 +++++++++++++++++-- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 4 ++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/core_esp8266_features.h b/cores/esp8266/core_esp8266_features.h index bafcd3aee..ae5d58539 100644 --- a/cores/esp8266/core_esp8266_features.h +++ b/cores/esp8266/core_esp8266_features.h @@ -26,6 +26,7 @@ #define CORE_HAS_LIBB64 +#define CORE_HAS_BASE64_CLASS #endif diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 5561c62ae..357aebda3 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -26,9 +26,11 @@ #include #include #include +#include #include "ESP8266HTTPClient.h" + /** * constractor */ @@ -219,6 +221,20 @@ void HTTPClient::setUserAgent(const char * userAgent) { _userAgent = userAgent; } +/** + * set the Authorizatio for the http request + * @param user const char * + * @param password const char * + */ +void HTTPClient::setAuthorization(const char * user, const char * password) { + if(user && password) { + String auth = user; + auth += ":"; + auth += password; + _base64Authorization = base64::encode(auth); + } +} + /** * send a GET request * @return http code @@ -490,7 +506,7 @@ String HTTPClient::errorToString(int error) { void HTTPClient::addHeader(const String& name, const String& value, bool first) { // not allow set of Header handled by code - if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host")) { + if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host") && !(_base64Authorization.length() && name.equalsIgnoreCase("Authorization"))) { String headerLine = name; headerLine += ": "; headerLine += value; @@ -622,7 +638,13 @@ bool HTTPClient::sendHeader(const char * type) { } else { header += "close"; } - header += "\r\n" + _Headers + "\r\n"; + header += "\r\n"; + + if(_base64Authorization.length()) { + header += "Authorization: Basic " + _base64Authorization + "\r\n"; + } + + header += _Headers + "\r\n"; return (_tcp->write(header.c_str(), header.length()) == header.length()); } diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 5d3fc51bc..f6c4bba6a 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -25,7 +25,7 @@ #ifndef ESP8266HTTPClient_H_ #define ESP8266HTTPClient_H_ -//#define DEBUG_HTTPCLIENT(...) Serial1.printf( __VA_ARGS__ ) +#define DEBUG_HTTPCLIENT(...) Serial1.printf( __VA_ARGS__ ) #ifndef DEBUG_HTTPCLIENT #define DEBUG_HTTPCLIENT(...) @@ -121,6 +121,7 @@ class HTTPClient { void setReuse(bool reuse); /// keep-alive void setUserAgent(const char * userAgent); + void setAuthorization(const char * user, const char * password); /// request handling int GET(); @@ -172,6 +173,7 @@ class HTTPClient { String _Headers; String _userAgent; + String _base64Authorization; /// Response handling RequestArgument* _currentHeaders;