Exemple #1
0
        private static async Task HandleRequest(Method method, HttpContext aspNetCoreContext, IRequestAuthenticator authenticator)
        {
            var(_, uri) = aspNetCoreContext.Request.Path.Value.TSplit(RootUri);
            var headers = new Headers(aspNetCoreContext.Request.Headers);
            var client  = GetClient(aspNetCoreContext);
            var context = new AspNetCoreRESTableContext(client, aspNetCoreContext);

            if (!authenticator.TryAuthenticate(context, ref uri, headers, out var notAuthorized))
            {
                WriteResponse(aspNetCoreContext, notAuthorized);
                return;
            }
            var body    = aspNetCoreContext.Request.Body;
            var request = context.CreateRequest(method, uri, body, headers);

            await using var result = await request.GetResult().ConfigureAwait(false);

            switch (result)
            {
            case WebSocketTransferSuccess:
                return;

            case WebSocketUpgradeSuccessful ws:
            {
                await using var webSocket = ws.WebSocket;
                await webSocket.LifetimeTask.ConfigureAwait(false);

                break;
            }

            default:
            {
                WriteResponse(aspNetCoreContext, result);
                var remote = aspNetCoreContext.Response.Body;

#if NETSTANDARD2_1
                await using (remote)
#else
                using (remote)
#endif
                {
                    await using var serializedResult = await result.Serialize(remote).ConfigureAwait(false);
                }
                break;
            }
            }
        }
Exemple #2
0
        private static async Task HandleOptionsRequest(HttpContext aspNetCoreContext)
        {
            var(_, uri) = aspNetCoreContext.Request.Path.Value.TSplit(RootUri);
            var headers = new Headers(aspNetCoreContext.Request.Headers);
            var client  = GetClient(aspNetCoreContext);
            var context = new AspNetCoreRESTableContext(client, aspNetCoreContext);
            var options = context.GetOptions(uri, headers);

            WriteResponse(aspNetCoreContext, options);

            var remote = aspNetCoreContext.Response.Body;

#if NETSTANDARD2_1
            await using (remote)
#else
            using (remote)
#endif
            {
                await using var serializedResult = await options.Serialize(remote).ConfigureAwait(false);
            }
        }