Skip to content

Commit 7c0a149

Browse files
authored
Merge branch 'main' into algavris/room-upgrade
2 parents 9f9e736 + 54c9c5b commit 7c0a149

File tree

1 file changed

+60
-50
lines changed

1 file changed

+60
-50
lines changed

lib/http/HttpResponseDecoder.cpp

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -169,72 +169,82 @@ namespace MAT_NS_BEGIN {
169169
{
170170
#ifdef HAVE_MAT_JSONHPP
171171
// TODO: [MG] - parse HTTP response without json.hpp library
172-
nlohmann::json responseBody;
173-
try
172+
auto bodyBegin = response.GetBody().begin();
173+
auto bodyEnd = response.GetBody().end();
174+
175+
// Handle empty body
176+
if (bodyBegin == bodyEnd)
177+
{
178+
LOG_ERROR("HTTP response: body is empty, skipping processing");
179+
return;
180+
}
181+
182+
// Parse JSON with exceptions disabled (pass iterators directly to avoid copy)
183+
nlohmann::json responseBody = nlohmann::json::parse(bodyBegin, bodyEnd, nullptr, false);
184+
185+
// Check if parsing failed (returns discarded value for invalid JSON)
186+
if (responseBody.is_discarded())
187+
{
188+
LOG_ERROR("HTTP response: body is not valid JSON, skipping processing");
189+
return;
190+
}
191+
192+
int accepted = 0;
193+
auto acc = responseBody.find("acc");
194+
if (responseBody.end() != acc)
174195
{
175-
std::string body(response.GetBody().begin(), response.GetBody().end());
176-
responseBody = nlohmann::json::parse(body.c_str());
177-
int accepted = 0;
178-
auto acc = responseBody.find("acc");
179-
if (responseBody.end() != acc)
196+
if (acc.value().is_number())
180197
{
181-
if (acc.value().is_number())
182-
{
183-
accepted = acc.value().get<int>();
184-
}
198+
accepted = acc.value().get<int>();
185199
}
200+
}
186201

187-
int rejected = 0;
188-
auto rej = responseBody.find("rej");
189-
if (responseBody.end() != rej)
202+
int rejected = 0;
203+
auto rej = responseBody.find("rej");
204+
if (responseBody.end() != rej)
205+
{
206+
if (rej.value().is_number())
190207
{
191-
if (rej.value().is_number())
192-
{
193-
rejected = rej.value().get<int>();
194-
}
208+
rejected = rej.value().get<int>();
195209
}
210+
}
196211

197-
auto efi = responseBody.find("efi");
198-
if (responseBody.end() != efi)
212+
auto efi = responseBody.find("efi");
213+
if (responseBody.end() != efi)
214+
{
215+
for (auto it = responseBody["efi"].begin(); it != responseBody["efi"].end(); ++it)
199216
{
200-
for (auto it = responseBody["efi"].begin(); it != responseBody["efi"].end(); ++it)
217+
std::string efiKey(it.key());
218+
nlohmann::json val = it.value();
219+
if (val.is_array())
201220
{
202-
std::string efiKey(it.key());
203-
nlohmann::json val = it.value();
204-
if (val.is_array())
205-
{
206-
//std::vector<int> failureVector = val.get<std::vector<int>>();
207-
// eventsRejected(ctx); with only the ids in the vector above
208-
}
209-
if (val.is_string())
221+
//std::vector<int> failureVector = val.get<std::vector<int>>();
222+
// eventsRejected(ctx); with only the ids in the vector above
223+
}
224+
if (val.is_string())
225+
{
226+
if ("all" == val.get<std::string>())
210227
{
211-
if ("all" == val.get<std::string>())
212-
{
213-
result = Rejected;
214-
}
228+
result = Rejected;
215229
}
216230
}
217231
}
232+
}
218233

219-
auto ticket = responseBody.find("TokenCrackingFailure");
220-
if (responseBody.end() != ticket)
221-
{
222-
DebugEvent evt;
223-
evt.type = DebugEventType::EVT_TICKET_EXPIRED;
224-
DispatchEvent(evt);
225-
}
226-
227-
if (result != Rejected)
228-
{
229-
LOG_TRACE("HTTP response: accepted=%d rejected=%d", accepted, rejected);
230-
} else
231-
{
232-
LOG_TRACE("HTTP response: all rejected");
233-
}
234+
auto ticket = responseBody.find("TokenCrackingFailure");
235+
if (responseBody.end() != ticket)
236+
{
237+
DebugEvent evt;
238+
evt.type = DebugEventType::EVT_TICKET_EXPIRED;
239+
DispatchEvent(evt);
234240
}
235-
catch (...)
241+
242+
if (result != Rejected)
243+
{
244+
LOG_TRACE("HTTP response: accepted=%d rejected=%d", accepted, rejected);
245+
} else
236246
{
237-
LOG_ERROR("HTTP response: JSON parsing failed");
247+
LOG_TRACE("HTTP response: all rejected");
238248
}
239249
#else
240250
UNREFERENCED_PARAMETER(response);

0 commit comments

Comments
 (0)