MCPcopy Create free account
hub / github.com/Pagghiu/SaneCppLibraries / tryParseResponse

Method tryParseResponse

Tests/Libraries/Http/HttpTestClient.cpp:367–423  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

365}
366
367void SC::HttpTestClient::tryParseResponse(AsyncSocketReceive::Result& result)
368{
369 receivedBytes += result.completionData.numBytes;
370 SC_ASSERT_RELEASE(content.resize(receivedBytes));
371 Span<const char> readData;
372 SC_ASSERT_RELEASE(content.toSpanConst().sliceStart(parsedBytes, readData));
373 bool parsedSuccessfully = true;
374 if (not headersReceived)
375 {
376 size_t readBytes;
377 while (parsedSuccessfully and not readData.empty())
378 {
379 Span<const char> parsedData;
380 parsedSuccessfully &= parser.parse(readData, readBytes, parsedData);
381 parsedSuccessfully &= readData.sliceStart(readBytes, readData);
382 parsedBytes += readBytes;
383 if (parser.state == HttpParser::State::Result and parser.token == HttpParser::Token::HeaderValue)
384 {
385 if (parser.matchesHeader(HttpParser::HeaderType::ContentLength))
386 {
387 contentLen = static_cast<size_t>(parser.contentLength);
388 }
389 }
390 if (parser.token == HttpParser::Token::HeadersEnd)
391 {
392 headersReceived = true;
393 break;
394 }
395 }
396 }
397 const size_t expectedContentLen = responseHasNoBody and headersReceived ? 0 : contentLen;
398 if (content.size() == parsedBytes + expectedContentLen)
399 {
400 // If we're not keeping the connection open, or if the server closed the connection, close it
401 if (not keepConnectionOpen or result.completionData.disconnected)
402 {
403 SC_ASSERT_RELEASE(clientSocket.close());
404 hasActiveConnection = false;
405 }
406 // Call the callback regardless
407 callback(*this);
408 }
409 else
410 {
411 SC_ASSERT_RELEASE(content.reserve(receivedBytes + 1024));
412 SC_ASSERT_RELEASE(content.resize(content.capacity()));
413 SC_ASSERT_RELEASE(content.toSpan().sliceStart(receivedBytes, receiveAsync.buffer));
414 if (parsedSuccessfully)
415 {
416 result.reactivateRequest(true);
417 }
418 else
419 {
420 // TODO: raise error
421 }
422 }
423}

Callers

nothing calls this directly

Calls 10

sliceStartMethod · 0.80
matchesHeaderMethod · 0.80
reserveMethod · 0.80
toSpanMethod · 0.80
reactivateRequestMethod · 0.80
resizeMethod · 0.45
parseMethod · 0.45
sizeMethod · 0.45
closeMethod · 0.45
capacityMethod · 0.45

Tested by

no test coverage detected