Skip to content

Commit 85341ff

Browse files
committed
HTTPClient phasing username + password from url
1 parent a9ce1b4 commit 85341ff

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,34 @@ void HTTPClient::begin(String url, String httpsFingerprint) {
102102
String protocol;
103103
// check for : (http: or https:
104104
int index = url.indexOf(':');
105-
int index2;
105+
//int index2;
106106
bool hasPort = false;
107107
if(index) {
108108
protocol = url.substring(0, index);
109109
url.remove(0, (index + 3)); // remove http:// or https://
110110

111-
index = url.indexOf(':');
112-
index2 = url.indexOf('/');
113-
114-
if(index >= 0 && ((index2 >= 0 && index < index2) || index2 == 0)) { // do we have a port?
115-
_host = url.substring(0, index); // hostname
116-
url.remove(0, (index + 1)); // remove hostname + :
111+
index = url.indexOf('/');
112+
String host = url.substring(0, index);
113+
url.remove(0, index); // remove host part
114+
115+
// get Authorization
116+
index = host.indexOf('@');
117+
if(index >= 0) {
118+
// auth info
119+
String auth = host.substring(0, index);
120+
host.remove(0, index +1); // remove auth part including @
121+
_base64Authorization = base64::encode(auth);
122+
}
117123

118-
index = url.indexOf('/');
119-
_port = url.substring(0, index).toInt(); // get port
120-
url.remove(0, index); // remove port
124+
// get port
125+
index = host.indexOf(':');
126+
if(index >= 0) {
127+
_host = host.substring(0, index); // hostname
128+
host.remove(0, (index + 1)); // remove hostname + :
129+
_port = host.toInt(); // get port
121130
hasPort = true;
122131
} else {
123-
index = index2;
124-
_host = url.substring(0, index);
125-
url.remove(0, index); // remove hostname
132+
_host = host;
126133
}
127134

128135
_url = url;
@@ -141,6 +148,7 @@ void HTTPClient::begin(String url, String httpsFingerprint) {
141148
DEBUG_HTTPCLIENT("[HTTP-Client][begin] protocol: %s unknown?!\n", protocol.c_str());
142149
return;
143150
}
151+
144152
}
145153

146154
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());
@@ -235,6 +243,16 @@ void HTTPClient::setAuthorization(const char * user, const char * password) {
235243
}
236244
}
237245

246+
/**
247+
* set the Authorizatio for the http request
248+
* @param auth const char * base64
249+
*/
250+
void HTTPClient::setAuthorization(const char * auth) {
251+
if(auth) {
252+
_base64Authorization = auth;
253+
}
254+
}
255+
238256
/**
239257
* send a GET request
240258
* @return http code

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#ifndef ESP8266HTTPClient_H_
2626
#define ESP8266HTTPClient_H_
2727

28-
#define DEBUG_HTTPCLIENT(...) Serial1.printf( __VA_ARGS__ )
28+
//#define DEBUG_HTTPCLIENT(...) Serial1.printf( __VA_ARGS__ )
2929

3030
#ifndef DEBUG_HTTPCLIENT
3131
#define DEBUG_HTTPCLIENT(...)
@@ -122,6 +122,7 @@ class HTTPClient {
122122
void setReuse(bool reuse); /// keep-alive
123123
void setUserAgent(const char * userAgent);
124124
void setAuthorization(const char * user, const char * password);
125+
void setAuthorization(const char * auth);
125126

126127
/// request handling
127128
int GET();

0 commit comments

Comments
 (0)