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

Method get

Tests/Libraries/Http/HttpTestClient.cpp:8–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6#include <stdio.h> // snprintf
7
8SC::Result SC::HttpTestClient::get(AsyncEventLoop& loop, StringSpan url, bool keepOpen)
9{
10 eventLoop = &loop;
11 keepConnectionOpen = keepOpen;
12
13 // Parse URL
14 HttpURLParser urlParser;
15 SC_TRY(urlParser.parse(url));
16 if (urlParser.protocol != "http")
17 return Result::Error(HttpResultCategory, HttpError::UnsupportedProtocol);
18
19 // Reset state for new request
20 parser = {};
21 parser.type = HttpParser::Type::Response;
22 receivedBytes = 0;
23 parsedBytes = 0;
24 contentLen = 0;
25 headersReceived = false;
26 responseHasNoBody = false;
27
28 {
29 GrowableBuffer<decltype(content)> gb = {content};
30
31 HttpStringAppend& sb = static_cast<HttpStringAppend&>(static_cast<IGrowableBuffer&>(gb));
32 sb.clear();
33 SC_TRY(sb.append("GET "));
34 SC_TRY(sb.append(urlParser.path));
35 SC_TRY(sb.append(" HTTP/1.1\r\n"));
36 SC_TRY(sb.append("User-agent: SC\r\n"));
37 SC_TRY(sb.append("Host: 127.0.0.1\r\n"));
38 if (keepConnectionOpen)
39 {
40 SC_TRY(sb.append("Connection: keep-alive\r\n"));
41 }
42 SC_TRY(sb.append("\r\n"));
43 headerBytes = content.size();
44 }
45
46 // If we don't have an active connection or we're not keeping connections open, create a new one
47 if (not hasActiveConnection or not keepConnectionOpen)
48 {
49 uint16_t port;
50 // TODO: Make DNS Resolution asynchronous
51 char buffer[256];
52 Span<char> ipAddress = {buffer};
53 SC_TRY(SocketDNS::resolveDNS(urlParser.hostname, ipAddress))
54 port = urlParser.port;
55 SocketIPAddress localHost;
56 SC_TRY(localHost.fromAddressPort({ipAddress, true, StringEncoding::Ascii}, port));
57 SC_TRY(eventLoop->createAsyncTCPSocket(localHost.getAddressFamily(), clientSocket));
58 hasActiveConnection = true;
59
60 connectAsync.callback.bind<HttpTestClient, &HttpTestClient::startSendingHeaders>(*this);
61 return connectAsync.start(*eventLoop, clientSocket, localHost);
62 }
63 else
64 {
65 // Reuse existing connection - go directly to sending headers

Callers 15

captureBuildActionOutputFunction · 0.45
customMimeLookupMethod · 0.45
httpFileServerTestMethod · 0.45
basicGetMethod · 0.45
onDeferredMethod · 0.45
keepAliveAndReconnectMethod · 0.45
zeroLengthResponseMethod · 0.45
chunkedResponseMethod · 0.45

Calls 8

createAsyncTCPSocketMethod · 0.80
getAddressFamilyMethod · 0.80
ResultClass · 0.50
parseMethod · 0.45
clearMethod · 0.45
appendMethod · 0.45
sizeMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected