From 914a3808ebefb0f7f0a728469a2ce6b56dfc316c Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Mon, 15 Nov 2021 20:45:21 -0500 Subject: Http protocol support (#2012) * #include an absolute path didn't work - because paths were taken to always be relative. * Use 'Process' to communicate with an command line tool. * Remove slang-win-stream * Tidy up windows ProcessUtil. * First version of BufferedReadStream. * Windows working IPC for steams. * Test proxy count option. * Split Process/ProcessUtil. Process is platform dependant. ProcessUtil are functions that are platform independent. * First implementation of Unix Process interface. * Unix process compiles on cygwin. * Fix typo in unix process. * Separate unix pipe stream error of invalid access, from pipe availability. * Fix in standard line extraction. * Make fd non blocking. * Fix issues with Windows Process streams. * Added UnixPipe. * Some fixes around UnixPipeStream. * Make a unix stream closed explicit. * Hack to debug linux process/stream. * Revert to old linux pipe handling. * Pass executable path for unit tests. Split out CommandLine into own source. * Small improvements in process/command line. * Check process behavior with crash. * Make stderr and stdout unbuffered for crash testing. * Only turn disable buffering in crash test. * Disable crash test on CI. * Fix crash on clang/linux. * Enable crash test. Remove _appendBuffer as can use StreamUtil functionality. * Added inital processing for http headers. * Small improvements to HttpHeader. * First pass HTTPPacketConnection working on windows. * Enable other Process communication tests. * Update comments. --- tools/slang-unit-test/unit-test-process.cpp | 66 +++++++++++++++++++++++++++++ tools/test-proxy/test-proxy-main.cpp | 50 +++++++++++++++++++++- 2 files changed, 115 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/slang-unit-test/unit-test-process.cpp b/tools/slang-unit-test/unit-test-process.cpp index 3dccf1eaf..8a55b0b29 100644 --- a/tools/slang-unit-test/unit-test-process.cpp +++ b/tools/slang-unit-test/unit-test-process.cpp @@ -3,11 +3,75 @@ #include "../../source/core/slang-string-util.h" #include "../../source/core/slang-process-util.h" #include "../../source/core/slang-io.h" +#include "../../source/core/slang-http.h" +#include "../../source/core/slang-random-generator.h" #include "tools/unit-test/slang-unit-test.h" using namespace Slang; +static SlangResult _httpReflectTest(UnitTestContext* context) +{ + SlangResult finalRes = SLANG_OK; + + RefPtr process; + + { + CommandLine cmdLine; + cmdLine.setExecutable(context->executableDirectory, "test-proxy"); + cmdLine.addArg("http-reflect"); + SLANG_RETURN_ON_FAIL(Process::create(cmdLine, Process::Flag::AttachDebugger, process)); + } + + Stream* writeStream = process->getStream(Process::StreamType::StdIn); + RefPtr readStream( new BufferedReadStream(process->getStream(Process::StreamType::StdOut))); + + RefPtr connection = new HTTPPacketConnection(readStream, writeStream); + + RefPtr rand = RandomGenerator::create(10000); + + for (Index i = 0; i < 100; i++) + { + if (process->isTerminated()) + { + return SLANG_FAIL; + } + + const Index size = Index(rand->nextInt32UpTo(8192)); + + List buf; + buf.setCount(size); + // Build up a buffer + rand->nextData(buf.getBuffer(), size_t(size)); + + // Write the data + SLANG_RETURN_ON_FAIL(connection->write(buf.getBuffer(), size_t(size))); + + // Wait for the response + SLANG_RETURN_ON_FAIL(connection->waitForContent()); + + auto readContent = connection->getContent(); + + if (readContent != buf.getArrayView()) + { + finalRes = SLANG_FAIL; + break; + } + + // Consume that packet + connection->consumeContent(); + } + + // Send the end + { + const char end[] = "end"; + SLANG_RETURN_ON_FAIL(connection->write(end, SLANG_COUNT_OF(end) - 1)); + } + + process->waitForTermination(); + return finalRes; +} + static SlangResult _countTest(UnitTestContext* context, Index size, Index crashIndex = -1) { RefPtr process; @@ -122,4 +186,6 @@ SLANG_UNIT_TEST(CommandLineProcess) SLANG_CHECK(SLANG_SUCCEEDED(_countTests(unitTestContext))); SLANG_CHECK(SLANG_SUCCEEDED(_reflectTest(unitTestContext))); + + SLANG_CHECK(SLANG_SUCCEEDED(_httpReflectTest(unitTestContext))); } diff --git a/tools/test-proxy/test-proxy-main.cpp b/tools/test-proxy/test-proxy-main.cpp index 70efc8907..9a3523d71 100644 --- a/tools/test-proxy/test-proxy-main.cpp +++ b/tools/test-proxy/test-proxy-main.cpp @@ -17,6 +17,7 @@ #include "../../source/core/slang-shared-library.h" #include "../../source/core/slang-test-tool-util.h" +#include "../../source/core/slang-http.h" #include "tools/unit-test/slang-unit-test.h" @@ -210,11 +211,53 @@ static SlangResult _outputReflect() } } +static SlangResult _httpReflect(int argc, const char* const* argv) +{ + SLANG_UNUSED(argc); + SLANG_UNUSED(argv); + + RefPtr stdinStream, stdoutStream; + + Process::getStdStream(Process::StreamType::StdIn, stdinStream); + Process::getStdStream(Process::StreamType::StdOut, stdoutStream); + + RefPtr readStream(new BufferedReadStream(stdinStream)); + + RefPtr packetConnection = new HTTPPacketConnection(readStream, stdoutStream); + + while (packetConnection->isActive()) + { + // Block waiting for content (or error/closed) + SLANG_RETURN_ON_FAIL(packetConnection->waitForContent()); + + // If we have content do something with it + if (packetConnection->hasContent()) + { + auto content = packetConnection->getContent(); + + // If it just holds 'end' then we are done + const UnownedStringSlice slice((const char*)content.begin(), content.getCount()); + + if (slice == UnownedStringSlice::fromLiteral("end")) + { + break; + } + + // Else reflect it back + SLANG_RETURN_ON_FAIL(packetConnection->write(content.begin(), content.getCount())); + + // Consume that content/packet + packetConnection->consumeContent(); + } + } + + return SLANG_OK; +} + static SlangResult execute(int argc, const char*const* argv) { typedef Slang::TestToolUtil::InnerMainFunc InnerMainFunc; - if (argc < 2) { return SLANG_FAIL; @@ -235,6 +278,11 @@ static SlangResult execute(int argc, const char*const* argv) return _outputCount(argc, argv); } + if (toolName == "http-reflect") + { + return _httpReflect(argc, argv); + } + { StringBuilder sharedLibToolBuilder; sharedLibToolBuilder.append(toolName); -- cgit v1.2.3