Skip to content

Commit 841974f

Browse files
mikee47slaff
authored andcommitted
More efficient bodyParser selection (SmingHub#1823)
Don't use Vector, avoid un-necessary memory heap allocations
1 parent 874eb93 commit 841974f

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

Sming/Core/Network/Http/HttpServerConnection.cpp

+12-17
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,22 @@ int HttpServerConnection::onHeadersComplete(const HttpHeaders& headers)
109109
contentType = contentType.substring(0, endPos);
110110
}
111111

112-
String majorType = contentType.substring(0, contentType.indexOf('/'));
113-
majorType += "/*";
114-
115112
// Content-Type for exact type: application/json
116-
// Wildcard type for application: application/*
117-
// Wildcard type for the rest*
118-
119-
Vector<String> types;
120-
types.add(contentType);
121-
types.add(majorType);
122-
types.add(String('*'));
123-
124-
for(unsigned i = 0; i < types.count(); i++) {
125-
const String& type = types[i];
126-
if(bodyParsers->contains(type)) {
127-
bodyParser = (*bodyParsers)[type];
128-
break;
113+
int i = bodyParsers->indexOf(contentType);
114+
if(i < 0) {
115+
// Wildcard type for application: application/*
116+
contentType.setLength(contentType.indexOf('/') + 1);
117+
contentType += '*';
118+
i = bodyParsers->indexOf(contentType);
119+
if(i < 0) {
120+
// Wildcard type for the rest*
121+
i = bodyParsers->indexOf(String('*'));
129122
}
130123
}
131124

132-
if(bodyParser) {
125+
if(i >= 0) {
126+
bodyParser = bodyParsers->valueAt(i);
127+
assert(bodyParser != nullptr);
133128
bodyParser(request, nullptr, PARSE_DATASTART);
134129
}
135130
}

0 commit comments

Comments
 (0)