Skip to content

Commit ebf89cf

Browse files
committed
More PROGMEM
There sure are a lot of long fixed strings.
1 parent 89b85b1 commit ebf89cf

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

src/ESPAsyncWebServer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ class AsyncWebServerRequest {
215215
const String& contentType() const { return _contentType; }
216216
size_t contentLength() const { return _contentLength; }
217217
bool multipart() const { return _isMultipart; }
218-
const char * methodToString() const;
219-
const char * requestedConnTypeToString() const;
218+
const __FlashStringHelper * methodToString() const;
219+
const __FlashStringHelper * requestedConnTypeToString() const;
220220
RequestedConnectionType requestedConnType() const { return _reqconntype; }
221221
bool isExpectedRequestedConnType(RequestedConnectionType erct1, RequestedConnectionType erct2 = RCT_NOT_USED, RequestedConnectionType erct3 = RCT_NOT_USED);
222222
void onDisconnect (ArDisconnectHandler fn);

src/WebAuthentication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ String requestDigestAuthentication(const char * realm){
134134
return header;
135135
}
136136

137-
bool checkDigestAuthentication(const char * header, const char * method, const char * username, const char * password, const char * realm, bool passwordIsHash, const char * nonce, const char * opaque, const char * uri){
137+
bool checkDigestAuthentication(const char * header, const __FlashStringHelper * method, const char * username, const char * password, const char * realm, bool passwordIsHash, const char * nonce, const char * opaque, const char * uri){
138138
if(username == NULL || password == NULL || header == NULL || method == NULL){
139139
//os_printf("AUTH FAIL: missing requred fields\n");
140140
return false;

src/WebAuthentication.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
bool checkBasicAuthentication(const char * header, const char * username, const char * password);
2828
String requestDigestAuthentication(const char * realm);
29-
bool checkDigestAuthentication(const char * header, const char * method, const char * username, const char * password, const char * realm, bool passwordIsHash, const char * nonce, const char * opaque, const char * uri);
29+
bool checkDigestAuthentication(const char * header, const __FlashStringHelper * method, const char * username, const char * password, const char * realm, bool passwordIsHash, const char * nonce, const char * opaque, const char * uri);
3030

3131
//for storing hashed versions on the device that can be authenticated against
3232
String generateDigestHash(const char * username, const char * password, const char * realm);

src/WebRequest.cpp

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len){
154154
_parsedLength += len;
155155
} else {
156156
if(_parsedLength == 0){
157-
if(_contentType.startsWith("application/x-www-form-urlencoded")){
157+
if(_contentType.startsWith(F("application/x-www-form-urlencoded"))){
158158
_isPlainPost = true;
159159
} else if(_contentType == FPSTR(CONTENT_TYPE_PLAIN) && __is_param_char(((char*)buf)[0])){
160160
size_t i = 0;
@@ -272,19 +272,19 @@ bool AsyncWebServerRequest::_parseReqHead(){
272272
String u = _temp.substring(m.length()+1, index);
273273
_temp = _temp.substring(index+1);
274274

275-
if(m == "GET"){
275+
if(m == F("GET")){
276276
_method = HTTP_GET;
277-
} else if(m == "POST"){
277+
} else if(m == F("POST")){
278278
_method = HTTP_POST;
279-
} else if(m == "DELETE"){
279+
} else if(m == F("DELETE")){
280280
_method = HTTP_DELETE;
281-
} else if(m == "PUT"){
281+
} else if(m == F("PUT")){
282282
_method = HTTP_PUT;
283-
} else if(m == "PATCH"){
283+
} else if(m == F("PATCH")){
284284
_method = HTTP_PATCH;
285-
} else if(m == "HEAD"){
285+
} else if(m == F("HEAD")){
286286
_method = HTTP_HEAD;
287-
} else if(m == "OPTIONS"){
287+
} else if(m == F("OPTIONS")){
288288
_method = HTTP_OPTIONS;
289289
}
290290

@@ -297,7 +297,7 @@ bool AsyncWebServerRequest::_parseReqHead(){
297297
_url = urlDecode(u);
298298
_addGetParams(g);
299299

300-
if(!_temp.startsWith("HTTP/1.0"))
300+
if(!_temp.startsWith(F("HTTP/1.0")))
301301
_version = 1;
302302

303303
_temp = String();
@@ -444,27 +444,27 @@ void AsyncWebServerRequest::_parseMultipartPostByte(uint8_t data, bool last){
444444
_temp += (char)data;
445445
if((char)data == '\n'){
446446
if(_temp.length()){
447-
if(_temp.length() > 12 && _temp.substring(0, 12).equalsIgnoreCase("Content-Type")){
447+
if(_temp.length() > 12 && _temp.substring(0, 12).equalsIgnoreCase(F("Content-Type"))){
448448
_itemType = _temp.substring(14);
449449
_itemIsFile = true;
450-
} else if(_temp.length() > 19 && _temp.substring(0, 19).equalsIgnoreCase("Content-Disposition")){
450+
} else if(_temp.length() > 19 && _temp.substring(0, 19).equalsIgnoreCase(F("Content-Disposition"))){
451451
_temp = _temp.substring(_temp.indexOf(';') + 2);
452452
while(_temp.indexOf(';') > 0){
453453
String name = _temp.substring(0, _temp.indexOf('='));
454454
String nameVal = _temp.substring(_temp.indexOf('=') + 2, _temp.indexOf(';') - 1);
455-
if(name == "name"){
455+
if(name == F("name")){
456456
_itemName = nameVal;
457-
} else if(name == "filename"){
457+
} else if(name == F("filename")){
458458
_itemFilename = nameVal;
459459
_itemIsFile = true;
460460
}
461461
_temp = _temp.substring(_temp.indexOf(';') + 2);
462462
}
463463
String name = _temp.substring(0, _temp.indexOf('='));
464464
String nameVal = _temp.substring(_temp.indexOf('=') + 2, _temp.length() - 1);
465-
if(name == "name"){
465+
if(name == F("name")){
466466
_itemName = nameVal;
467-
} else if(name == "filename"){
467+
} else if(name == F("filename")){
468468
_itemFilename = nameVal;
469469
_itemIsFile = true;
470470
}
@@ -584,8 +584,10 @@ void AsyncWebServerRequest::_parseLine(){
584584
_server->_attachHandler(this);
585585
_removeNotInterestingHeaders();
586586
if(_expectingContinue){
587-
const char * response = "HTTP/1.1 100 Continue\r\n\r\n";
588-
_client->write(response, os_strlen(response));
587+
const static char response[] PROGMEM = "HTTP/1.1 100 Continue\r\n\r\n";
588+
char response_stack[sizeof(response)]; // stack, so we can pull it out of flash memory
589+
memcpy_P(response_stack, response, sizeof(response));
590+
_client->write(response_stack, os_strlen(response_stack));
589591
}
590592
//check handler for authentication
591593
if(_contentLength){
@@ -858,17 +860,18 @@ bool AsyncWebServerRequest::authenticate(const char * hash){
858860

859861
void AsyncWebServerRequest::requestAuthentication(const char * realm, bool isDigest){
860862
AsyncWebServerResponse * r = beginResponse(401);
863+
const static char hdr[] PROGMEM = "WWW-Authenticate";
861864
if(!isDigest && realm == NULL){
862-
r->addHeader("WWW-Authenticate", "Basic realm=\"Login Required\"");
865+
r->addHeader(FPSTR(hdr), F("Basic realm=\"Login Required\""));
863866
} else if(!isDigest){
864-
String header = "Basic realm=\"";
867+
String header = F("Basic realm=\"");
865868
header.concat(realm);
866869
header.concat("\"");
867-
r->addHeader("WWW-Authenticate", header);
870+
r->addHeader(FPSTR(hdr), header);
868871
} else {
869-
String header = "Digest ";
872+
String header = F("Digest ");
870873
header.concat(requestDigestAuthentication(realm));
871-
r->addHeader("WWW-Authenticate", header);
874+
r->addHeader(FPSTR(hdr), header);
872875
}
873876
send(r);
874877
}
@@ -988,26 +991,26 @@ String AsyncWebServerRequest::urlDecode(const String& text) const {
988991
}
989992

990993

991-
const char * AsyncWebServerRequest::methodToString() const {
992-
if(_method == HTTP_ANY) return "ANY";
993-
else if(_method & HTTP_GET) return "GET";
994-
else if(_method & HTTP_POST) return "POST";
995-
else if(_method & HTTP_DELETE) return "DELETE";
996-
else if(_method & HTTP_PUT) return "PUT";
997-
else if(_method & HTTP_PATCH) return "PATCH";
998-
else if(_method & HTTP_HEAD) return "HEAD";
999-
else if(_method & HTTP_OPTIONS) return "OPTIONS";
1000-
return "UNKNOWN";
994+
const __FlashStringHelper* AsyncWebServerRequest::methodToString() const {
995+
if(_method == HTTP_ANY) return F("ANY");
996+
else if(_method & HTTP_GET) return F("GET");
997+
else if(_method & HTTP_POST) return F("POST");
998+
else if(_method & HTTP_DELETE) return F("DELETE");
999+
else if(_method & HTTP_PUT) return F("PUT");
1000+
else if(_method & HTTP_PATCH) return F("PATCH");
1001+
else if(_method & HTTP_HEAD) return F("HEAD");
1002+
else if(_method & HTTP_OPTIONS) return F("OPTIONS");
1003+
return F("UNKNOWN");
10011004
}
10021005

1003-
const char *AsyncWebServerRequest::requestedConnTypeToString() const {
1006+
const __FlashStringHelper *AsyncWebServerRequest::requestedConnTypeToString() const {
10041007
switch (_reqconntype) {
1005-
case RCT_NOT_USED: return "RCT_NOT_USED";
1006-
case RCT_DEFAULT: return "RCT_DEFAULT";
1007-
case RCT_HTTP: return "RCT_HTTP";
1008-
case RCT_WS: return "RCT_WS";
1009-
case RCT_EVENT: return "RCT_EVENT";
1010-
default: return "ERROR";
1008+
case RCT_NOT_USED: return F("RCT_NOT_USED");
1009+
case RCT_DEFAULT: return F("RCT_DEFAULT");
1010+
case RCT_HTTP: return F("RCT_HTTP");
1011+
case RCT_WS: return F("RCT_WS");
1012+
case RCT_EVENT: return F("RCT_EVENT");
1013+
default: return F("ERROR");
10111014
}
10121015
}
10131016

0 commit comments

Comments
 (0)