| 509 | serverContext.numRequests++; |
| 510 | SC_TEST_EXPECT(response.startResponse(200)); |
| 511 | SC_TEST_EXPECT(response.addHeader("Connection", "Closed")); |
| 512 | SC_TEST_EXPECT(response.addHeader("Content-Type", "text/html")); |
| 513 | SC_TEST_EXPECT(response.addHeader("Server", "SC")); |
| 514 | SC_TEST_EXPECT(response.addHeader("Date", "Mon, 27 Aug 2023 16:37:00 GMT")); |
| 515 | SC_TEST_EXPECT(response.addHeader("Last-Modified", "Wed, 27 Aug 2023 16:37:00 GMT")); |
| 516 | const char sampleHtml[] = "<html>\r\n" |
| 517 | "<body bgcolor=\"#000000\" text=\"#ffffff\">\r\n" |
| 518 | "<h1>This is a title {}!</h1>\r\n" |
| 519 | "We must start from somewhere\r\n" |
| 520 | "</body>\r\n" |
| 521 | "</html>\r\n"; |
| 522 | |
| 523 | // Create a "user provided" dynamically allocated string, to show this is possible |
| 524 | String content; |
| 525 | SC_TEST_EXPECT(StringBuilder::format(content, sampleHtml, serverContext.numRequests)); |
| 526 | SmallString<16> contentLength; |
| 527 | SC_TEST_EXPECT(StringBuilder::format(contentLength, "{}", content.view().sizeInBytes())); |
| 528 | SC_TEST_EXPECT(response.addHeader("Content-Length", contentLength.view())); |
| 529 | SC_TEST_EXPECT(response.sendHeaders()); |
| 530 | // Note that the system takes ownership of the dynamically allocated user provided string |
| 531 | // through type erasure and it will invoke its destructor after th write operation will finish, |
| 532 | // freeing user memory as expected. |
| 533 | // This write operation succeeds because EXTRA_SLICES allocates one more slot buffer exactly |
| 534 | // to hold this user provided buffer, that is not part of the "re-usable" buffers created |
| 535 | // at the beginning of this sample. |
| 536 | SC_TEST_EXPECT(response.getWritableStream().write(move(content))); |
| 537 | SC_TEST_EXPECT(response.end()); |
| 538 | }; |
| 539 | |
| 540 | //! [HttpAsyncServerSnippet] |
| 541 | |
| 542 | HttpTestClient client[3]; |
| 543 | struct ClientContext |
| 544 | { |
| 545 | int numRequests; |
| 546 | int wantedNumRequests; |
| 547 | |
| 548 | HttpAsyncServer& httpServer; |
| 549 | } clientContext = {0, 3, httpServer}; |
| 550 | String endpoint = StringEncoding::Ascii; |
| 551 | SC_TEST_EXPECT(StringBuilder::format(endpoint, "http://127.0.0.1:{}/index.html", serverPort)); |
nothing calls this directly
no test coverage detected