1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-13 13:01:55 +03:00

Further const correctness / String by reference passing cleanups (#6571)

There are actually several instances where we pass in read-only
parameters as pass-by-value, where in the case of String() that
is inefficient as it involves copy-constructor/temp string creations.

We can avoid that, similarly to single character string concatenations
done via string literals instead of char literals.
This commit is contained in:
Dirk Mueller
2019-10-31 16:02:40 +01:00
committed by david gauchard
parent ba971fe7e9
commit 8bc5a10d6d
11 changed files with 78 additions and 75 deletions

View File

@ -150,7 +150,7 @@ void HTTPClient::clear()
* @param https bool
* @return success bool
*/
bool HTTPClient::begin(WiFiClient &client, String url) {
bool HTTPClient::begin(WiFiClient &client, const String& url) {
#if HTTPCLIENT_1_1_COMPATIBLE
if(_tcpDeprecated) {
DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n");
@ -188,7 +188,7 @@ bool HTTPClient::begin(WiFiClient &client, String url) {
* @param https bool
* @return success bool
*/
bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String uri, bool https)
bool HTTPClient::begin(WiFiClient &client, const String& host, uint16_t port, const String& uri, bool https)
{
#if HTTPCLIENT_1_1_COMPATIBLE
if(_tcpDeprecated) {
@ -281,8 +281,10 @@ bool HTTPClient::begin(String url)
}
#endif // HTTPCLIENT_1_1_COMPATIBLE
bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
bool HTTPClient::beginInternal(const String& __url, const char* expectedProtocol)
{
String url(__url);
DEBUG_HTTPCLIENT("[HTTP-Client][begin] url: %s\n", url.c_str());
clear();
@ -500,7 +502,7 @@ void HTTPClient::setAuthorization(const char * user, const char * password)
{
if(user && password) {
String auth = user;
auth += ":";
auth += ':';
auth += password;
_base64Authorization = base64::encode(auth);
}
@ -533,7 +535,7 @@ void HTTPClient::setTimeout(uint16_t timeout)
* set the URL to a new value. Handy for following redirects.
* @param url
*/
bool HTTPClient::setURL(String url)
bool HTTPClient::setURL(const String& url)
{
// if the new location is only a path then only update the URI
if (url && url[0] == '/') {
@ -542,7 +544,7 @@ bool HTTPClient::setURL(String url)
return true;
}
if (!url.startsWith(_protocol + ":")) {
if (!url.startsWith(_protocol + ':')) {
DEBUG_HTTPCLIENT("[HTTP-Client][setURL] new URL not the same protocol, expected '%s', URL: '%s'\n", _protocol.c_str(), url.c_str());
return false;
}
@ -587,16 +589,16 @@ int HTTPClient::GET()
/**
* sends a post request to the server
* @param payload uint8_t *
* @param payload const uint8_t *
* @param size size_t
* @return http code
*/
int HTTPClient::POST(uint8_t * payload, size_t size)
int HTTPClient::POST(const uint8_t* payload, size_t size)
{
return sendRequest("POST", payload, size);
}
int HTTPClient::POST(String payload)
int HTTPClient::POST(const String& payload)
{
return POST((uint8_t *) payload.c_str(), payload.length());
}
@ -607,26 +609,26 @@ int HTTPClient::POST(String payload)
* @param size size_t
* @return http code
*/
int HTTPClient::PUT(uint8_t * payload, size_t size) {
int HTTPClient::PUT(const uint8_t* payload, size_t size) {
return sendRequest("PUT", payload, size);
}
int HTTPClient::PUT(String payload) {
return PUT((uint8_t *) payload.c_str(), payload.length());
int HTTPClient::PUT(const String& payload) {
return PUT((const uint8_t *) payload.c_str(), payload.length());
}
/**
* sends a patch request to the server
* @param payload uint8_t *
* @param payload const uint8_t *
* @param size size_t
* @return http code
*/
int HTTPClient::PATCH(uint8_t * payload, size_t size) {
int HTTPClient::PATCH(const uint8_t * payload, size_t size) {
return sendRequest("PATCH", payload, size);
}
int HTTPClient::PATCH(String payload) {
return PATCH((uint8_t *) payload.c_str(), payload.length());
int HTTPClient::PATCH(const String& payload) {
return PATCH((const uint8_t *) payload.c_str(), payload.length());
}
/**
@ -635,19 +637,19 @@ int HTTPClient::PATCH(String payload) {
* @param payload String data for the message body
* @return
*/
int HTTPClient::sendRequest(const char * type, String payload)
int HTTPClient::sendRequest(const char * type, const String& payload)
{
return sendRequest(type, (uint8_t *) payload.c_str(), payload.length());
return sendRequest(type, (const uint8_t *) payload.c_str(), payload.length());
}
/**
* sendRequest
* @param type const char * "GET", "POST", ....
* @param payload uint8_t * data for the message body if null not send
* @param size size_t size for the message body if 0 not send
* @param type const char * "GET", "POST", ....
* @param payload const uint8_t * data for the message body if null not send
* @param size size_t size for the message body if 0 not send
* @return -1 if no info or > 0 when Content-Length is set by server
*/
int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size)
int HTTPClient::sendRequest(const char * type, const uint8_t * payload, size_t size)
{
bool redirect = false;
int code = 0;
@ -1212,12 +1214,12 @@ bool HTTPClient::sendHeader(const char * type)
return false;
}
String header = String(type) + " " + (_uri.length() ? _uri : F("/")) + F(" HTTP/1.");
String header = String(type) + ' ' + (_uri.length() ? _uri : F("/")) + F(" HTTP/1.");
if(_useHTTP10) {
header += "0";
header += '0';
} else {
header += "1";
header += '1';
}
header += String(F("\r\nHost: ")) + _host;
@ -1316,7 +1318,8 @@ int HTTPClient::handleHeaderResponse()
if(_currentHeaders[i].key.equalsIgnoreCase(headerName)) {
if (_currentHeaders[i].value != "") {
// Existing value, append this one with a comma
_currentHeaders[i].value += "," + headerValue;
_currentHeaders[i].value += ',';
_currentHeaders[i].value += headerValue;
} else {
_currentHeaders[i].value = headerValue;
}

View File

@ -147,8 +147,8 @@ public:
* Since both begin() functions take a reference to client as a parameter, you need to
* ensure the client object lives the entire time of the HTTPClient
*/
bool begin(WiFiClient &client, String url);
bool begin(WiFiClient &client, String host, uint16_t port, String uri = "/", bool https = false);
bool begin(WiFiClient &client, const String& url);
bool begin(WiFiClient &client, const String& host, uint16_t port, const String& uri = "/", bool https = false);
#if HTTPCLIENT_1_1_COMPATIBLE
// Plain HTTP connection, unencrypted
@ -175,20 +175,20 @@ public:
void setTimeout(uint16_t timeout);
void setFollowRedirects(bool follow);
void setRedirectLimit(uint16_t limit); // max redirects to follow for a single request
bool setURL(String url); // handy for handling redirects
bool setURL(const String& url); // handy for handling redirects
void useHTTP10(bool usehttp10 = true);
/// request handling
int GET();
int POST(uint8_t * payload, size_t size);
int POST(String payload);
int PUT(uint8_t * payload, size_t size);
int PUT(String payload);
int PATCH(uint8_t * payload, size_t size);
int PATCH(String payload);
int sendRequest(const char * type, String payload);
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
int sendRequest(const char * type, Stream * stream, size_t size = 0);
int POST(const uint8_t* payload, size_t size);
int POST(const String& payload);
int PUT(const uint8_t* payload, size_t size);
int PUT(const String& payload);
int PATCH(const uint8_t* payload, size_t size);
int PATCH(const String& payload);
int sendRequest(const char* type, const String& payload);
int sendRequest(const char* type, const uint8_t* payload = NULL, size_t size = 0);
int sendRequest(const char* type, Stream * stream, size_t size = 0);
void addHeader(const String& name, const String& value, bool first = false, bool replace = true);
@ -216,7 +216,7 @@ protected:
String value;
};
bool beginInternal(String url, const char* expectedProtocol);
bool beginInternal(const String& url, const char* expectedProtocol);
void disconnect(bool preserveClient = false);
void clear();
int returnError(int error);