8000 Const reference to String as method parameters to prevent additional … · me-no-dev/ESPAsyncWebServer@cc160cd · GitHub
[go: up one dir, main page]

Skip to content 10000
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit cc160cd

Browse files
author
Maksym Lunin
committed
Const reference to String as method parameters to prevent additional copy and memory allocation when String passed by value
1 parent 085b1a9 commit cc160cd

File tree

5 files changed

+61
-62
lines changed

5 files changed

+61
-62
lines changed

src/ESPAsyncWebServer.h

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class AsyncWebServerRequest {
170170
void _parseLine();
171171
void _parsePlainPostChar(uint8_t data);
172172
void _parseMultipartPostByte(uint8_t data, bool last);
173-
void _addGetParams(String params);
173+
void _addGetParams(const String& params);
174174

175175
void _handleUploadStart();
176176
void _handleUploadByte(uint8_t data, bool last);
@@ -202,50 +202,50 @@ class AsyncWebServerRequest {
202202
void requestAuthentication(const char * realm = NULL, bool isDigest = true);
203203

204204
void setHandler(AsyncWebHandler *handler){ _handler = handler; }
205-
void addInterestingHeader(String name);
205+
void addInterestingHeader(const String& name);
206206

207-
void redirect(String url);
207+
void redirect(const String& url);
208208

209209
void send(AsyncWebServerResponse *response);
210-
void send(int code, String contentType=String(), String content=String());
211-
void send(FS &fs, String path, String contentType=String(), bool download=false);
212-
void send(File content, String path, String contentType=String(), bool download=false);
213-
void send(Stream &stream, String contentType, size_t len);
214-
void send(String contentType, size_t len, AwsResponseFiller callback);
215-
void sendChunked(String contentType, AwsResponseFiller callback);
216-
void send_P(int code, String contentType, const uint8_t * content, size_t len);
217-
void send_P(int code, String contentType, PGM_P content);
218-
219-
AsyncWebServerResponse *beginResponse(int code, String contentType=String(), String content=String());
220-
AsyncWebServerResponse *beginResponse(FS &fs, String path, String contentType=String(), bool download=false);
221-
AsyncWebServerResponse *beginResponse(File content, String path, String contentType=String(), bool download=false);
222-
AsyncWebServerResponse *beginResponse(Stream &stream, String contentType, size_t len);
223-
AsyncWebServerResponse *beginResponse(String contentType, size_t len, AwsResponseFiller callback);
224-
AsyncWebServerResponse *beginChunkedResponse(String contentType, AwsResponseFiller callback);
225-
AsyncResponseStream *beginResponseStream(String contentType, size_t bufferSize=1460);
226-
AsyncWebServerResponse *beginResponse_P(int code, String contentType, const uint8_t * content, size_t len);
227-
AsyncWebServerResponse *beginResponse_P(int code, String contentType, PGM_P content);
210+
void send(int code, const String& contentType=String(), const String& content=String());
211+
void send(FS &fs, const String& path, const String& contentType=String(), bool download=false);
212+
void send(File content, const String& path, const String& contentType=String(), bool download=false);
213+
void send(Stream &stream, const String& contentType, size_t len);
214+
void send(const String& contentType, size_t len, AwsResponseFiller callback);
215+
void sendChunked(const String& contentType, AwsResponseFiller callback);
216+
void send_P(int code, const String& contentType, const uint8_t * content, size_t len);
217+
void send_P(int code, const String& contentType, PGM_P content);
218+
219+
AsyncWebServerResponse *beginResponse(int code, const String& contentType=String(), const String& content=String());
220+
AsyncWebServerResponse *beginResponse(FS &fs, const String& path, const String& contentType=String(), bool download=false);
221+
AsyncWebServerResponse *beginResponse(File content, const String& path, const String& contentType=String(), bool download=false);
222+
AsyncWebServerResponse *beginResponse(Stream &stream, const String& contentType, size_t len);
223+
AsyncWebServerResponse *beginResponse(const String& contentType, size_t len, AwsResponseFiller callback);
224+
AsyncWebServerResponse *beginChunkedResponse(const String& contentType, AwsResponseFiller callback);
225+
AsyncResponseStream *beginResponseStream(const String& contentType, size_t bufferSize=1460);
226+
AsyncWebServerResponse *beginResponse_P(int code, const String& contentType, const uint8_t * content, size_t len);
227+
AsyncWebServerResponse *beginResponse_P(int code, const String& contentType, PGM_P content);
228228

229229
size_t headers() const; // get header count
230-
bool hasHeader(String name) const; // check if header exists
231-
AsyncWebHeader* getHeader(String name) const;
230+
bool hasHeader(const String& name) const; // check if header exists
231+
AsyncWebHeader* getHeader(const String& name) const;
232232
AsyncWebHeader* getHeader(size_t num) const;
233233

234234
size_t params() const; // get arguments count
235-
bool hasParam(String name, bool post=false, bool file=false) const;
236-
AsyncWebParameter* getParam(String name, bool post=false, bool file=false) const;
235+
bool hasParam(const String& name, bool post=false, bool file=false) const;
236+
AsyncWebParameter* getParam(const String& name, bool post=false, bool file=false) const;
237237
AsyncWebParameter* getParam(size_t num) const;
238238

239239
size_t args() const { return params(); } // get arguments count
240-
String arg(String name) const;// get request argument value by name
240+
String arg(const String& name) const;// get request argument value by name
241241
String arg(size_t i) const; // get request argument value by number
242242
String argName(size_t i) const; // get request argument name by number
243243
bool hasArg(const char* name) const; // check if argument exists
244244

245245
String header(const char* name) const; // get request header value by name
246246
String header(size_t i) const; // get request header value by number
247247
String headerName(size_t i) const; // get request header name by number
248-
String urlDecode(String text) const;
248+
String urlDecode(const String& text) const;
249249
};
250250

251251
/*
@@ -335,8 +335,8 @@ class AsyncWebServerResponse {
335335
virtual ~AsyncWebServerResponse();
336336
virtual void setCode(int code);
337337
virtual void setContentLength(size_t len);
338-
virtual void setContentType(String type);
339-
virtual void addHeader(String name, String value);
338+
virtual void setContentType(const String& type);
339+
virtual void addHeader(const String& name, const String& value);
340340
virtual String _assembleHead(uint8_t version);
341341
virtual bool _started() const;
342342
virtual bool _finished() const;
@@ -351,7 +351,7 @@ class AsyncWebServerResponse {
351351
* */
352352

353353
typedef std::function<void(AsyncWebServerRequest *request)> ArRequestHandlerFunction;
354-
typedef std::function<void(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)> ArUploadHandlerFunction;
354+
typedef std::function<void(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final)> ArUploadHandlerFunction;
355355
typedef std::function<void(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)> ArBodyHandlerFunction;
356356

357357
class AsyncWebServer {

src/WebHandlerImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class AsyncCallbackWebHandler: public AsyncWebHandler {
6565
ArBodyHandlerFunction _onBody;
6666
public:
6767
AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL){}
68-
void setUri(String uri){ _uri = uri; }
68+
void setUri(const String& uri){ _uri = uri; }
6969
void setMethod(WebRequestMethodComposite method){ _method = method; }
7070
void onRequest(ArRequestHandlerFunction fn){ _onRequest = fn; }
7171
void onUpload(ArUploadHandlerFunction fn){ _onUpload = fn; }

src/WebRequest.cpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void AsyncWebServerRequest::_addParam(AsyncWebParameter *p){
191191
_params.add(p);
192192
}
193193

194-
void AsyncWebServerRequest::_addGetParams(String params){
194+
void AsyncWebServerRequest::_addGetParams(const String& params){
195195
size_t start = 0;
196196
while (start < params.length()){
197197
int end = params.indexOf('&', start);
@@ -519,7 +519,7 @@ size_t AsyncWebServerRequest::headers() const{
519519
_headers.count();
520520
}
521521

522-
bool AsyncWebServerRequest::hasHeader(String name) const {
522+
bool AsyncWebServerRequest::hasHeader(const String& name) const {
523523
for(const auto& h: _headers){
524524
if(h->name().equalsIgnoreCase(name)){
525525
return true;
@@ -528,7 +528,7 @@ bool AsyncWebServerRequest::hasHeader(String name) const {
528528
return false;
529529
}
530530

531-
AsyncWebHeader* AsyncWebServerRequest::getHeader(String name) const {
531+
AsyncWebHeader* AsyncWebServerRequest::getHeader(const String& name) const {
532532
for(const auto& h: _headers){
533533
if(h->name().equalsIgnoreCase(name)){
534534
return h;
@@ -545,7 +545,7 @@ size_t AsyncWebServerRequest::params() const {
545545
return _params.count();
546546
}
547547

548-
bool AsyncWebServerRequest::hasParam(String name, bool post, bool file) const {
548+
bool AsyncWebServerRequest::hasParam(const String& name, bool post, bool file) const {
549549
for(const auto& p: _params){
550550
if(p->name() == name && p->isPost() == post && p->isFile() == file){
551551
return true;
@@ -554,7 +554,7 @@ bool AsyncWebServerRequest::hasParam(String name, bool post, bool file) const {
554554
return false;
555555
}
556556

557-
AsyncWebParameter* AsyncWebServerRequest::getParam(String name, bool post, bool file) const {
557+
AsyncWebParameter* AsyncWebServerRequest::getParam(const String& name, bool post, bool file) const {
558558
for(const auto& p: _params){
559559
if(p->name() == name && p->isPost() == post && p->isFile() == file){
560560
return p;
@@ -567,7 +567,7 @@ AsyncWebParameter* AsyncWebServerRequest::getParam(size_t num) const {
567567
return _params.nth(num);
568568
}
569569

570-
void AsyncWebServerRequest::addInterestingHeader(String name){
570+
void AsyncWebServerRequest::addInterestingHeader(const String& name){
571571
if(!_interestingHeaders.containsIgnoreCase(name))
572572
_interestingHeaders.add(name);
573573
}
@@ -588,85 +588,85 @@ void AsyncWebServerRequest::send(AsyncWebServerResponse *response){
588588
_response->_respond(this);
589589
}
590590

591-
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(int code, String contentType, String content){
591+
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(int code, const String& contentType, const String& content){
592592
return new AsyncBasicResponse(code, contentType, content);
593593
}
594594

595-
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(FS &fs, String path, String contentType, bool download){
595+
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(FS &fs, const String& path, const String& contentType, bool download){
596596
if(fs.exists(path) || (!download && fs.exists(path+".gz")))
597597
return new AsyncFileResponse(fs, path, contentType, download);
598598
return NULL;
599599
}
600600

601-
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(File content, String path, String contentType, bool download){
601+
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(File content, const String& path, const String& contentType, bool download){
602602
if(content == true)
603603
return new AsyncFileResponse(content, path, contentType, download);
604604
return NULL;
605605
}
606606

607-
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(Stream &stream, String contentType, size_t len){
607+
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(Stream &stream, const String& contentType, size_t len){
608608
return new AsyncStreamResponse(stream, contentType, len);
609609
}
610610

611-
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(String contentType, size_t len, AwsResponseFiller callback){
611+
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(const String& contentType, size_t len, AwsResponseFiller callback){
612612
return new AsyncCallbackResponse(contentType, len, callback);
613613
}
614614

615-
AsyncWebServerResponse * AsyncWebServerRequest::beginChunkedResponse(String contentType, AwsResponseFiller callback){
615+
AsyncWebServerResponse * AsyncWebServerRequest::beginChunkedResponse(const String& contentType, AwsResponseFiller callback){
616616
if(_version)
617617
return new AsyncChunkedResponse(contentType, callback);
618618
return new AsyncCallbackResponse(contentType, 0, callback);
619619
}
620620

621-
AsyncResponseStream * AsyncWebServerRequest::beginResponseStream(String contentType, size_t bufferSize){
621+
AsyncResponseStream * AsyncWebServerRequest::beginResponseStream(const String& contentType, size_t bufferSize){
622622
return new AsyncResponseStream(contentType, bufferSize);
623623
}
624624

625-
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse_P(int code, String contentType, const uint8_t * content, size_t len){
625+
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse_P(int code, const String& contentType, const uint8_t * content, size_t len){
626626
return new AsyncProgmemResponse(code, contentType, content, len);
627627
}
628628

629-
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse_P(int code, String contentType, PGM_P content){
629+
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse_P(int code, const String& contentType, PGM_P content){
630630
return beginResponse_P(code, contentType, (const uint8_t *)content, strlen_P(content));
631631
}
632632

633-
void AsyncWebServerRequest::send(int code, String contentType, String content){
633+
void AsyncWebServerRequest::send(int code, const String& contentType, const String& content){
634634
send(beginResponse(code, contentType, content));
635635
}
636636

637-
void AsyncWebServerRequest::send(FS &fs, String path, String contentType, bool download){
637+
void AsyncWebServerRequest::send(FS &fs, const String& path, const String& contentType, bool download){
638638
if(fs.exists(path) || (!download && fs.exists(path+".gz"))){
639639
send(beginResponse(fs, path, contentType, download));
640640
} else send(404);
641641
}
642642

643-
void AsyncWebServerRequest::send(File content, String path, String contentType, bool download){
643+
void AsyncWebServerRequest::send(File content, const String& path, const String& contentType, bool download){
644644
if(content == true){
645645
send(beginResponse(content, path, contentType, download));
646646
} else send(404);
647647
}
648648

649-
void AsyncWebServerRequest::send(Stream &stream, String contentType, size_t len){
649+
void AsyncWebServerRequest::send(Stream &stream, const String& contentType, size_t len){
650650
send(beginResponse(stream, contentType, len));
651651
}
652652

653-
void AsyncWebServerRequest::send(String contentType, size_t len, AwsResponseFiller callback){
653+
void AsyncWebServerRequest::send(const String& contentType, size_t len, AwsResponseFiller callback){
654654
send(beginResponse(contentType, len, callback));
655655
}
656656

657-
void AsyncWebServerRequest::sendChunked(String contentType, AwsResponseFiller callback){
657+
void AsyncWebServerRequest::sendChunked(const String& contentType, AwsResponseFiller callback){
658658
send(beginChunkedResponse(contentType, callback));
659659
}
660660

661-
void AsyncWebServerRequest::send_P(int code, String contentType, const uint8_t * content, size_t len){
661+
void AsyncWebServerRequest::send_P(int code, const String& contentType, const uint8_t * content, size_t len){
662662
send(beginResponse_P(code, contentType, content, len));
663663
}
664664

665-
void AsyncWebServerRequest::send_P(int code, String contentType, PGM_P content){
665+
void AsyncWebServerRequest::send_P(int code, const String& contentType, PGM_P content){
666666
send(beginResponse_P(code, contentType, content));
667667
}
668668

669-
void AsyncWebServerRequest::redirect(String url){
669+
void AsyncWebServerRequest::redirect(const String& url){
670670
AsyncWebServerResponse * response = beginResponse(302);
671671
response->addHeader("Location",url);
672672
send(response);
@@ -732,7 +732,7 @@ bool AsyncWebServerRequest::hasArg(const char* name) const {
732732
return false;
733733
}
734734

735-
String AsyncWebServerRequest::arg(String name) const {
735+
String AsyncWebServerRequest::arg(const String& name) const {
736736
for(const auto& arg: _params){
737737
if(arg->name() == name){
738738
return arg->value();
@@ -764,8 +764,7 @@ String AsyncWebServerRequest::headerName(size_t i) const {
764764
return h ? h->name() : String();
765765
}
766766

767-
768-
String AsyncWebServerRequest::urlDecode(String text) const {
767+
String AsyncWebServerRequest::urlDecode(const String& text) const {
769768
char temp[] = "0x00";
770769
unsigned int len = text.length();
771770
unsigned int i = 0;

src/WebResponseImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class AsyncFileResponse: public AsyncAbstractResponse {
4545
private:
4646
File _content;
4747
String _path;
48-
void _setContentType(String path);
48+
void _setContentType(const String& path);
4949
public:
5050
AsyncFileResponse(FS &fs, String path, String contentType=String(), bool download=false);
5151
AsyncFileResponse(File content, String path, String contentType=String(), bool download=false);

src/WebResponses.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ void AsyncWebServerResponse::setContentLength(size_t len){
9999
_contentLength = len;
100100
}
101101

102-
void AsyncWebServerResponse::setContentType(String type){
102+
void AsyncWebServerResponse::setContentType(const String& type){
103103
if(_state == RESPONSE_SETUP)
104104
_contentType = type;
105105
}
106106

107-
void AsyncWebServerResponse::addHeader(String name, String value){
107+
void AsyncWebServerResponse::addHeader(const String& name, const String& value){
108108
_headers.add(new AsyncWebHeader(name, value));
109109
}
110110

@@ -330,7 +330,7 @@ AsyncFileResponse::~AsyncFileResponse(){
330330
_content.close();
331331
}
332332

333-
void AsyncFileResponse::_setContentType(String path){
333+
void AsyncFileResponse::_setContentType(const String& path){
334334
if (path.endsWith(".html")) _contentType = "text/html";
335335
else if (path.endsWith(".htm")) _contentType = "text/html";
336336
else if (path.endsWith(".css")) _contentType = "text/css";

0 commit comments

Comments
 (0)
0