diff --git a/eng/liveBuilds.targets b/eng/liveBuilds.targets index db8aee3799b44..514b61cc1b91a 100644 --- a/eng/liveBuilds.targets +++ b/eng/liveBuilds.targets @@ -244,6 +244,7 @@ diff --git a/eng/testing/linker/SupportFiles/Directory.Build.targets b/eng/testing/linker/SupportFiles/Directory.Build.targets index 6a43ace84f6f5..1ba8ed737f4df 100644 --- a/eng/testing/linker/SupportFiles/Directory.Build.targets +++ b/eng/testing/linker/SupportFiles/Directory.Build.targets @@ -10,7 +10,7 @@ + Condition="'$(TargetOS)' == 'browser' or '$(TargetOS)' == 'wasi'" /> <_IlcReferencedAsPackage>false diff --git a/eng/testing/tests.wasi.targets b/eng/testing/tests.wasi.targets index 47d68ce7abe6f..e316863525fdc 100644 --- a/eng/testing/tests.wasi.targets +++ b/eng/testing/tests.wasi.targets @@ -47,7 +47,9 @@ <_XHarnessArgs Condition="'$(IsFunctionalTest)' == 'true'" >$(_XHarnessArgs) --expected-exit-code=$(ExpectedExitCode) <_XHarnessArgs Condition="'$(WasmXHarnessArgs)' != ''" >$(_XHarnessArgs) $(WasmXHarnessArgs) <_XHarnessArgs Condition="'$(WasmXHarnessTestsTimeout)' != ''" >$(_XHarnessArgs) "--timeout=$(WasmXHarnessTestsTimeout)" - <_XHarnessArgs >$(_XHarnessArgs) --engine-arg=-W --engine-arg=max-wasm-stack=134217728 + <_XHarnessArgs >$(_XHarnessArgs) --engine-arg=--wasm --engine-arg=max-wasm-stack=134217728 + <_XHarnessArgs >$(_XHarnessArgs) --engine-arg=--wasi --engine-arg=http + <_XHarnessArgs >$(_XHarnessArgs) --engine-arg=--env --engine-arg=DOTNET_WASI_PRINT_EXIT_CODE=1 <_XHarnessArgs Condition="'$(WasmXHarnessArgsCli)' != ''" >$(_XHarnessArgs) $(WasmXHarnessArgsCli) <_InvariantGlobalization Condition="'$(InvariantGlobalization)' == 'true'">--env=DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true diff --git a/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props b/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props index 69fbf0e5d44db..c38417c2af449 100644 --- a/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props +++ b/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props @@ -270,6 +270,7 @@ + diff --git a/src/libraries/Common/tests/System/Net/Prerequisites/LocalEchoServer.props b/src/libraries/Common/tests/System/Net/Prerequisites/LocalEchoServer.props index 53fe2308aa545..fe97a98899d19 100644 --- a/src/libraries/Common/tests/System/Net/Prerequisites/LocalEchoServer.props +++ b/src/libraries/Common/tests/System/Net/Prerequisites/LocalEchoServer.props @@ -1,5 +1,5 @@ - + <_TargetFrameworkForXHarness>$(AspNetCoreAppCurrent) $(MSBuildThisFileDirectory)LocalEchoServer.helix.targets @@ -19,7 +19,7 @@ $(WasmXHarnessArgs) --web-server-middleware=$(_TestEchoMiddleware)/NetCoreServer.dll,NetCoreServer.GenericHandler - + diff --git a/src/libraries/System.Net.Http/src/System.Net.Http.csproj b/src/libraries/System.Net.Http/src/System.Net.Http.csproj index 5a824c7c5aa7c..5c5f3a96bc579 100644 --- a/src/libraries/System.Net.Http/src/System.Net.Http.csproj +++ b/src/libraries/System.Net.Http/src/System.Net.Http.csproj @@ -455,6 +455,22 @@ Link="Common\System\Net\Http\HttpHandlerDefaults.cs" /> + + + + + + + + + + + + + + + + diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs index 5a34491ab4c9f..afba32455bc2f 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs @@ -10,7 +10,11 @@ using System.Threading; using System.Threading.Tasks; using System.Diagnostics.Metrics; -#if TARGET_BROWSER +#if TARGET_WASI +using System.Diagnostics; +using System.Net.Http.Metrics; +using HttpHandlerType = System.Net.Http.WasiHttpHandler; +#elif TARGET_BROWSER using System.Diagnostics; using System.Net.Http.Metrics; using HttpHandlerType = System.Net.Http.BrowserHttpHandler; @@ -24,7 +28,7 @@ public partial class HttpClientHandler : HttpMessageHandler { private readonly HttpHandlerType _underlyingHandler; -#if TARGET_BROWSER +#if TARGET_BROWSER || TARGET_WASI private IMeterFactory? _meterFactory; private HttpMessageHandler? _firstHandler; // DiagnosticsHandler or MetricsHandler, depending on global configuration. @@ -94,7 +98,7 @@ protected override void Dispose(bool disposing) [CLSCompliant(false)] public IMeterFactory? MeterFactory { -#if TARGET_BROWSER +#if TARGET_BROWSER || TARGET_WASI get => _meterFactory; set { @@ -262,14 +266,14 @@ public ClientCertificateOption ClientCertificateOptions switch (value) { case ClientCertificateOption.Manual: -#if !TARGET_BROWSER +#if !(TARGET_BROWSER || TARGET_WASI) ThrowForModifiedManagedSslOptionsIfStarted(); _underlyingHandler.SslOptions.LocalCertificateSelectionCallback = (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => CertificateHelper.GetEligibleClientCertificate(_underlyingHandler.SslOptions.ClientCertificates)!; #endif break; case ClientCertificateOption.Automatic: -#if !TARGET_BROWSER +#if !(TARGET_BROWSER || TARGET_WASI) ThrowForModifiedManagedSslOptionsIfStarted(); _underlyingHandler.SslOptions.LocalCertificateSelectionCallback = (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => CertificateHelper.GetEligibleClientCertificate()!; #endif @@ -300,7 +304,7 @@ public X509CertificateCollection ClientCertificates [UnsupportedOSPlatform("browser")] public Func? ServerCertificateCustomValidationCallback { -#if TARGET_BROWSER +#if TARGET_BROWSER || TARGET_WASI get => throw new PlatformNotSupportedException(); set => throw new PlatformNotSupportedException(); #else @@ -349,7 +353,7 @@ public SslProtocols SslProtocols //[UnsupportedOSPlatform("tvos")] protected internal override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) { -#if TARGET_BROWSER +#if TARGET_BROWSER || TARGET_WASI throw new PlatformNotSupportedException(); #else ArgumentNullException.ThrowIfNull(request); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttp.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttp.cs new file mode 100644 index 0000000000000..0ad976a3bb7be --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttp.cs @@ -0,0 +1,121 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld { + + internal interface IWasiHttpWorld { + } + + internal readonly struct None {} + + [StructLayout(LayoutKind.Sequential)] + internal readonly struct Result + { + internal readonly byte Tag; + private readonly object value; + + private Result(byte tag, object value) + { + Tag = tag; + this.value = value; + } + + internal static Result ok(Ok ok) + { + return new Result(OK, ok!); + } + + internal static Result err(Err err) + { + return new Result(ERR, err!); + } + + internal bool IsOk => Tag == OK; + internal bool IsErr => Tag == ERR; + + internal Ok AsOk + { + get + { + if (Tag == OK) + return (Ok)value; + else + throw new ArgumentException("expected OK, got " + Tag); + } + } + + internal Err AsErr + { + get + { + if (Tag == ERR) + return (Err)value; + else + throw new ArgumentException("expected ERR, got " + Tag); + } + } + + internal const byte OK = 0; + internal const byte ERR = 1; + } + + internal class Option { + private static Option none = new (); + + private Option() + { + HasValue = false; + } + + internal Option(T v) + { + HasValue = true; + Value = v; + } + + internal static Option None => none; + + [MemberNotNullWhen(true, nameof(Value))] + internal bool HasValue { get; } + + internal T? Value { get; } + } + + internal static class InteropString + { + internal static IntPtr FromString(string input, out int length) + { + var utf8Bytes = Encoding.UTF8.GetBytes(input); + length = utf8Bytes.Length; + var gcHandle = GCHandle.Alloc(utf8Bytes, GCHandleType.Pinned); + return gcHandle.AddrOfPinnedObject(); + } + } + + internal class WitException: Exception { + internal object Value { get; } + internal uint NestingLevel { get; } + + internal WitException(object v, uint level) + { + Value = v; + NestingLevel = level; + } + } + + namespace exports { + internal static class WasiHttpWorld + { + } + } + +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpHandler.cs new file mode 100644 index 0000000000000..ea6d9e600dadb --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpHandler.cs @@ -0,0 +1,747 @@ +// 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.Collections; +using System.Collections.Generic; +using System.IO; +using System.Net.Http.Headers; +using System.Net.Security; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using WasiHttpWorld; +using WasiHttpWorld.wit.imports.wasi.http.v0_2_0; +using WasiHttpWorld.wit.imports.wasi.io.v0_2_0; + +namespace System.Net.Http +{ + internal sealed class WasiHttpHandler : HttpMessageHandler + { + #region PlatformNotSupported +#pragma warning disable CA1822 + public bool UseCookies + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public CookieContainer CookieContainer + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public DecompressionMethods AutomaticDecompression + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public bool UseProxy + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public IWebProxy? Proxy + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public ICredentials? DefaultProxyCredentials + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public bool PreAuthenticate + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public ICredentials? Credentials + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public int MaxAutomaticRedirections + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public int MaxConnectionsPerServer + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public int MaxResponseHeadersLength + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public SslClientAuthenticationOptions SslOptions + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public bool AllowAutoRedirect + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } +#pragma warning restore CA1822 + #endregion + + internal ClientCertificateOption ClientCertificateOptions; + + public const bool SupportsAutomaticDecompression = false; + public const bool SupportsProxy = false; + public const bool SupportsRedirectConfiguration = false; + + private Dictionary? _properties; + public IDictionary Properties => + _properties ??= new Dictionary(); + + protected internal override HttpResponseMessage Send( + HttpRequestMessage request, + CancellationToken cancellationToken + ) + { + throw new PlatformNotSupportedException(); + } + + protected internal override async Task SendAsync( + HttpRequestMessage request, + CancellationToken cancellationToken + ) + { + if (request.RequestUri is null) + { + throw new ArgumentException(); + } + + var requestMethod = request.Method.ToString(); + var uri = request.RequestUri; + + ITypes.Method method; + switch (requestMethod) + { + case "": + case "GET": + method = ITypes.Method.get(); + break; + case "HEAD": + method = ITypes.Method.head(); + break; + case "POST": + method = ITypes.Method.post(); + break; + case "PUT": + method = ITypes.Method.put(); + break; + case "DELETE": + method = ITypes.Method.delete(); + break; + case "CONNECT": + method = ITypes.Method.connect(); + break; + case "OPTIONS": + method = ITypes.Method.options(); + break; + case "TRACE": + method = ITypes.Method.trace(); + break; + case "PATCH": + method = ITypes.Method.patch(); + break; + default: + method = ITypes.Method.other(requestMethod); + break; + } + + ITypes.Scheme scheme; + switch (uri.Scheme) + { + case "": + case "http": + scheme = ITypes.Scheme.http(); + break; + case "https": + scheme = ITypes.Scheme.https(); + break; + default: + scheme = ITypes.Scheme.other(uri.Scheme); + break; + } + + string authority; + if (uri.Authority.Length == 0) + { + // `wasi:http/outgoing-handler` requires a non-empty authority, + // so we set one here: + if (scheme.Tag == ITypes.Scheme.HTTPS) + { + authority = ":443"; + } + else + { + authority = ":80"; + } + } + else + { + authority = uri.Authority; + } + + var headers = new List<(string, byte[])>(); + foreach (var pair in request.Headers) + { + foreach (var value in pair.Value) + { + headers.Add((pair.Key, Encoding.UTF8.GetBytes(value))); + } + } + if (request.Content is not null) + { + foreach (var pair in request.Content.Headers) + { + foreach (var value in pair.Value) + { + headers.Add((pair.Key, Encoding.UTF8.GetBytes(value))); + } + } + } + + var outgoingRequest = new ITypes.OutgoingRequest(ITypes.Fields.FromList(headers)); + outgoingRequest.SetMethod(method); + outgoingRequest.SetScheme(scheme); + outgoingRequest.SetAuthority(authority); + outgoingRequest.SetPathWithQuery(uri.PathAndQuery); + + var outgoingStream = new OutputStream(outgoingRequest.Body()); + + Func> sendContent = async () => + { + await SendContentAsync(request.Content, outgoingStream).ConfigureAwait(false); + return null; + }; + + // Concurrently send the request and the content stream, allowing + // the server to start sending a response before it's received the + // entire request body. + var incomingResponse = ( + await Task.WhenAll( + new Task[] + { + SendRequestAsync(outgoingRequest), + sendContent() + } + ) + .ConfigureAwait(false) + )[0]; + + if (incomingResponse is null) + { + // Shouldn't be possible, since `SendRequestAsync` always + // returns a non-null value. + throw new Exception("unreachable code"); + } + + var response = new HttpResponseMessage((HttpStatusCode)incomingResponse.Status()); + var responseHeaders = incomingResponse.Headers().Entries(); + response.Content = new StreamContent(new InputStream(incomingResponse.Consume())); + foreach ((var key, var value) in responseHeaders) + { + var valueString = Encoding.UTF8.GetString(value); + if ( + HeaderDescriptor.TryGet(key, out HeaderDescriptor descriptor) + && (descriptor.HeaderType & HttpHeaderType.Content) != 0 + ) + { + response.Content.Headers.Add(key, valueString); + } + else + { + response.Headers.Add(key, valueString); + } + } + + return response; + } + + private static async Task SendRequestAsync( + ITypes.OutgoingRequest request + ) + { + ITypes.FutureIncomingResponse future; + try + { + future = OutgoingHandlerInterop.Handle(request, null); + } + catch (WasiHttpWorld.WitException e) + { + var message = ErrorCodeToString((ITypes.ErrorCode)e.Value); + throw new Exception($"Request Error: {message}"); + } + + while (true) + { + var response = future.Get(); + if (response is not null) + { + var result = ( + (Result, None>)response + ).AsOk; + + if (result.IsOk) + { + return result.AsOk; + } + else + { + var message = ErrorCodeToString(result.AsErr); + throw new Exception($"Request Error: {message}"); + } + } + else + { + await WasiEventLoop.RegisterWasiPollable(future.Subscribe()).ConfigureAwait(false); + } + } + } + + private static string ErrorCodeToString(ITypes.ErrorCode code) + { + // TODO: include payload data in result where applicable + switch (code.Tag) + { + case ITypes.ErrorCode.DNS_TIMEOUT: + return "DNS_TIMEOUT"; + + case ITypes.ErrorCode.DNS_ERROR: + return "DNS_ERROR"; + + case ITypes.ErrorCode.DESTINATION_NOT_FOUND: + return "DESTINATION_NOT_FOUND"; + + case ITypes.ErrorCode.DESTINATION_UNAVAILABLE: + return "DESTINATION_UNAVAILABLE"; + + case ITypes.ErrorCode.DESTINATION_IP_PROHIBITED: + return "DESTINATION_IP_PROHIBITED"; + + case ITypes.ErrorCode.DESTINATION_IP_UNROUTABLE: + return "DESTINATION_IP_UNROUTABLE"; + + case ITypes.ErrorCode.CONNECTION_REFUSED: + return "CONNECTION_REFUSED"; + + case ITypes.ErrorCode.CONNECTION_TERMINATED: + return "CONNECTION_TERMINATED"; + + case ITypes.ErrorCode.CONNECTION_TIMEOUT: + return "CONNECTION_TIMEOUT"; + + case ITypes.ErrorCode.CONNECTION_READ_TIMEOUT: + return "CONNECTION_READ_TIMEOUT"; + + case ITypes.ErrorCode.CONNECTION_WRITE_TIMEOUT: + return "CONNECTION_WRITE_TIMEOUT"; + + case ITypes.ErrorCode.CONNECTION_LIMIT_REACHED: + return "CONNECTION_LIMIT_REACHED"; + + case ITypes.ErrorCode.TLS_PROTOCOL_ERROR: + return "TLS_PROTOCOL_ERROR"; + + case ITypes.ErrorCode.TLS_CERTIFICATE_ERROR: + return "TLS_CERTIFICATE_ERROR"; + + case ITypes.ErrorCode.TLS_ALERT_RECEIVED: + return "TLS_ALERT_RECEIVED"; + + case ITypes.ErrorCode.HTTP_REQUEST_DENIED: + return "HTTP_REQUEST_DENIED"; + + case ITypes.ErrorCode.HTTP_REQUEST_LENGTH_REQUIRED: + return "HTTP_REQUEST_LENGTH_REQUIRED"; + + case ITypes.ErrorCode.HTTP_REQUEST_BODY_SIZE: + return "HTTP_REQUEST_BODY_SIZE"; + + case ITypes.ErrorCode.HTTP_REQUEST_METHOD_INVALID: + return "HTTP_REQUEST_METHOD_INVALID"; + + case ITypes.ErrorCode.HTTP_REQUEST_URI_INVALID: + return "HTTP_REQUEST_URI_INVALID"; + + case ITypes.ErrorCode.HTTP_REQUEST_URI_TOO_LONG: + return "HTTP_REQUEST_URI_TOO_LONG"; + + case ITypes.ErrorCode.HTTP_REQUEST_HEADER_SECTION_SIZE: + return "HTTP_REQUEST_HEADER_SECTION_SIZE"; + + case ITypes.ErrorCode.HTTP_REQUEST_HEADER_SIZE: + return "HTTP_REQUEST_HEADER_SIZE"; + + case ITypes.ErrorCode.HTTP_REQUEST_TRAILER_SECTION_SIZE: + return "HTTP_REQUEST_TRAILER_SECTION_SIZE"; + + case ITypes.ErrorCode.HTTP_REQUEST_TRAILER_SIZE: + return "HTTP_REQUEST_TRAILER_SIZE"; + + case ITypes.ErrorCode.HTTP_RESPONSE_INCOMPLETE: + return "HTTP_RESPONSE_INCOMPLETE"; + + case ITypes.ErrorCode.HTTP_RESPONSE_HEADER_SECTION_SIZE: + return "HTTP_RESPONSE_HEADER_SECTION_SIZE"; + + case ITypes.ErrorCode.HTTP_RESPONSE_HEADER_SIZE: + return "HTTP_RESPONSE_HEADER_SIZE"; + + case ITypes.ErrorCode.HTTP_RESPONSE_BODY_SIZE: + return "HTTP_RESPONSE_BODY_SIZE"; + + case ITypes.ErrorCode.HTTP_RESPONSE_TRAILER_SECTION_SIZE: + return "HTTP_RESPONSE_TRAILER_SECTION_SIZE"; + + case ITypes.ErrorCode.HTTP_RESPONSE_TRAILER_SIZE: + return "HTTP_RESPONSE_TRAILER_SIZE"; + + case ITypes.ErrorCode.HTTP_RESPONSE_TRANSFER_CODING: + return "HTTP_RESPONSE_TRANSFER_CODING"; + + case ITypes.ErrorCode.HTTP_RESPONSE_CONTENT_CODING: + return "HTTP_RESPONSE_CONTENT_CODING"; + + case ITypes.ErrorCode.HTTP_RESPONSE_TIMEOUT: + return "HTTP_RESPONSE_TIMEOUT"; + + case ITypes.ErrorCode.HTTP_UPGRADE_FAILED: + return "HTTP_UPGRADE_FAILED"; + + case ITypes.ErrorCode.HTTP_PROTOCOL_ERROR: + return "HTTP_PROTOCOL_ERROR"; + + case ITypes.ErrorCode.LOOP_DETECTED: + return "LOOP_DETECTED"; + + case ITypes.ErrorCode.CONFIGURATION_ERROR: + return "CONFIGURATION_ERROR"; + + case ITypes.ErrorCode.INTERNAL_ERROR: + return "INTERNAL_ERROR"; + + default: + return $"{code.Tag}"; + } + } + + private static async Task SendContentAsync(HttpContent? content, Stream stream) + { + try + { + if (content is not null) + { + await content.CopyToAsync(stream).ConfigureAwait(false); + } + } + finally + { + stream.Dispose(); + } + } + + private static class WasiEventLoop + { + internal static Task RegisterWasiPollable(IPoll.Pollable pollable) + { + var handle = pollable.Handle; + pollable.Handle = 0; + return CallRegisterWasiPollable((Thread)null!, handle); + + [UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "RegisterWasiPollable")] + static extern Task CallRegisterWasiPollable(Thread t, int handle); + } + } + + private sealed class InputStream : Stream + { + private ITypes.IncomingBody body; + private IStreams.InputStream stream; + private int offset; + private byte[]? buffer; + private bool closed; + + public InputStream(ITypes.IncomingBody body) + { + this.body = body; + this.stream = body.Stream(); + } + + ~InputStream() + { + Dispose(false); + } + + public override bool CanRead => true; + public override bool CanWrite => false; + public override bool CanSeek => false; + public override long Length => throw new NotImplementedException(); + public override long Position + { + get => throw new NotImplementedException(); + set => throw new NotImplementedException(); + } + + protected override void Dispose(bool disposing) + { + stream.Dispose(); + ITypes.IncomingBody.Finish(body); + } + + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotImplementedException(); + } + + public override void Flush() + { + // ignore + } + + public override void SetLength(long length) + { + throw new NotImplementedException(); + } + + public override int Read(byte[] buffer, int offset, int length) + { + throw new NotImplementedException(); + } + + public override void Write(byte[] buffer, int offset, int length) + { + throw new NotImplementedException(); + } + + public override async Task ReadAsync( + byte[] bytes, + int offset, + int length, + CancellationToken cancellationToken + ) + { + // TODO: handle `cancellationToken` + while (true) + { + if (closed) + { + return 0; + } + else if (this.buffer == null) + { + try + { + // TODO: should we add a special case to the bindings generator + // to allow passing a buffer to IStreams.InputStream.Read and + // avoid the extra copy? + var result = stream.Read(16 * 1024); + var buffer = result; + if (buffer.Length == 0) + { + await WasiEventLoop + .RegisterWasiPollable(stream.Subscribe()) + .ConfigureAwait(false); + } + else + { + this.buffer = buffer; + this.offset = 0; + } + } + catch (WitException e) + { + if (((IStreams.StreamError)e.Value).Tag == IStreams.StreamError.CLOSED) + { + closed = true; + return 0; + } + else + { + throw e; + } + } + } + else + { + var min = Math.Min(this.buffer.Length - this.offset, length); + Array.Copy(this.buffer, this.offset, bytes, offset, min); + if (min < buffer.Length - this.offset) + { + this.offset += min; + } + else + { + this.buffer = null; + } + return min; + } + } + } + + public override async ValueTask ReadAsync( + Memory buffer, + CancellationToken cancellationToken = default + ) + { + // TODO: avoid copy when possible and use ArrayPool when not + var dst = new byte[buffer.Length]; + // We disable "CA1835: Prefer the memory-based overloads of + // ReadAsync/WriteAsync methods in stream-based classes" for + // now, since `ReadyAsync(byte[], int, int, CancellationToken)` + // is where the implementation currently resides, but we should + // revisit this if/when `wit-bindgen` learns to generate + // memory-based bindings. +#pragma warning disable CA1835 + var result = await ReadAsync(dst, 0, buffer.Length, cancellationToken) + .ConfigureAwait(false); +#pragma warning restore CA1835 + new ReadOnlySpan(dst, 0, result).CopyTo(buffer.Span); + return result; + } + } + + private sealed class OutputStream : Stream + { + private ITypes.OutgoingBody body; + private IStreams.OutputStream stream; + + public OutputStream(ITypes.OutgoingBody body) + { + this.body = body; + this.stream = body.Write(); + } + + ~OutputStream() + { + Dispose(false); + } + + public override bool CanRead => false; + public override bool CanWrite => true; + public override bool CanSeek => false; + public override long Length => throw new NotImplementedException(); + public override long Position + { + get => throw new NotImplementedException(); + set => throw new NotImplementedException(); + } + + protected override void Dispose(bool disposing) + { + stream.Dispose(); + ITypes.OutgoingBody.Finish(body, null); + } + + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotImplementedException(); + } + + public override void Flush() + { + // ignore + // + // Note that flushing a `wasi:io/streams/output-stream` is an + // asynchronous operation, so it's not clear how we would + // implement it here instead of taking care of it as part of + // `WriteAsync`. + } + + public override void SetLength(long length) + { + throw new NotImplementedException(); + } + + public override int Read(byte[] buffer, int offset, int length) + { + throw new NotImplementedException(); + } + + public override void Write(byte[] buffer, int offset, int length) + { + throw new NotImplementedException(); + } + + public override async Task WriteAsync( + byte[] bytes, + int offset, + int length, + CancellationToken cancellationToken + ) + { + var limit = offset + length; + var flushing = false; + while (true) + { + var count = (int)stream.CheckWrite(); + if (count == 0) + { + await WasiEventLoop.RegisterWasiPollable(stream.Subscribe()).ConfigureAwait(false); + } + else if (offset == limit) + { + if (flushing) + { + return; + } + else + { + stream.Flush(); + flushing = true; + } + } + else + { + var min = Math.Min(count, limit - offset); + if (offset == 0 && min == bytes.Length) + { + stream.Write(bytes); + } + else + { + // TODO: is there a more efficient option than copying here? + // Do we need to change the binding generator to accept + // e.g. `Span`s? + var copy = new byte[min]; + Array.Copy(bytes, offset, copy, 0, min); + stream.Write(copy); + } + offset += min; + } + } + } + + public override ValueTask WriteAsync( + ReadOnlyMemory buffer, + CancellationToken cancellationToken = default + ) + { + // TODO: avoid copy when possible and use ArrayPool when not + var copy = new byte[buffer.Length]; + buffer.Span.CopyTo(copy); + return new ValueTask(WriteAsync(copy, 0, buffer.Length, cancellationToken)); + } + } + } +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.clocks.v0_2_0.MonotonicClockInterop.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.clocks.v0_2_0.MonotonicClockInterop.cs new file mode 100644 index 0000000000000..cb17593768647 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.clocks.v0_2_0.MonotonicClockInterop.cs @@ -0,0 +1,81 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld.wit.imports.wasi.clocks.v0_2_0 +{ + internal static class MonotonicClockInterop { + + internal static class NowWasmInterop + { + [DllImport("wasi:clocks/monotonic-clock@0.2.0", EntryPoint = "now"), WasmImportLinkage] + internal static extern long wasmImportNow(); + + } + + internal static unsafe ulong Now() + { + var result = NowWasmInterop.wasmImportNow(); + return unchecked((ulong)(result)); + + //TODO: free alloc handle (interopString) if exists + } + + internal static class ResolutionWasmInterop + { + [DllImport("wasi:clocks/monotonic-clock@0.2.0", EntryPoint = "resolution"), WasmImportLinkage] + internal static extern long wasmImportResolution(); + + } + + internal static unsafe ulong Resolution() + { + var result = ResolutionWasmInterop.wasmImportResolution(); + return unchecked((ulong)(result)); + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SubscribeInstantWasmInterop + { + [DllImport("wasi:clocks/monotonic-clock@0.2.0", EntryPoint = "subscribe-instant"), WasmImportLinkage] + internal static extern int wasmImportSubscribeInstant(long p0); + + } + + internal static unsafe global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable SubscribeInstant(ulong when) + { + var result = SubscribeInstantWasmInterop.wasmImportSubscribeInstant(unchecked((long)(when))); + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SubscribeDurationWasmInterop + { + [DllImport("wasi:clocks/monotonic-clock@0.2.0", EntryPoint = "subscribe-duration"), WasmImportLinkage] + internal static extern int wasmImportSubscribeDuration(long p0); + + } + + internal static unsafe global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable SubscribeDuration(ulong when) + { + var result = SubscribeDurationWasmInterop.wasmImportSubscribeDuration(unchecked((long)(when))); + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + } +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.cs new file mode 100644 index 0000000000000..ddcb79ed49d43 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.cs @@ -0,0 +1,5958 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld.wit.imports.wasi.http.v0_2_0; + +internal interface ITypes { + + /** + * This type corresponds to HTTP standard Methods. + */ + + internal class Method { + internal readonly byte Tag; + private readonly object? value; + + private Method(byte tag, object? value) { + this.Tag = tag; + this.value = value; + } + + internal static Method get() { + return new Method(GET, null); + } + + internal static Method head() { + return new Method(HEAD, null); + } + + internal static Method post() { + return new Method(POST, null); + } + + internal static Method put() { + return new Method(PUT, null); + } + + internal static Method delete() { + return new Method(DELETE, null); + } + + internal static Method connect() { + return new Method(CONNECT, null); + } + + internal static Method options() { + return new Method(OPTIONS, null); + } + + internal static Method trace() { + return new Method(TRACE, null); + } + + internal static Method patch() { + return new Method(PATCH, null); + } + + internal static Method other(string other) { + return new Method(OTHER, other); + } + + internal string AsOther + { + get + { + if (Tag == OTHER) + return (string)value!; + else + throw new ArgumentException("expected OTHER, got " + Tag); + } + } + + internal const byte GET = 0; + internal const byte HEAD = 1; + internal const byte POST = 2; + internal const byte PUT = 3; + internal const byte DELETE = 4; + internal const byte CONNECT = 5; + internal const byte OPTIONS = 6; + internal const byte TRACE = 7; + internal const byte PATCH = 8; + internal const byte OTHER = 9; + } + + /** + * This type corresponds to HTTP standard Related Schemes. + */ + + internal class Scheme { + internal readonly byte Tag; + private readonly object? value; + + private Scheme(byte tag, object? value) { + this.Tag = tag; + this.value = value; + } + + internal static Scheme http() { + return new Scheme(HTTP, null); + } + + internal static Scheme https() { + return new Scheme(HTTPS, null); + } + + internal static Scheme other(string other) { + return new Scheme(OTHER, other); + } + + internal string AsOther + { + get + { + if (Tag == OTHER) + return (string)value!; + else + throw new ArgumentException("expected OTHER, got " + Tag); + } + } + + internal const byte HTTP = 0; + internal const byte HTTPS = 1; + internal const byte OTHER = 2; + } + + /** + * Defines the case payload type for `DNS-error` above: + */ + + internal class DnsErrorPayload { + internal readonly string? rcode; + internal readonly ushort? infoCode; + + internal DnsErrorPayload(string? rcode, ushort? infoCode) { + this.rcode = rcode; + this.infoCode = infoCode; + } + } + + /** + * Defines the case payload type for `TLS-alert-received` above: + */ + + internal class TlsAlertReceivedPayload { + internal readonly byte? alertId; + internal readonly string? alertMessage; + + internal TlsAlertReceivedPayload(byte? alertId, string? alertMessage) { + this.alertId = alertId; + this.alertMessage = alertMessage; + } + } + + /** + * Defines the case payload type for `HTTP-response-{header,trailer}-size` above: + */ + + internal class FieldSizePayload { + internal readonly string? fieldName; + internal readonly uint? fieldSize; + + internal FieldSizePayload(string? fieldName, uint? fieldSize) { + this.fieldName = fieldName; + this.fieldSize = fieldSize; + } + } + + /** + * These cases are inspired by the IANA HTTP Proxy Error Types: + * https://www.iana.org/assignments/http-proxy-status/http-proxy-status.xhtml#table-http-proxy-error-types + */ + + internal class ErrorCode { + internal readonly byte Tag; + private readonly object? value; + + private ErrorCode(byte tag, object? value) { + this.Tag = tag; + this.value = value; + } + + internal static ErrorCode dnsTimeout() { + return new ErrorCode(DNS_TIMEOUT, null); + } + + internal static ErrorCode dnsError(DnsErrorPayload dnsError) { + return new ErrorCode(DNS_ERROR, dnsError); + } + + internal static ErrorCode destinationNotFound() { + return new ErrorCode(DESTINATION_NOT_FOUND, null); + } + + internal static ErrorCode destinationUnavailable() { + return new ErrorCode(DESTINATION_UNAVAILABLE, null); + } + + internal static ErrorCode destinationIpProhibited() { + return new ErrorCode(DESTINATION_IP_PROHIBITED, null); + } + + internal static ErrorCode destinationIpUnroutable() { + return new ErrorCode(DESTINATION_IP_UNROUTABLE, null); + } + + internal static ErrorCode connectionRefused() { + return new ErrorCode(CONNECTION_REFUSED, null); + } + + internal static ErrorCode connectionTerminated() { + return new ErrorCode(CONNECTION_TERMINATED, null); + } + + internal static ErrorCode connectionTimeout() { + return new ErrorCode(CONNECTION_TIMEOUT, null); + } + + internal static ErrorCode connectionReadTimeout() { + return new ErrorCode(CONNECTION_READ_TIMEOUT, null); + } + + internal static ErrorCode connectionWriteTimeout() { + return new ErrorCode(CONNECTION_WRITE_TIMEOUT, null); + } + + internal static ErrorCode connectionLimitReached() { + return new ErrorCode(CONNECTION_LIMIT_REACHED, null); + } + + internal static ErrorCode tlsProtocolError() { + return new ErrorCode(TLS_PROTOCOL_ERROR, null); + } + + internal static ErrorCode tlsCertificateError() { + return new ErrorCode(TLS_CERTIFICATE_ERROR, null); + } + + internal static ErrorCode tlsAlertReceived(TlsAlertReceivedPayload tlsAlertReceived) { + return new ErrorCode(TLS_ALERT_RECEIVED, tlsAlertReceived); + } + + internal static ErrorCode httpRequestDenied() { + return new ErrorCode(HTTP_REQUEST_DENIED, null); + } + + internal static ErrorCode httpRequestLengthRequired() { + return new ErrorCode(HTTP_REQUEST_LENGTH_REQUIRED, null); + } + + internal static ErrorCode httpRequestBodySize(ulong? httpRequestBodySize) { + return new ErrorCode(HTTP_REQUEST_BODY_SIZE, httpRequestBodySize); + } + + internal static ErrorCode httpRequestMethodInvalid() { + return new ErrorCode(HTTP_REQUEST_METHOD_INVALID, null); + } + + internal static ErrorCode httpRequestUriInvalid() { + return new ErrorCode(HTTP_REQUEST_URI_INVALID, null); + } + + internal static ErrorCode httpRequestUriTooLong() { + return new ErrorCode(HTTP_REQUEST_URI_TOO_LONG, null); + } + + internal static ErrorCode httpRequestHeaderSectionSize(uint? httpRequestHeaderSectionSize) { + return new ErrorCode(HTTP_REQUEST_HEADER_SECTION_SIZE, httpRequestHeaderSectionSize); + } + + internal static ErrorCode httpRequestHeaderSize(FieldSizePayload? httpRequestHeaderSize) { + return new ErrorCode(HTTP_REQUEST_HEADER_SIZE, httpRequestHeaderSize); + } + + internal static ErrorCode httpRequestTrailerSectionSize(uint? httpRequestTrailerSectionSize) { + return new ErrorCode(HTTP_REQUEST_TRAILER_SECTION_SIZE, httpRequestTrailerSectionSize); + } + + internal static ErrorCode httpRequestTrailerSize(FieldSizePayload httpRequestTrailerSize) { + return new ErrorCode(HTTP_REQUEST_TRAILER_SIZE, httpRequestTrailerSize); + } + + internal static ErrorCode httpResponseIncomplete() { + return new ErrorCode(HTTP_RESPONSE_INCOMPLETE, null); + } + + internal static ErrorCode httpResponseHeaderSectionSize(uint? httpResponseHeaderSectionSize) { + return new ErrorCode(HTTP_RESPONSE_HEADER_SECTION_SIZE, httpResponseHeaderSectionSize); + } + + internal static ErrorCode httpResponseHeaderSize(FieldSizePayload httpResponseHeaderSize) { + return new ErrorCode(HTTP_RESPONSE_HEADER_SIZE, httpResponseHeaderSize); + } + + internal static ErrorCode httpResponseBodySize(ulong? httpResponseBodySize) { + return new ErrorCode(HTTP_RESPONSE_BODY_SIZE, httpResponseBodySize); + } + + internal static ErrorCode httpResponseTrailerSectionSize(uint? httpResponseTrailerSectionSize) { + return new ErrorCode(HTTP_RESPONSE_TRAILER_SECTION_SIZE, httpResponseTrailerSectionSize); + } + + internal static ErrorCode httpResponseTrailerSize(FieldSizePayload httpResponseTrailerSize) { + return new ErrorCode(HTTP_RESPONSE_TRAILER_SIZE, httpResponseTrailerSize); + } + + internal static ErrorCode httpResponseTransferCoding(string? httpResponseTransferCoding) { + return new ErrorCode(HTTP_RESPONSE_TRANSFER_CODING, httpResponseTransferCoding); + } + + internal static ErrorCode httpResponseContentCoding(string? httpResponseContentCoding) { + return new ErrorCode(HTTP_RESPONSE_CONTENT_CODING, httpResponseContentCoding); + } + + internal static ErrorCode httpResponseTimeout() { + return new ErrorCode(HTTP_RESPONSE_TIMEOUT, null); + } + + internal static ErrorCode httpUpgradeFailed() { + return new ErrorCode(HTTP_UPGRADE_FAILED, null); + } + + internal static ErrorCode httpProtocolError() { + return new ErrorCode(HTTP_PROTOCOL_ERROR, null); + } + + internal static ErrorCode loopDetected() { + return new ErrorCode(LOOP_DETECTED, null); + } + + internal static ErrorCode configurationError() { + return new ErrorCode(CONFIGURATION_ERROR, null); + } + + internal static ErrorCode internalError(string? internalError) { + return new ErrorCode(INTERNAL_ERROR, internalError); + } + + internal DnsErrorPayload AsDnsError + { + get + { + if (Tag == DNS_ERROR) + return (DnsErrorPayload)value!; + else + throw new ArgumentException("expected DNS_ERROR, got " + Tag); + } + } + + internal TlsAlertReceivedPayload AsTlsAlertReceived + { + get + { + if (Tag == TLS_ALERT_RECEIVED) + return (TlsAlertReceivedPayload)value!; + else + throw new ArgumentException("expected TLS_ALERT_RECEIVED, got " + Tag); + } + } + + internal ulong? AsHttpRequestBodySize + { + get + { + if (Tag == HTTP_REQUEST_BODY_SIZE) + return (ulong?)value!; + else + throw new ArgumentException("expected HTTP_REQUEST_BODY_SIZE, got " + Tag); + } + } + + internal uint? AsHttpRequestHeaderSectionSize + { + get + { + if (Tag == HTTP_REQUEST_HEADER_SECTION_SIZE) + return (uint?)value!; + else + throw new ArgumentException("expected HTTP_REQUEST_HEADER_SECTION_SIZE, got " + Tag); + } + } + + internal FieldSizePayload? AsHttpRequestHeaderSize + { + get + { + if (Tag == HTTP_REQUEST_HEADER_SIZE) + return (FieldSizePayload?)value!; + else + throw new ArgumentException("expected HTTP_REQUEST_HEADER_SIZE, got " + Tag); + } + } + + internal uint? AsHttpRequestTrailerSectionSize + { + get + { + if (Tag == HTTP_REQUEST_TRAILER_SECTION_SIZE) + return (uint?)value!; + else + throw new ArgumentException("expected HTTP_REQUEST_TRAILER_SECTION_SIZE, got " + Tag); + } + } + + internal FieldSizePayload AsHttpRequestTrailerSize + { + get + { + if (Tag == HTTP_REQUEST_TRAILER_SIZE) + return (FieldSizePayload)value!; + else + throw new ArgumentException("expected HTTP_REQUEST_TRAILER_SIZE, got " + Tag); + } + } + + internal uint? AsHttpResponseHeaderSectionSize + { + get + { + if (Tag == HTTP_RESPONSE_HEADER_SECTION_SIZE) + return (uint?)value!; + else + throw new ArgumentException("expected HTTP_RESPONSE_HEADER_SECTION_SIZE, got " + Tag); + } + } + + internal FieldSizePayload AsHttpResponseHeaderSize + { + get + { + if (Tag == HTTP_RESPONSE_HEADER_SIZE) + return (FieldSizePayload)value!; + else + throw new ArgumentException("expected HTTP_RESPONSE_HEADER_SIZE, got " + Tag); + } + } + + internal ulong? AsHttpResponseBodySize + { + get + { + if (Tag == HTTP_RESPONSE_BODY_SIZE) + return (ulong?)value!; + else + throw new ArgumentException("expected HTTP_RESPONSE_BODY_SIZE, got " + Tag); + } + } + + internal uint? AsHttpResponseTrailerSectionSize + { + get + { + if (Tag == HTTP_RESPONSE_TRAILER_SECTION_SIZE) + return (uint?)value!; + else + throw new ArgumentException("expected HTTP_RESPONSE_TRAILER_SECTION_SIZE, got " + Tag); + } + } + + internal FieldSizePayload AsHttpResponseTrailerSize + { + get + { + if (Tag == HTTP_RESPONSE_TRAILER_SIZE) + return (FieldSizePayload)value!; + else + throw new ArgumentException("expected HTTP_RESPONSE_TRAILER_SIZE, got " + Tag); + } + } + + internal string? AsHttpResponseTransferCoding + { + get + { + if (Tag == HTTP_RESPONSE_TRANSFER_CODING) + return (string?)value!; + else + throw new ArgumentException("expected HTTP_RESPONSE_TRANSFER_CODING, got " + Tag); + } + } + + internal string? AsHttpResponseContentCoding + { + get + { + if (Tag == HTTP_RESPONSE_CONTENT_CODING) + return (string?)value!; + else + throw new ArgumentException("expected HTTP_RESPONSE_CONTENT_CODING, got " + Tag); + } + } + + internal string? AsInternalError + { + get + { + if (Tag == INTERNAL_ERROR) + return (string?)value!; + else + throw new ArgumentException("expected INTERNAL_ERROR, got " + Tag); + } + } + + internal const byte DNS_TIMEOUT = 0; + internal const byte DNS_ERROR = 1; + internal const byte DESTINATION_NOT_FOUND = 2; + internal const byte DESTINATION_UNAVAILABLE = 3; + internal const byte DESTINATION_IP_PROHIBITED = 4; + internal const byte DESTINATION_IP_UNROUTABLE = 5; + internal const byte CONNECTION_REFUSED = 6; + internal const byte CONNECTION_TERMINATED = 7; + internal const byte CONNECTION_TIMEOUT = 8; + internal const byte CONNECTION_READ_TIMEOUT = 9; + internal const byte CONNECTION_WRITE_TIMEOUT = 10; + internal const byte CONNECTION_LIMIT_REACHED = 11; + internal const byte TLS_PROTOCOL_ERROR = 12; + internal const byte TLS_CERTIFICATE_ERROR = 13; + internal const byte TLS_ALERT_RECEIVED = 14; + internal const byte HTTP_REQUEST_DENIED = 15; + internal const byte HTTP_REQUEST_LENGTH_REQUIRED = 16; + internal const byte HTTP_REQUEST_BODY_SIZE = 17; + internal const byte HTTP_REQUEST_METHOD_INVALID = 18; + internal const byte HTTP_REQUEST_URI_INVALID = 19; + internal const byte HTTP_REQUEST_URI_TOO_LONG = 20; + internal const byte HTTP_REQUEST_HEADER_SECTION_SIZE = 21; + internal const byte HTTP_REQUEST_HEADER_SIZE = 22; + internal const byte HTTP_REQUEST_TRAILER_SECTION_SIZE = 23; + internal const byte HTTP_REQUEST_TRAILER_SIZE = 24; + internal const byte HTTP_RESPONSE_INCOMPLETE = 25; + internal const byte HTTP_RESPONSE_HEADER_SECTION_SIZE = 26; + internal const byte HTTP_RESPONSE_HEADER_SIZE = 27; + internal const byte HTTP_RESPONSE_BODY_SIZE = 28; + internal const byte HTTP_RESPONSE_TRAILER_SECTION_SIZE = 29; + internal const byte HTTP_RESPONSE_TRAILER_SIZE = 30; + internal const byte HTTP_RESPONSE_TRANSFER_CODING = 31; + internal const byte HTTP_RESPONSE_CONTENT_CODING = 32; + internal const byte HTTP_RESPONSE_TIMEOUT = 33; + internal const byte HTTP_UPGRADE_FAILED = 34; + internal const byte HTTP_PROTOCOL_ERROR = 35; + internal const byte LOOP_DETECTED = 36; + internal const byte CONFIGURATION_ERROR = 37; + internal const byte INTERNAL_ERROR = 38; + } + + /** + * This type enumerates the different kinds of errors that may occur when + * setting or appending to a `fields` resource. + */ + + internal class HeaderError { + internal readonly byte Tag; + private readonly object? value; + + private HeaderError(byte tag, object? value) { + this.Tag = tag; + this.value = value; + } + + internal static HeaderError invalidSyntax() { + return new HeaderError(INVALID_SYNTAX, null); + } + + internal static HeaderError forbidden() { + return new HeaderError(FORBIDDEN, null); + } + + internal static HeaderError immutable() { + return new HeaderError(IMMUTABLE, null); + } + + internal const byte INVALID_SYNTAX = 0; + internal const byte FORBIDDEN = 1; + internal const byte IMMUTABLE = 2; + } + + /** + * This following block defines the `fields` resource which corresponds to + * HTTP standard Fields. Fields are a common representation used for both + * Headers and Trailers. + * + * A `fields` may be mutable or immutable. A `fields` created using the + * constructor, `from-list`, or `clone` will be mutable, but a `fields` + * resource given by other means (including, but not limited to, + * `incoming-request.headers`, `outgoing-request.headers`) might be be + * immutable. In an immutable fields, the `set`, `append`, and `delete` + * operations will fail with `header-error.immutable`. + */ + + internal class Fields: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal Fields(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]fields"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~Fields() { + Dispose(false); + } + + internal static class ConstructorWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[constructor]fields"), WasmImportLinkage] + internal static extern int wasmImportConstructor(); + + } + + internal unsafe Fields() + { + var result = ConstructorWasmInterop.wasmImportConstructor(); + this.Handle = result; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class FromListWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[static]fields.from-list"), WasmImportLinkage] + internal static extern void wasmImportFromList(nint p0, int p1, nint p2); + + } + + internal static unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields FromList(List<(string, byte[])> entries) + { + + byte[] buffer0 = new byte[16 * entries.Count]; + var gcHandle = GCHandle.Alloc(buffer0, GCHandleType.Pinned); + var address = gcHandle.AddrOfPinnedObject(); + + for (int index = 0; index < entries.Count; ++index) { + (string, byte[]) element = entries[index]; + int basePtr = (int)address + (index * 16); + + var result = element.Item1; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + BitConverter.TryWriteBytes(new Span((void*)(basePtr + 4), 4), unchecked((int)lengthresult)); + BitConverter.TryWriteBytes(new Span((void*)(basePtr + 0), 4), unchecked((int)interopString.ToInt32())); + + void* buffer = stackalloc byte[(element.Item2).Length]; + element.Item2.AsSpan().CopyTo(new Span(buffer, element.Item2.Length)); + BitConverter.TryWriteBytes(new Span((void*)(basePtr + 12), 4), unchecked((int)(element.Item2).Length)); + BitConverter.TryWriteBytes(new Span((void*)(basePtr + 8), 4), unchecked((int)(int)buffer)); + + } + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + FromListWasmInterop.wasmImportFromList((int)address, entries.Count, ptr); + + Result lifted11; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)))); + + lifted11 = Result.ok(resource); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.invalidSyntax(); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.forbidden(); + break; + } + case 2: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.immutable(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted11 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + gcHandle.Free(); + if (lifted11.IsOk) { + var tmp = lifted11.AsOk; + return tmp; + } else { + throw new WitException(lifted11.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class GetWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]fields.get"), WasmImportLinkage] + internal static extern void wasmImportGet(int p0, nint p1, int p2, nint p3); + + } + + internal unsafe List Get(string name) + { + var handle = this.Handle; + + var result = name; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + GetWasmInterop.wasmImportGet(handle, interopString.ToInt32(), lengthresult, ptr); + + var array0 = new List(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4))); + for (int index = 0; index < BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)); ++index) { + nint basePtr = BitConverter.ToInt32(new Span((void*)(ptr + 0), 4)) + (index * 8); + + var array = new byte[BitConverter.ToInt32(new Span((void*)(basePtr + 4), 4))]; + new Span((void*)(BitConverter.ToInt32(new Span((void*)(basePtr + 0), 4))), BitConverter.ToInt32(new Span((void*)(basePtr + 4), 4))).CopyTo(new Span(array)); + + array0.Add(array); + } + return array0; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class HasWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]fields.has"), WasmImportLinkage] + internal static extern int wasmImportHas(int p0, nint p1, int p2); + + } + + internal unsafe bool Has(string name) + { + var handle = this.Handle; + + var result = name; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + var result0 = HasWasmInterop.wasmImportHas(handle, interopString.ToInt32(), lengthresult); + return (result0 != 0); + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SetWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]fields.set"), WasmImportLinkage] + internal static extern void wasmImportSet(int p0, nint p1, int p2, nint p3, int p4, nint p5); + + } + + internal unsafe void Set(string name, List value) + { + var handle = this.Handle; + + var result = name; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + + byte[] buffer0 = new byte[8 * value.Count]; + var gcHandle = GCHandle.Alloc(buffer0, GCHandleType.Pinned); + var address = gcHandle.AddrOfPinnedObject(); + + for (int index = 0; index < value.Count; ++index) { + byte[] element = value[index]; + int basePtr = (int)address + (index * 8); + + void* buffer = stackalloc byte[(element).Length]; + element.AsSpan().CopyTo(new Span(buffer, element.Length)); + BitConverter.TryWriteBytes(new Span((void*)(basePtr + 4), 4), unchecked((int)(element).Length)); + BitConverter.TryWriteBytes(new Span((void*)(basePtr + 0), 4), unchecked((int)(int)buffer)); + + } + + var retArea = new byte[2]; + fixed (byte* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + SetWasmInterop.wasmImportSet(handle, interopString.ToInt32(), lengthresult, (int)address, value.Count, ptr); + + Result lifted11; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted11 = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError lifted; + + switch (new Span((void*)(ptr + 1), 1)[0]) { + case 0: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.invalidSyntax(); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.forbidden(); + break; + } + case 2: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.immutable(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 1), 1)[0]}"); + } + + lifted11 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + gcHandle.Free(); + if (lifted11.IsOk) { + var tmp = lifted11.AsOk; + return ; + } else { + throw new WitException(lifted11.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class DeleteWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]fields.delete"), WasmImportLinkage] + internal static extern void wasmImportDelete(int p0, nint p1, int p2, nint p3); + + } + + internal unsafe void Delete(string name) + { + var handle = this.Handle; + + var result = name; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + + var retArea = new byte[2]; + fixed (byte* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + DeleteWasmInterop.wasmImportDelete(handle, interopString.ToInt32(), lengthresult, ptr); + + Result lifted8; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted8 = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError lifted; + + switch (new Span((void*)(ptr + 1), 1)[0]) { + case 0: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.invalidSyntax(); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.forbidden(); + break; + } + case 2: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.immutable(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 1), 1)[0]}"); + } + + lifted8 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted8.IsOk) { + var tmp = lifted8.AsOk; + return ; + } else { + throw new WitException(lifted8.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class AppendWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]fields.append"), WasmImportLinkage] + internal static extern void wasmImportAppend(int p0, nint p1, int p2, nint p3, int p4, nint p5); + + } + + internal unsafe void Append(string name, byte[] value) + { + var handle = this.Handle; + + var result = name; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + + void* buffer = stackalloc byte[(value).Length]; + value.AsSpan().CopyTo(new Span(buffer, value.Length)); + + var retArea = new byte[2]; + fixed (byte* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + AppendWasmInterop.wasmImportAppend(handle, interopString.ToInt32(), lengthresult, (int)buffer, (value).Length, ptr); + + Result lifted8; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted8 = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError lifted; + + switch (new Span((void*)(ptr + 1), 1)[0]) { + case 0: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.invalidSyntax(); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.forbidden(); + break; + } + case 2: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.HeaderError.immutable(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 1), 1)[0]}"); + } + + lifted8 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted8.IsOk) { + var tmp = lifted8.AsOk; + return ; + } else { + throw new WitException(lifted8.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class EntriesWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]fields.entries"), WasmImportLinkage] + internal static extern void wasmImportEntries(int p0, nint p1); + + } + + internal unsafe List<(string, byte[])> Entries() + { + var handle = this.Handle; + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + EntriesWasmInterop.wasmImportEntries(handle, ptr); + + var array0 = new List<(string, byte[])>(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4))); + for (int index = 0; index < BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)); ++index) { + nint basePtr = BitConverter.ToInt32(new Span((void*)(ptr + 0), 4)) + (index * 16); + + var array = new byte[BitConverter.ToInt32(new Span((void*)(basePtr + 12), 4))]; + new Span((void*)(BitConverter.ToInt32(new Span((void*)(basePtr + 8), 4))), BitConverter.ToInt32(new Span((void*)(basePtr + 12), 4))).CopyTo(new Span(array)); + + array0.Add((Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(basePtr + 0), 4)), BitConverter.ToInt32(new Span((void*)(basePtr + 4), 4))), array + )); + } + return array0; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class CloneWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]fields.clone"), WasmImportLinkage] + internal static extern int wasmImportClone(int p0); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields Clone() + { + var handle = this.Handle; + var result = CloneWasmInterop.wasmImportClone(handle); + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * Represents an incoming HTTP Request. + */ + + internal class IncomingRequest: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal IncomingRequest(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]incoming-request"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~IncomingRequest() { + Dispose(false); + } + + internal static class MethodWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]incoming-request.method"), WasmImportLinkage] + internal static extern void wasmImportMethod(int p0, nint p1); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method Method() + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + MethodWasmInterop.wasmImportMethod(handle, ptr); + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.get(); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.head(); + break; + } + case 2: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.post(); + break; + } + case 3: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.put(); + break; + } + case 4: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.delete(); + break; + } + case 5: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.connect(); + break; + } + case 6: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.options(); + break; + } + case 7: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.trace(); + break; + } + case 8: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.patch(); + break; + } + case 9: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.other(Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + return lifted; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class PathWithQueryWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]incoming-request.path-with-query"), WasmImportLinkage] + internal static extern void wasmImportPathWithQuery(int p0, nint p1); + + } + + internal unsafe string? PathWithQuery() + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + PathWithQueryWasmInterop.wasmImportPathWithQuery(handle, ptr); + + string? lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 8), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SchemeWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]incoming-request.scheme"), WasmImportLinkage] + internal static extern void wasmImportScheme(int p0, nint p1); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme? Scheme() + { + var handle = this.Handle; + + var retArea = new uint[4]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + SchemeWasmInterop.wasmImportScheme(handle, ptr); + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme? lifted8; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted8 = null; + break; + } + + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme.http(); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme.https(); + break; + } + case 2: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme.other(Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 12), 4)))); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted8 = lifted; + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted8; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class AuthorityWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]incoming-request.authority"), WasmImportLinkage] + internal static extern void wasmImportAuthority(int p0, nint p1); + + } + + internal unsafe string? Authority() + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + AuthorityWasmInterop.wasmImportAuthority(handle, ptr); + + string? lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 8), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class HeadersWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]incoming-request.headers"), WasmImportLinkage] + internal static extern int wasmImportHeaders(int p0); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields Headers() + { + var handle = this.Handle; + var result = HeadersWasmInterop.wasmImportHeaders(handle); + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class ConsumeWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]incoming-request.consume"), WasmImportLinkage] + internal static extern void wasmImportConsume(int p0, nint p1); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.IncomingBody Consume() + { + var handle = this.Handle; + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + ConsumeWasmInterop.wasmImportConsume(handle, ptr); + + Result lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.IncomingBody(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.IncomingBody.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)))); + + lifted = Result.ok(resource); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return tmp; + } else { + throw new WitException(lifted.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * Represents an outgoing HTTP Request. + */ + + internal class OutgoingRequest: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal OutgoingRequest(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]outgoing-request"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~OutgoingRequest() { + Dispose(false); + } + + internal static class ConstructorWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[constructor]outgoing-request"), WasmImportLinkage] + internal static extern int wasmImportConstructor(int p0); + + } + + internal unsafe OutgoingRequest(global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields headers) + { + var handle = headers.Handle; + headers.Handle = 0; + var result = ConstructorWasmInterop.wasmImportConstructor(handle); + this.Handle = result; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BodyWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-request.body"), WasmImportLinkage] + internal static extern void wasmImportBody(int p0, nint p1); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.OutgoingBody Body() + { + var handle = this.Handle; + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + BodyWasmInterop.wasmImportBody(handle, ptr); + + Result lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.OutgoingBody(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.OutgoingBody.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)))); + + lifted = Result.ok(resource); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return tmp; + } else { + throw new WitException(lifted.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class MethodWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-request.method"), WasmImportLinkage] + internal static extern void wasmImportMethod(int p0, nint p1); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method Method() + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + MethodWasmInterop.wasmImportMethod(handle, ptr); + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.get(); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.head(); + break; + } + case 2: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.post(); + break; + } + case 3: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.put(); + break; + } + case 4: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.delete(); + break; + } + case 5: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.connect(); + break; + } + case 6: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.options(); + break; + } + case 7: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.trace(); + break; + } + case 8: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.patch(); + break; + } + case 9: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method.other(Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + return lifted; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SetMethodWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-request.set-method"), WasmImportLinkage] + internal static extern int wasmImportSetMethod(int p0, int p1, nint p2, int p3); + + } + + internal unsafe void SetMethod(global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Method method) + { + var handle = this.Handle; + + int lowered; + nint lowered27; + int lowered28; + + switch (method.Tag) { + case 0: { + + lowered = 0; + lowered27 = 0; + lowered28 = 0; + + break; + } + case 1: { + + lowered = 1; + lowered27 = 0; + lowered28 = 0; + + break; + } + case 2: { + + lowered = 2; + lowered27 = 0; + lowered28 = 0; + + break; + } + case 3: { + + lowered = 3; + lowered27 = 0; + lowered28 = 0; + + break; + } + case 4: { + + lowered = 4; + lowered27 = 0; + lowered28 = 0; + + break; + } + case 5: { + + lowered = 5; + lowered27 = 0; + lowered28 = 0; + + break; + } + case 6: { + + lowered = 6; + lowered27 = 0; + lowered28 = 0; + + break; + } + case 7: { + + lowered = 7; + lowered27 = 0; + lowered28 = 0; + + break; + } + case 8: { + + lowered = 8; + lowered27 = 0; + lowered28 = 0; + + break; + } + case 9: { + string payload26 = method.AsOther; + + var result = payload26; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + + lowered = 9; + lowered27 = interopString.ToInt32(); + lowered28 = lengthresult; + + break; + } + + default: throw new ArgumentException($"invalid discriminant: {method}"); + } + var result29 = SetMethodWasmInterop.wasmImportSetMethod(handle, lowered, lowered27, lowered28); + + Result lifted; + + switch (result29) { + case 0: { + + lifted = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {result29}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return ; + } else { + throw new WitException(lifted.AsErr!, 0); + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class PathWithQueryWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-request.path-with-query"), WasmImportLinkage] + internal static extern void wasmImportPathWithQuery(int p0, nint p1); + + } + + internal unsafe string? PathWithQuery() + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + PathWithQueryWasmInterop.wasmImportPathWithQuery(handle, ptr); + + string? lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 8), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SetPathWithQueryWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-request.set-path-with-query"), WasmImportLinkage] + internal static extern int wasmImportSetPathWithQuery(int p0, int p1, nint p2, int p3); + + } + + internal unsafe void SetPathWithQuery(string? pathWithQuery) + { + var handle = this.Handle; + + int lowered; + nint lowered3; + int lowered4; + + if (pathWithQuery != null) { + var payload2 = (string) pathWithQuery; + + var result = payload2; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + + lowered = 1; + lowered3 = interopString.ToInt32(); + lowered4 = lengthresult; + + } else { + + lowered = 0; + lowered3 = 0; + lowered4 = 0; + + } + var result5 = SetPathWithQueryWasmInterop.wasmImportSetPathWithQuery(handle, lowered, lowered3, lowered4); + + Result lifted; + + switch (result5) { + case 0: { + + lifted = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {result5}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return ; + } else { + throw new WitException(lifted.AsErr!, 0); + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SchemeWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-request.scheme"), WasmImportLinkage] + internal static extern void wasmImportScheme(int p0, nint p1); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme? Scheme() + { + var handle = this.Handle; + + var retArea = new uint[4]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + SchemeWasmInterop.wasmImportScheme(handle, ptr); + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme? lifted8; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted8 = null; + break; + } + + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme.http(); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme.https(); + break; + } + case 2: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme.other(Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 12), 4)))); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted8 = lifted; + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted8; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SetSchemeWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-request.set-scheme"), WasmImportLinkage] + internal static extern int wasmImportSetScheme(int p0, int p1, int p2, nint p3, int p4); + + } + + internal unsafe void SetScheme(global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme? scheme) + { + var handle = this.Handle; + + int lowered14; + int lowered15; + nint lowered16; + int lowered17; + + if (scheme != null) { + var payload2 = (global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Scheme) scheme; + + int lowered; + nint lowered12; + int lowered13; + + switch (payload2.Tag) { + case 0: { + + lowered = 0; + lowered12 = 0; + lowered13 = 0; + + break; + } + case 1: { + + lowered = 1; + lowered12 = 0; + lowered13 = 0; + + break; + } + case 2: { + string payload11 = payload2.AsOther; + + var result = payload11; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + + lowered = 2; + lowered12 = interopString.ToInt32(); + lowered13 = lengthresult; + + break; + } + + default: throw new ArgumentException($"invalid discriminant: {payload2}"); + } + + lowered14 = 1; + lowered15 = lowered; + lowered16 = lowered12; + lowered17 = lowered13; + + } else { + + lowered14 = 0; + lowered15 = 0; + lowered16 = 0; + lowered17 = 0; + + } + var result18 = SetSchemeWasmInterop.wasmImportSetScheme(handle, lowered14, lowered15, lowered16, lowered17); + + Result lifted; + + switch (result18) { + case 0: { + + lifted = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {result18}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return ; + } else { + throw new WitException(lifted.AsErr!, 0); + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class AuthorityWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-request.authority"), WasmImportLinkage] + internal static extern void wasmImportAuthority(int p0, nint p1); + + } + + internal unsafe string? Authority() + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + AuthorityWasmInterop.wasmImportAuthority(handle, ptr); + + string? lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 8), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SetAuthorityWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-request.set-authority"), WasmImportLinkage] + internal static extern int wasmImportSetAuthority(int p0, int p1, nint p2, int p3); + + } + + internal unsafe void SetAuthority(string? authority) + { + var handle = this.Handle; + + int lowered; + nint lowered3; + int lowered4; + + if (authority != null) { + var payload2 = (string) authority; + + var result = payload2; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + + lowered = 1; + lowered3 = interopString.ToInt32(); + lowered4 = lengthresult; + + } else { + + lowered = 0; + lowered3 = 0; + lowered4 = 0; + + } + var result5 = SetAuthorityWasmInterop.wasmImportSetAuthority(handle, lowered, lowered3, lowered4); + + Result lifted; + + switch (result5) { + case 0: { + + lifted = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {result5}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return ; + } else { + throw new WitException(lifted.AsErr!, 0); + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class HeadersWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-request.headers"), WasmImportLinkage] + internal static extern int wasmImportHeaders(int p0); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields Headers() + { + var handle = this.Handle; + var result = HeadersWasmInterop.wasmImportHeaders(handle); + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * Parameters for making an HTTP Request. Each of these parameters is + * currently an optional timeout applicable to the transport layer of the + * HTTP protocol. + * + * These timeouts are separate from any the user may use to bound a + * blocking call to `wasi:io/poll.poll`. + */ + + internal class RequestOptions: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal RequestOptions(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]request-options"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~RequestOptions() { + Dispose(false); + } + + internal static class ConstructorWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[constructor]request-options"), WasmImportLinkage] + internal static extern int wasmImportConstructor(); + + } + + internal unsafe RequestOptions() + { + var result = ConstructorWasmInterop.wasmImportConstructor(); + this.Handle = result; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class ConnectTimeoutWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]request-options.connect-timeout"), WasmImportLinkage] + internal static extern void wasmImportConnectTimeout(int p0, nint p1); + + } + + internal unsafe ulong? ConnectTimeout() + { + var handle = this.Handle; + + var retArea = new ulong[2]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + ConnectTimeoutWasmInterop.wasmImportConnectTimeout(handle, ptr); + + ulong? lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 8), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SetConnectTimeoutWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]request-options.set-connect-timeout"), WasmImportLinkage] + internal static extern int wasmImportSetConnectTimeout(int p0, int p1, long p2); + + } + + internal unsafe void SetConnectTimeout(ulong? duration) + { + var handle = this.Handle; + + int lowered; + long lowered3; + + if (duration != null) { + var payload2 = (ulong) duration; + + lowered = 1; + lowered3 = unchecked((long)(payload2)); + + } else { + + lowered = 0; + lowered3 = 0L; + + } + var result = SetConnectTimeoutWasmInterop.wasmImportSetConnectTimeout(handle, lowered, lowered3); + + Result lifted; + + switch (result) { + case 0: { + + lifted = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {result}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return ; + } else { + throw new WitException(lifted.AsErr!, 0); + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class FirstByteTimeoutWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]request-options.first-byte-timeout"), WasmImportLinkage] + internal static extern void wasmImportFirstByteTimeout(int p0, nint p1); + + } + + internal unsafe ulong? FirstByteTimeout() + { + var handle = this.Handle; + + var retArea = new ulong[2]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + FirstByteTimeoutWasmInterop.wasmImportFirstByteTimeout(handle, ptr); + + ulong? lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 8), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SetFirstByteTimeoutWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]request-options.set-first-byte-timeout"), WasmImportLinkage] + internal static extern int wasmImportSetFirstByteTimeout(int p0, int p1, long p2); + + } + + internal unsafe void SetFirstByteTimeout(ulong? duration) + { + var handle = this.Handle; + + int lowered; + long lowered3; + + if (duration != null) { + var payload2 = (ulong) duration; + + lowered = 1; + lowered3 = unchecked((long)(payload2)); + + } else { + + lowered = 0; + lowered3 = 0L; + + } + var result = SetFirstByteTimeoutWasmInterop.wasmImportSetFirstByteTimeout(handle, lowered, lowered3); + + Result lifted; + + switch (result) { + case 0: { + + lifted = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {result}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return ; + } else { + throw new WitException(lifted.AsErr!, 0); + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BetweenBytesTimeoutWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]request-options.between-bytes-timeout"), WasmImportLinkage] + internal static extern void wasmImportBetweenBytesTimeout(int p0, nint p1); + + } + + internal unsafe ulong? BetweenBytesTimeout() + { + var handle = this.Handle; + + var retArea = new ulong[2]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + BetweenBytesTimeoutWasmInterop.wasmImportBetweenBytesTimeout(handle, ptr); + + ulong? lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 8), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted; + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SetBetweenBytesTimeoutWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]request-options.set-between-bytes-timeout"), WasmImportLinkage] + internal static extern int wasmImportSetBetweenBytesTimeout(int p0, int p1, long p2); + + } + + internal unsafe void SetBetweenBytesTimeout(ulong? duration) + { + var handle = this.Handle; + + int lowered; + long lowered3; + + if (duration != null) { + var payload2 = (ulong) duration; + + lowered = 1; + lowered3 = unchecked((long)(payload2)); + + } else { + + lowered = 0; + lowered3 = 0L; + + } + var result = SetBetweenBytesTimeoutWasmInterop.wasmImportSetBetweenBytesTimeout(handle, lowered, lowered3); + + Result lifted; + + switch (result) { + case 0: { + + lifted = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {result}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return ; + } else { + throw new WitException(lifted.AsErr!, 0); + } + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * Represents the ability to send an HTTP Response. + * + * This resource is used by the `wasi:http/incoming-handler` interface to + * allow a Response to be sent corresponding to the Request provided as the + * other argument to `incoming-handler.handle`. + */ + + internal class ResponseOutparam: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal ResponseOutparam(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]response-outparam"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~ResponseOutparam() { + Dispose(false); + } + + internal static class SetWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[static]response-outparam.set"), WasmImportLinkage] + internal static extern void wasmImportSet(int p0, int p1, int p2, int p3, long p4, nint p5, nint p6, int p7, int p8); + + } + + internal static unsafe void Set(global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ResponseOutparam param, Result response) + { + var handle = param.Handle; + param.Handle = 0; + + int lowered332; + int lowered333; + int lowered334; + long lowered335; + nint lowered336; + nint lowered337; + int lowered338; + int lowered339; + + switch (response.Tag) { + case 0: { + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.OutgoingResponse payload = response.AsOk; + var handle0 = payload.Handle; + payload.Handle = 0; + + lowered332 = 0; + lowered333 = handle0; + lowered334 = 0; + lowered335 = 0L; + lowered336 = 0; + lowered337 = 0; + lowered338 = 0; + lowered339 = 0; + + break; + } + case 1: { + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode payload3 = response.AsErr; + + int lowered325; + int lowered326; + long lowered327; + nint lowered328; + nint lowered329; + int lowered330; + int lowered331; + + switch (payload3.Tag) { + case 0: { + + lowered325 = 0; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 1: { + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.DnsErrorPayload payload9 = payload3.AsDnsError; + + int lowered; + nint lowered16; + int lowered17; + + if (payload9.rcode != null) { + var payload15 = (string) payload9.rcode; + + var result = payload15; + IntPtr interopString = InteropString.FromString(result, out int lengthresult); + + lowered = 1; + lowered16 = interopString.ToInt32(); + lowered17 = lengthresult; + + } else { + + lowered = 0; + lowered16 = 0; + lowered17 = 0; + + } + + int lowered24; + int lowered25; + + if (payload9.infoCode != null) { + var payload23 = (ushort) payload9.infoCode; + + lowered24 = 1; + lowered25 = payload23; + + } else { + + lowered24 = 0; + lowered25 = 0; + + } + + lowered325 = 1; + lowered326 = lowered; + lowered327 = (long) (lowered16); + lowered328 = lowered17; + lowered329 = lowered24; + lowered330 = lowered25; + lowered331 = 0; + + break; + } + case 2: { + + lowered325 = 2; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 3: { + + lowered325 = 3; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 4: { + + lowered325 = 4; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 5: { + + lowered325 = 5; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 6: { + + lowered325 = 6; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 7: { + + lowered325 = 7; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 8: { + + lowered325 = 8; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 9: { + + lowered325 = 9; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 10: { + + lowered325 = 10; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 11: { + + lowered325 = 11; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 12: { + + lowered325 = 12; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 13: { + + lowered325 = 13; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 14: { + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.TlsAlertReceivedPayload payload64 = payload3.AsTlsAlertReceived; + + int lowered71; + int lowered72; + + if (payload64.alertId != null) { + var payload70 = (byte) payload64.alertId; + + lowered71 = 1; + lowered72 = payload70; + + } else { + + lowered71 = 0; + lowered72 = 0; + + } + + int lowered81; + nint lowered82; + int lowered83; + + if (payload64.alertMessage != null) { + var payload78 = (string) payload64.alertMessage; + + var result80 = payload78; + IntPtr interopString79 = InteropString.FromString(result80, out int lengthresult80); + + lowered81 = 1; + lowered82 = interopString79.ToInt32(); + lowered83 = lengthresult80; + + } else { + + lowered81 = 0; + lowered82 = 0; + lowered83 = 0; + + } + + lowered325 = 14; + lowered326 = lowered71; + lowered327 = (long) (lowered72); + lowered328 = lowered81; + lowered329 = lowered82; + lowered330 = lowered83; + lowered331 = 0; + + break; + } + case 15: { + + lowered325 = 15; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 16: { + + lowered325 = 16; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 17: { + ulong? payload92 = payload3.AsHttpRequestBodySize; + + int lowered99; + long lowered100; + + if (payload92 != null) { + var payload98 = (ulong) payload92; + + lowered99 = 1; + lowered100 = unchecked((long)(payload98)); + + } else { + + lowered99 = 0; + lowered100 = 0L; + + } + + lowered325 = 17; + lowered326 = lowered99; + lowered327 = lowered100; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 18: { + + lowered325 = 18; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 19: { + + lowered325 = 19; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 20: { + + lowered325 = 20; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 21: { + uint? payload112 = payload3.AsHttpRequestHeaderSectionSize; + + int lowered119; + int lowered120; + + if (payload112 != null) { + var payload118 = (uint) payload112; + + lowered119 = 1; + lowered120 = unchecked((int)(payload118)); + + } else { + + lowered119 = 0; + lowered120 = 0; + + } + + lowered325 = 21; + lowered326 = lowered119; + lowered327 = (long) (lowered120); + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 22: { + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload? payload123 = payload3.AsHttpRequestHeaderSize; + + int lowered149; + int lowered150; + nint lowered151; + int lowered152; + int lowered153; + int lowered154; + + if (payload123 != null) { + var payload129 = (global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload) payload123; + + int lowered138; + nint lowered139; + int lowered140; + + if (payload129.fieldName != null) { + var payload135 = (string) payload129.fieldName; + + var result137 = payload135; + IntPtr interopString136 = InteropString.FromString(result137, out int lengthresult137); + + lowered138 = 1; + lowered139 = interopString136.ToInt32(); + lowered140 = lengthresult137; + + } else { + + lowered138 = 0; + lowered139 = 0; + lowered140 = 0; + + } + + int lowered147; + int lowered148; + + if (payload129.fieldSize != null) { + var payload146 = (uint) payload129.fieldSize; + + lowered147 = 1; + lowered148 = unchecked((int)(payload146)); + + } else { + + lowered147 = 0; + lowered148 = 0; + + } + + lowered149 = 1; + lowered150 = lowered138; + lowered151 = lowered139; + lowered152 = lowered140; + lowered153 = lowered147; + lowered154 = lowered148; + + } else { + + lowered149 = 0; + lowered150 = 0; + lowered151 = 0; + lowered152 = 0; + lowered153 = 0; + lowered154 = 0; + + } + + lowered325 = 22; + lowered326 = lowered149; + lowered327 = (long) (lowered150); + lowered328 = lowered151; + lowered329 = lowered152; + lowered330 = lowered153; + lowered331 = lowered154; + + break; + } + case 23: { + uint? payload157 = payload3.AsHttpRequestTrailerSectionSize; + + int lowered164; + int lowered165; + + if (payload157 != null) { + var payload163 = (uint) payload157; + + lowered164 = 1; + lowered165 = unchecked((int)(payload163)); + + } else { + + lowered164 = 0; + lowered165 = 0; + + } + + lowered325 = 23; + lowered326 = lowered164; + lowered327 = (long) (lowered165); + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 24: { + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload payload168 = payload3.AsHttpRequestTrailerSize; + + int lowered177; + nint lowered178; + int lowered179; + + if (payload168.fieldName != null) { + var payload174 = (string) payload168.fieldName; + + var result176 = payload174; + IntPtr interopString175 = InteropString.FromString(result176, out int lengthresult176); + + lowered177 = 1; + lowered178 = interopString175.ToInt32(); + lowered179 = lengthresult176; + + } else { + + lowered177 = 0; + lowered178 = 0; + lowered179 = 0; + + } + + int lowered186; + int lowered187; + + if (payload168.fieldSize != null) { + var payload185 = (uint) payload168.fieldSize; + + lowered186 = 1; + lowered187 = unchecked((int)(payload185)); + + } else { + + lowered186 = 0; + lowered187 = 0; + + } + + lowered325 = 24; + lowered326 = lowered177; + lowered327 = (long) (lowered178); + lowered328 = lowered179; + lowered329 = lowered186; + lowered330 = lowered187; + lowered331 = 0; + + break; + } + case 25: { + + lowered325 = 25; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 26: { + uint? payload193 = payload3.AsHttpResponseHeaderSectionSize; + + int lowered200; + int lowered201; + + if (payload193 != null) { + var payload199 = (uint) payload193; + + lowered200 = 1; + lowered201 = unchecked((int)(payload199)); + + } else { + + lowered200 = 0; + lowered201 = 0; + + } + + lowered325 = 26; + lowered326 = lowered200; + lowered327 = (long) (lowered201); + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 27: { + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload payload204 = payload3.AsHttpResponseHeaderSize; + + int lowered213; + nint lowered214; + int lowered215; + + if (payload204.fieldName != null) { + var payload210 = (string) payload204.fieldName; + + var result212 = payload210; + IntPtr interopString211 = InteropString.FromString(result212, out int lengthresult212); + + lowered213 = 1; + lowered214 = interopString211.ToInt32(); + lowered215 = lengthresult212; + + } else { + + lowered213 = 0; + lowered214 = 0; + lowered215 = 0; + + } + + int lowered222; + int lowered223; + + if (payload204.fieldSize != null) { + var payload221 = (uint) payload204.fieldSize; + + lowered222 = 1; + lowered223 = unchecked((int)(payload221)); + + } else { + + lowered222 = 0; + lowered223 = 0; + + } + + lowered325 = 27; + lowered326 = lowered213; + lowered327 = (long) (lowered214); + lowered328 = lowered215; + lowered329 = lowered222; + lowered330 = lowered223; + lowered331 = 0; + + break; + } + case 28: { + ulong? payload226 = payload3.AsHttpResponseBodySize; + + int lowered233; + long lowered234; + + if (payload226 != null) { + var payload232 = (ulong) payload226; + + lowered233 = 1; + lowered234 = unchecked((long)(payload232)); + + } else { + + lowered233 = 0; + lowered234 = 0L; + + } + + lowered325 = 28; + lowered326 = lowered233; + lowered327 = lowered234; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 29: { + uint? payload237 = payload3.AsHttpResponseTrailerSectionSize; + + int lowered244; + int lowered245; + + if (payload237 != null) { + var payload243 = (uint) payload237; + + lowered244 = 1; + lowered245 = unchecked((int)(payload243)); + + } else { + + lowered244 = 0; + lowered245 = 0; + + } + + lowered325 = 29; + lowered326 = lowered244; + lowered327 = (long) (lowered245); + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 30: { + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload payload248 = payload3.AsHttpResponseTrailerSize; + + int lowered257; + nint lowered258; + int lowered259; + + if (payload248.fieldName != null) { + var payload254 = (string) payload248.fieldName; + + var result256 = payload254; + IntPtr interopString255 = InteropString.FromString(result256, out int lengthresult256); + + lowered257 = 1; + lowered258 = interopString255.ToInt32(); + lowered259 = lengthresult256; + + } else { + + lowered257 = 0; + lowered258 = 0; + lowered259 = 0; + + } + + int lowered266; + int lowered267; + + if (payload248.fieldSize != null) { + var payload265 = (uint) payload248.fieldSize; + + lowered266 = 1; + lowered267 = unchecked((int)(payload265)); + + } else { + + lowered266 = 0; + lowered267 = 0; + + } + + lowered325 = 30; + lowered326 = lowered257; + lowered327 = (long) (lowered258); + lowered328 = lowered259; + lowered329 = lowered266; + lowered330 = lowered267; + lowered331 = 0; + + break; + } + case 31: { + string? payload270 = payload3.AsHttpResponseTransferCoding; + + int lowered279; + nint lowered280; + int lowered281; + + if (payload270 != null) { + var payload276 = (string) payload270; + + var result278 = payload276; + IntPtr interopString277 = InteropString.FromString(result278, out int lengthresult278); + + lowered279 = 1; + lowered280 = interopString277.ToInt32(); + lowered281 = lengthresult278; + + } else { + + lowered279 = 0; + lowered280 = 0; + lowered281 = 0; + + } + + lowered325 = 31; + lowered326 = lowered279; + lowered327 = (long) (lowered280); + lowered328 = lowered281; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 32: { + string? payload284 = payload3.AsHttpResponseContentCoding; + + int lowered293; + nint lowered294; + int lowered295; + + if (payload284 != null) { + var payload290 = (string) payload284; + + var result292 = payload290; + IntPtr interopString291 = InteropString.FromString(result292, out int lengthresult292); + + lowered293 = 1; + lowered294 = interopString291.ToInt32(); + lowered295 = lengthresult292; + + } else { + + lowered293 = 0; + lowered294 = 0; + lowered295 = 0; + + } + + lowered325 = 32; + lowered326 = lowered293; + lowered327 = (long) (lowered294); + lowered328 = lowered295; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 33: { + + lowered325 = 33; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 34: { + + lowered325 = 34; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 35: { + + lowered325 = 35; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 36: { + + lowered325 = 36; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 37: { + + lowered325 = 37; + lowered326 = 0; + lowered327 = 0L; + lowered328 = 0; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + case 38: { + string? payload313 = payload3.AsInternalError; + + int lowered322; + nint lowered323; + int lowered324; + + if (payload313 != null) { + var payload319 = (string) payload313; + + var result321 = payload319; + IntPtr interopString320 = InteropString.FromString(result321, out int lengthresult321); + + lowered322 = 1; + lowered323 = interopString320.ToInt32(); + lowered324 = lengthresult321; + + } else { + + lowered322 = 0; + lowered323 = 0; + lowered324 = 0; + + } + + lowered325 = 38; + lowered326 = lowered322; + lowered327 = (long) (lowered323); + lowered328 = lowered324; + lowered329 = 0; + lowered330 = 0; + lowered331 = 0; + + break; + } + + default: throw new ArgumentException($"invalid discriminant: {payload3}"); + } + + lowered332 = 1; + lowered333 = lowered325; + lowered334 = lowered326; + lowered335 = lowered327; + lowered336 = lowered328; + lowered337 = lowered329; + lowered338 = lowered330; + lowered339 = lowered331; + + break; + } + + default: throw new ArgumentException($"invalid discriminant: {response}"); + } + SetWasmInterop.wasmImportSet(handle, lowered332, lowered333, lowered334, lowered335, lowered336, lowered337, lowered338, lowered339); + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * Represents an incoming HTTP Response. + */ + + internal class IncomingResponse: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal IncomingResponse(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]incoming-response"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~IncomingResponse() { + Dispose(false); + } + + internal static class StatusWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]incoming-response.status"), WasmImportLinkage] + internal static extern int wasmImportStatus(int p0); + + } + + internal unsafe ushort Status() + { + var handle = this.Handle; + var result = StatusWasmInterop.wasmImportStatus(handle); + return ((ushort)result); + + //TODO: free alloc handle (interopString) if exists + } + + internal static class HeadersWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]incoming-response.headers"), WasmImportLinkage] + internal static extern int wasmImportHeaders(int p0); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields Headers() + { + var handle = this.Handle; + var result = HeadersWasmInterop.wasmImportHeaders(handle); + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class ConsumeWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]incoming-response.consume"), WasmImportLinkage] + internal static extern void wasmImportConsume(int p0, nint p1); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.IncomingBody Consume() + { + var handle = this.Handle; + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + ConsumeWasmInterop.wasmImportConsume(handle, ptr); + + Result lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.IncomingBody(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.IncomingBody.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)))); + + lifted = Result.ok(resource); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return tmp; + } else { + throw new WitException(lifted.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * Represents an incoming HTTP Request or Response's Body. + * + * A body has both its contents - a stream of bytes - and a (possibly + * empty) set of trailers, indicating that the full contents of the + * body have been received. This resource represents the contents as + * an `input-stream` and the delivery of trailers as a `future-trailers`, + * and ensures that the user of this interface may only be consuming either + * the body contents or waiting on trailers at any given time. + */ + + internal class IncomingBody: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal IncomingBody(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]incoming-body"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~IncomingBody() { + Dispose(false); + } + + internal static class StreamWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]incoming-body.stream"), WasmImportLinkage] + internal static extern void wasmImportStream(int p0, nint p1); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.InputStream Stream() + { + var handle = this.Handle; + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + StreamWasmInterop.wasmImportStream(handle, ptr); + + Result lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.InputStream(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.InputStream.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)))); + + lifted = Result.ok(resource); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return tmp; + } else { + throw new WitException(lifted.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class FinishWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[static]incoming-body.finish"), WasmImportLinkage] + internal static extern int wasmImportFinish(int p0); + + } + + internal static unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FutureTrailers Finish(global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.IncomingBody @this) + { + var handle = @this.Handle; + @this.Handle = 0; + var result = FinishWasmInterop.wasmImportFinish(handle); + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FutureTrailers(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FutureTrailers.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * Represents a future which may eventaully return trailers, or an error. + * + * In the case that the incoming HTTP Request or Response did not have any + * trailers, this future will resolve to the empty set of trailers once the + * complete Request or Response body has been received. + */ + + internal class FutureTrailers: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal FutureTrailers(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]future-trailers"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~FutureTrailers() { + Dispose(false); + } + + internal static class SubscribeWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]future-trailers.subscribe"), WasmImportLinkage] + internal static extern int wasmImportSubscribe(int p0); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable Subscribe() + { + var handle = this.Handle; + var result = SubscribeWasmInterop.wasmImportSubscribe(handle); + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class GetWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]future-trailers.get"), WasmImportLinkage] + internal static extern void wasmImportGet(int p0, nint p1); + + } + + internal unsafe Result, None>? Get() + { + var handle = this.Handle; + + var retArea = new ulong[7]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + GetWasmInterop.wasmImportGet(handle, ptr); + + Result, None>? lifted205; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted205 = null; + break; + } + + case 1: { + + Result, None> lifted204; + + switch (new Span((void*)(ptr + 8), 1)[0]) { + case 0: { + + Result lifted201; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields? lifted; + + switch (new Span((void*)(ptr + 24), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 28), 4)))); + + lifted = resource; + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 24), 1)[0])); + } + + lifted201 = Result.ok(lifted); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode lifted200; + + switch (new Span((void*)(ptr + 24), 1)[0]) { + case 0: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.dnsTimeout(); + break; + } + case 1: { + + string? lifted20; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted20 = null; + break; + } + + case 1: { + + lifted20 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + ushort? lifted25; + + switch (new Span((void*)(ptr + 44), 1)[0]) { + case 0: { + lifted25 = null; + break; + } + + case 1: { + + lifted25 = ((ushort)BitConverter.ToUInt16(new Span((void*)(ptr + 46), 2))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 44), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.dnsError(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.DnsErrorPayload ( + lifted20, lifted25)); + break; + } + case 2: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationNotFound(); + break; + } + case 3: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationUnavailable(); + break; + } + case 4: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationIpProhibited(); + break; + } + case 5: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationIpUnroutable(); + break; + } + case 6: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionRefused(); + break; + } + case 7: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionTerminated(); + break; + } + case 8: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionTimeout(); + break; + } + case 9: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionReadTimeout(); + break; + } + case 10: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionWriteTimeout(); + break; + } + case 11: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionLimitReached(); + break; + } + case 12: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsProtocolError(); + break; + } + case 13: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsCertificateError(); + break; + } + case 14: { + + byte? lifted56; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted56 = null; + break; + } + + case 1: { + + lifted56 = ((byte)new Span((void*)(ptr + 33), 1)[0]); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + string? lifted61; + + switch (new Span((void*)(ptr + 36), 1)[0]) { + case 0: { + lifted61 = null; + break; + } + + case 1: { + + lifted61 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 40), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 44), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 36), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsAlertReceived(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.TlsAlertReceivedPayload ( + lifted56, lifted61)); + break; + } + case 15: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestDenied(); + break; + } + case 16: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestLengthRequired(); + break; + } + case 17: { + + ulong? lifted72; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted72 = null; + break; + } + + case 1: { + + lifted72 = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 40), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestBodySize(lifted72); + break; + } + case 18: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestMethodInvalid(); + break; + } + case 19: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestUriInvalid(); + break; + } + case 20: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestUriTooLong(); + break; + } + case 21: { + + uint? lifted85; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted85 = null; + break; + } + + case 1: { + + lifted85 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestHeaderSectionSize(lifted85); + break; + } + case 22: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload? lifted102; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted102 = null; + break; + } + + case 1: { + + string? lifted96; + + switch (new Span((void*)(ptr + 36), 1)[0]) { + case 0: { + lifted96 = null; + break; + } + + case 1: { + + lifted96 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 40), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 44), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 36), 1)[0])); + } + + uint? lifted101; + + switch (new Span((void*)(ptr + 48), 1)[0]) { + case 0: { + lifted101 = null; + break; + } + + case 1: { + + lifted101 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 52), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 48), 1)[0])); + } + + lifted102 = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted96, lifted101); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestHeaderSize(lifted102); + break; + } + case 23: { + + uint? lifted109; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted109 = null; + break; + } + + case 1: { + + lifted109 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestTrailerSectionSize(lifted109); + break; + } + case 24: { + + string? lifted116; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted116 = null; + break; + } + + case 1: { + + lifted116 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + uint? lifted121; + + switch (new Span((void*)(ptr + 44), 1)[0]) { + case 0: { + lifted121 = null; + break; + } + + case 1: { + + lifted121 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 48), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 44), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestTrailerSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted116, lifted121)); + break; + } + case 25: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseIncomplete(); + break; + } + case 26: { + + uint? lifted130; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted130 = null; + break; + } + + case 1: { + + lifted130 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseHeaderSectionSize(lifted130); + break; + } + case 27: { + + string? lifted137; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted137 = null; + break; + } + + case 1: { + + lifted137 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + uint? lifted142; + + switch (new Span((void*)(ptr + 44), 1)[0]) { + case 0: { + lifted142 = null; + break; + } + + case 1: { + + lifted142 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 48), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 44), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseHeaderSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted137, lifted142)); + break; + } + case 28: { + + ulong? lifted149; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted149 = null; + break; + } + + case 1: { + + lifted149 = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 40), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseBodySize(lifted149); + break; + } + case 29: { + + uint? lifted156; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted156 = null; + break; + } + + case 1: { + + lifted156 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTrailerSectionSize(lifted156); + break; + } + case 30: { + + string? lifted163; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted163 = null; + break; + } + + case 1: { + + lifted163 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + uint? lifted168; + + switch (new Span((void*)(ptr + 44), 1)[0]) { + case 0: { + lifted168 = null; + break; + } + + case 1: { + + lifted168 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 48), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 44), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTrailerSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted163, lifted168)); + break; + } + case 31: { + + string? lifted175; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted175 = null; + break; + } + + case 1: { + + lifted175 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTransferCoding(lifted175); + break; + } + case 32: { + + string? lifted182; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted182 = null; + break; + } + + case 1: { + + lifted182 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseContentCoding(lifted182); + break; + } + case 33: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTimeout(); + break; + } + case 34: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpUpgradeFailed(); + break; + } + case 35: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpProtocolError(); + break; + } + case 36: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.loopDetected(); + break; + } + case 37: { + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.configurationError(); + break; + } + case 38: { + + string? lifted199; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted199 = null; + break; + } + + case 1: { + + lifted199 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted200 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.internalError(lifted199); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 24), 1)[0]}"); + } + + lifted201 = Result.err(lifted200); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 16), 1)[0]}"); + } + + lifted204 = Result, None>.ok(lifted201); + break; + } + case 1: { + + lifted204 = Result, None>.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 8), 1)[0]}"); + } + + lifted205 = lifted204; + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted205; + } + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * Represents an outgoing HTTP Response. + */ + + internal class OutgoingResponse: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal OutgoingResponse(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]outgoing-response"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~OutgoingResponse() { + Dispose(false); + } + + internal static class ConstructorWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[constructor]outgoing-response"), WasmImportLinkage] + internal static extern int wasmImportConstructor(int p0); + + } + + internal unsafe OutgoingResponse(global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields headers) + { + var handle = headers.Handle; + headers.Handle = 0; + var result = ConstructorWasmInterop.wasmImportConstructor(handle); + this.Handle = result; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class StatusCodeWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-response.status-code"), WasmImportLinkage] + internal static extern int wasmImportStatusCode(int p0); + + } + + internal unsafe ushort StatusCode() + { + var handle = this.Handle; + var result = StatusCodeWasmInterop.wasmImportStatusCode(handle); + return ((ushort)result); + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SetStatusCodeWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-response.set-status-code"), WasmImportLinkage] + internal static extern int wasmImportSetStatusCode(int p0, int p1); + + } + + internal unsafe void SetStatusCode(ushort statusCode) + { + var handle = this.Handle; + var result = SetStatusCodeWasmInterop.wasmImportSetStatusCode(handle, statusCode); + + Result lifted; + + switch (result) { + case 0: { + + lifted = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {result}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return ; + } else { + throw new WitException(lifted.AsErr!, 0); + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class HeadersWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-response.headers"), WasmImportLinkage] + internal static extern int wasmImportHeaders(int p0); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields Headers() + { + var handle = this.Handle; + var result = HeadersWasmInterop.wasmImportHeaders(handle); + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BodyWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-response.body"), WasmImportLinkage] + internal static extern void wasmImportBody(int p0, nint p1); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.OutgoingBody Body() + { + var handle = this.Handle; + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + BodyWasmInterop.wasmImportBody(handle, ptr); + + Result lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.OutgoingBody(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.OutgoingBody.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)))); + + lifted = Result.ok(resource); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return tmp; + } else { + throw new WitException(lifted.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * Represents an outgoing HTTP Request or Response's Body. + * + * A body has both its contents - a stream of bytes - and a (possibly + * empty) set of trailers, inducating the full contents of the body + * have been sent. This resource represents the contents as an + * `output-stream` child resource, and the completion of the body (with + * optional trailers) with a static function that consumes the + * `outgoing-body` resource, and ensures that the user of this interface + * may not write to the body contents after the body has been finished. + * + * If the user code drops this resource, as opposed to calling the static + * method `finish`, the implementation should treat the body as incomplete, + * and that an error has occured. The implementation should propogate this + * error to the HTTP protocol by whatever means it has available, + * including: corrupting the body on the wire, aborting the associated + * Request, or sending a late status code for the Response. + */ + + internal class OutgoingBody: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal OutgoingBody(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]outgoing-body"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~OutgoingBody() { + Dispose(false); + } + + internal static class WriteWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]outgoing-body.write"), WasmImportLinkage] + internal static extern void wasmImportWrite(int p0, nint p1); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.OutputStream Write() + { + var handle = this.Handle; + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + WriteWasmInterop.wasmImportWrite(handle, ptr); + + Result lifted; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.OutputStream(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.OutputStream.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4)))); + + lifted = Result.ok(resource); + break; + } + case 1: { + + lifted = Result.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted.IsOk) { + var tmp = lifted.AsOk; + return tmp; + } else { + throw new WitException(lifted.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class FinishWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[static]outgoing-body.finish"), WasmImportLinkage] + internal static extern void wasmImportFinish(int p0, int p1, int p2, nint p3); + + } + + internal static unsafe void Finish(global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.OutgoingBody @this, global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields? trailers) + { + var handle = @this.Handle; + @this.Handle = 0; + + int lowered; + int lowered4; + + if (trailers != null) { + var payload2 = (global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.Fields) trailers; + var handle3 = payload2.Handle; + payload2.Handle = 0; + + lowered = 1; + lowered4 = handle3; + + } else { + + lowered = 0; + lowered4 = 0; + + } + + var retArea = new ulong[5]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + FinishWasmInterop.wasmImportFinish(handle, lowered, lowered4, ptr); + + Result lifted197; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted197 = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode lifted196; + + switch (new Span((void*)(ptr + 8), 1)[0]) { + case 0: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.dnsTimeout(); + break; + } + case 1: { + + string? lifted; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + ushort? lifted21; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted21 = null; + break; + } + + case 1: { + + lifted21 = ((ushort)BitConverter.ToUInt16(new Span((void*)(ptr + 30), 2))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.dnsError(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.DnsErrorPayload ( + lifted, lifted21)); + break; + } + case 2: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationNotFound(); + break; + } + case 3: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationUnavailable(); + break; + } + case 4: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationIpProhibited(); + break; + } + case 5: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationIpUnroutable(); + break; + } + case 6: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionRefused(); + break; + } + case 7: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionTerminated(); + break; + } + case 8: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionTimeout(); + break; + } + case 9: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionReadTimeout(); + break; + } + case 10: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionWriteTimeout(); + break; + } + case 11: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionLimitReached(); + break; + } + case 12: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsProtocolError(); + break; + } + case 13: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsCertificateError(); + break; + } + case 14: { + + byte? lifted52; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted52 = null; + break; + } + + case 1: { + + lifted52 = ((byte)new Span((void*)(ptr + 17), 1)[0]); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + string? lifted57; + + switch (new Span((void*)(ptr + 20), 1)[0]) { + case 0: { + lifted57 = null; + break; + } + + case 1: { + + lifted57 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 24), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 28), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 20), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsAlertReceived(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.TlsAlertReceivedPayload ( + lifted52, lifted57)); + break; + } + case 15: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestDenied(); + break; + } + case 16: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestLengthRequired(); + break; + } + case 17: { + + ulong? lifted68; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted68 = null; + break; + } + + case 1: { + + lifted68 = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 24), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestBodySize(lifted68); + break; + } + case 18: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestMethodInvalid(); + break; + } + case 19: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestUriInvalid(); + break; + } + case 20: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestUriTooLong(); + break; + } + case 21: { + + uint? lifted81; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted81 = null; + break; + } + + case 1: { + + lifted81 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestHeaderSectionSize(lifted81); + break; + } + case 22: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload? lifted98; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted98 = null; + break; + } + + case 1: { + + string? lifted92; + + switch (new Span((void*)(ptr + 20), 1)[0]) { + case 0: { + lifted92 = null; + break; + } + + case 1: { + + lifted92 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 24), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 28), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 20), 1)[0])); + } + + uint? lifted97; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted97 = null; + break; + } + + case 1: { + + lifted97 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted98 = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted92, lifted97); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestHeaderSize(lifted98); + break; + } + case 23: { + + uint? lifted105; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted105 = null; + break; + } + + case 1: { + + lifted105 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestTrailerSectionSize(lifted105); + break; + } + case 24: { + + string? lifted112; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted112 = null; + break; + } + + case 1: { + + lifted112 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + uint? lifted117; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted117 = null; + break; + } + + case 1: { + + lifted117 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 32), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestTrailerSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted112, lifted117)); + break; + } + case 25: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseIncomplete(); + break; + } + case 26: { + + uint? lifted126; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted126 = null; + break; + } + + case 1: { + + lifted126 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseHeaderSectionSize(lifted126); + break; + } + case 27: { + + string? lifted133; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted133 = null; + break; + } + + case 1: { + + lifted133 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + uint? lifted138; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted138 = null; + break; + } + + case 1: { + + lifted138 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 32), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseHeaderSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted133, lifted138)); + break; + } + case 28: { + + ulong? lifted145; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted145 = null; + break; + } + + case 1: { + + lifted145 = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 24), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseBodySize(lifted145); + break; + } + case 29: { + + uint? lifted152; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted152 = null; + break; + } + + case 1: { + + lifted152 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTrailerSectionSize(lifted152); + break; + } + case 30: { + + string? lifted159; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted159 = null; + break; + } + + case 1: { + + lifted159 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + uint? lifted164; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted164 = null; + break; + } + + case 1: { + + lifted164 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 32), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTrailerSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted159, lifted164)); + break; + } + case 31: { + + string? lifted171; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted171 = null; + break; + } + + case 1: { + + lifted171 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTransferCoding(lifted171); + break; + } + case 32: { + + string? lifted178; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted178 = null; + break; + } + + case 1: { + + lifted178 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseContentCoding(lifted178); + break; + } + case 33: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTimeout(); + break; + } + case 34: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpUpgradeFailed(); + break; + } + case 35: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpProtocolError(); + break; + } + case 36: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.loopDetected(); + break; + } + case 37: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.configurationError(); + break; + } + case 38: { + + string? lifted195; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted195 = null; + break; + } + + case 1: { + + lifted195 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.internalError(lifted195); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 8), 1)[0]}"); + } + + lifted197 = Result.err(lifted196); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted197.IsOk) { + var tmp = lifted197.AsOk; + return ; + } else { + throw new WitException(lifted197.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * Represents a future which may eventaully return an incoming HTTP + * Response, or an error. + * + * This resource is returned by the `wasi:http/outgoing-handler` interface to + * provide the HTTP Response corresponding to the sent Request. + */ + + internal class FutureIncomingResponse: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal FutureIncomingResponse(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[resource-drop]future-incoming-response"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~FutureIncomingResponse() { + Dispose(false); + } + + internal static class SubscribeWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]future-incoming-response.subscribe"), WasmImportLinkage] + internal static extern int wasmImportSubscribe(int p0); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable Subscribe() + { + var handle = this.Handle; + var result = SubscribeWasmInterop.wasmImportSubscribe(handle); + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class GetWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "[method]future-incoming-response.get"), WasmImportLinkage] + internal static extern void wasmImportGet(int p0, nint p1); + + } + + internal unsafe Result, None>? Get() + { + var handle = this.Handle; + + var retArea = new ulong[7]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + GetWasmInterop.wasmImportGet(handle, ptr); + + Result, None>? lifted200; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted200 = null; + break; + } + + case 1: { + + Result, None> lifted199; + + switch (new Span((void*)(ptr + 8), 1)[0]) { + case 0: { + + Result lifted196; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.IncomingResponse(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.IncomingResponse.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 24), 4)))); + + lifted196 = Result.ok(resource); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode lifted195; + + switch (new Span((void*)(ptr + 24), 1)[0]) { + case 0: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.dnsTimeout(); + break; + } + case 1: { + + string? lifted; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + ushort? lifted20; + + switch (new Span((void*)(ptr + 44), 1)[0]) { + case 0: { + lifted20 = null; + break; + } + + case 1: { + + lifted20 = ((ushort)BitConverter.ToUInt16(new Span((void*)(ptr + 46), 2))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 44), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.dnsError(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.DnsErrorPayload ( + lifted, lifted20)); + break; + } + case 2: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationNotFound(); + break; + } + case 3: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationUnavailable(); + break; + } + case 4: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationIpProhibited(); + break; + } + case 5: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationIpUnroutable(); + break; + } + case 6: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionRefused(); + break; + } + case 7: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionTerminated(); + break; + } + case 8: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionTimeout(); + break; + } + case 9: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionReadTimeout(); + break; + } + case 10: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionWriteTimeout(); + break; + } + case 11: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionLimitReached(); + break; + } + case 12: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsProtocolError(); + break; + } + case 13: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsCertificateError(); + break; + } + case 14: { + + byte? lifted51; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted51 = null; + break; + } + + case 1: { + + lifted51 = ((byte)new Span((void*)(ptr + 33), 1)[0]); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + string? lifted56; + + switch (new Span((void*)(ptr + 36), 1)[0]) { + case 0: { + lifted56 = null; + break; + } + + case 1: { + + lifted56 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 40), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 44), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 36), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsAlertReceived(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.TlsAlertReceivedPayload ( + lifted51, lifted56)); + break; + } + case 15: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestDenied(); + break; + } + case 16: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestLengthRequired(); + break; + } + case 17: { + + ulong? lifted67; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted67 = null; + break; + } + + case 1: { + + lifted67 = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 40), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestBodySize(lifted67); + break; + } + case 18: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestMethodInvalid(); + break; + } + case 19: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestUriInvalid(); + break; + } + case 20: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestUriTooLong(); + break; + } + case 21: { + + uint? lifted80; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted80 = null; + break; + } + + case 1: { + + lifted80 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestHeaderSectionSize(lifted80); + break; + } + case 22: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload? lifted97; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted97 = null; + break; + } + + case 1: { + + string? lifted91; + + switch (new Span((void*)(ptr + 36), 1)[0]) { + case 0: { + lifted91 = null; + break; + } + + case 1: { + + lifted91 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 40), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 44), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 36), 1)[0])); + } + + uint? lifted96; + + switch (new Span((void*)(ptr + 48), 1)[0]) { + case 0: { + lifted96 = null; + break; + } + + case 1: { + + lifted96 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 52), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 48), 1)[0])); + } + + lifted97 = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted91, lifted96); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestHeaderSize(lifted97); + break; + } + case 23: { + + uint? lifted104; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted104 = null; + break; + } + + case 1: { + + lifted104 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestTrailerSectionSize(lifted104); + break; + } + case 24: { + + string? lifted111; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted111 = null; + break; + } + + case 1: { + + lifted111 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + uint? lifted116; + + switch (new Span((void*)(ptr + 44), 1)[0]) { + case 0: { + lifted116 = null; + break; + } + + case 1: { + + lifted116 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 48), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 44), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestTrailerSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted111, lifted116)); + break; + } + case 25: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseIncomplete(); + break; + } + case 26: { + + uint? lifted125; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted125 = null; + break; + } + + case 1: { + + lifted125 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseHeaderSectionSize(lifted125); + break; + } + case 27: { + + string? lifted132; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted132 = null; + break; + } + + case 1: { + + lifted132 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + uint? lifted137; + + switch (new Span((void*)(ptr + 44), 1)[0]) { + case 0: { + lifted137 = null; + break; + } + + case 1: { + + lifted137 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 48), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 44), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseHeaderSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted132, lifted137)); + break; + } + case 28: { + + ulong? lifted144; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted144 = null; + break; + } + + case 1: { + + lifted144 = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 40), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseBodySize(lifted144); + break; + } + case 29: { + + uint? lifted151; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted151 = null; + break; + } + + case 1: { + + lifted151 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTrailerSectionSize(lifted151); + break; + } + case 30: { + + string? lifted158; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted158 = null; + break; + } + + case 1: { + + lifted158 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + uint? lifted163; + + switch (new Span((void*)(ptr + 44), 1)[0]) { + case 0: { + lifted163 = null; + break; + } + + case 1: { + + lifted163 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 48), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 44), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTrailerSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted158, lifted163)); + break; + } + case 31: { + + string? lifted170; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted170 = null; + break; + } + + case 1: { + + lifted170 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTransferCoding(lifted170); + break; + } + case 32: { + + string? lifted177; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted177 = null; + break; + } + + case 1: { + + lifted177 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseContentCoding(lifted177); + break; + } + case 33: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTimeout(); + break; + } + case 34: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpUpgradeFailed(); + break; + } + case 35: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpProtocolError(); + break; + } + case 36: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.loopDetected(); + break; + } + case 37: { + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.configurationError(); + break; + } + case 38: { + + string? lifted194; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted194 = null; + break; + } + + case 1: { + + lifted194 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 40), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted195 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.internalError(lifted194); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 24), 1)[0]}"); + } + + lifted196 = Result.err(lifted195); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 16), 1)[0]}"); + } + + lifted199 = Result, None>.ok(lifted196); + break; + } + case 1: { + + lifted199 = Result, None>.err(new global::WasiHttpWorld.None()); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 8), 1)[0]}"); + } + + lifted200 = lifted199; + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted200; + } + + //TODO: free alloc handle (interopString) if exists + } + + } + +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.http.v0_2_0.OutgoingHandlerInterop.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.http.v0_2_0.OutgoingHandlerInterop.cs new file mode 100644 index 0000000000000..1641b081e2416 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.http.v0_2_0.OutgoingHandlerInterop.cs @@ -0,0 +1,665 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld.wit.imports.wasi.http.v0_2_0 +{ + internal static class OutgoingHandlerInterop { + + internal static class HandleWasmInterop + { + [DllImport("wasi:http/outgoing-handler@0.2.0", EntryPoint = "handle"), WasmImportLinkage] + internal static extern void wasmImportHandle(int p0, int p1, int p2, nint p3); + + } + + internal static unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FutureIncomingResponse Handle(global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.OutgoingRequest request, global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.RequestOptions? options) + { + var handle = request.Handle; + request.Handle = 0; + + int lowered; + int lowered4; + + if (options != null) { + var payload2 = (global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.RequestOptions) options; + var handle3 = payload2.Handle; + payload2.Handle = 0; + + lowered = 1; + lowered4 = handle3; + + } else { + + lowered = 0; + lowered4 = 0; + + } + + var retArea = new ulong[5]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + HandleWasmInterop.wasmImportHandle(handle, lowered, lowered4, ptr); + + Result lifted197; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FutureIncomingResponse(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FutureIncomingResponse.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + + lifted197 = Result.ok(resource); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode lifted196; + + switch (new Span((void*)(ptr + 8), 1)[0]) { + case 0: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.dnsTimeout(); + break; + } + case 1: { + + string? lifted; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + ushort? lifted21; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted21 = null; + break; + } + + case 1: { + + lifted21 = ((ushort)BitConverter.ToUInt16(new Span((void*)(ptr + 30), 2))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.dnsError(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.DnsErrorPayload ( + lifted, lifted21)); + break; + } + case 2: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationNotFound(); + break; + } + case 3: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationUnavailable(); + break; + } + case 4: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationIpProhibited(); + break; + } + case 5: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationIpUnroutable(); + break; + } + case 6: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionRefused(); + break; + } + case 7: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionTerminated(); + break; + } + case 8: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionTimeout(); + break; + } + case 9: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionReadTimeout(); + break; + } + case 10: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionWriteTimeout(); + break; + } + case 11: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionLimitReached(); + break; + } + case 12: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsProtocolError(); + break; + } + case 13: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsCertificateError(); + break; + } + case 14: { + + byte? lifted52; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted52 = null; + break; + } + + case 1: { + + lifted52 = ((byte)new Span((void*)(ptr + 17), 1)[0]); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + string? lifted57; + + switch (new Span((void*)(ptr + 20), 1)[0]) { + case 0: { + lifted57 = null; + break; + } + + case 1: { + + lifted57 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 24), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 28), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 20), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsAlertReceived(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.TlsAlertReceivedPayload ( + lifted52, lifted57)); + break; + } + case 15: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestDenied(); + break; + } + case 16: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestLengthRequired(); + break; + } + case 17: { + + ulong? lifted68; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted68 = null; + break; + } + + case 1: { + + lifted68 = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 24), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestBodySize(lifted68); + break; + } + case 18: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestMethodInvalid(); + break; + } + case 19: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestUriInvalid(); + break; + } + case 20: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestUriTooLong(); + break; + } + case 21: { + + uint? lifted81; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted81 = null; + break; + } + + case 1: { + + lifted81 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestHeaderSectionSize(lifted81); + break; + } + case 22: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload? lifted98; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted98 = null; + break; + } + + case 1: { + + string? lifted92; + + switch (new Span((void*)(ptr + 20), 1)[0]) { + case 0: { + lifted92 = null; + break; + } + + case 1: { + + lifted92 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 24), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 28), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 20), 1)[0])); + } + + uint? lifted97; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted97 = null; + break; + } + + case 1: { + + lifted97 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted98 = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted92, lifted97); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestHeaderSize(lifted98); + break; + } + case 23: { + + uint? lifted105; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted105 = null; + break; + } + + case 1: { + + lifted105 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestTrailerSectionSize(lifted105); + break; + } + case 24: { + + string? lifted112; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted112 = null; + break; + } + + case 1: { + + lifted112 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + uint? lifted117; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted117 = null; + break; + } + + case 1: { + + lifted117 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 32), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestTrailerSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted112, lifted117)); + break; + } + case 25: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseIncomplete(); + break; + } + case 26: { + + uint? lifted126; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted126 = null; + break; + } + + case 1: { + + lifted126 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseHeaderSectionSize(lifted126); + break; + } + case 27: { + + string? lifted133; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted133 = null; + break; + } + + case 1: { + + lifted133 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + uint? lifted138; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted138 = null; + break; + } + + case 1: { + + lifted138 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 32), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseHeaderSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted133, lifted138)); + break; + } + case 28: { + + ulong? lifted145; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted145 = null; + break; + } + + case 1: { + + lifted145 = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 24), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseBodySize(lifted145); + break; + } + case 29: { + + uint? lifted152; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted152 = null; + break; + } + + case 1: { + + lifted152 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTrailerSectionSize(lifted152); + break; + } + case 30: { + + string? lifted159; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted159 = null; + break; + } + + case 1: { + + lifted159 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + uint? lifted164; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted164 = null; + break; + } + + case 1: { + + lifted164 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 32), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTrailerSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted159, lifted164)); + break; + } + case 31: { + + string? lifted171; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted171 = null; + break; + } + + case 1: { + + lifted171 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTransferCoding(lifted171); + break; + } + case 32: { + + string? lifted178; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted178 = null; + break; + } + + case 1: { + + lifted178 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseContentCoding(lifted178); + break; + } + case 33: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTimeout(); + break; + } + case 34: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpUpgradeFailed(); + break; + } + case 35: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpProtocolError(); + break; + } + case 36: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.loopDetected(); + break; + } + case 37: { + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.configurationError(); + break; + } + case 38: { + + string? lifted195; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted195 = null; + break; + } + + case 1: { + + lifted195 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted196 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.internalError(lifted195); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 8), 1)[0]}"); + } + + lifted197 = Result.err(lifted196); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted197.IsOk) { + var tmp = lifted197.AsOk; + return tmp; + } else { + throw new WitException(lifted197.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + } +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.http.v0_2_0.TypesInterop.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.http.v0_2_0.TypesInterop.cs new file mode 100644 index 0000000000000..cc21627df23c8 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.http.v0_2_0.TypesInterop.cs @@ -0,0 +1,640 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld.wit.imports.wasi.http.v0_2_0 +{ + internal static class TypesInterop { + + internal static class HttpErrorCodeWasmInterop + { + [DllImport("wasi:http/types@0.2.0", EntryPoint = "http-error-code"), WasmImportLinkage] + internal static extern void wasmImportHttpErrorCode(int p0, nint p1); + + } + + internal static unsafe global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode? HttpErrorCode(global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error err) + { + var handle = err.Handle; + + var retArea = new ulong[5]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + HttpErrorCodeWasmInterop.wasmImportHttpErrorCode(handle, ptr); + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode? lifted190; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + lifted190 = null; + break; + } + + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode lifted189; + + switch (new Span((void*)(ptr + 8), 1)[0]) { + case 0: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.dnsTimeout(); + break; + } + case 1: { + + string? lifted; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted = null; + break; + } + + case 1: { + + lifted = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + ushort? lifted14; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted14 = null; + break; + } + + case 1: { + + lifted14 = ((ushort)BitConverter.ToUInt16(new Span((void*)(ptr + 30), 2))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.dnsError(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.DnsErrorPayload ( + lifted, lifted14)); + break; + } + case 2: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationNotFound(); + break; + } + case 3: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationUnavailable(); + break; + } + case 4: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationIpProhibited(); + break; + } + case 5: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.destinationIpUnroutable(); + break; + } + case 6: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionRefused(); + break; + } + case 7: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionTerminated(); + break; + } + case 8: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionTimeout(); + break; + } + case 9: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionReadTimeout(); + break; + } + case 10: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionWriteTimeout(); + break; + } + case 11: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.connectionLimitReached(); + break; + } + case 12: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsProtocolError(); + break; + } + case 13: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsCertificateError(); + break; + } + case 14: { + + byte? lifted45; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted45 = null; + break; + } + + case 1: { + + lifted45 = ((byte)new Span((void*)(ptr + 17), 1)[0]); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + string? lifted50; + + switch (new Span((void*)(ptr + 20), 1)[0]) { + case 0: { + lifted50 = null; + break; + } + + case 1: { + + lifted50 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 24), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 28), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 20), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.tlsAlertReceived(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.TlsAlertReceivedPayload ( + lifted45, lifted50)); + break; + } + case 15: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestDenied(); + break; + } + case 16: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestLengthRequired(); + break; + } + case 17: { + + ulong? lifted61; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted61 = null; + break; + } + + case 1: { + + lifted61 = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 24), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestBodySize(lifted61); + break; + } + case 18: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestMethodInvalid(); + break; + } + case 19: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestUriInvalid(); + break; + } + case 20: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestUriTooLong(); + break; + } + case 21: { + + uint? lifted74; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted74 = null; + break; + } + + case 1: { + + lifted74 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestHeaderSectionSize(lifted74); + break; + } + case 22: { + + global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload? lifted91; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted91 = null; + break; + } + + case 1: { + + string? lifted85; + + switch (new Span((void*)(ptr + 20), 1)[0]) { + case 0: { + lifted85 = null; + break; + } + + case 1: { + + lifted85 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 24), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 28), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 20), 1)[0])); + } + + uint? lifted90; + + switch (new Span((void*)(ptr + 32), 1)[0]) { + case 0: { + lifted90 = null; + break; + } + + case 1: { + + lifted90 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 36), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 32), 1)[0])); + } + + lifted91 = new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted85, lifted90); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestHeaderSize(lifted91); + break; + } + case 23: { + + uint? lifted98; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted98 = null; + break; + } + + case 1: { + + lifted98 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestTrailerSectionSize(lifted98); + break; + } + case 24: { + + string? lifted105; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted105 = null; + break; + } + + case 1: { + + lifted105 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + uint? lifted110; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted110 = null; + break; + } + + case 1: { + + lifted110 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 32), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpRequestTrailerSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted105, lifted110)); + break; + } + case 25: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseIncomplete(); + break; + } + case 26: { + + uint? lifted119; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted119 = null; + break; + } + + case 1: { + + lifted119 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseHeaderSectionSize(lifted119); + break; + } + case 27: { + + string? lifted126; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted126 = null; + break; + } + + case 1: { + + lifted126 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + uint? lifted131; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted131 = null; + break; + } + + case 1: { + + lifted131 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 32), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseHeaderSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted126, lifted131)); + break; + } + case 28: { + + ulong? lifted138; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted138 = null; + break; + } + + case 1: { + + lifted138 = unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 24), 8)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseBodySize(lifted138); + break; + } + case 29: { + + uint? lifted145; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted145 = null; + break; + } + + case 1: { + + lifted145 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTrailerSectionSize(lifted145); + break; + } + case 30: { + + string? lifted152; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted152 = null; + break; + } + + case 1: { + + lifted152 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + uint? lifted157; + + switch (new Span((void*)(ptr + 28), 1)[0]) { + case 0: { + lifted157 = null; + break; + } + + case 1: { + + lifted157 = unchecked((uint)(BitConverter.ToInt32(new Span((void*)(ptr + 32), 4)))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 28), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTrailerSize(new global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.FieldSizePayload ( + lifted152, lifted157)); + break; + } + case 31: { + + string? lifted164; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted164 = null; + break; + } + + case 1: { + + lifted164 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTransferCoding(lifted164); + break; + } + case 32: { + + string? lifted171; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted171 = null; + break; + } + + case 1: { + + lifted171 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseContentCoding(lifted171); + break; + } + case 33: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpResponseTimeout(); + break; + } + case 34: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpUpgradeFailed(); + break; + } + case 35: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.httpProtocolError(); + break; + } + case 36: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.loopDetected(); + break; + } + case 37: { + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.configurationError(); + break; + } + case 38: { + + string? lifted188; + + switch (new Span((void*)(ptr + 16), 1)[0]) { + case 0: { + lifted188 = null; + break; + } + + case 1: { + + lifted188 = Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 20), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 24), 4))); + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 16), 1)[0])); + } + + lifted189 = global::WasiHttpWorld.wit.imports.wasi.http.v0_2_0.ITypes.ErrorCode.internalError(lifted188); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 8), 1)[0]}"); + } + + lifted190 = lifted189; + break; + } + + default: throw new ArgumentException("invalid discriminant: " + (new Span((void*)(ptr + 0), 1)[0])); + } + return lifted190; + } + + //TODO: free alloc handle (interopString) if exists + } + + } +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.ErrorInterop.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.ErrorInterop.cs new file mode 100644 index 0000000000000..f546b50aa2a12 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.ErrorInterop.cs @@ -0,0 +1,19 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld.wit.imports.wasi.io.v0_2_0 +{ + internal static class ErrorInterop { + + } +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.cs new file mode 100644 index 0000000000000..96dc6d4bd8263 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.cs @@ -0,0 +1,92 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld.wit.imports.wasi.io.v0_2_0; + +internal interface IError { + + /** + * A resource which represents some error information. + * + * The only method provided by this resource is `to-debug-string`, + * which provides some human-readable information about the error. + * + * In the `wasi:io` package, this resource is returned through the + * `wasi:io/streams/stream-error` type. + * + * To provide more specific error information, other interfaces may + * provide functions to further "downcast" this error into more specific + * error information. For example, `error`s returned in streams derived + * from filesystem types to be described using the filesystem's own + * error-code type, using the function + * `wasi:filesystem/types/filesystem-error-code`, which takes a parameter + * `borrow<error>` and returns + * `option<wasi:filesystem/types/error-code>`. + * + * The set of functions which can "downcast" an `error` into a more + * concrete type is open. + */ + + internal class Error: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal Error(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:io/error@0.2.0", EntryPoint = "[resource-drop]error"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~Error() { + Dispose(false); + } + + internal static class ToDebugStringWasmInterop + { + [DllImport("wasi:io/error@0.2.0", EntryPoint = "[method]error.to-debug-string"), WasmImportLinkage] + internal static extern void wasmImportToDebugString(int p0, nint p1); + + } + + internal unsafe string ToDebugString() + { + var handle = this.Handle; + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + ToDebugStringWasmInterop.wasmImportToDebugString(handle, ptr); + return Encoding.UTF8.GetString((byte*)BitConverter.ToInt32(new Span((void*)(ptr + 0), 4)), BitConverter.ToInt32(new Span((void*)(ptr + 4), 4))); + } + + //TODO: free alloc handle (interopString) if exists + } + + } + +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.cs new file mode 100644 index 0000000000000..2d8ecda8659a7 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.cs @@ -0,0 +1,83 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld.wit.imports.wasi.io.v0_2_0; + +internal interface IPoll { + + /** + * `pollable` represents a single I/O event which may be ready, or not. + */ + + internal class Pollable: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal Pollable(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:io/poll@0.2.0", EntryPoint = "[resource-drop]pollable"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~Pollable() { + Dispose(false); + } + + internal static class ReadyWasmInterop + { + [DllImport("wasi:io/poll@0.2.0", EntryPoint = "[method]pollable.ready"), WasmImportLinkage] + internal static extern int wasmImportReady(int p0); + + } + + internal unsafe bool Ready() + { + var handle = this.Handle; + var result = ReadyWasmInterop.wasmImportReady(handle); + return (result != 0); + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BlockWasmInterop + { + [DllImport("wasi:io/poll@0.2.0", EntryPoint = "[method]pollable.block"), WasmImportLinkage] + internal static extern void wasmImportBlock(int p0); + + } + + internal unsafe void Block() + { + var handle = this.Handle; + BlockWasmInterop.wasmImportBlock(handle); + + //TODO: free alloc handle (interopString) if exists + } + + } + +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.cs new file mode 100644 index 0000000000000..465e30cf8a0ad --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.cs @@ -0,0 +1,990 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld.wit.imports.wasi.io.v0_2_0; + +internal interface IStreams { + + /** + * An error for input-stream and output-stream operations. + */ + + internal class StreamError { + internal readonly byte Tag; + private readonly object? value; + + private StreamError(byte tag, object? value) { + this.Tag = tag; + this.value = value; + } + + internal static StreamError lastOperationFailed(global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error lastOperationFailed) { + return new StreamError(LAST_OPERATION_FAILED, lastOperationFailed); + } + + internal static StreamError closed() { + return new StreamError(CLOSED, null); + } + + internal global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error AsLastOperationFailed + { + get + { + if (Tag == LAST_OPERATION_FAILED) + return (global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error)value!; + else + throw new ArgumentException("expected LAST_OPERATION_FAILED, got " + Tag); + } + } + + internal const byte LAST_OPERATION_FAILED = 0; + internal const byte CLOSED = 1; + } + + /** + * An input bytestream. + * + * `input-stream`s are *non-blocking* to the extent practical on underlying + * platforms. I/O operations always return promptly; if fewer bytes are + * promptly available than requested, they return the number of bytes promptly + * available, which could even be zero. To wait for data to be available, + * use the `subscribe` function to obtain a `pollable` which can be polled + * for using `wasi:io/poll`. + */ + + internal class InputStream: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal InputStream(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[resource-drop]input-stream"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~InputStream() { + Dispose(false); + } + + internal static class ReadWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]input-stream.read"), WasmImportLinkage] + internal static extern void wasmImportRead(int p0, long p1, nint p2); + + } + + internal unsafe byte[] Read(ulong len) + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + ReadWasmInterop.wasmImportRead(handle, unchecked((long)(len)), ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + var array = new byte[BitConverter.ToInt32(new Span((void*)(ptr + 8), 4))]; + new Span((void*)(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4))), BitConverter.ToInt32(new Span((void*)(ptr + 8), 4))).CopyTo(new Span(array)); + + lifted6 = Result.ok(array); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return tmp; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BlockingReadWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]input-stream.blocking-read"), WasmImportLinkage] + internal static extern void wasmImportBlockingRead(int p0, long p1, nint p2); + + } + + internal unsafe byte[] BlockingRead(ulong len) + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + BlockingReadWasmInterop.wasmImportBlockingRead(handle, unchecked((long)(len)), ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + var array = new byte[BitConverter.ToInt32(new Span((void*)(ptr + 8), 4))]; + new Span((void*)(BitConverter.ToInt32(new Span((void*)(ptr + 4), 4))), BitConverter.ToInt32(new Span((void*)(ptr + 8), 4))).CopyTo(new Span(array)); + + lifted6 = Result.ok(array); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return tmp; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SkipWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]input-stream.skip"), WasmImportLinkage] + internal static extern void wasmImportSkip(int p0, long p1, nint p2); + + } + + internal unsafe ulong Skip(ulong len) + { + var handle = this.Handle; + + var retArea = new ulong[2]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + SkipWasmInterop.wasmImportSkip(handle, unchecked((long)(len)), ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted6 = Result.ok(unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 8), 8))))); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 8), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 12), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 8), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return tmp; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BlockingSkipWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]input-stream.blocking-skip"), WasmImportLinkage] + internal static extern void wasmImportBlockingSkip(int p0, long p1, nint p2); + + } + + internal unsafe ulong BlockingSkip(ulong len) + { + var handle = this.Handle; + + var retArea = new ulong[2]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + BlockingSkipWasmInterop.wasmImportBlockingSkip(handle, unchecked((long)(len)), ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted6 = Result.ok(unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 8), 8))))); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 8), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 12), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 8), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return tmp; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SubscribeWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]input-stream.subscribe"), WasmImportLinkage] + internal static extern int wasmImportSubscribe(int p0); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable Subscribe() + { + var handle = this.Handle; + var result = SubscribeWasmInterop.wasmImportSubscribe(handle); + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + } + + /** + * An output bytestream. + * + * `output-stream`s are *non-blocking* to the extent practical on + * underlying platforms. Except where specified otherwise, I/O operations also + * always return promptly, after the number of bytes that can be written + * promptly, which could even be zero. To wait for the stream to be ready to + * accept data, the `subscribe` function to obtain a `pollable` which can be + * polled for using `wasi:io/poll`. + */ + + internal class OutputStream: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal OutputStream(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[resource-drop]output-stream"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~OutputStream() { + Dispose(false); + } + + internal static class CheckWriteWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]output-stream.check-write"), WasmImportLinkage] + internal static extern void wasmImportCheckWrite(int p0, nint p1); + + } + + internal unsafe ulong CheckWrite() + { + var handle = this.Handle; + + var retArea = new ulong[2]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + CheckWriteWasmInterop.wasmImportCheckWrite(handle, ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted6 = Result.ok(unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 8), 8))))); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 8), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 12), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 8), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return tmp; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class WriteWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]output-stream.write"), WasmImportLinkage] + internal static extern void wasmImportWrite(int p0, nint p1, int p2, nint p3); + + } + + internal unsafe void Write(byte[] contents) + { + var handle = this.Handle; + + void* buffer = stackalloc byte[(contents).Length]; + contents.AsSpan().CopyTo(new Span(buffer, contents.Length)); + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + WriteWasmInterop.wasmImportWrite(handle, (int)buffer, (contents).Length, ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted6 = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return ; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BlockingWriteAndFlushWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]output-stream.blocking-write-and-flush"), WasmImportLinkage] + internal static extern void wasmImportBlockingWriteAndFlush(int p0, nint p1, int p2, nint p3); + + } + + internal unsafe void BlockingWriteAndFlush(byte[] contents) + { + var handle = this.Handle; + + void* buffer = stackalloc byte[(contents).Length]; + contents.AsSpan().CopyTo(new Span(buffer, contents.Length)); + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + BlockingWriteAndFlushWasmInterop.wasmImportBlockingWriteAndFlush(handle, (int)buffer, (contents).Length, ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted6 = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return ; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class FlushWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]output-stream.flush"), WasmImportLinkage] + internal static extern void wasmImportFlush(int p0, nint p1); + + } + + internal unsafe void Flush() + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + FlushWasmInterop.wasmImportFlush(handle, ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted6 = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return ; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BlockingFlushWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]output-stream.blocking-flush"), WasmImportLinkage] + internal static extern void wasmImportBlockingFlush(int p0, nint p1); + + } + + internal unsafe void BlockingFlush() + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + BlockingFlushWasmInterop.wasmImportBlockingFlush(handle, ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted6 = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return ; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SubscribeWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]output-stream.subscribe"), WasmImportLinkage] + internal static extern int wasmImportSubscribe(int p0); + + } + + internal unsafe global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable Subscribe() + { + var handle = this.Handle; + var result = SubscribeWasmInterop.wasmImportSubscribe(handle); + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable.THandle(result)); + return resource; + + //TODO: free alloc handle (interopString) if exists + } + + internal static class WriteZeroesWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]output-stream.write-zeroes"), WasmImportLinkage] + internal static extern void wasmImportWriteZeroes(int p0, long p1, nint p2); + + } + + internal unsafe void WriteZeroes(ulong len) + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + WriteZeroesWasmInterop.wasmImportWriteZeroes(handle, unchecked((long)(len)), ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted6 = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return ; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BlockingWriteZeroesAndFlushWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]output-stream.blocking-write-zeroes-and-flush"), WasmImportLinkage] + internal static extern void wasmImportBlockingWriteZeroesAndFlush(int p0, long p1, nint p2); + + } + + internal unsafe void BlockingWriteZeroesAndFlush(ulong len) + { + var handle = this.Handle; + + var retArea = new uint[3]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + BlockingWriteZeroesAndFlushWasmInterop.wasmImportBlockingWriteZeroesAndFlush(handle, unchecked((long)(len)), ptr); + + Result lifted6; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted6 = Result.ok(new global::WasiHttpWorld.None()); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 4), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 8), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 4), 1)[0]}"); + } + + lifted6 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted6.IsOk) { + var tmp = lifted6.AsOk; + return ; + } else { + throw new WitException(lifted6.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class SpliceWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]output-stream.splice"), WasmImportLinkage] + internal static extern void wasmImportSplice(int p0, int p1, long p2, nint p3); + + } + + internal unsafe ulong Splice(global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.InputStream src, ulong len) + { + var handle = this.Handle; + var handle0 = src.Handle; + + var retArea = new ulong[2]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + SpliceWasmInterop.wasmImportSplice(handle, handle0, unchecked((long)(len)), ptr); + + Result lifted7; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted7 = Result.ok(unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 8), 8))))); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 8), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 12), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 8), 1)[0]}"); + } + + lifted7 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted7.IsOk) { + var tmp = lifted7.AsOk; + return tmp; + } else { + throw new WitException(lifted7.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BlockingSpliceWasmInterop + { + [DllImport("wasi:io/streams@0.2.0", EntryPoint = "[method]output-stream.blocking-splice"), WasmImportLinkage] + internal static extern void wasmImportBlockingSplice(int p0, int p1, long p2, nint p3); + + } + + internal unsafe ulong BlockingSplice(global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.InputStream src, ulong len) + { + var handle = this.Handle; + var handle0 = src.Handle; + + var retArea = new ulong[2]; + fixed (ulong* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + BlockingSpliceWasmInterop.wasmImportBlockingSplice(handle, handle0, unchecked((long)(len)), ptr); + + Result lifted7; + + switch (new Span((void*)(ptr + 0), 1)[0]) { + case 0: { + + lifted7 = Result.ok(unchecked((ulong)(BitConverter.ToInt64(new Span((void*)(ptr + 8), 8))))); + break; + } + case 1: { + + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError lifted; + + switch (new Span((void*)(ptr + 8), 1)[0]) { + case 0: { + var resource = new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error(new global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IError.Error.THandle(BitConverter.ToInt32(new Span((void*)(ptr + 12), 4)))); + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.lastOperationFailed(resource); + break; + } + case 1: { + + lifted = global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IStreams.StreamError.closed(); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 8), 1)[0]}"); + } + + lifted7 = Result.err(lifted); + break; + } + + default: throw new ArgumentException($"invalid discriminant: {new Span((void*)(ptr + 0), 1)[0]}"); + } + if (lifted7.IsOk) { + var tmp = lifted7.AsOk; + return tmp; + } else { + throw new WitException(lifted7.AsErr!, 0); + } + } + + //TODO: free alloc handle (interopString) if exists + } + + } + +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.PollInterop.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.PollInterop.cs new file mode 100644 index 0000000000000..0f2fe911fae82 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.PollInterop.cs @@ -0,0 +1,56 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld.wit.imports.wasi.io.v0_2_0 +{ + internal static class PollInterop { + + internal static class PollWasmInterop + { + [DllImport("wasi:io/poll@0.2.0", EntryPoint = "poll"), WasmImportLinkage] + internal static extern void wasmImportPoll(nint p0, int p1, nint p2); + + } + + internal static unsafe uint[] Poll(List @in) + { + + byte[] buffer = new byte[4 * @in.Count]; + var gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); + var address = gcHandle.AddrOfPinnedObject(); + + for (int index = 0; index < @in.Count; ++index) { + global::WasiHttpWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable element = @in[index]; + int basePtr = (int)address + (index * 4); + var handle = element.Handle; + BitConverter.TryWriteBytes(new Span((void*)(basePtr + 0), 4), unchecked((int)handle)); + + } + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + PollWasmInterop.wasmImportPoll((int)address, @in.Count, ptr); + + var array = new uint[BitConverter.ToInt32(new Span((void*)(ptr + 4), 4))]; + new Span((void*)(BitConverter.ToInt32(new Span((void*)(ptr + 0), 4))), BitConverter.ToInt32(new Span((void*)(ptr + 4), 4))).CopyTo(new Span(array)); + gcHandle.Free(); + return array; + } + + //TODO: free alloc handle (interopString) if exists + } + + } +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.StreamsInterop.cs b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.StreamsInterop.cs new file mode 100644 index 0000000000000..1f301c70475b0 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld.wit.imports.wasi.io.v0_2_0.StreamsInterop.cs @@ -0,0 +1,19 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiHttpWorld.wit.imports.wasi.io.v0_2_0 +{ + internal static class StreamsInterop { + + } +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld_component_type.wit b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld_component_type.wit new file mode 100644 index 0000000000000..219ff8341416f --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld_component_type.wit @@ -0,0 +1,270 @@ +package wasi:io@0.2.0 { + interface poll { + resource pollable { + ready: func() -> bool; + block: func(); + } + + poll: func(in: list>) -> list; + } + + interface error { + resource error { + to-debug-string: func() -> string; + } + } + + interface streams { + use error.{error}; + use poll.{pollable}; + + variant stream-error { + last-operation-failed(error), + closed, + } + + resource input-stream { + read: func(len: u64) -> result, stream-error>; + blocking-read: func(len: u64) -> result, stream-error>; + skip: func(len: u64) -> result; + blocking-skip: func(len: u64) -> result; + subscribe: func() -> pollable; + } + + resource output-stream { + check-write: func() -> result; + write: func(contents: list) -> result<_, stream-error>; + blocking-write-and-flush: func(contents: list) -> result<_, stream-error>; + flush: func() -> result<_, stream-error>; + blocking-flush: func() -> result<_, stream-error>; + subscribe: func() -> pollable; + write-zeroes: func(len: u64) -> result<_, stream-error>; + blocking-write-zeroes-and-flush: func(len: u64) -> result<_, stream-error>; + splice: func(src: borrow, len: u64) -> result; + blocking-splice: func(src: borrow, len: u64) -> result; + } + } + +} + + +package wasi:clocks@0.2.0 { + interface monotonic-clock { + use wasi:io/poll@0.2.0.{pollable}; + + type instant = u64; + + type duration = u64; + + now: func() -> instant; + + resolution: func() -> duration; + + subscribe-instant: func(when: instant) -> pollable; + + subscribe-duration: func(when: duration) -> pollable; + } + +} + + +package wasi:http@0.2.0 { + interface types { + use wasi:clocks/monotonic-clock@0.2.0.{duration}; + use wasi:io/streams@0.2.0.{input-stream, output-stream}; + use wasi:io/error@0.2.0.{error as io-error}; + use wasi:io/poll@0.2.0.{pollable}; + + variant method { + get, + head, + post, + put, + delete, + connect, + options, + trace, + patch, + other(string), + } + + variant scheme { + HTTP, + HTTPS, + other(string), + } + + record DNS-error-payload { + rcode: option, + info-code: option, + } + + record TLS-alert-received-payload { + alert-id: option, + alert-message: option, + } + + record field-size-payload { + field-name: option, + field-size: option, + } + + variant error-code { + DNS-timeout, + DNS-error(DNS-error-payload), + destination-not-found, + destination-unavailable, + destination-IP-prohibited, + destination-IP-unroutable, + connection-refused, + connection-terminated, + connection-timeout, + connection-read-timeout, + connection-write-timeout, + connection-limit-reached, + TLS-protocol-error, + TLS-certificate-error, + TLS-alert-received(TLS-alert-received-payload), + HTTP-request-denied, + HTTP-request-length-required, + HTTP-request-body-size(option), + HTTP-request-method-invalid, + HTTP-request-URI-invalid, + HTTP-request-URI-too-long, + HTTP-request-header-section-size(option), + HTTP-request-header-size(option), + HTTP-request-trailer-section-size(option), + HTTP-request-trailer-size(field-size-payload), + HTTP-response-incomplete, + HTTP-response-header-section-size(option), + HTTP-response-header-size(field-size-payload), + HTTP-response-body-size(option), + HTTP-response-trailer-section-size(option), + HTTP-response-trailer-size(field-size-payload), + HTTP-response-transfer-coding(option), + HTTP-response-content-coding(option), + HTTP-response-timeout, + HTTP-upgrade-failed, + HTTP-protocol-error, + loop-detected, + configuration-error, + internal-error(option), + } + + variant header-error { + invalid-syntax, + forbidden, + immutable, + } + + type field-key = string; + + type field-value = list; + + resource fields { + constructor(); + from-list: static func(entries: list>) -> result; + get: func(name: field-key) -> list; + has: func(name: field-key) -> bool; + set: func(name: field-key, value: list) -> result<_, header-error>; + delete: func(name: field-key) -> result<_, header-error>; + append: func(name: field-key, value: field-value) -> result<_, header-error>; + entries: func() -> list>; + clone: func() -> fields; + } + + type headers = fields; + + type trailers = fields; + + resource incoming-request { + method: func() -> method; + path-with-query: func() -> option; + scheme: func() -> option; + authority: func() -> option; + headers: func() -> headers; + consume: func() -> result; + } + + resource outgoing-request { + constructor(headers: headers); + body: func() -> result; + method: func() -> method; + set-method: func(method: method) -> result; + path-with-query: func() -> option; + set-path-with-query: func(path-with-query: option) -> result; + scheme: func() -> option; + set-scheme: func(scheme: option) -> result; + authority: func() -> option; + set-authority: func(authority: option) -> result; + headers: func() -> headers; + } + + resource request-options { + constructor(); + connect-timeout: func() -> option; + set-connect-timeout: func(duration: option) -> result; + first-byte-timeout: func() -> option; + set-first-byte-timeout: func(duration: option) -> result; + between-bytes-timeout: func() -> option; + set-between-bytes-timeout: func(duration: option) -> result; + } + + resource response-outparam { + set: static func(param: response-outparam, response: result); + } + + type status-code = u16; + + resource incoming-response { + status: func() -> status-code; + headers: func() -> headers; + consume: func() -> result; + } + + resource incoming-body { + %stream: func() -> result; + finish: static func(this: incoming-body) -> future-trailers; + } + + resource future-trailers { + subscribe: func() -> pollable; + get: func() -> option, error-code>>>; + } + + resource outgoing-response { + constructor(headers: headers); + status-code: func() -> status-code; + set-status-code: func(status-code: status-code) -> result; + headers: func() -> headers; + body: func() -> result; + } + + resource outgoing-body { + write: func() -> result; + finish: static func(this: outgoing-body, trailers: option) -> result<_, error-code>; + } + + resource future-incoming-response { + subscribe: func() -> pollable; + get: func() -> option>>; + } + + http-error-code: func(err: borrow) -> option; + } + + interface outgoing-handler { + use types.{outgoing-request, request-options, future-incoming-response, error-code}; + + handle: func(request: outgoing-request, options: option) -> result; + } + + world wasi-http { + import wasi:io/poll@0.2.0; + import wasi:clocks/monotonic-clock@0.2.0; + import wasi:io/error@0.2.0; + import wasi:io/streams@0.2.0; + import types; + import outgoing-handler; + } +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/generate-wasi-http-bindings.sh b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/generate-wasi-http-bindings.sh new file mode 100644 index 0000000000000..e58891f50877c --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/generate-wasi-http-bindings.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +set -ex + +# This script will regenerate the `wit-bindgen`-generated files in this +# directory. + +# Prerequisites: +# POSIX shell +# tar +# [cargo](https://rustup.rs/) +# [curl](https://curl.se/download.html) + +# TODO: Update to the next crates.io release containing fa19e08a884ec62f95191319d8d296874424c736: +cargo install --locked --no-default-features --features csharp --git https://github.com/bytecodealliance/wit-bindgen --rev fa19e08a884ec62f95191319d8d296874424c736 wit-bindgen-cli +curl -OL https://github.com/WebAssembly/wasi-http/archive/refs/tags/v0.2.0.tar.gz +tar xzf v0.2.0.tar.gz +cat >wasi-http-0.2.0/wit/world.wit < - + ../../src/Resources/Strings.resx $(DefineConstants);SYSNETHTTP_NO_OPENSSL;HTTP3 true true - $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-android;$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-osx + $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-android;$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-wasi;$(NetCoreAppCurrent)-osx true true $(NoWarn);SYSLIB0057 @@ -30,11 +30,16 @@ true + + WasmTestOnWasmtime + $(DefineConstants);TARGET_WASI + + $(DefineConstants);TARGETS_ANDROID - + @@ -42,7 +47,10 @@ - + + + + diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 533ff2cb96b21..0b21a8c4e16c2 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -2801,4 +2801,10 @@ + + + + + + \ No newline at end of file diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.Unix.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.Unix.cs index 895cbac36a3f9..2535f8a413966 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.Unix.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.Unix.cs @@ -10,6 +10,19 @@ namespace System.Threading { public sealed partial class Thread { + // these methods are temporarily accessed via UnsafeAccessor from generated code until we have it in public API, probably in WASI preview3 and promises +#if TARGET_WASI + internal static System.Threading.Tasks.Task RegisterWasiPollable(int handle) + { + return WasiEventLoop.RegisterWasiPollable(handle); + } + + internal static void DispatchWasiEventLoop() + { + WasiEventLoop.DispatchWasiEventLoop(); + } +#endif + // the closest analog to Sleep(0) on Unix is sched_yield internal static void UninterruptibleSleep0() => Thread.Yield(); diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs index 832b3f773a11f..bc0fe4556bb31 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs @@ -906,7 +906,10 @@ internal static bool Dispatch() // thread because it sees a Determining or Scheduled stage, and the current thread is the last thread processing // work items, the current thread must either see the work item queued by the enqueuer, or it must see a stage of // Scheduled, and try to dequeue again or request another thread. +#if !TARGET_WASI + // TODO https://github.com/dotnet/runtime/issues/104803 Debug.Assert(workQueue._separated.queueProcessingStage == QueueProcessingStage.Scheduled); +#endif workQueue._separated.queueProcessingStage = QueueProcessingStage.Determining; Interlocked.MemoryBarrier(); diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiEventLoop.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiEventLoop.cs new file mode 100644 index 0000000000000..35176fa5a1752 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiEventLoop.cs @@ -0,0 +1,54 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.Threading.Tasks; +using WasiPollWorld.wit.imports.wasi.io.v0_2_0; + +namespace System.Threading +{ + internal static class WasiEventLoop + { + private static List<(IPoll.Pollable, TaskCompletionSource)> pollables = new(); + + internal static Task RegisterWasiPollable(int handle) + { + var source = new TaskCompletionSource(TaskCreationOptions.AttachedToParent); + pollables.Add((new IPoll.Pollable(new IPoll.Pollable.THandle(handle)), source)); + return source.Task; + } + + internal static void DispatchWasiEventLoop() + { + ThreadPoolWorkQueue.Dispatch(); + + if (WasiEventLoop.pollables.Count > 0) + { + var pollables = WasiEventLoop.pollables; + WasiEventLoop.pollables = new(); + var arguments = new List(); + var sources = new List(); + foreach ((var pollable, var source) in pollables) + { + arguments.Add(pollable); + sources.Add(source); + } + var results = PollInterop.Poll(arguments); + var ready = new bool[arguments.Count]; + foreach (var result in results) + { + ready[result] = true; + arguments[(int)result].Dispose(); + sources[(int)result].SetResult(); + } + for (var i = 0; i < arguments.Count; ++i) + { + if (!ready[i]) + { + WasiEventLoop.pollables.Add((arguments[i], sources[i])); + } + } + } + } + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiPoll.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiPoll.cs new file mode 100644 index 0000000000000..f2ec995ccb40d --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiPoll.cs @@ -0,0 +1,77 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiPollWorld { + + internal interface IWasiPollWorld { + } + + internal readonly struct None {} + + [StructLayout(LayoutKind.Sequential)] + internal readonly struct Result + { + internal readonly byte Tag; + private readonly object value; + + private Result(byte tag, object value) + { + Tag = tag; + this.value = value; + } + + internal static Result ok(Ok ok) + { + return new Result(OK, ok!); + } + + internal static Result err(Err err) + { + return new Result(ERR, err!); + } + + internal bool IsOk => Tag == OK; + internal bool IsErr => Tag == ERR; + + internal Ok AsOk + { + get + { + if (Tag == OK) + return (Ok)value; + else + throw new ArgumentException("expected OK, got " + Tag); + } + } + + internal Err AsErr + { + get + { + if (Tag == ERR) + return (Err)value; + else + throw new ArgumentException("expected ERR, got " + Tag); + } + } + + internal const byte OK = 0; + internal const byte ERR = 1; + } + + namespace exports { + internal static class WasiPollWorld + { + } + } + +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiPollWorld.wit.imports.wasi.io.v0_2_0.IPoll.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiPollWorld.wit.imports.wasi.io.v0_2_0.IPoll.cs new file mode 100644 index 0000000000000..ff8857006fa79 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiPollWorld.wit.imports.wasi.io.v0_2_0.IPoll.cs @@ -0,0 +1,83 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiPollWorld.wit.imports.wasi.io.v0_2_0; + +internal interface IPoll { + + /** + * `pollable` represents a single I/O event which may be ready, or not. + */ + + internal class Pollable: IDisposable { + internal int Handle { get; set; } + + internal readonly record struct THandle(int Handle); + + internal Pollable(THandle handle) { + Handle = handle.Handle; + } + + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + [DllImport("wasi:io/poll@0.2.0", EntryPoint = "[resource-drop]pollable"), WasmImportLinkage] + private static extern void wasmImportResourceDrop(int p0); + + protected virtual void Dispose(bool disposing) { + if (Handle != 0) { + wasmImportResourceDrop(Handle); + Handle = 0; + } + } + + ~Pollable() { + Dispose(false); + } + + internal static class ReadyWasmInterop + { + [DllImport("wasi:io/poll@0.2.0", EntryPoint = "[method]pollable.ready"), WasmImportLinkage] + internal static extern int wasmImportReady(int p0); + + } + + internal unsafe bool Ready() + { + var handle = this.Handle; + var result = ReadyWasmInterop.wasmImportReady(handle); + return (result != 0); + + //TODO: free alloc handle (interopString) if exists + } + + internal static class BlockWasmInterop + { + [DllImport("wasi:io/poll@0.2.0", EntryPoint = "[method]pollable.block"), WasmImportLinkage] + internal static extern void wasmImportBlock(int p0); + + } + + internal unsafe void Block() + { + var handle = this.Handle; + BlockWasmInterop.wasmImportBlock(handle); + + //TODO: free alloc handle (interopString) if exists + } + + } + +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiPollWorld.wit.imports.wasi.io.v0_2_0.PollInterop.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiPollWorld.wit.imports.wasi.io.v0_2_0.PollInterop.cs new file mode 100644 index 0000000000000..7db2f8591b3cd --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiPollWorld.wit.imports.wasi.io.v0_2_0.PollInterop.cs @@ -0,0 +1,56 @@ +// Generated by `wit-bindgen` 0.27.0. DO NOT EDIT! +// +#nullable enable + +using System; +using System.Runtime.CompilerServices; +using System.Collections; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace WasiPollWorld.wit.imports.wasi.io.v0_2_0 +{ + internal static class PollInterop { + + internal static class PollWasmInterop + { + [DllImport("wasi:io/poll@0.2.0", EntryPoint = "poll"), WasmImportLinkage] + internal static extern void wasmImportPoll(nint p0, int p1, nint p2); + + } + + internal static unsafe uint[] Poll(List @in) + { + + byte[] buffer = new byte[4 * @in.Count]; + var gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); + var address = gcHandle.AddrOfPinnedObject(); + + for (int index = 0; index < @in.Count; ++index) { + global::WasiPollWorld.wit.imports.wasi.io.v0_2_0.IPoll.Pollable element = @in[index]; + int basePtr = (int)address + (index * 4); + var handle = element.Handle; + BitConverter.TryWriteBytes(new Span((void*)(basePtr + 0), 4), unchecked((int)handle)); + + } + + var retArea = new uint[2]; + fixed (uint* retAreaByte0 = &retArea[0]) + { + var ptr = (nint)retAreaByte0; + PollWasmInterop.wasmImportPoll((int)address, @in.Count, ptr); + + var array = new uint[BitConverter.ToInt32(new Span((void*)(ptr + 4), 4))]; + new Span((void*)(BitConverter.ToInt32(new Span((void*)(ptr + 0), 4))), BitConverter.ToInt32(new Span((void*)(ptr + 4), 4))).CopyTo(new Span(array)); + gcHandle.Free(); + return array; + } + + //TODO: free alloc handle (interopString) if exists + } + + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/generate-wasi-poll-bindings.sh b/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/generate-wasi-poll-bindings.sh new file mode 100644 index 0000000000000..eccd07494bab8 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/generate-wasi-poll-bindings.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +set -ex + +# This script will regenerate the `wit-bindgen`-generated files in this +# directory. + +# Prerequisites: +# POSIX shell +# tar +# [cargo](https://rustup.rs/) +# [curl](https://curl.se/download.html) + +cargo install --locked --no-default-features --features csharp --version 0.27.0 wit-bindgen-cli +curl -OL https://github.com/WebAssembly/wasi-http/archive/refs/tags/v0.2.0.tar.gz +tar xzf v0.2.0.tar.gz +cat >wasi-http-0.2.0/wit/world.wit <01:15:00 <_workItemTimeout Condition="'$(_workItemTimeout)' == '' and '$(TargetOS)' == 'android'">00:30:00 <_workItemTimeout Condition="'$(_workItemTimeout)' == '' and '$(TargetOS)' == 'browser'">00:30:00 + <_workItemTimeout Condition="'$(_workItemTimeout)' == '' and '$(TargetOS)' == 'wasi'">00:30:00 <_workItemTimeout Condition="'$(Scenario)' == '' and '$(_workItemTimeout)' == '' and ('$(TargetArchitecture)' == 'arm64' or '$(TargetArchitecture)' == 'arm')">00:45:00 <_workItemTimeout Condition="'$(Scenario)' != '' and '$(_workItemTimeout)' == '' and ('$(TargetArchitecture)' == 'arm64' or '$(TargetArchitecture)' == 'arm')">01:00:00 <_workItemTimeout Condition="'$(Scenario)' == '' and '$(_workItemTimeout)' == '' and ('$(TestScope)' == 'outerloop' or '$(TestScope)' == 'all')">00:20:00 diff --git a/src/mono/System.Private.CoreLib/src/System/Threading/ThreadPool.Wasi.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Threading/ThreadPool.Wasi.Mono.cs index 688c1a3b05de9..bad9fbdbaaddf 100644 --- a/src/mono/System.Private.CoreLib/src/System/Threading/ThreadPool.Wasi.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Threading/ThreadPool.Wasi.Mono.cs @@ -35,7 +35,7 @@ public static partial class ThreadPool { // Indicates whether the thread pool should yield the thread from the dispatch loop to the runtime periodically so that // the runtime may use the thread for processing other work - internal static bool YieldFromDispatchLoop => true; + internal static bool YieldFromDispatchLoop => false; private const bool IsWorkerTrackingEnabledInConfig = false; @@ -77,7 +77,6 @@ public static void GetAvailableThreads(out int workerThreads, out int completion internal static unsafe void RequestWorkerThread() { - throw new PlatformNotSupportedException(); } internal static void NotifyWorkItemProgress() @@ -88,15 +87,11 @@ internal static void NotifyWorkItemProgress() internal static void NotifyThreadUnblocked() { - throw new PlatformNotSupportedException(); } internal static object? GetOrCreateThreadLocalCompletionCountObject() => null; - internal static bool NotifyWorkItemComplete(object? _1, int _2) - { - throw new PlatformNotSupportedException(); - } + internal static bool NotifyWorkItemComplete(object? _1, int _2) => true; private static RegisteredWaitHandle RegisterWaitForSingleObject( WaitHandle? waitObject, diff --git a/src/mono/sample/wasi/Directory.Build.targets b/src/mono/sample/wasi/Directory.Build.targets index c40891fa24797..8e098fca7e92f 100644 --- a/src/mono/sample/wasi/Directory.Build.targets +++ b/src/mono/sample/wasi/Directory.Build.targets @@ -11,7 +11,26 @@ <_ExeExt Condition="$([MSBuild]::IsOSPlatform('WINDOWS'))">.exe - + + + <_ScriptExt Condition="'$(OS)' == 'Windows_NT'">.cmd + <_ScriptExt Condition="'$(OS)' != 'Windows_NT'">.sh + <_Dotnet>$(RepoRoot)dotnet$(_ScriptExt) + <_AOTFlag Condition="'$(RunAOTCompilation)' != ''">/p:RunAOTCompilation=$(RunAOTCompilation) + + + + + @@ -21,13 +40,19 @@ <_DotnetWasmName Condition="'$(WasmSingleFileBundle)' == 'true'">$([System.IO.Path]::ChangeExtension($(_SampleAssembly), '.wasm')) <_AppBundlePath>$([MSBuild]::NormalizeDirectory('bin/wasi-wasm/AppBundle')) $(WasmtimeDir)wasmtime$(_ExeExt) $(_MONO_LOG_LEVEL) $(_DOTNET_WASI_PRINT_EXIT_CODE) - $(WasiCommand) --dir . $(_DotnetWasmName) $(_SampleProjectName) - $(WasiCommand) --dir . $(_DotnetWasmName) - $(WasiCommand) $(_DotnetWasmName) + + + $(WasiCommand) --wasi http + $(WasiCommand) --dir . + $(WasiCommand) $(_DotnetWasmName) + $(WasiCommand) $(_SampleProjectName) - + diff --git a/src/mono/sample/wasi/console/Wasi.Console.Sample.csproj b/src/mono/sample/wasi/console/Wasi.Console.Sample.csproj index cb410fa78f843..8525cbaebc77e 100644 --- a/src/mono/sample/wasi/console/Wasi.Console.Sample.csproj +++ b/src/mono/sample/wasi/console/Wasi.Console.Sample.csproj @@ -5,6 +5,9 @@ true true --> + + true + 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 0000000000000..f2687d511fc15 --- /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 0000000000000..28a455f930de6 --- /dev/null +++ b/src/mono/sample/wasi/http-p2/Wasip2.Http.Console.Sample.csproj @@ -0,0 +1,12 @@ + + + $(NetCoreAppCurrent) + <_WasiNeedsHttp>true + + + + + diff --git a/src/mono/sample/wasm/Directory.Build.targets b/src/mono/sample/wasm/Directory.Build.targets index 5ec9cf77532f5..2bc82f4fce0d3 100644 --- a/src/mono/sample/wasm/Directory.Build.targets +++ b/src/mono/sample/wasm/Directory.Build.targets @@ -45,7 +45,7 @@ <_SampleAssembly Condition="'$(_SampleAssembly)' == ''">$(TargetFileName) $(BuildAdditionalArgs) /p:MonoDiagnosticsMock=$(MonoDiagnosticsMock) - + diff --git a/src/mono/wasi/Wasi.Build.Tests/BuildTestBase.cs b/src/mono/wasi/Wasi.Build.Tests/BuildTestBase.cs index 0920103041db8..ae3fab94bbdc8 100644 --- a/src/mono/wasi/Wasi.Build.Tests/BuildTestBase.cs +++ b/src/mono/wasi/Wasi.Build.Tests/BuildTestBase.cs @@ -695,7 +695,8 @@ public static int Main() protected CommandResult RunWithoutBuild(string config, string id) { - string runArgs = $"run --no-build -c {config} --forward-exit-code"; + // wasmtime --wasi http is necessary because the default dotnet.wasm (without native rebuild depends on wasi:http world) + string runArgs = $"run --no-build -c {config} --forward-exit-code --extra-host-arg=--wasi --extra-host-arg=http"; runArgs += " x y z"; int expectedExitCode = 42; CommandResult res = new RunCommand(s_buildEnv, _testOutput, label: id) diff --git a/src/mono/wasi/Wasi.Build.Tests/WasiTemplateTests.cs b/src/mono/wasi/Wasi.Build.Tests/WasiTemplateTests.cs index 9b46e29d05fd4..5a909d54b1374 100644 --- a/src/mono/wasi/Wasi.Build.Tests/WasiTemplateTests.cs +++ b/src/mono/wasi/Wasi.Build.Tests/WasiTemplateTests.cs @@ -135,7 +135,8 @@ public void ConsoleBuildAndRunForDifferentOutputPaths(string config, bool append CommandResult res = new RunCommand(s_buildEnv, _testOutput) .WithWorkingDirectory(_projectDir!) - .ExecuteWithCapturedOutput($"run --no-silent --no-build -c {config} x y z") + // wasmtime --wasi http is necessary because the default dotnet.wasm (without native rebuild depends on wasi:http world) + .ExecuteWithCapturedOutput($"run --no-silent --no-build -c {config} --extra-host-arg=--wasi --extra-host-arg=http x y z") .EnsureSuccessful(); Assert.Contains("Hello, Wasi Console!", res.Output); diff --git a/src/mono/wasi/build/WasiApp.InTree.props b/src/mono/wasi/build/WasiApp.InTree.props index 1b75c8218604b..0ab876a736fd6 100644 --- a/src/mono/wasi/build/WasiApp.InTree.props +++ b/src/mono/wasi/build/WasiApp.InTree.props @@ -7,7 +7,7 @@ $(NetCoreAppCurrent) false true - partial + full false diff --git a/src/mono/wasi/build/WasiApp.targets b/src/mono/wasi/build/WasiApp.targets index ca8d1e72b83fc..c0064b8f6edeb 100644 --- a/src/mono/wasi/build/WasiApp.targets +++ b/src/mono/wasi/build/WasiApp.targets @@ -37,7 +37,7 @@ false - partial + full false @@ -346,7 +346,14 @@ <_WasiLinkStepArgs Include="-Wl,--export=malloc,--export=free,--export=__heap_base,--export=__data_end" /> + <_WasiLinkStepArgs Include="-Wl,-z,stack-size=8388608,-lwasi-emulated-process-clocks,-lwasi-emulated-signal,-lwasi-emulated-mman"/> + + <_WasiLinkStepArgs Include="-Wl,--component-type,"$([MSBuild]::NormalizePath('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)', 'WasiHttpWorld_component_type.wit').Replace('\','/'))""/> <_WasiLinkStepArgs Include="-Wl,-s" Condition="'$(WasmNativeStrip)' == 'true'"/> <_WasiLinkStepArgs Include=""@$(_WasmDefaultLinkFlagsRsp.Replace('\', '/'))"" /> @@ -385,7 +392,7 @@ <_WasiOutputFileName Condition="'$(WasmSingleFileBundle)' == 'true'">$([System.IO.Path]::GetFileNameWithoutExtension('$(WasmMainAssemblyFileName)')).wasm <_WasiOutputFileName Condition="'$(WasmSingleFileBundle)' != 'true'">dotnet.wasm - <_WasiOutputFileName Condition="'$(_WasiOutputFileName)' != ''">$([System.IO.Path]::Combine($(_WasiIntermediateOutputPath), $(_WasiOutputFileName))) + <_WasiOutputFileName Condition="'$(_WasiOutputFileName)' != ''">$([System.IO.Path]::Combine($(_WasmIntermediateOutputPath), $(_WasiOutputFileName))) diff --git a/src/mono/wasi/wasi.proj b/src/mono/wasi/wasi.proj index c4a576f3bc4ef..b591a393ebb6a 100644 --- a/src/mono/wasi/wasi.proj +++ b/src/mono/wasi/wasi.proj @@ -147,8 +147,8 @@ <_WasiCompileFlags Include="-D_WASI_EMULATED_PROCESS_CLOCKS"/> <_WasiCompileFlags Include="-D_WASI_EMULATED_SIGNAL"/> <_WasiCompileFlags Include="-D_WASI_EMULATED_MMAN"/> - - + + <_WasiLinkFlags Include="-Wl,-z,stack-size=8388608,--initial-memory=52428800,-lwasi-emulated-process-clocks,-lwasi-emulated-signal,-lwasi-emulated-mman"/> @@ -202,6 +202,7 @@ $(ICULibDir)/libicui18n.a; $(ICULibDir)/libicudata.a" /> + $(WasiObjDir)\pinvoke-table.h @@ -212,6 +213,13 @@ $(CMakeConfigurationWasiFlags) -O2 + + $(CMakeBuildRuntimeConfigureCmd) -Wl,--component-type,"$([MSBuild]::NormalizePath('$(RepoRoot)', 'src/libraries/System.Net.Http/src/System/Net/Http/WasiHttpHandler/WasiHttpWorld_component_type.wit').Replace('\','/'))" + cmake $(MSBuildThisFileDirectory)runtime cmake -G Ninja $(MSBuildThisFileDirectory)runtime @@ -266,7 +274,8 @@ @@ -284,7 +293,7 @@ DestinationFolder="$(MicrosoftNetCoreAppRuntimePackNativeDir)" SkipUnchangedFiles="true" /> - diff --git a/src/mono/wasm/build/WasmApp.Common.targets b/src/mono/wasm/build/WasmApp.Common.targets index df8a8a33cc710..2641a239ac4e5 100644 --- a/src/mono/wasm/build/WasmApp.Common.targets +++ b/src/mono/wasm/build/WasmApp.Common.targets @@ -321,7 +321,7 @@ <_WasmOutputFileName Condition="'$(WasmSingleFileBundle)' == 'true'">$([System.IO.Path]::GetFileNameWithoutExtension('$(WasmMainAssemblyFileName)')).wasm <_WasmOutputFileName Condition="'$(WasmSingleFileBundle)' != 'true'">dotnet.wasm - <_WasmOutputFileName Condition="'$(_WasmOutputFileName)' != ''">$([System.IO.Path]::Combine($(_WasiIntermediateOutputPath), $(_WasmOutputFileName))) + <_WasmOutputFileName Condition="'$(_WasmOutputFileName)' != ''">$([System.IO.Path]::Combine($(_WasmIntermediateOutputPath), $(_WasmOutputFileName))) diff --git a/src/mono/wasm/host/wasi/WasiEngineHost.cs b/src/mono/wasm/host/wasi/WasiEngineHost.cs index 4c8f8bc363e23..2e8cacf7d9f4f 100644 --- a/src/mono/wasm/host/wasi/WasiEngineHost.cs +++ b/src/mono/wasm/host/wasi/WasiEngineHost.cs @@ -35,8 +35,6 @@ public static async Task InvokeAsync(CommonConfiguration commonArgs, private async Task RunAsync() { - string[] engineArgs = Array.Empty(); - string engineBinary = _args.Host switch { WasmHost.Wasmtime => "wasmtime", @@ -65,7 +63,8 @@ private async Task RunAsync() args.AddRange(["--env", "DOTNET_WASI_PRINT_EXIT_CODE=1"]); }; - args.AddRange(engineArgs); + args.AddRange(_args.CommonConfig.HostArguments); + args.Add("--"); if (_args.IsSingleFileBundle) diff --git a/src/native/libs/CMakeLists.txt b/src/native/libs/CMakeLists.txt index eb1df8fa2a210..23bdf3c26d37a 100644 --- a/src/native/libs/CMakeLists.txt +++ b/src/native/libs/CMakeLists.txt @@ -55,6 +55,8 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER OR CLR_CMAKE_TARGET_WASI) add_compile_options(-D_WASI_EMULATED_PROCESS_CLOCKS) add_compile_options(-D_WASI_EMULATED_SIGNAL) add_compile_options(-D_WASI_EMULATED_MMAN) + # keep in sync with src\mono\wasi\build\WasiApp.targets + # keep in sync with src\mono\wasi\wasi.proj add_link_options(-Wl,-z,stack-size=1048576,--initial-memory=5242880,--max-memory=52428800,-lwasi-emulated-process-clocks,-lwasi-emulated-signal,-lwasi-emulated-mman) endif () diff --git a/src/tests/build.proj b/src/tests/build.proj index 5ea8d9ceca443..65ceee0cd7330 100644 --- a/src/tests/build.proj +++ b/src/tests/build.proj @@ -607,6 +607,7 @@ $(GroupBuildCmd) /maxcpucount $(GroupBuildCmd) /m:1 + $(GroupBuildCmd) /m:1 $(GroupBuildCmd) "/p:DevTeamProvisioning=-" $(GroupBuildCmd) /bl:$(ArtifactsDir)/log/$(Configuration)/InnerManagedTestBuild.$(__TestGroupToBuild).binlog $(GroupBuildCmd) "/p:CrossBuild=true"