Skip to content

Commit 0e8db73

Browse files
pavelsavaradicej
andauthored
[wasi] wasi:http + WasiHttpHandler (#103752)
Co-authored-by: Joel Dice <joel.dice@fermyon.com>
1 parent c0a31c5 commit 0e8db73

File tree

46 files changed

+10266
-41
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+10266
-41
lines changed

eng/liveBuilds.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
<LibrariesRuntimeFiles
245245
Include="
246246
$(LibrariesNativeArtifactsPath)dotnet.wasm;
247+
$(LibrariesNativeArtifactsPath)*.wit;
247248
$(LibrariesNativeArtifactsPath)*.dat;"
248249
IsNative="true" />
249250

eng/testing/linker/SupportFiles/Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Target Name="CreateTestWasmAppBundle"
1111
AfterTargets="Publish"
1212
DependsOnTargets="BundleTestWasmApp"
13-
Condition="'$(TargetArchitecture)' == 'wasm' And '$(TargetOS)' == 'browser'" />
13+
Condition="'$(TargetOS)' == 'browser' or '$(TargetOS)' == 'wasi'" />
1414

1515
<PropertyGroup Condition="'$(PublishAot)' == 'true'">
1616
<_IlcReferencedAsPackage>false</_IlcReferencedAsPackage>

eng/testing/tests.wasi.targets

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
<_XHarnessArgs Condition="'$(IsFunctionalTest)' == 'true'" >$(_XHarnessArgs) --expected-exit-code=$(ExpectedExitCode)</_XHarnessArgs>
4848
<_XHarnessArgs Condition="'$(WasmXHarnessArgs)' != ''" >$(_XHarnessArgs) $(WasmXHarnessArgs)</_XHarnessArgs>
4949
<_XHarnessArgs Condition="'$(WasmXHarnessTestsTimeout)' != ''" >$(_XHarnessArgs) &quot;--timeout=$(WasmXHarnessTestsTimeout)&quot;</_XHarnessArgs>
50-
<_XHarnessArgs >$(_XHarnessArgs) --engine-arg=-W --engine-arg=max-wasm-stack=134217728</_XHarnessArgs>
50+
<_XHarnessArgs >$(_XHarnessArgs) --engine-arg=--wasm --engine-arg=max-wasm-stack=134217728</_XHarnessArgs>
51+
<_XHarnessArgs >$(_XHarnessArgs) --engine-arg=--wasi --engine-arg=http</_XHarnessArgs>
52+
<_XHarnessArgs >$(_XHarnessArgs) --engine-arg=--env --engine-arg=DOTNET_WASI_PRINT_EXIT_CODE=1</_XHarnessArgs>
5153
<_XHarnessArgs Condition="'$(WasmXHarnessArgsCli)' != ''" >$(_XHarnessArgs) $(WasmXHarnessArgsCli)</_XHarnessArgs>
5254

5355
<_InvariantGlobalization Condition="'$(InvariantGlobalization)' == 'true'">--env=DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true</_InvariantGlobalization>

src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
<PlatformManifestFileEntry Include="stubs.c" IsNative="true" />
271271
<PlatformManifestFileEntry Include="synthetic-pthread.c" IsNative="true" />
272272
<PlatformManifestFileEntry Include="dotnet.wasm" IsNative="true" />
273+
<PlatformManifestFileEntry Include="WasiHttpWorld_component_type.wit" IsNative="true" />
273274
<!-- ICU-specific files -->
274275
<PlatformManifestFileEntry Include="libicudata.a" IsNative="true" />
275276
<PlatformManifestFileEntry Include="libicui18n.a" IsNative="true" />

src/libraries/Common/tests/System/Net/Prerequisites/LocalEchoServer.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
2-
<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
2+
<PropertyGroup Condition="'$(TargetOS)' == 'browser' or '$(TargetOS)' == 'wasi'">
33
<_TargetFrameworkForXHarness>$(AspNetCoreAppCurrent)</_TargetFrameworkForXHarness>
44
<HelixTargetsFile>$(MSBuildThisFileDirectory)LocalEchoServer.helix.targets</HelixTargetsFile>
55

@@ -19,7 +19,7 @@
1919
<WasmXHarnessArgs>$(WasmXHarnessArgs) --web-server-middleware=$(_TestEchoMiddleware)/NetCoreServer.dll,NetCoreServer.GenericHandler</WasmXHarnessArgs>
2020
</PropertyGroup>
2121

22-
<ItemGroup Condition="'$(TargetOS)' == 'browser'">
22+
<ItemGroup Condition="'$(TargetOS)' == 'browser' or '$(TargetOS)' == 'wasi'">
2323
<!-- The middleware doesn't need to be built for browser-wasm, so remove the relevant properties.
2424
Also, due to https://github.com/dotnet/runtime/issues/77707 RunAOTCompilation needs to be removed
2525
as a workaround. -->

src/libraries/System.Net.Http/src/System.Net.Http.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,22 @@
455455
Link="Common\System\Net\Http\HttpHandlerDefaults.cs" />
456456
</ItemGroup>
457457

458+
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'wasi'">
459+
<Reference Include="System.Threading.Thread" />
460+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpHandler.cs" />
461+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttp.cs" />
462+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpWorld.wit.imports.wasi.clocks.v0_2_0.MonotonicClockInterop.cs" />
463+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.cs" />
464+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpWorld.wit.imports.wasi.http.v0_2_0.OutgoingHandlerInterop.cs" />
465+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpWorld.wit.imports.wasi.http.v0_2_0.TypesInterop.cs" />
466+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpWorld.wit.imports.wasi.io.v0_2_0.ErrorInterop.cs" />
467+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.cs" />
468+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.cs" />
469+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.cs" />
470+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpWorld.wit.imports.wasi.io.v0_2_0.PollInterop.cs" />
471+
<Compile Include="System\Net\Http\WasiHttpHandler\WasiHttpWorld.wit.imports.wasi.io.v0_2_0.StreamsInterop.cs" />
472+
</ItemGroup>
473+
458474
<ItemGroup>
459475
<Reference Include="Microsoft.Win32.Primitives" />
460476
<Reference Include="System.Collections.NonGeneric" />

src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
using System.Threading;
1111
using System.Threading.Tasks;
1212
using System.Diagnostics.Metrics;
13-
#if TARGET_BROWSER
13+
#if TARGET_WASI
14+
using System.Diagnostics;
15+
using System.Net.Http.Metrics;
16+
using HttpHandlerType = System.Net.Http.WasiHttpHandler;
17+
#elif TARGET_BROWSER
1418
using System.Diagnostics;
1519
using System.Net.Http.Metrics;
1620
using HttpHandlerType = System.Net.Http.BrowserHttpHandler;
@@ -24,7 +28,7 @@ public partial class HttpClientHandler : HttpMessageHandler
2428
{
2529
private readonly HttpHandlerType _underlyingHandler;
2630

27-
#if TARGET_BROWSER
31+
#if TARGET_BROWSER || TARGET_WASI
2832
private IMeterFactory? _meterFactory;
2933
private HttpMessageHandler? _firstHandler; // DiagnosticsHandler or MetricsHandler, depending on global configuration.
3034

@@ -94,7 +98,7 @@ protected override void Dispose(bool disposing)
9498
[CLSCompliant(false)]
9599
public IMeterFactory? MeterFactory
96100
{
97-
#if TARGET_BROWSER
101+
#if TARGET_BROWSER || TARGET_WASI
98102
get => _meterFactory;
99103
set
100104
{
@@ -262,14 +266,14 @@ public ClientCertificateOption ClientCertificateOptions
262266
switch (value)
263267
{
264268
case ClientCertificateOption.Manual:
265-
#if !TARGET_BROWSER
269+
#if !(TARGET_BROWSER || TARGET_WASI)
266270
ThrowForModifiedManagedSslOptionsIfStarted();
267271
_underlyingHandler.SslOptions.LocalCertificateSelectionCallback = (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => CertificateHelper.GetEligibleClientCertificate(_underlyingHandler.SslOptions.ClientCertificates)!;
268272
#endif
269273
break;
270274

271275
case ClientCertificateOption.Automatic:
272-
#if !TARGET_BROWSER
276+
#if !(TARGET_BROWSER || TARGET_WASI)
273277
ThrowForModifiedManagedSslOptionsIfStarted();
274278
_underlyingHandler.SslOptions.LocalCertificateSelectionCallback = (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => CertificateHelper.GetEligibleClientCertificate()!;
275279
#endif
@@ -300,7 +304,7 @@ public X509CertificateCollection ClientCertificates
300304
[UnsupportedOSPlatform("browser")]
301305
public Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool>? ServerCertificateCustomValidationCallback
302306
{
303-
#if TARGET_BROWSER
307+
#if TARGET_BROWSER || TARGET_WASI
304308
get => throw new PlatformNotSupportedException();
305309
set => throw new PlatformNotSupportedException();
306310
#else
@@ -349,7 +353,7 @@ public SslProtocols SslProtocols
349353
//[UnsupportedOSPlatform("tvos")]
350354
protected internal override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
351355
{
352-
#if TARGET_BROWSER
356+
#if TARGET_BROWSER || TARGET_WASI
353357
throw new PlatformNotSupportedException();
354358
#else
355359
ArgumentNullException.ThrowIfNull(request);
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT!
2+
// <auto-generated />
3+
#nullable enable
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
using System.Collections;
7+
using System.Runtime.InteropServices;
8+
using System.Text;
9+
using System.Collections.Generic;
10+
using System.Diagnostics;
11+
using System.Diagnostics.CodeAnalysis;
12+
13+
namespace WasiHttpWorld {
14+
15+
internal interface IWasiHttpWorld {
16+
}
17+
18+
internal readonly struct None {}
19+
20+
[StructLayout(LayoutKind.Sequential)]
21+
internal readonly struct Result<Ok, Err>
22+
{
23+
internal readonly byte Tag;
24+
private readonly object value;
25+
26+
private Result(byte tag, object value)
27+
{
28+
Tag = tag;
29+
this.value = value;
30+
}
31+
32+
internal static Result<Ok, Err> ok(Ok ok)
33+
{
34+
return new Result<Ok, Err>(OK, ok!);
35+
}
36+
37+
internal static Result<Ok, Err> err(Err err)
38+
{
39+
return new Result<Ok, Err>(ERR, err!);
40+
}
41+
42+
internal bool IsOk => Tag == OK;
43+
internal bool IsErr => Tag == ERR;
44+
45+
internal Ok AsOk
46+
{
47+
get
48+
{
49+
if (Tag == OK)
50+
return (Ok)value;
51+
else
52+
throw new ArgumentException("expected OK, got " + Tag);
53+
}
54+
}
55+
56+
internal Err AsErr
57+
{
58+
get
59+
{
60+
if (Tag == ERR)
61+
return (Err)value;
62+
else
63+
throw new ArgumentException("expected ERR, got " + Tag);
64+
}
65+
}
66+
67+
internal const byte OK = 0;
68+
internal const byte ERR = 1;
69+
}
70+
71+
internal class Option<T> {
72+
private static Option<T> none = new ();
73+
74+
private Option()
75+
{
76+
HasValue = false;
77+
}
78+
79+
internal Option(T v)
80+
{
81+
HasValue = true;
82+
Value = v;
83+
}
84+
85+
internal static Option<T> None => none;
86+
87+
[MemberNotNullWhen(true, nameof(Value))]
88+
internal bool HasValue { get; }
89+
90+
internal T? Value { get; }
91+
}
92+
93+
internal static class InteropString
94+
{
95+
internal static IntPtr FromString(string input, out int length)
96+
{
97+
var utf8Bytes = Encoding.UTF8.GetBytes(input);
98+
length = utf8Bytes.Length;
99+
var gcHandle = GCHandle.Alloc(utf8Bytes, GCHandleType.Pinned);
100+
return gcHandle.AddrOfPinnedObject();
101+
}
102+
}
103+
104+
internal class WitException: Exception {
105+
internal object Value { get; }
106+
internal uint NestingLevel { get; }
107+
108+
internal WitException(object v, uint level)
109+
{
110+
Value = v;
111+
NestingLevel = level;
112+
}
113+
}
114+
115+
namespace exports {
116+
internal static class WasiHttpWorld
117+
{
118+
}
119+
}
120+
121+
}

0 commit comments

Comments
 (0)