-
Notifications
You must be signed in to change notification settings - Fork 2
/
OcelotSwaggerMiddleware.cs
89 lines (70 loc) · 2.74 KB
/
OcelotSwaggerMiddleware.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
namespace Ocelot.Swagger
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Ocelot.Swagger.Configuration;
internal class OcelotSwaggerMiddleware
{
private readonly OcelotSwaggerOptions _options;
private readonly RequestDelegate _next;
public OcelotSwaggerMiddleware(
RequestDelegate next,
IOptionsMonitor<OcelotSwaggerOptions> optionsAccessor)
{
this._next = next;
this._options = optionsAccessor.CurrentValue;
}
public async Task InvokeAsync(HttpContext httpContext)
{
var path = httpContext.Request.Path.Value;
if (path.IndexOf("/swagger") > -1)
{
var content = await this.ReadContentAsync(httpContext);
foreach (var replace in _options.SwaggerReplaces)
{
Regex reg = new Regex(replace.UpstreamPathRouteRegex);
Match match = reg.Match(path);
if (match.Success)
{
string value = match.Groups[1].Value;
content = Regex.Replace(content, replace.DownstreamPathRouteRegex, $"/{value}/");
}
}
await this.WriteContentAsync(httpContext, content);
}
else
{
await this._next(httpContext);
}
}
private async Task<string> ReadContentAsync([NotNull] HttpContext httpContext)
{
var existingBody = httpContext.Response.Body;
using (var newBody = new MemoryStream())
{
// We set the response body to our stream so we can read after the chain of middlewares have been called.
httpContext.Response.Body = newBody;
await this._next(httpContext);
// Reset the body so nothing from the latter middlewares goes to the output.
httpContext.Response.Body = existingBody;
newBody.Seek(0, SeekOrigin.Begin);
var newContent = await new StreamReader(newBody).ReadToEndAsync();
return newContent;
}
}
private async Task WriteContentAsync([NotNull] HttpContext httpContext, string content)
{
httpContext.Response.ContentLength = Encoding.UTF8.GetByteCount(content);
await httpContext.Response.WriteAsync(content);
}
}
}