-
Notifications
You must be signed in to change notification settings - Fork 368
/
DiscoveryRouter.cs
51 lines (43 loc) · 1.55 KB
/
DiscoveryRouter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace Microsoft.DotNet.Interactive.Http;
public class DiscoveryRouter : IRouter
{
private readonly HtmlNotebookFrontendEnvironment _frontendEnvironment;
public DiscoveryRouter(HtmlNotebookFrontendEnvironment frontendEnvironment)
{
_frontendEnvironment = frontendEnvironment;
}
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
return null;
}
public async Task RouteAsync(RouteContext context)
{
if (context.HttpContext.Request.Method == HttpMethods.Post)
{
var segments =
context.HttpContext
.Request
.Path
.Value
.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (segments[0] == "discovery")
{
using var reader = new StreamReader(context.HttpContext.Request.Body);
var source = await reader.ReadToEndAsync();
var apiUri = new Uri(source);
_frontendEnvironment.SetApiUri(apiUri);
context.Handler = async httpContext =>
{
await httpContext.Response.CompleteAsync();
};
}
}
}
}