| 61 | } |
| 62 | }; |
| 63 | |
| 64 | Result saneMain(Span<StringSpan> args) |
| 65 | { |
| 66 | Console console; |
| 67 | Console::tryAttachingToParentConsole(); |
| 68 | |
| 69 | if (args.sizeInElements() < 1) |
| 70 | { |
| 71 | console.print("Usage: HttpClientAsyncGet <url>\n"); |
| 72 | console.print(" Example: HttpClientAsyncGet https://example.com\n"); |
| 73 | return Result::Error("Missing URL argument"); |
| 74 | } |
| 75 | |
| 76 | AsyncEventLoop loop; |
| 77 | SC_TRY(loop.create()); |
| 78 | |
| 79 | HttpClient client; |
| 80 | SC_TRY(client.init()); |
| 81 | |
| 82 | HttpClientResponseBuffer responseBuffers[8]; |
| 83 | HttpClientOperationEvent eventQueue[16]; |
| 84 | AsyncBufferView asyncResponseBuffers[8]; |
| 85 | AsyncReadableStream::Request responseReadQueue[8]; |
| 86 | AsyncWritableStream::Request requestWriteQueue[4]; |
| 87 | |
| 88 | char responseMemory[64 * 1024]; |
| 89 | char responseHeaders[8 * 1024]; |
| 90 | char responseMetadata[4 * 1024]; |
| 91 | char backendScratch[16 * 1024]; |
| 92 | |
| 93 | HttpClientOperationMemory operationMemory; |
| 94 | operationMemory.responseBuffers = {responseBuffers, 8}; |
| 95 | operationMemory.responseBufferMemory = {responseMemory, sizeof(responseMemory)}; |
| 96 | operationMemory.eventQueue = {eventQueue, 16}; |
| 97 | operationMemory.responseHeaders = {responseHeaders, sizeof(responseHeaders)}; |
| 98 | operationMemory.responseMetadata = {responseMetadata, sizeof(responseMetadata)}; |
| 99 | operationMemory.backendScratch = {backendScratch, sizeof(backendScratch)}; |
| 100 | |
| 101 | HttpClientAsyncOperationMemoryT<AsyncStreams> asyncMemory; |
| 102 | asyncMemory.responseBuffers = {asyncResponseBuffers, 8}; |
| 103 | asyncMemory.responseBufferMemory = {responseMemory, sizeof(responseMemory)}; |
| 104 | asyncMemory.responseReadQueue = {responseReadQueue, 8}; |
| 105 | asyncMemory.requestWriteQueue = {requestWriteQueue, 4}; |
| 106 | |
| 107 | AsyncHttpClientOperation operation; |
| 108 | SC_TRY(operation.init(client, loop, operationMemory, asyncMemory)); |
| 109 | |
| 110 | HttpClientRequest request; |
| 111 | request.url = args[0]; |
| 112 | request.options.timeouts.requestTimeoutMs = 30000; |
| 113 | |
| 114 | HttpClientResponse response; |
| 115 | char body[256 * 1024]; |
| 116 | AsyncResponseCollector collector; |
| 117 | collector.operation = &operation; |
| 118 | collector.body = {body, sizeof(body)}; |
| 119 | |
| 120 | const bool dataAdded = |