Skip to content

Add support for http proxy to esp8266 http client #8094

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 1 commit
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
79 changes: 73 additions & 6 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,19 +858,74 @@ bool HTTPClient::connect(void)

_client->setTimeout(_tcpTimeout);

if(!_client->connect(_host.c_str(), _port)) {
DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u\n", _host.c_str(), _port);
String host = _host;
uint16_t port = _port;
if (_useProxy)
{
host = _proxyHost;
port = _proxyPort;
DEBUG_HTTPCLIENT("[HTTP-Client] using proxy %s:%u\n", host.c_str(), port);
}

if(!_client->connect(host.c_str(), port)) {
DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u\n", host.c_str(), port);
return false;
}

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);

#ifdef ESP8266
_client->setNoDelay(true);
#endif
return connected();
}

bool HTTPClient::setProxyHost(const String &proxyUrl)
{
String url(proxyUrl);

DEBUG_HTTPCLIENT("[HTTP-Client][proxy] url: %s\n", url.c_str());

// check for : (http: or https:
int index = url.indexOf(':');
if (index < 0) {
DEBUG_HTTPCLIENT("[HTTP-Client][proxy] failed to parse protocol\n");
return false;
}

_proxyProtocol = url.substring(0, index);
url.remove(0, (index + 3)); // remove http:// or https://

if (_proxyProtocol == "http") {
// set default port for 'http'
_port = 80;
} else if (_proxyProtocol == "https") {
// set default port for 'https'
_port = 443;
} else {
DEBUG_HTTPCLIENT("[HTTP-Client][proxy] unsupported protocol: %s\n", _proxyProtocol.c_str());
return false;
}

index = url.indexOf('/');
String host = url.substring(0, index);
url.remove(0, index); // remove host part

// get port
index = host.indexOf(':');
if (index >= 0) {
_proxyHost = host.substring(0, index); // hostname
host.remove(0, (index + 1)); // remove hostname + :
_proxyPort = host.toInt(); // get port
} else {
_proxyHost = host;
}

DEBUG_HTTPCLIENT("[HTTP-Client][proxy] host: %s port: %d\n", _proxyHost.c_str(), _proxyPort);
_useProxy = true;
return true;
}

/**
* sends HTTP request header
* @param type (GET, POST, ...)
Expand All @@ -888,11 +943,23 @@ bool HTTPClient::sendHeader(const char * type)
_base64Authorization.length() + _host.length() + _userAgent.length() + 128);
header += type;
header += ' ';
if (_uri.length()) {
header += _uri;

String target;
if (_useProxy) {
// If using proxy, use full uri in request line (http://foo:123/bar)
target = _protocol + F("://") + _host;
if (_port != 80 && _port != 443) {
target += ':';
target += String(_port);
}
target += _uri.length() ? _uri : F("/");
} else if (_uri.length()) {
target = _uri;
} else {
header += '/';
target = F("/");
}

header += target;
header += F(" HTTP/1.");

if(_useHTTP10) {
Expand Down
6 changes: 6 additions & 0 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class HTTPClient
void setAuthorization(const char * user, const char * password);
void setAuthorization(const char * auth);
void setTimeout(uint16_t timeout);
bool setProxyHost(const String &proxyUrl);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sample usage

WiFiClient client;
HTTPClient http;

http.setProxyHost("http://192.168.1.1:8080");
http.begin(client, "http://foo.bar/");
  
// If the proxy requires auth
http.addHeader("Proxy-Authorization", "Basic aaaaaaaaaaaa=");
http.GET();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about putting a line in the documentation, or adding an example ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, can you point me to where those are located?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the HTTPClient examples location.
As you can see, the file hierarchy is myExampleName/myExampleName.ino.

Documentation is ...
... there's absolutely no official documentation on HTTPClient.
So your addition will be added in the documentation when it will be written for the whole class, or if we add doxygen at some point.


// Redirections
void setFollowRedirects(followRedirects_t follow);
Expand Down Expand Up @@ -247,6 +248,11 @@ class HTTPClient
String _userAgent;
String _base64Authorization;

bool _useProxy = false;
String _proxyHost;
String _proxyProtocol;
uint16_t _proxyPort = 0;

/// Response handling
RequestArgument* _currentHeaders = nullptr;
size_t _headerKeysCount = 0;
Expand Down