Skip to content

Further const correctness / String by reference passing cleanups #6571

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

Merged
merged 4 commits into from
Oct 31, 2019
Merged
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
2 changes: 1 addition & 1 deletion libraries/ArduinoOTA/ArduinoOTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ void ArduinoOTAClass::_onRx(){
return;
}

String challenge = _password + ":" + String(_nonce) + ":" + cnonce;
String challenge = _password + ':' + String(_nonce) + ':' + cnonce;
MD5Builder _challengemd5;
_challengemd5.begin();
_challengemd5.add(challenge);
Expand Down
55 changes: 29 additions & 26 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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] == '/') {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
26 changes: 13 additions & 13 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ class HTTPClient
* 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
Expand All @@ -175,20 +175,20 @@ class HTTPClient
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);

Expand Down Expand Up @@ -216,7 +216,7 @@ class HTTPClient
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);
Expand Down
14 changes: 7 additions & 7 deletions libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ void ESP8266WebServerTemplate<ServerType>::requestAuthentication(HTTPAuthMethod
_srealm = String(realm);
}
if(mode == BASIC_AUTH) {
sendHeader(String(FPSTR(WWW_Authenticate)), String(F("Basic realm=\"")) + _srealm + String(F("\"")));
sendHeader(String(FPSTR(WWW_Authenticate)), String(F("Basic realm=\"")) + _srealm + String('\"'));
} else {
_snonce=_getRandomHexString();
_sopaque=_getRandomHexString();
sendHeader(String(FPSTR(WWW_Authenticate)), String(F("Digest realm=\"")) +_srealm + String(F("\", qop=\"auth\", nonce=\"")) + _snonce + String(F("\", opaque=\"")) + _sopaque + String(F("\"")));
sendHeader(String(FPSTR(WWW_Authenticate)), String(F("Digest realm=\"")) +_srealm + String(F("\", qop=\"auth\", nonce=\"")) + _snonce + String(F("\", opaque=\"")) + _sopaque + String('\"'));
}
using namespace mime;
send(401, String(FPSTR(mimeTable[html].mimeType)), authFailMsg);
Expand Down Expand Up @@ -524,13 +524,13 @@ String ESP8266WebServerTemplate<ServerType>::credentialHash(const String& userna
{
MD5Builder md5;
md5.begin();
md5.add(username + ":" + realm + ":" + password); // md5 of the user:realm:password
md5.add(username + ':' + realm + ':' + password); // md5 of the user:realm:password
md5.calculate();
return md5.toString();
}

template <typename ServerType>
void ESP8266WebServerTemplate<ServerType>::_streamFileCore(const size_t fileSize, const String & fileName, const String & contentType)
void ESP8266WebServerTemplate<ServerType>::_streamFileCore(const size_t fileSize, const String &fileName, const String &contentType)
{
using namespace mime;
setContentLength(fileSize);
Expand All @@ -544,7 +544,7 @@ void ESP8266WebServerTemplate<ServerType>::_streamFileCore(const size_t fileSize


template <typename ServerType>
const String& ESP8266WebServerTemplate<ServerType>::arg(String name) const {
const String& ESP8266WebServerTemplate<ServerType>::arg(const String& name) const {
for (int j = 0; j < _postArgsLen; ++j) {
if ( _postArgs[j].key == name )
return _postArgs[j].value;
Expand Down Expand Up @@ -590,7 +590,7 @@ bool ESP8266WebServerTemplate<ServerType>::hasArg(const String& name) const {


template <typename ServerType>
const String& ESP8266WebServerTemplate<ServerType>::header(String name) const {
const String& ESP8266WebServerTemplate<ServerType>::header(const String& name) const {
for (int i = 0; i < _headerKeysCount; ++i) {
if (_currentHeaders[i].key.equalsIgnoreCase(name))
return _currentHeaders[i].value;
Expand Down Expand Up @@ -630,7 +630,7 @@ int ESP8266WebServerTemplate<ServerType>::headers() const {
}

template <typename ServerType>
bool ESP8266WebServerTemplate<ServerType>::hasHeader(String name) const {
bool ESP8266WebServerTemplate<ServerType>::hasHeader(const String& name) const {
for (int i = 0; i < _headerKeysCount; ++i) {
if ((_currentHeaders[i].key.equalsIgnoreCase(name)) && (_currentHeaders[i].value.length() > 0))
return true;
Expand Down
6 changes: 3 additions & 3 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ class ESP8266WebServerTemplate
// Allows setting server options (i.e. SSL keys) by the instantiator
ServerType &getServer() { return _server; }

const String& arg(String name) const; // get request argument value by name
const String& arg(const String& name) const; // get request argument value by name
const String& arg(int i) const; // get request argument value by number
const String& argName(int i) const; // get request argument name by number
int args() const; // get arguments count
bool hasArg(const String& name) const; // check if argument exists
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect
const String& header(String name) const; // get request header value by name
const String& header(const String& name) const; // get request header value by name
const String& header(int i) const; // get request header value by number
const String& headerName(int i) const; // get request header name by number
int headers() const; // get header count
bool hasHeader(String name) const; // check if header exists
bool hasHeader(const String& name) const; // check if header exists
const String& hostHeader() const; // get request host header if available or empty String if not

// send response to the client
Expand Down
8 changes: 4 additions & 4 deletions libraries/ESP8266WiFiMesh/src/ESP8266WiFiMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ transmission_status_t ESP8266WiFiMesh::exchangeInfo(WiFiClient &currClient)
{
verboseModePrint("Transmitting"); // Not storing strings in flash (via F()) to avoid performance impacts when using the string.

currClient.print(getMessage() + "\r");
currClient.print(getMessage() + '\r');
yield();

if (!waitForClientTransmission(currClient, _stationModeTimeoutMs))
Expand Down Expand Up @@ -582,11 +582,11 @@ void ESP8266WiFiMesh::attemptTransmission(const String &message, bool concluding

if(_verboseMode) // Avoid string generation if not required
{
verboseModePrint(String(F("AP acquired: ")) + currentSSID + String(F(", Ch:")) + String(currentWiFiChannel) + " ", false);
verboseModePrint(String(F("AP acquired: ")) + currentSSID + String(F(", Ch:")) + String(currentWiFiChannel) + ' ', false);

if(currentNetwork.networkIndex != NETWORK_INFO_DEFAULT_INT)
{
verboseModePrint("(" + String(WiFi.RSSI(currentNetwork.networkIndex)) + String(F("dBm) ")) +
verboseModePrint(String('(') + String(WiFi.RSSI(currentNetwork.networkIndex)) + String(F("dBm) ")) +
(WiFi.encryptionType(currentNetwork.networkIndex) == ENC_TYPE_NONE ? String(F("open")) : ""), false);
}

Expand Down Expand Up @@ -662,7 +662,7 @@ void ESP8266WiFiMesh::acceptRequest()
if (_client.connected())
{
verboseModePrint("Responding"); // Not storing strings in flash (via F()) to avoid performance impacts when using the string.
_client.print(response + "\r");
_client.print(response + '\r');
_client.flush();
yield();
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String&
* @param md5 String
* @return true if Update ok
*/
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command)
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, const String& md5, int command)
{

StreamString error;
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class ESP8266HTTPUpdate

protected:
t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false);
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
bool runUpdate(Stream& in, uint32_t size, const String& md5, int command = U_FLASH);

int _lastError;
bool _rebootOnUpdate = true;
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266mDNS/src/ESP8266mDNS_Legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *valu
return false; //max txt record size
}
MDNSTxt *newtxt = new MDNSTxt;
newtxt->_txt = String(key) + "=" + String(value);
newtxt->_txt = String(key) + '=' + String(value);
newtxt->_next = 0;
if (servicePtr->_txts == 0) //no services have been added
{
Expand Down
Loading