| 19 | |
| 20 | namespace SC |
| 21 | { |
| 22 | Result saneMain(Span<StringSpan> args) |
| 23 | { |
| 24 | Console console; |
| 25 | Console::tryAttachingToParentConsole(); |
| 26 | |
| 27 | if (args.sizeInElements() < 1) |
| 28 | { |
| 29 | console.print("Usage: SaneHttpGet <url>\n"); |
| 30 | console.print(" Example: SaneHttpGet https://example.com\n"); |
| 31 | return Result::Error("Missing URL argument"); |
| 32 | } |
| 33 | |
| 34 | const StringSpan urlArg = args[0]; |
| 35 | |
| 36 | console.print("GET {}\n", urlArg); |
| 37 | |
| 38 | HttpClientRequest request; |
| 39 | request.url = urlArg; |
| 40 | |
| 41 | request.options.timeouts.requestTimeoutMs = 30000; |
| 42 | |
| 43 | HttpClientResponse response; |
| 44 | HttpClientResponseBuffer responseBuffers[16]; |
| 45 | HttpClientOperationEvent eventQueue[32]; |
| 46 | |
| 47 | size_t bodyLength = 0; |
| 48 | |
| 49 | char bodyBuf[256 * 1024]; // 256 KB max response body |
| 50 | char responseMemory[64 * 1024]; |
| 51 | char headerBuf[8 * 1024]; |
| 52 | char metadataBuf[4 * 1024]; |
| 53 | char backendScratch[16 * 1024]; |
| 54 | |
| 55 | HttpClientOperationMemory operationMemory; |
| 56 | operationMemory.responseBuffers = {responseBuffers, 16}; |
| 57 | operationMemory.responseBufferMemory = {responseMemory, sizeof(responseMemory)}; |
| 58 | operationMemory.eventQueue = {eventQueue, 32}; |
| 59 | operationMemory.responseHeaders = {headerBuf, sizeof(headerBuf)}; |
| 60 | operationMemory.responseMetadata = {metadataBuf, sizeof(metadataBuf)}; |
| 61 | operationMemory.backendScratch = {backendScratch, sizeof(backendScratch)}; |
| 62 | |
| 63 | Result res = |
| 64 | HttpClient::executeBlocking(request, response, {bodyBuf, sizeof(bodyBuf)}, bodyLength, operationMemory); |
| 65 | |
| 66 | if (not res) |
| 67 | { |
| 68 | console.print("Error: request failed\n"); |
| 69 | return res; |
| 70 | } |
| 71 | |
| 72 | console.print("Status: {}\n", response.statusCode); |
| 73 | console.print("Effective URL: {}\n", StringView(response.effectiveUrl)); |
| 74 | console.print("Headers ({} bytes):\n", response.headersLength); |
| 75 | |
| 76 | StringView headers({headerBuf, response.headersLength}, false, StringEncoding::Ascii); |
| 77 | console.print("{}\n", headers); |
| 78 | |