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

Method executeBlocking

Libraries/HttpClient/HttpClient.cpp:1702–1772  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1700}
1701
1702SC::Result SC::HttpClient::executeBlocking(const HttpClientRequest& request, HttpClientResponse& response,
1703 Span<char> bodyBuffer, size_t& bodyLength,
1704 const HttpClientOperationMemory& memory)
1705{
1706 bodyLength = 0;
1707
1708 HttpClient client;
1709 SC_TRY(client.init());
1710
1711 struct BlockingListener final : public HttpClientOperationListener
1712 {
1713 Span<char> bodyBuffer;
1714 size_t* bodyLength = nullptr;
1715 bool* completed = nullptr;
1716 Result* finalRes = nullptr;
1717 HttpClientOperation* operation = nullptr;
1718
1719 virtual void onResponseBody(Span<const char> data) override
1720 {
1721 const size_t remaining = bodyBuffer.sizeInBytes() - *bodyLength;
1722 const size_t toCopy = data.sizeInBytes() < remaining ? data.sizeInBytes() : remaining;
1723 if (toCopy > 0)
1724 {
1725 memcpy(bodyBuffer.data() + *bodyLength, data.data(), toCopy);
1726 *bodyLength += toCopy;
1727 }
1728 if (toCopy != data.sizeInBytes())
1729 {
1730 *finalRes = Result::Error("HttpClient: blocking response body buffer too small");
1731 *completed = true;
1732 (void)operation->cancel();
1733 }
1734 }
1735
1736 virtual void onResponseComplete() override { *completed = true; }
1737
1738 virtual void onError(Result error) override
1739 {
1740 *completed = true;
1741 if (*finalRes)
1742 {
1743 *finalRes = error;
1744 }
1745 }
1746 };
1747
1748 HttpClientOperation operation;
1749 SC_TRY(operation.init(client, memory));
1750
1751 bool completed = false;
1752 Result finalRes(true);
1753
1754 BlockingListener listener;
1755 listener.bodyBuffer = bodyBuffer;
1756 listener.bodyLength = &bodyLength;
1757 listener.completed = &completed;
1758 listener.finalRes = &finalRes;
1759 listener.operation = &operation;

Callers

nothing calls this directly

Calls 5

isRequestInFlightMethod · 0.80
pollMethod · 0.80
initMethod · 0.45
startMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected