From 4f5c7b28f88b2ae2166a148bf95bcb9cc3e5b998 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Tue, 2 Jul 2024 18:09:31 +0200 Subject: [PATCH] http sample --- src/mono/sample/wasi/Directory.Build.targets | 6 +- src/mono/sample/wasi/http-p2/Program.cs | 57 +++++++++++++++++++ .../http-p2/Wasip2.Http.Console.Sample.csproj | 11 ++++ 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 src/mono/sample/wasi/http-p2/Program.cs create mode 100644 src/mono/sample/wasi/http-p2/Wasip2.Http.Console.Sample.csproj diff --git a/src/mono/sample/wasi/Directory.Build.targets b/src/mono/sample/wasi/Directory.Build.targets index 75af1e423eef42..aead4c44795909 100644 --- a/src/mono/sample/wasi/Directory.Build.targets +++ b/src/mono/sample/wasi/Directory.Build.targets @@ -16,13 +16,13 @@ --> + Command="$(WasmtimeDir)wasmtime$(_ExeExt) $(MONO_LOG_LEVEL) -S http --dir . dotnet.wasm $(_SampleProjectName)" IgnoreExitCode="true" /> + Command="$(WasmtimeDir)wasmtime$(_ExeExt) $(MONO_LOG_LEVEL) -S http --dir . dotnet.wasm" IgnoreExitCode="true" /> + Command="$(WasmtimeDir)wasmtime$(_ExeExt) $(MONO_LOG_LEVEL) -S http $([System.IO.Path]::ChangeExtension($(_SampleAssembly), '.wasm'))" IgnoreExitCode="true" /> diff --git a/src/mono/sample/wasi/http-p2/Program.cs b/src/mono/sample/wasi/http-p2/Program.cs new file mode 100644 index 00000000000000..f2687d511fc151 --- /dev/null +++ b/src/mono/sample/wasi/http-p2/Program.cs @@ -0,0 +1,57 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Net.Http.Headers; +using System.Net.Http; +using System.Threading.Tasks; +using System.Threading; +using System.Runtime.CompilerServices; + +public class Test +{ + public static int Main(string[] args) + { + var task = Work(); + while (!task.IsCompleted) + { + WasiEventLoop.DispatchWasiEventLoop(); + } + var exception = task.Exception; + if (exception is not null) + { + throw exception; + } + + return 0; + } + + public static async Task Work() + { + using HttpClient client = new(); + client.Timeout = Timeout.InfiniteTimeSpan; + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add( + new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); + client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter"); + + var query="https://api.github.com/orgs/dotnet/repos?per_page=1"; + var json = await client.GetStringAsync(query); + + Console.WriteLine(); + Console.WriteLine("GET "+query); + Console.WriteLine(); + Console.WriteLine(json); + } + + private static class WasiEventLoop + { + internal static void DispatchWasiEventLoop() + { + CallDispatchWasiEventLoop((Thread)null!); + + [UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "DispatchWasiEventLoop")] + static extern void CallDispatchWasiEventLoop(Thread t); + } + } +} diff --git a/src/mono/sample/wasi/http-p2/Wasip2.Http.Console.Sample.csproj b/src/mono/sample/wasi/http-p2/Wasip2.Http.Console.Sample.csproj new file mode 100644 index 00000000000000..cb410fa78f843a --- /dev/null +++ b/src/mono/sample/wasi/http-p2/Wasip2.Http.Console.Sample.csproj @@ -0,0 +1,11 @@ + + + $(NetCoreAppCurrent) + + + + +