|
26 | 26 | #include <ESP8266WiFi.h> |
27 | 27 | #include <WiFiClientSecure.h> |
28 | 28 | #include <StreamString.h> |
| 29 | +#include <base64.h> |
29 | 30 |
|
30 | 31 | #include "ESP8266HTTPClient.h" |
31 | 32 |
|
| 33 | + |
32 | 34 | /** |
33 | 35 | * constractor |
34 | 36 | */ |
@@ -219,6 +221,20 @@ void HTTPClient::setUserAgent(const char * userAgent) { |
219 | 221 | _userAgent = userAgent; |
220 | 222 | } |
221 | 223 |
|
| 224 | +/** |
| 225 | + * set the Authorizatio for the http request |
| 226 | + * @param user const char * |
| 227 | + * @param password const char * |
| 228 | + */ |
| 229 | +void HTTPClient::setAuthorization(const char * user, const char * password) { |
| 230 | + if(user && password) { |
| 231 | + String auth = user; |
| 232 | + auth += ":"; |
| 233 | + auth += password; |
| 234 | + _base64Authorization = base64::encode(auth); |
| 235 | + } |
| 236 | +} |
| 237 | + |
222 | 238 | /** |
223 | 239 | * send a GET request |
224 | 240 | * @return http code |
@@ -490,7 +506,7 @@ String HTTPClient::errorToString(int error) { |
490 | 506 | void HTTPClient::addHeader(const String& name, const String& value, bool first) { |
491 | 507 |
|
492 | 508 | // not allow set of Header handled by code |
493 | | - if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host")) { |
| 509 | + if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host") && !(_base64Authorization.length() && name.equalsIgnoreCase("Authorization"))) { |
494 | 510 | String headerLine = name; |
495 | 511 | headerLine += ": "; |
496 | 512 | headerLine += value; |
@@ -622,7 +638,13 @@ bool HTTPClient::sendHeader(const char * type) { |
622 | 638 | } else { |
623 | 639 | header += "close"; |
624 | 640 | } |
625 | | - header += "\r\n" + _Headers + "\r\n"; |
| 641 | + header += "\r\n"; |
| 642 | + |
| 643 | + if(_base64Authorization.length()) { |
| 644 | + header += "Authorization: Basic " + _base64Authorization + "\r\n"; |
| 645 | + } |
| 646 | + |
| 647 | + header += _Headers + "\r\n"; |
626 | 648 |
|
627 | 649 | return (_tcp->write(header.c_str(), header.length()) == header.length()); |
628 | 650 | } |
|
0 commit comments