Skip to content

HTTP Client method for specifying whole authorization data #4404

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
// auth info
String auth = host.substring(0, index);
host.remove(0, index + 1); // remove auth part including @
_base64Authorization = base64::encode(auth);
_rawAuthorization = "Basic ";
_rawAuthorization += base64::encode(auth);
}

// get port
Expand Down Expand Up @@ -482,7 +483,7 @@ void HTTPClient::setUserAgent(const String& userAgent)
}

/**
* set the Authorizatio for the http request
* set the Authorization for the http request
* @param user const char *
* @param password const char *
*/
Expand All @@ -492,18 +493,31 @@ void HTTPClient::setAuthorization(const char * user, const char * password)
String auth = user;
auth += ":";
auth += password;
_base64Authorization = base64::encode(auth);
_rawAuthorization = "Basic ";
_rawAuthorization += base64::encode(auth);
}
}

/**
* set the Authorizatio for the http request
* set the Authorization for the http request
* @param auth const char * base64
*/
void HTTPClient::setAuthorization(const char * auth)
{
if(auth) {
_base64Authorization = auth;
_rawAuthorization = "Basic ";
_rawAuthorization += auth;
}
}

/**
* set the full raw Authorization header value for the http request
* @param rawAuth const char *
*/
void HTTPClient::setRawAuthorization(const char * rawAuth)
{
if(rawAuth) {
_rawAuthorization = rawAuth;
}
}

Expand Down Expand Up @@ -1057,7 +1071,7 @@ void HTTPClient::addHeader(const String& name, const String& value, bool first,
if(!name.equalsIgnoreCase(F("Connection")) &&
!name.equalsIgnoreCase(F("User-Agent")) &&
!name.equalsIgnoreCase(F("Host")) &&
!(name.equalsIgnoreCase(F("Authorization")) && _base64Authorization.length())){
!(name.equalsIgnoreCase(F("Authorization")) && _rawAuthorization.length())){

String headerLine = name;
headerLine += ": ";
Expand Down Expand Up @@ -1223,10 +1237,10 @@ bool HTTPClient::sendHeader(const char * type)
header += F("Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0\r\n");
}

if(_base64Authorization.length()) {
_base64Authorization.replace("\n", "");
header += F("Authorization: Basic ");
header += _base64Authorization;
if (_rawAuthorization.length()) {
_rawAuthorization.replace("\n", "");
header += F("Authorization: ");
header += _rawAuthorization;
header += "\r\n";
}

Expand Down
3 changes: 2 additions & 1 deletion libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class HTTPClient
void setUserAgent(const String& userAgent);
void setAuthorization(const char * user, const char * password);
void setAuthorization(const char * auth);
void setRawAuthorization(const char * rawAuth);
void setTimeout(uint16_t timeout);
void setFollowRedirects(bool follow);
void setRedirectLimit(uint16_t limit); // max redirects to follow for a single request
Expand Down Expand Up @@ -243,7 +244,7 @@ class HTTPClient
String _protocol;
String _headers;
String _userAgent = "ESP8266HTTPClient";
String _base64Authorization;
String _rawAuthorization;

/// Response handling
RequestArgument* _currentHeaders = nullptr;
Expand Down