| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Net; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using CoreWCF.Configuration; |
| | | 8 | | using CoreWCF.Description; |
| | | 9 | | using Microsoft.AspNetCore.Builder; |
| | | 10 | | using Microsoft.AspNetCore.Http; |
| | | 11 | | using Microsoft.Extensions.DependencyInjection; |
| | | 12 | | using Microsoft.Extensions.Logging; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Channels |
| | | 15 | | { |
| | | 16 | | internal class MetadataMiddleware |
| | | 17 | | { |
| | | 18 | | private const string RestorePathsDelegateItemName = nameof(MetadataMiddleware) + "_RestorePathsDelegate"; |
| | | 19 | | private readonly IApplicationBuilder _app; |
| | | 20 | | private readonly IServiceBuilder _serviceBuilder; |
| | | 21 | | private readonly IDispatcherBuilder _dispatcherBuilder; |
| | | 22 | | private readonly ServiceMetadataBehavior _metadataBehavior; |
| | | 23 | | private readonly RequestDelegate _next; |
| | | 24 | | private readonly ILogger<MetadataMiddleware> _logger; |
| | | 25 | | private RequestDelegate _branch; |
| | | 26 | | private bool _branchBuilt; |
| | | 27 | | |
| | 24 | 28 | | public MetadataMiddleware(RequestDelegate next, IApplicationBuilder app, IServiceBuilder serviceBuilder, IDispat |
| | | 29 | | { |
| | 24 | 30 | | _app = app; |
| | 24 | 31 | | _serviceBuilder = serviceBuilder; |
| | 24 | 32 | | _dispatcherBuilder = dispatcherBuilder; |
| | 24 | 33 | | _metadataBehavior = metadataBehavior; |
| | 24 | 34 | | _next = next; |
| | 24 | 35 | | _logger = logger; |
| | 24 | 36 | | _branch = BuildBranchAndInvoke; |
| | 24 | 37 | | serviceBuilder.Opened += ServiceBuilderOpenedCallback; |
| | 24 | 38 | | } |
| | | 39 | | |
| | | 40 | | public async Task InvokeAsync(HttpContext context) |
| | | 41 | | { |
| | | 42 | | // Update the path |
| | 26 | 43 | | var path = context.Request.Path; |
| | 26 | 44 | | var pathBase = context.Request.PathBase; |
| | 26 | 45 | | context.Request.Path = pathBase.Add(path); |
| | 26 | 46 | | context.Request.PathBase = ""; |
| | 26 | 47 | | Action restorePaths = () => |
| | 26 | 48 | | { |
| | 1 | 49 | | context.Request.PathBase = pathBase; |
| | 1 | 50 | | context.Request.Path = path; |
| | 27 | 51 | | }; |
| | 26 | 52 | | context.Items[RestorePathsDelegateItemName] = restorePaths; |
| | 26 | 53 | | await _branch(context); |
| | 26 | 54 | | } |
| | | 55 | | |
| | | 56 | | private Task BuildBranchAndInvoke(HttpContext request) |
| | | 57 | | { |
| | 0 | 58 | | EnsureBranchBuilt(); |
| | 0 | 59 | | return _branch(request); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private void EnsureBranchBuilt() |
| | | 63 | | { |
| | 24 | 64 | | lock (this) |
| | | 65 | | { |
| | 24 | 66 | | if (!_branchBuilt) |
| | | 67 | | { |
| | 24 | 68 | | _branch = BuildBranch(); |
| | 24 | 69 | | _branchBuilt = true; |
| | | 70 | | } |
| | 24 | 71 | | } |
| | 24 | 72 | | } |
| | | 73 | | |
| | | 74 | | private void ServiceBuilderOpenedCallback(object sender, EventArgs e) |
| | | 75 | | { |
| | 24 | 76 | | EnsureBranchBuilt(); |
| | 24 | 77 | | } |
| | | 78 | | |
| | | 79 | | private RequestDelegate BuildBranch() |
| | | 80 | | { |
| | 24 | 81 | | _logger.LogDebug("Building branch map"); |
| | 24 | 82 | | IApplicationBuilder branchApp = _app.New(); |
| | 24 | 83 | | branchApp.Use(branchNext => { |
| | 24 | 84 | | return reqContext => |
| | 24 | 85 | | { |
| | 26 | 86 | | if ("GET".Equals(reqContext.Request.Method, StringComparison.OrdinalIgnoreCase)) |
| | 24 | 87 | | { |
| | 24 | 88 | | // If request is a GET request, continue on the branchApp middleware chain |
| | 24 | 89 | | // to handle requests for WSDL, HelpPage etc |
| | 25 | 90 | | return branchNext(reqContext); |
| | 24 | 91 | | } |
| | 24 | 92 | | |
| | 24 | 93 | | // Not a GET request so short circuit to the next middleware after MetadataMiddleware. |
| | 1 | 94 | | if (reqContext.Items.TryGetValue(RestorePathsDelegateItemName, out object restorePathsDelegateAsObje |
| | 24 | 95 | | { |
| | 1 | 96 | | (restorePathsDelegateAsObject as Action)?.Invoke(); |
| | 24 | 97 | | } |
| | 24 | 98 | | else |
| | 24 | 99 | | { |
| | 0 | 100 | | _logger.LogWarning("RequestContext missing delegate with key {key}", RestorePathsDelegateItemNam |
| | 24 | 101 | | } |
| | 24 | 102 | | |
| | 1 | 103 | | return _next(reqContext); |
| | 24 | 104 | | }; |
| | 24 | 105 | | }); |
| | | 106 | | |
| | 98 | 107 | | foreach (Type serviceType in _serviceBuilder.Services) |
| | | 108 | | { |
| | | 109 | | void MapMetadata(IApplicationBuilder app, string path, Func<RequestDelegate, RequestDelegate> middleware |
| | | 110 | | { |
| | 34 | 111 | | _logger.LogInformation("Configuring metadata to {path}", path); |
| | 34 | 112 | | app.Use(middleware); |
| | 34 | 113 | | } |
| | | 114 | | |
| | 25 | 115 | | var dispatchers = _dispatcherBuilder.BuildDispatchers(serviceType); |
| | 25 | 116 | | var serviceHost = _app.ApplicationServices.GetRequiredService(typeof(ServiceHostObjectModel<>).MakeGener |
| | 25 | 117 | | var metadataExtension = serviceHost.Extensions.Find<ServiceMetadataExtension>(); |
| | 25 | 118 | | if (metadataExtension == null) |
| | | 119 | | { |
| | | 120 | | continue; // No ServiceMetadataExtension on this service |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | // Look for endpoints listening on HTTP and HTTPS and use them for fallback paths |
| | 25 | 124 | | Uri fallbackHttpEndpointAddress = null; |
| | 25 | 125 | | Uri fallbackHttpsEndpointAddress = null; |
| | 116 | 126 | | foreach (var dispatcher in dispatchers) |
| | | 127 | | { |
| | 33 | 128 | | var scheme = dispatcher.BaseAddress?.Scheme; |
| | 33 | 129 | | if (fallbackHttpEndpointAddress == null && Uri.UriSchemeHttp.Equals(scheme)) |
| | | 130 | | { |
| | 19 | 131 | | fallbackHttpEndpointAddress = dispatcher.BaseAddress; |
| | 19 | 132 | | if (IPAddress.TryParse(fallbackHttpEndpointAddress.Host, out IPAddress hostAsAddress)) |
| | | 133 | | { |
| | 1 | 134 | | var uriBuilder = new UriBuilder(fallbackHttpEndpointAddress); |
| | 1 | 135 | | if (hostAsAddress.Equals(IPAddress.Any) || hostAsAddress.Equals(IPAddress.IPv6Any)) |
| | | 136 | | { |
| | 0 | 137 | | uriBuilder.Host = DnsCache.MachineName; |
| | 0 | 138 | | fallbackHttpEndpointAddress = uriBuilder.Uri; |
| | | 139 | | } |
| | 1 | 140 | | else if (IPAddress.IsLoopback(hostAsAddress)) |
| | | 141 | | { |
| | 1 | 142 | | uriBuilder.Host = "localhost"; |
| | 1 | 143 | | fallbackHttpEndpointAddress = uriBuilder.Uri; |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | 1 | 147 | | continue; |
| | | 148 | | } |
| | | 149 | | |
| | 14 | 150 | | if (fallbackHttpsEndpointAddress == null && Uri.UriSchemeHttps.Equals(scheme)) |
| | | 151 | | { |
| | 6 | 152 | | fallbackHttpsEndpointAddress = dispatcher.BaseAddress; |
| | 6 | 153 | | if (IPAddress.TryParse(fallbackHttpsEndpointAddress.Host, out IPAddress hostAsAddress)) |
| | | 154 | | { |
| | 0 | 155 | | var uriBuilder = new UriBuilder(fallbackHttpsEndpointAddress); |
| | 0 | 156 | | if (hostAsAddress.Equals(IPAddress.Any) || hostAsAddress.Equals(IPAddress.IPv6Any)) |
| | | 157 | | { |
| | 0 | 158 | | uriBuilder.Host = DnsCache.MachineName; |
| | 0 | 159 | | fallbackHttpsEndpointAddress = uriBuilder.Uri; |
| | | 160 | | } |
| | 0 | 161 | | else if (IPAddress.IsLoopback(hostAsAddress)) |
| | | 162 | | { |
| | 0 | 163 | | uriBuilder.Host = "localhost"; |
| | 0 | 164 | | fallbackHttpsEndpointAddress = uriBuilder.Uri; |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | continue; |
| | | 169 | | } |
| | | 170 | | } |
| | | 171 | | |
| | 25 | 172 | | Uri httpMetadataUri = null; |
| | 25 | 173 | | Uri httpsMetadataUri = null; |
| | 25 | 174 | | if (metadataExtension.HttpGetEnabled) |
| | | 175 | | { |
| | 21 | 176 | | if (metadataExtension.HttpGetUrl != null && metadataExtension.HttpGetUrl.AbsolutePath != "/") |
| | | 177 | | { |
| | 21 | 178 | | httpMetadataUri = metadataExtension.HttpGetUrl; |
| | | 179 | | } |
| | | 180 | | else |
| | | 181 | | { |
| | 0 | 182 | | httpMetadataUri = fallbackHttpEndpointAddress; |
| | 0 | 183 | | metadataExtension.HttpGetUrl = fallbackHttpEndpointAddress; |
| | | 184 | | } |
| | | 185 | | |
| | 21 | 186 | | if(httpMetadataUri != null) |
| | | 187 | | { |
| | 21 | 188 | | MapMetadata(branchApp, httpMetadataUri.AbsolutePath, metadataExtension.CreateMiddleware(httpMeta |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | 25 | 192 | | if (metadataExtension.HttpHelpPageEnabled) |
| | | 193 | | { |
| | | 194 | | Uri helpPageUri; |
| | 25 | 195 | | if (metadataExtension.HttpHelpPageUrl != null && metadataExtension.HttpHelpPageUrl.AbsolutePath != " |
| | | 196 | | { |
| | 5 | 197 | | helpPageUri = metadataExtension.HttpHelpPageUrl; |
| | | 198 | | } |
| | | 199 | | else |
| | | 200 | | { |
| | 20 | 201 | | helpPageUri = fallbackHttpEndpointAddress; |
| | 20 | 202 | | metadataExtension.HttpHelpPageUrl = fallbackHttpEndpointAddress; |
| | | 203 | | } |
| | 25 | 204 | | if (helpPageUri != null && !helpPageUri.Equals(httpMetadataUri)) |
| | | 205 | | { |
| | | 206 | | // Only map the help page uri if it's not null, and different from http metadata uri. |
| | 4 | 207 | | MapMetadata(branchApp, helpPageUri.AbsolutePath, metadataExtension.CreateMiddleware(helpPageUri, |
| | | 208 | | } |
| | | 209 | | } |
| | | 210 | | |
| | 25 | 211 | | if (metadataExtension.HttpsGetEnabled) |
| | | 212 | | { |
| | 6 | 213 | | if (metadataExtension.HttpsGetUrl != null && metadataExtension.HttpsGetUrl.AbsolutePath != "/") |
| | | 214 | | { |
| | 6 | 215 | | httpsMetadataUri = metadataExtension.HttpsGetUrl; |
| | | 216 | | } |
| | | 217 | | else |
| | | 218 | | { |
| | 0 | 219 | | httpsMetadataUri = fallbackHttpsEndpointAddress; |
| | 0 | 220 | | metadataExtension.HttpsGetUrl = fallbackHttpsEndpointAddress; |
| | | 221 | | } |
| | | 222 | | |
| | 6 | 223 | | if (httpsMetadataUri != null) |
| | | 224 | | { |
| | 6 | 225 | | MapMetadata(branchApp, httpsMetadataUri.AbsolutePath, metadataExtension.CreateMiddleware(httpsMe |
| | | 226 | | } |
| | | 227 | | } |
| | | 228 | | |
| | 25 | 229 | | if (metadataExtension.HttpsHelpPageEnabled) |
| | | 230 | | { |
| | | 231 | | Uri helpPageUri; |
| | 25 | 232 | | if (metadataExtension.HttpsHelpPageUrl != null && metadataExtension.HttpsHelpPageUrl.AbsolutePath != |
| | | 233 | | { |
| | 3 | 234 | | helpPageUri = metadataExtension.HttpsHelpPageUrl; |
| | | 235 | | } |
| | | 236 | | else |
| | | 237 | | { |
| | 22 | 238 | | helpPageUri = fallbackHttpsEndpointAddress; |
| | 22 | 239 | | metadataExtension.HttpsHelpPageUrl = fallbackHttpsEndpointAddress; |
| | | 240 | | } |
| | 25 | 241 | | if (helpPageUri != null && !helpPageUri.Equals(httpsMetadataUri)) |
| | | 242 | | { |
| | | 243 | | // Only map the help page uri if it's not null, and different from https metadata uri. |
| | 3 | 244 | | MapMetadata(branchApp, helpPageUri.AbsolutePath, metadataExtension.CreateMiddleware(helpPageUri, |
| | | 245 | | } |
| | | 246 | | } |
| | | 247 | | } |
| | | 248 | | |
| | 24 | 249 | | branchApp.Use(_ => { |
| | 24 | 250 | | return reqContext => |
| | 24 | 251 | | { |
| | 0 | 252 | | if (reqContext.Items.TryGetValue(RestorePathsDelegateItemName, out object restorePathsDelegateAsObje |
| | 24 | 253 | | { |
| | 0 | 254 | | (restorePathsDelegateAsObject as Action)?.Invoke(); |
| | 24 | 255 | | } |
| | 24 | 256 | | else |
| | 24 | 257 | | { |
| | 0 | 258 | | _logger.LogWarning("RequestContext missing delegate with key {key}", RestorePathsDelegateItemNam |
| | 24 | 259 | | } |
| | 24 | 260 | | |
| | 0 | 261 | | return _next(reqContext); |
| | 24 | 262 | | }; |
| | 24 | 263 | | }); |
| | 24 | 264 | | return branchApp.Build(); |
| | | 265 | | } |
| | | 266 | | } |
| | | 267 | | } |