| | | 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.Collections.Generic; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Net; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using System.Xml; |
| | | 13 | | using System.Xml.Schema; |
| | | 14 | | using CoreWCF.Channels; |
| | | 15 | | using CoreWCF.Configuration; |
| | | 16 | | using CoreWCF.Runtime; |
| | | 17 | | using Microsoft.AspNetCore.Http; |
| | | 18 | | using WsdlNS = System.Web.Services.Description; |
| | | 19 | | |
| | | 20 | | namespace CoreWCF.Description |
| | | 21 | | { |
| | | 22 | | // the description/metadata "mix-in" |
| | | 23 | | public class ServiceMetadataExtension : IExtension<ServiceHostBase> |
| | | 24 | | { |
| | | 25 | | private const string BaseAddressPattern = "{%BaseAddress%}"; |
| | 0 | 26 | | private static readonly Uri s_emptyUri = new Uri(string.Empty, UriKind.Relative); |
| | | 27 | | private MetadataSet _metadata; |
| | | 28 | | private WsdlNS.ServiceDescription _singleWsdl; |
| | | 29 | | private bool _isInitialized = false; |
| | | 30 | | private bool _isSingleWsdlInitialized = false; |
| | | 31 | | private ServiceHostBase _owner; |
| | 576 | 32 | | private readonly object _syncRoot = new object(); |
| | 576 | 33 | | private readonly object _singleWsdlSyncRoot = new object(); |
| | | 34 | | |
| | 1152 | 35 | | public ServiceMetadataExtension() : this(null) { } |
| | | 36 | | |
| | 576 | 37 | | internal ServiceMetadataExtension(ServiceMetadataBehavior.MetadataExtensionInitializer initializer) |
| | | 38 | | { |
| | 576 | 39 | | Initializer = initializer; |
| | 576 | 40 | | } |
| | | 41 | | |
| | 673 | 42 | | internal ServiceMetadataBehavior.MetadataExtensionInitializer Initializer { get; set; } |
| | | 43 | | |
| | | 44 | | public MetadataSet Metadata |
| | | 45 | | { |
| | | 46 | | get |
| | | 47 | | { |
| | 69 | 48 | | EnsureInitialized(); |
| | 69 | 49 | | return _metadata; |
| | | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public WsdlNS.ServiceDescription SingleWsdl |
| | | 54 | | { |
| | | 55 | | get |
| | | 56 | | { |
| | 19 | 57 | | EnsureSingleWsdlInitialized(); |
| | 19 | 58 | | return _singleWsdl; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | 29 | 62 | | internal Uri ExternalMetadataLocation { get; set; } |
| | | 63 | | |
| | 4 | 64 | | internal bool MexEnabled { get; set; } = false; |
| | | 65 | | |
| | 77 | 66 | | internal bool HttpGetEnabled { get; set; } = false; |
| | | 67 | | |
| | 87 | 68 | | internal bool HttpsGetEnabled { get; set; } = false; |
| | | 69 | | |
| | 65 | 70 | | internal bool HelpPageEnabled => HttpHelpPageEnabled || HttpsHelpPageEnabled; |
| | | 71 | | |
| | 4 | 72 | | internal bool MetadataEnabled => MexEnabled || HttpGetEnabled || HttpsGetEnabled; |
| | | 73 | | |
| | 667 | 74 | | internal bool HttpHelpPageEnabled { get; set; } = false; |
| | | 75 | | |
| | 602 | 76 | | internal bool HttpsHelpPageEnabled { get; set; } = false; |
| | | 77 | | |
| | 0 | 78 | | internal Uri MexUrl { get; set; } |
| | | 79 | | |
| | 90 | 80 | | internal Uri HttpGetUrl { get; set; } |
| | | 81 | | |
| | 45 | 82 | | internal Uri HttpsGetUrl { get; set; } |
| | | 83 | | |
| | 652 | 84 | | internal Uri HttpHelpPageUrl { get; set; } |
| | | 85 | | |
| | 633 | 86 | | internal Uri HttpsHelpPageUrl { get; set; } |
| | | 87 | | |
| | 29 | 88 | | internal bool UpdateAddressDynamically => DynamicMetadataEndpointAddressProvider != null; |
| | | 89 | | |
| | | 90 | | // This dictionary should not be mutated after open |
| | 7 | 91 | | internal IDictionary<string, int> UpdatePortsByScheme { get; set; } |
| | | 92 | | |
| | 44 | 93 | | public IMetadataEndpointAddressProvider DynamicMetadataEndpointAddressProvider { get; internal set; } |
| | | 94 | | |
| | | 95 | | internal bool TryGetHttpHostAndPort(Uri listenUri, HttpRequest httpRequest, out string host, out int port, out s |
| | | 96 | | { |
| | 4 | 97 | | host = null; |
| | 4 | 98 | | port = 0; |
| | 4 | 99 | | scheme = null; |
| | | 100 | | |
| | 4 | 101 | | Uri customUri = DynamicMetadataEndpointAddressProvider?.GetEndpointAddress(httpRequest); |
| | 4 | 102 | | if (customUri != null) |
| | | 103 | | { |
| | 4 | 104 | | host = customUri.Host; |
| | 4 | 105 | | port = customUri.Port; |
| | 4 | 106 | | scheme = DynamicMetadataEndpointAddressProvider is UseRequestHeadersForMetadataAddressBehavior.UseHostHe |
| | 4 | 107 | | ? listenUri.Scheme |
| | 4 | 108 | | : customUri.Scheme; |
| | 4 | 109 | | return true; |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | return false; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | internal Func<RequestDelegate, RequestDelegate> CreateMiddleware(Uri baseAddress, bool isHttps) |
| | | 116 | | { |
| | 34 | 117 | | HttpGetImpl impl = new HttpGetImpl(this, baseAddress, isHttps); |
| | 34 | 118 | | return impl.MetadataMiddleware; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | private void EnsureInitialized() |
| | | 122 | | { |
| | 69 | 123 | | if (!_isInitialized) |
| | | 124 | | { |
| | 24 | 125 | | lock (_syncRoot) |
| | | 126 | | { |
| | 24 | 127 | | if (!_isInitialized) |
| | | 128 | | { |
| | 24 | 129 | | if (Initializer != null) |
| | | 130 | | { |
| | | 131 | | // the following call will initialize this |
| | | 132 | | // it will use the Metadata property to do the initialization |
| | | 133 | | // this will call back into this method, but exit because isInitialized is set. |
| | | 134 | | // if other threads try to call these methods, they will block on the lock |
| | 24 | 135 | | _metadata = Initializer.GenerateMetadata(); |
| | | 136 | | } |
| | | 137 | | |
| | 24 | 138 | | if (_metadata == null) |
| | | 139 | | { |
| | 0 | 140 | | _metadata = new MetadataSet(); |
| | | 141 | | } |
| | | 142 | | |
| | 24 | 143 | | Thread.MemoryBarrier(); |
| | | 144 | | |
| | 24 | 145 | | _isInitialized = true; |
| | 24 | 146 | | Initializer = null; |
| | | 147 | | } |
| | 24 | 148 | | } |
| | | 149 | | } |
| | 69 | 150 | | } |
| | | 151 | | |
| | | 152 | | private void EnsureSingleWsdlInitialized() |
| | | 153 | | { |
| | 19 | 154 | | if (!_isSingleWsdlInitialized) |
| | | 155 | | { |
| | 19 | 156 | | lock (_singleWsdlSyncRoot) |
| | | 157 | | { |
| | 19 | 158 | | if (!_isSingleWsdlInitialized) |
| | | 159 | | { |
| | | 160 | | // Could throw NotSupportedException if multiple contract namespaces. Let the exception propagat |
| | 19 | 161 | | _singleWsdl = WsdlHelper.GetSingleWsdl(Metadata); |
| | 19 | 162 | | _isSingleWsdlInitialized = true; |
| | | 163 | | } |
| | 19 | 164 | | } |
| | | 165 | | } |
| | 19 | 166 | | } |
| | | 167 | | |
| | | 168 | | void IExtension<ServiceHostBase>.Attach(ServiceHostBase owner) |
| | | 169 | | { |
| | 576 | 170 | | if (owner == null) |
| | | 171 | | { |
| | 0 | 172 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(owner))); |
| | | 173 | | } |
| | | 174 | | |
| | 576 | 175 | | if (_owner != null) |
| | | 176 | | { |
| | 0 | 177 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.TheServiceMet |
| | | 178 | | } |
| | | 179 | | |
| | 576 | 180 | | if (owner.State != CommunicationState.Created && owner.State != CommunicationState.Opening) |
| | | 181 | | { |
| | 0 | 182 | | throw new InvalidOperationException(owner.State.ToString()); |
| | | 183 | | } |
| | | 184 | | |
| | 576 | 185 | | _owner = owner; |
| | 576 | 186 | | } |
| | | 187 | | |
| | | 188 | | void IExtension<ServiceHostBase>.Detach(ServiceHostBase owner) |
| | | 189 | | { |
| | 0 | 190 | | if (owner == null) |
| | | 191 | | { |
| | 0 | 192 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(owner)); |
| | | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | if (_owner == null) |
| | | 196 | | { |
| | 0 | 197 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.TheServiceMet |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | if (_owner != owner) |
| | | 201 | | { |
| | 0 | 202 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(owner), SR.TheServiceMetadataExtensi |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | if (owner.State != CommunicationState.Created && owner.State != CommunicationState.Opening) |
| | | 206 | | { |
| | 0 | 207 | | throw new InvalidOperationException(owner.State.ToString()); |
| | | 208 | | } |
| | | 209 | | |
| | 0 | 210 | | _owner = null; |
| | 0 | 211 | | } |
| | | 212 | | |
| | | 213 | | internal static ServiceMetadataExtension EnsureServiceMetadataExtension(ServiceHostBase host) |
| | | 214 | | { |
| | 605 | 215 | | ServiceMetadataExtension mex = host.Extensions.Find<ServiceMetadataExtension>(); |
| | 605 | 216 | | if (mex == null) |
| | | 217 | | { |
| | 576 | 218 | | mex = new ServiceMetadataExtension(); |
| | 576 | 219 | | host.Extensions.Add(mex); |
| | | 220 | | } |
| | | 221 | | |
| | 605 | 222 | | return mex; |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | private WriteFilter GetWriteFilter(HttpRequest httpRequest, Uri listenUri, bool removeBaseAddress) |
| | | 226 | | { |
| | 25 | 227 | | WriteFilter result = null; |
| | 25 | 228 | | if (UpdateAddressDynamically) |
| | | 229 | | { |
| | | 230 | | // Update address dynamically based on the request URI |
| | 4 | 231 | | result = GetDynamicAddressWriter(httpRequest, listenUri, removeBaseAddress); |
| | | 232 | | } |
| | | 233 | | |
| | 25 | 234 | | if (result == null) |
| | | 235 | | { |
| | | 236 | | // Just use the statically known listen URI |
| | 22 | 237 | | if (removeBaseAddress) |
| | | 238 | | { |
| | 0 | 239 | | result = new LocationUpdatingWriter(BaseAddressPattern, null); |
| | | 240 | | } |
| | | 241 | | else |
| | | 242 | | { |
| | 22 | 243 | | result = new LocationUpdatingWriter(BaseAddressPattern, listenUri.ToString()); |
| | | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | 25 | 247 | | return result; |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | private WriteFilter GetWriteFilter(Message request, Uri listenUri, bool removeBaseAddress) |
| | | 251 | | { |
| | 0 | 252 | | WriteFilter result = null; |
| | 0 | 253 | | if (UpdateAddressDynamically) |
| | | 254 | | { |
| | | 255 | | // Update address dynamically based on the request URI |
| | 0 | 256 | | result = GetDynamicAddressWriter(request, listenUri, removeBaseAddress); |
| | | 257 | | } |
| | 0 | 258 | | if (result == null) |
| | | 259 | | { |
| | | 260 | | // Just use the statically known listen URI |
| | 0 | 261 | | if (removeBaseAddress) |
| | | 262 | | { |
| | 0 | 263 | | result = new LocationUpdatingWriter(BaseAddressPattern, null); |
| | | 264 | | } |
| | | 265 | | else |
| | | 266 | | { |
| | 0 | 267 | | result = new LocationUpdatingWriter(BaseAddressPattern, listenUri.ToString()); |
| | | 268 | | } |
| | | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | return result; |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | private DynamicAddressUpdateWriter GetDynamicAddressWriter(HttpRequest httpRequest, Uri listenUri, bool removeBa |
| | | 275 | | { |
| | 4 | 276 | | if (!TryGetHttpHostAndPort(listenUri, httpRequest, out string requestHost, out int requestPort, out string r |
| | | 277 | | { |
| | 0 | 278 | | return null; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | // Perf optimization: don't do dynamic update if it would be a no-op. |
| | | 282 | | // Ordinal string comparison is okay; it just means we don't get the perf optimization |
| | | 283 | | // if the listen host and request host are case-insensitively equal. |
| | 4 | 284 | | if (requestHost == listenUri.Host && |
| | 4 | 285 | | requestPort == listenUri.Port && |
| | 4 | 286 | | requestScheme == listenUri.Scheme && |
| | 4 | 287 | | (UpdatePortsByScheme == null || UpdatePortsByScheme.Count == 0)) |
| | | 288 | | { |
| | 1 | 289 | | return null; |
| | | 290 | | } |
| | | 291 | | |
| | 3 | 292 | | if (DynamicMetadataEndpointAddressProvider is UseRequestHeadersForMetadataAddressBehavior.UseHostHeaderMetad |
| | | 293 | | { |
| | 2 | 294 | | return new RequestHeadersDynamicAddressUpdateWriter(listenUri, requestHost, requestPort, UpdatePortsBySc |
| | | 295 | | } |
| | | 296 | | |
| | 1 | 297 | | return new CustomDynamicAddressUpdateWriter(listenUri, requestScheme, requestHost, requestPort); |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | private DynamicAddressUpdateWriter GetDynamicAddressWriter(Message request, Uri listenUri, bool removeBaseAddres |
| | | 301 | | { |
| | 0 | 302 | | HttpContext context = null; |
| | 0 | 303 | | if (request.Properties.TryGetValue("Microsoft.AspNetCore.Http.HttpContext", out object contextObj)) |
| | | 304 | | { |
| | 0 | 305 | | context = contextObj as HttpContext; |
| | | 306 | | } |
| | | 307 | | |
| | 0 | 308 | | if (context==null || !TryGetHttpHostAndPort(listenUri, context.Request, out string requestHost, out int requ |
| | | 309 | | { |
| | 0 | 310 | | if (request.Headers.To == null) |
| | | 311 | | { |
| | 0 | 312 | | return null; |
| | | 313 | | } |
| | 0 | 314 | | requestHost = request.Headers.To.Host; |
| | 0 | 315 | | requestPort = request.Headers.To.Port; |
| | 0 | 316 | | requestScheme = request.Headers.To.Scheme; |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | // Perf optimization: don't do dynamic update if it would be a no-op. |
| | | 320 | | // Ordinal string comparison is okay; it just means we don't get the perf optimization |
| | | 321 | | // if the listen host and request host are case-insensitively equal. |
| | 0 | 322 | | if (requestHost == listenUri.Host && |
| | 0 | 323 | | requestPort == listenUri.Port && |
| | 0 | 324 | | requestScheme == listenUri.Scheme && |
| | 0 | 325 | | (UpdatePortsByScheme == null || UpdatePortsByScheme.Count == 0)) |
| | | 326 | | { |
| | 0 | 327 | | return null; |
| | | 328 | | } |
| | | 329 | | |
| | 0 | 330 | | if (DynamicMetadataEndpointAddressProvider is UseRequestHeadersForMetadataAddressBehavior.UseHostHeaderMetad |
| | | 331 | | { |
| | 0 | 332 | | return new RequestHeadersDynamicAddressUpdateWriter(listenUri, requestHost, requestPort, UpdatePortsBySc |
| | | 333 | | } |
| | | 334 | | |
| | 0 | 335 | | return new CustomDynamicAddressUpdateWriter(listenUri, requestScheme, requestHost, requestPort); |
| | | 336 | | } |
| | | 337 | | |
| | | 338 | | internal class WSMexImpl : IMetadataExchange |
| | | 339 | | { |
| | | 340 | | internal const string MetadataMexBinding = "ServiceMetadataBehaviorMexBinding"; |
| | | 341 | | internal const string ContractName = MetadataStrings.WSTransfer.Name; |
| | | 342 | | internal const string ContractNamespace = MetadataStrings.WSTransfer.Namespace; |
| | | 343 | | internal const string GetMethodName = "Get"; |
| | | 344 | | internal const string RequestAction = MetadataStrings.WSTransfer.GetAction; |
| | | 345 | | internal const string ReplyAction = MetadataStrings.WSTransfer.GetResponseAction; |
| | | 346 | | private readonly ServiceMetadataExtension _parent; |
| | | 347 | | private readonly MetadataSet _metadataLocationSet; |
| | | 348 | | private TypedMessageConverter _converter; |
| | | 349 | | private readonly Uri _listenUri; |
| | | 350 | | |
| | 0 | 351 | | internal WSMexImpl(ServiceMetadataExtension parent, bool isListeningOnHttps, Uri listenUri) |
| | | 352 | | { |
| | 0 | 353 | | _parent = parent; |
| | 0 | 354 | | IsListeningOnHttps = isListeningOnHttps; |
| | 0 | 355 | | _listenUri = listenUri; |
| | | 356 | | |
| | 0 | 357 | | if (_parent.ExternalMetadataLocation != null && _parent.ExternalMetadataLocation != s_emptyUri) |
| | | 358 | | { |
| | 0 | 359 | | _metadataLocationSet = new MetadataSet(); |
| | 0 | 360 | | string location = GetLocationToReturn(); |
| | 0 | 361 | | MetadataSection metadataLocationSection = new MetadataSection(MetadataSection.ServiceDescriptionDial |
| | 0 | 362 | | _metadataLocationSet.MetadataSections.Add(metadataLocationSection); |
| | | 363 | | } |
| | 0 | 364 | | } |
| | | 365 | | |
| | 0 | 366 | | internal bool IsListeningOnHttps { get; set; } |
| | | 367 | | |
| | | 368 | | private string GetLocationToReturn() |
| | | 369 | | { |
| | | 370 | | Fx.Assert(_parent.ExternalMetadataLocation != null, ""); |
| | 0 | 371 | | Uri location = _parent.ExternalMetadataLocation; |
| | | 372 | | |
| | 0 | 373 | | if (!location.IsAbsoluteUri) |
| | | 374 | | { |
| | 0 | 375 | | Uri httpAddr = _parent._owner.GetVia(Uri.UriSchemeHttp, location); |
| | 0 | 376 | | Uri httpsAddr = _parent._owner.GetVia(Uri.UriSchemeHttps, location); |
| | | 377 | | |
| | 0 | 378 | | if (IsListeningOnHttps && httpsAddr != null) |
| | | 379 | | { |
| | 0 | 380 | | location = httpsAddr; |
| | | 381 | | } |
| | 0 | 382 | | else if (httpAddr != null) |
| | | 383 | | { |
| | 0 | 384 | | location = httpAddr; |
| | | 385 | | } |
| | | 386 | | else |
| | | 387 | | { |
| | 0 | 388 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(_parent.ExternalMetadataLoca |
| | | 389 | | } |
| | | 390 | | } |
| | | 391 | | |
| | 0 | 392 | | return location.ToString(); |
| | | 393 | | } |
| | | 394 | | |
| | | 395 | | private MetadataSet GatherMetadata(string dialect, string identifier) |
| | | 396 | | { |
| | 0 | 397 | | if (_metadataLocationSet != null) |
| | | 398 | | { |
| | 0 | 399 | | return _metadataLocationSet; |
| | | 400 | | } |
| | | 401 | | else |
| | | 402 | | { |
| | 0 | 403 | | MetadataSet metadataSet = new MetadataSet(); |
| | 0 | 404 | | foreach (MetadataSection document in _parent.Metadata.MetadataSections) |
| | | 405 | | { |
| | 0 | 406 | | if ((dialect == null || dialect == document.Dialect) && |
| | 0 | 407 | | (identifier == null || identifier == document.Identifier)) |
| | | 408 | | { |
| | 0 | 409 | | metadataSet.MetadataSections.Add(document); |
| | | 410 | | } |
| | | 411 | | } |
| | | 412 | | |
| | 0 | 413 | | return metadataSet; |
| | | 414 | | } |
| | | 415 | | } |
| | | 416 | | |
| | | 417 | | public Message Get(Message request) |
| | | 418 | | { |
| | 0 | 419 | | GetResponse response = new GetResponse |
| | 0 | 420 | | { |
| | 0 | 421 | | Metadata = GatherMetadata(null, null) |
| | 0 | 422 | | }; |
| | | 423 | | |
| | 0 | 424 | | response.Metadata.WriteFilter = _parent.GetWriteFilter(request, _listenUri, true); |
| | | 425 | | |
| | 0 | 426 | | if (_converter == null) |
| | | 427 | | { |
| | 0 | 428 | | _converter = TypedMessageConverter.Create(typeof(GetResponse), ReplyAction); |
| | | 429 | | } |
| | | 430 | | |
| | 0 | 431 | | return _converter.ToMessage(response, request.Version); |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | public Task<Message> GetAsync(Message request) |
| | | 435 | | { |
| | 0 | 436 | | return Task.FromException<Message>(DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplemente |
| | | 437 | | } |
| | | 438 | | } |
| | | 439 | | |
| | | 440 | | internal class HttpGetImpl |
| | | 441 | | { |
| | | 442 | | private const string DiscoToken = "disco token"; |
| | | 443 | | private const string DiscoQueryString = "disco"; |
| | | 444 | | private const string WsdlQueryString = "wsdl"; |
| | | 445 | | private const string XsdQueryString = "xsd"; |
| | | 446 | | private const string SingleWsdlQueryString = "singleWsdl"; |
| | | 447 | | private const string HtmlContentType = "text/html; charset=UTF-8"; |
| | | 448 | | private const string XmlContentType = "text/xml; charset=UTF-8"; |
| | | 449 | | private const int MaxQueryStringChars = 2048; |
| | | 450 | | |
| | | 451 | | internal const string MetadataHttpGetBinding = "ServiceMetadataBehaviorHttpGetBinding"; |
| | | 452 | | internal const string ContractName = "IHttpGetHelpPageAndMetadataContract"; |
| | | 453 | | internal const string ContractNamespace = "http://schemas.microsoft.com/2006/04/http/metadata"; |
| | | 454 | | internal const string GetMethodName = "Get"; |
| | | 455 | | internal const string RequestAction = "*"; // MessageHeaders.WildcardAction; |
| | | 456 | | internal const string ReplyAction = "*"; // MessageHeaders.WildcardAction; |
| | | 457 | | internal const string HtmlBreak = "<BR/>"; |
| | | 458 | | |
| | | 459 | | private readonly ServiceMetadataExtension _parent; |
| | 34 | 460 | | private readonly object _sync = new object(); |
| | | 461 | | private InitializationData _initData; |
| | | 462 | | private readonly Uri _listenUri; |
| | | 463 | | private readonly bool _isHttps; |
| | | 464 | | |
| | 34 | 465 | | internal HttpGetImpl(ServiceMetadataExtension parent, Uri listenUri, bool isHttps) |
| | | 466 | | { |
| | 34 | 467 | | _parent = parent; |
| | 34 | 468 | | _listenUri = listenUri; |
| | 34 | 469 | | GetWsdlEnabled = parent.HttpsGetEnabled || parent.HttpGetEnabled; |
| | 34 | 470 | | HelpPageEnabled = parent.HelpPageEnabled; |
| | 34 | 471 | | _isHttps = isHttps; |
| | 34 | 472 | | } |
| | | 473 | | |
| | 42 | 474 | | public bool HelpPageEnabled { get; set; } = false; |
| | | 475 | | |
| | 59 | 476 | | public bool GetWsdlEnabled { get; set; } |
| | | 477 | | |
| | | 478 | | private InitializationData GetInitData() |
| | | 479 | | { |
| | 31 | 480 | | if (_initData == null) |
| | | 481 | | { |
| | 25 | 482 | | lock (_sync) |
| | | 483 | | { |
| | 25 | 484 | | if (_initData == null) |
| | | 485 | | { |
| | 25 | 486 | | _initData = InitializationData.InitializeFrom(_parent); |
| | | 487 | | } |
| | 25 | 488 | | } |
| | | 489 | | } |
| | | 490 | | |
| | 31 | 491 | | return _initData; |
| | | 492 | | } |
| | | 493 | | |
| | | 494 | | private string FindWsdlReference(DynamicAddressUpdateWriter addressUpdater) |
| | | 495 | | { |
| | 4 | 496 | | if (_parent.ExternalMetadataLocation == null || _parent.ExternalMetadataLocation == s_emptyUri) |
| | | 497 | | { |
| | 4 | 498 | | return null; |
| | | 499 | | } |
| | | 500 | | else |
| | | 501 | | { |
| | 0 | 502 | | Uri location = _parent.ExternalMetadataLocation; |
| | | 503 | | |
| | 0 | 504 | | Uri result = GetUri(_listenUri, location); |
| | 0 | 505 | | if (addressUpdater != null) |
| | | 506 | | { |
| | 0 | 507 | | addressUpdater.UpdateUri(ref result); |
| | | 508 | | } |
| | | 509 | | |
| | 0 | 510 | | return result.ToString(); |
| | | 511 | | } |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | private async Task<bool> TryHandleDocumentationRequestAsync(HttpContext requestContext) |
| | | 515 | | { |
| | 4 | 516 | | if (!HelpPageEnabled) |
| | | 517 | | { |
| | 0 | 518 | | return false; |
| | | 519 | | } |
| | | 520 | | |
| | | 521 | | MetadataResult result; |
| | 4 | 522 | | if (_parent.MetadataEnabled) |
| | | 523 | | { |
| | 4 | 524 | | string discoUrl = null; |
| | 4 | 525 | | string singleWsdlUrl = null; |
| | 4 | 526 | | bool linkMetadata = true; |
| | | 527 | | |
| | 4 | 528 | | DynamicAddressUpdateWriter addressUpdater = null; |
| | 4 | 529 | | if (_parent.UpdateAddressDynamically) |
| | | 530 | | { |
| | 0 | 531 | | addressUpdater = _parent.GetDynamicAddressWriter(requestContext.Request, _listenUri, false); |
| | | 532 | | } |
| | | 533 | | |
| | 4 | 534 | | string wsdlUrl = FindWsdlReference(addressUpdater); |
| | | 535 | | |
| | 4 | 536 | | string httpGetUrl = GetHttpGetUrl(addressUpdater); |
| | | 537 | | |
| | 4 | 538 | | if (wsdlUrl == null && httpGetUrl != null) |
| | | 539 | | { |
| | 4 | 540 | | wsdlUrl = httpGetUrl + "?" + WsdlQueryString; |
| | 4 | 541 | | singleWsdlUrl = httpGetUrl + "?" + SingleWsdlQueryString; |
| | | 542 | | } |
| | | 543 | | |
| | 4 | 544 | | if (httpGetUrl != null) |
| | | 545 | | { |
| | 4 | 546 | | discoUrl = httpGetUrl + "?" + DiscoQueryString; |
| | | 547 | | } |
| | | 548 | | |
| | 4 | 549 | | if (wsdlUrl == null) |
| | | 550 | | { |
| | 0 | 551 | | wsdlUrl = GetMexUrl(addressUpdater); |
| | 0 | 552 | | linkMetadata = false; |
| | | 553 | | } |
| | | 554 | | |
| | 4 | 555 | | result = new MetadataOnHelpPageResult(discoUrl, wsdlUrl, singleWsdlUrl, GetInitData().ServiceName, G |
| | | 556 | | } |
| | | 557 | | else |
| | | 558 | | { |
| | 0 | 559 | | result = new MetadataOffHelpPageMessage(); |
| | | 560 | | } |
| | | 561 | | |
| | 4 | 562 | | SetResponseStatusAndContentType(requestContext.Response, HttpStatusCode.OK, HtmlContentType); |
| | 4 | 563 | | await result.WriteResponseAsync(requestContext.Response); |
| | 4 | 564 | | return true; |
| | 4 | 565 | | } |
| | | 566 | | |
| | | 567 | | private string GetHttpGetUrl(DynamicAddressUpdateWriter addressUpdater) |
| | | 568 | | { |
| | 4 | 569 | | Uri result = null; |
| | 4 | 570 | | if (_listenUri.Scheme == Uri.UriSchemeHttp) |
| | | 571 | | { |
| | 2 | 572 | | if (_parent.HttpGetEnabled) |
| | | 573 | | { |
| | 2 | 574 | | result = _parent.HttpGetUrl; |
| | | 575 | | } |
| | 0 | 576 | | else if (_parent.HttpsGetEnabled) |
| | | 577 | | { |
| | 0 | 578 | | result = _parent.HttpsGetUrl; |
| | | 579 | | } |
| | | 580 | | } |
| | | 581 | | else |
| | | 582 | | { |
| | 2 | 583 | | if (_parent.HttpsGetEnabled) |
| | | 584 | | { |
| | 2 | 585 | | result = _parent.HttpsGetUrl; |
| | | 586 | | } |
| | 0 | 587 | | else if (_parent.HttpGetEnabled) |
| | | 588 | | { |
| | 0 | 589 | | result = _parent.HttpGetUrl; |
| | | 590 | | } |
| | | 591 | | } |
| | | 592 | | |
| | 4 | 593 | | if (result != null) |
| | | 594 | | { |
| | 4 | 595 | | if (addressUpdater != null) |
| | | 596 | | { |
| | 0 | 597 | | addressUpdater.UpdateUri(ref result, _listenUri.Scheme != result.Scheme /*updateBaseAddressOnly* |
| | | 598 | | } |
| | | 599 | | |
| | 4 | 600 | | return result.ToString(); |
| | | 601 | | } |
| | | 602 | | |
| | 0 | 603 | | return null; |
| | | 604 | | } |
| | | 605 | | |
| | | 606 | | private string GetMexUrl(DynamicAddressUpdateWriter addressUpdater) |
| | | 607 | | { |
| | 0 | 608 | | if (_parent.MexEnabled) |
| | | 609 | | { |
| | 0 | 610 | | Uri result = _parent.MexUrl; |
| | 0 | 611 | | if (addressUpdater != null) |
| | | 612 | | { |
| | 0 | 613 | | addressUpdater.UpdateUri(ref result); |
| | | 614 | | } |
| | | 615 | | |
| | 0 | 616 | | return result.ToString(); |
| | | 617 | | } |
| | | 618 | | |
| | 0 | 619 | | return null; |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | private async Task<bool> TryHandleMetadataRequestAsync(HttpContext requestContext, IQueryCollection queries) |
| | | 623 | | { |
| | 25 | 624 | | if (!GetWsdlEnabled) |
| | | 625 | | { |
| | 0 | 626 | | return false; |
| | | 627 | | } |
| | | 628 | | |
| | 25 | 629 | | requestContext.Items["CoreWCF.Description.ServiceMetadataExtension.HttpGetImpl._listenUri"] = _listenUri |
| | | 630 | | |
| | 25 | 631 | | WriteFilter writeFilter = _parent.GetWriteFilter(requestContext.Request, _listenUri, false); |
| | | 632 | | |
| | 25 | 633 | | string query = FindQuery(queries); |
| | | 634 | | |
| | | 635 | | MetadataResult result; |
| | 25 | 636 | | if (string.IsNullOrEmpty(query)) |
| | | 637 | | { |
| | | 638 | | //if the documentation page is not available return the default wsdl if it exists |
| | 4 | 639 | | if (!HelpPageEnabled && GetInitData().DefaultWsdl != null) |
| | | 640 | | { |
| | | 641 | | // use the default WSDL |
| | 0 | 642 | | result = new ServiceDescriptionResult(GetInitData().DefaultWsdl, writeFilter); |
| | 0 | 643 | | SetResponseStatusAndContentType(requestContext.Response, HttpStatusCode.OK, XmlContentType); |
| | 0 | 644 | | GetInitData().FixImportAddresses(); |
| | 0 | 645 | | await result.WriteResponseAsync(requestContext.Response); |
| | 0 | 646 | | return true; |
| | | 647 | | } |
| | | 648 | | |
| | 4 | 649 | | return false; |
| | | 650 | | } |
| | | 651 | | |
| | | 652 | | // try to look the document up in the query table |
| | 21 | 653 | | if (GetInitData().TryQueryLookup(query, out object doc)) |
| | | 654 | | { |
| | 2 | 655 | | if (doc is WsdlNS.ServiceDescription description) |
| | | 656 | | { |
| | 0 | 657 | | result = new ServiceDescriptionResult(description, writeFilter); |
| | | 658 | | } |
| | 2 | 659 | | else if (doc is XmlSchema schema) |
| | | 660 | | { |
| | 0 | 661 | | result = new XmlSchemaResult(schema, writeFilter); |
| | | 662 | | } |
| | 2 | 663 | | else if (doc is string @string) |
| | | 664 | | { |
| | 2 | 665 | | if (@string == DiscoToken) |
| | | 666 | | { |
| | 2 | 667 | | result = CreateDiscoMessage(writeFilter as DynamicAddressUpdateWriter); |
| | | 668 | | } |
| | | 669 | | else |
| | | 670 | | { |
| | | 671 | | Fx.Assert("Bad object in HttpGetImpl docFromQuery table"); |
| | 0 | 672 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(stri |
| | | 673 | | } |
| | | 674 | | } |
| | | 675 | | else |
| | | 676 | | { |
| | | 677 | | Fx.Assert("Bad object in HttpGetImpl docFromQuery table"); |
| | 0 | 678 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(string.F |
| | | 679 | | } |
| | | 680 | | |
| | 2 | 681 | | SetResponseStatusAndContentType(requestContext.Response, HttpStatusCode.OK, XmlContentType); |
| | 2 | 682 | | GetInitData().FixImportAddresses(); |
| | 2 | 683 | | await result.WriteResponseAsync(requestContext.Response); |
| | 2 | 684 | | return true; |
| | | 685 | | } |
| | | 686 | | |
| | | 687 | | // otherwise see if they just wanted ?WSDL |
| | 19 | 688 | | if (string.Compare(query, WsdlQueryString, StringComparison.OrdinalIgnoreCase) == 0) |
| | | 689 | | { |
| | 0 | 690 | | if (GetInitData().DefaultWsdl != null) |
| | | 691 | | { |
| | | 692 | | // use the default WSDL |
| | 0 | 693 | | result = new ServiceDescriptionResult(GetInitData().DefaultWsdl, writeFilter); |
| | 0 | 694 | | SetResponseStatusAndContentType(requestContext.Response, HttpStatusCode.OK, XmlContentType); |
| | 0 | 695 | | GetInitData().FixImportAddresses(); |
| | 0 | 696 | | await result.WriteResponseAsync(requestContext.Response); |
| | 0 | 697 | | return true; |
| | | 698 | | } |
| | | 699 | | |
| | | 700 | | // or redirect to an external WSDL |
| | 0 | 701 | | string wsdlReference = FindWsdlReference(writeFilter as DynamicAddressUpdateWriter); |
| | 0 | 702 | | if (wsdlReference != null) |
| | | 703 | | { |
| | 0 | 704 | | RespondWithRedirect(requestContext.Response, wsdlReference); |
| | 0 | 705 | | return true; |
| | | 706 | | } |
| | | 707 | | } |
| | | 708 | | |
| | | 709 | | // ?singleWSDL |
| | 19 | 710 | | if (string.Compare(query, SingleWsdlQueryString, StringComparison.OrdinalIgnoreCase) == 0) |
| | | 711 | | { |
| | 19 | 712 | | WsdlNS.ServiceDescription singleWSDL = _parent.SingleWsdl; |
| | 19 | 713 | | if (singleWSDL != null) |
| | | 714 | | { |
| | 19 | 715 | | result = new ServiceDescriptionResult(singleWSDL, writeFilter); |
| | 19 | 716 | | SetResponseStatusAndContentType(requestContext.Response, HttpStatusCode.OK, XmlContentType); |
| | 19 | 717 | | await result.WriteResponseAsync(requestContext.Response); |
| | 19 | 718 | | return true; |
| | | 719 | | } |
| | | 720 | | } |
| | | 721 | | |
| | | 722 | | // we weren't able to handle the request -- return the documentation page if available |
| | 0 | 723 | | return false; |
| | 25 | 724 | | } |
| | | 725 | | |
| | | 726 | | private MetadataResult CreateDiscoMessage(DynamicAddressUpdateWriter addressUpdater) |
| | | 727 | | { |
| | 2 | 728 | | Uri wsdlUrlBase = _listenUri; |
| | 2 | 729 | | if (addressUpdater != null) |
| | | 730 | | { |
| | 0 | 731 | | addressUpdater.UpdateUri(ref wsdlUrlBase); |
| | | 732 | | } |
| | | 733 | | |
| | 2 | 734 | | string wsdlUrl = wsdlUrlBase.ToString() + "?" + WsdlQueryString; |
| | | 735 | | |
| | 2 | 736 | | Uri docUrl = null; |
| | 2 | 737 | | if (_listenUri.Scheme == Uri.UriSchemeHttp) |
| | | 738 | | { |
| | 1 | 739 | | if (_parent.HttpHelpPageEnabled) |
| | | 740 | | { |
| | 1 | 741 | | docUrl = _parent.HttpHelpPageUrl; |
| | | 742 | | } |
| | 0 | 743 | | else if (_parent.HttpsHelpPageEnabled) |
| | | 744 | | { |
| | 0 | 745 | | docUrl = _parent.HttpsGetUrl; |
| | | 746 | | } |
| | | 747 | | } |
| | | 748 | | else |
| | | 749 | | { |
| | 1 | 750 | | if (_parent.HttpsHelpPageEnabled) |
| | | 751 | | { |
| | 1 | 752 | | docUrl = _parent.HttpsHelpPageUrl; |
| | | 753 | | } |
| | 0 | 754 | | else if (_parent.HttpHelpPageEnabled) |
| | | 755 | | { |
| | 0 | 756 | | docUrl = _parent.HttpGetUrl; |
| | | 757 | | } |
| | | 758 | | } |
| | 2 | 759 | | if (addressUpdater != null) |
| | | 760 | | { |
| | 0 | 761 | | addressUpdater.UpdateUri(ref docUrl); |
| | | 762 | | } |
| | | 763 | | |
| | 2 | 764 | | return new DiscoResult(wsdlUrl, docUrl.ToString()); |
| | | 765 | | } |
| | | 766 | | |
| | | 767 | | private string FindQuery(IQueryCollection queries) |
| | | 768 | | { |
| | | 769 | | string CombineKeyAndValues(string key, string values) |
| | | 770 | | { |
| | 0 | 771 | | return string.IsNullOrWhiteSpace(values) |
| | 0 | 772 | | ? key |
| | 0 | 773 | | : $"{key}={values}"; |
| | | 774 | | } |
| | | 775 | | |
| | 25 | 776 | | if (queries.TryGetValue(WsdlQueryString, out var values)) |
| | | 777 | | { |
| | 0 | 778 | | return CombineKeyAndValues(WsdlQueryString, values); |
| | | 779 | | } |
| | | 780 | | |
| | 25 | 781 | | if (queries.TryGetValue(XsdQueryString, out values)) |
| | | 782 | | { |
| | 0 | 783 | | return CombineKeyAndValues(XsdQueryString, values); |
| | | 784 | | } |
| | | 785 | | |
| | 25 | 786 | | if (queries.TryGetValue(SingleWsdlQueryString, out _)) |
| | | 787 | | { |
| | 19 | 788 | | return SingleWsdlQueryString; |
| | | 789 | | } |
| | | 790 | | |
| | 6 | 791 | | if (_parent.HelpPageEnabled && queries.ContainsKey(DiscoQueryString)) |
| | | 792 | | { |
| | 2 | 793 | | return DiscoQueryString; |
| | | 794 | | } |
| | | 795 | | |
| | 4 | 796 | | return null; |
| | | 797 | | } |
| | | 798 | | |
| | | 799 | | private async Task<bool> ProcessHttpRequest(HttpContext requestContext) |
| | | 800 | | { |
| | 25 | 801 | | if (!"GET".Equals(requestContext.Request.Method, StringComparison.OrdinalIgnoreCase)) |
| | | 802 | | { |
| | 0 | 803 | | return false; |
| | | 804 | | } |
| | | 805 | | |
| | 25 | 806 | | string queryString = requestContext.Request.QueryString.Value; |
| | | 807 | | |
| | 25 | 808 | | if (queryString.Length > MaxQueryStringChars) |
| | | 809 | | { |
| | 0 | 810 | | SetHttpResponseStatus(requestContext.Response, HttpStatusCode.RequestUriTooLong); |
| | 0 | 811 | | return true; |
| | | 812 | | } |
| | | 813 | | |
| | 25 | 814 | | var queries = requestContext.Request.Query; |
| | 25 | 815 | | if (await TryHandleMetadataRequestAsync(requestContext, queries)) |
| | | 816 | | { |
| | 21 | 817 | | return true; |
| | | 818 | | } |
| | | 819 | | |
| | 4 | 820 | | if (await TryHandleDocumentationRequestAsync(requestContext)) |
| | | 821 | | { |
| | 4 | 822 | | return true; |
| | | 823 | | } |
| | | 824 | | |
| | 0 | 825 | | return false; |
| | 25 | 826 | | } |
| | | 827 | | |
| | | 828 | | private class InitializationData |
| | | 829 | | { |
| | | 830 | | private readonly Dictionary<string, object> _docFromQuery; |
| | | 831 | | private readonly Dictionary<object, string> _queryFromDoc; |
| | | 832 | | private readonly WsdlNS.ServiceDescriptionCollection _wsdls; |
| | | 833 | | private readonly XmlSchemaSet _xsds; |
| | | 834 | | |
| | | 835 | | public string ServiceName; |
| | | 836 | | public string ClientName; |
| | | 837 | | public WsdlNS.ServiceDescription DefaultWsdl; |
| | | 838 | | |
| | 25 | 839 | | private InitializationData( |
| | 25 | 840 | | Dictionary<string, object> docFromQuery, |
| | 25 | 841 | | Dictionary<object, string> queryFromDoc, |
| | 25 | 842 | | WsdlNS.ServiceDescriptionCollection wsdls, |
| | 25 | 843 | | XmlSchemaSet xsds) |
| | | 844 | | { |
| | 25 | 845 | | _docFromQuery = docFromQuery; |
| | 25 | 846 | | _queryFromDoc = queryFromDoc; |
| | 25 | 847 | | _wsdls = wsdls; |
| | 25 | 848 | | _xsds = xsds; |
| | 25 | 849 | | } |
| | | 850 | | |
| | | 851 | | public bool TryQueryLookup(string query, out object doc) |
| | | 852 | | { |
| | 21 | 853 | | return _docFromQuery.TryGetValue(query, out doc); |
| | | 854 | | } |
| | | 855 | | |
| | | 856 | | public static InitializationData InitializeFrom(ServiceMetadataExtension extension) |
| | | 857 | | { |
| | 25 | 858 | | Dictionary<string, object> docFromQueryInit = new Dictionary<string, object>(StringComparer.OrdinalI |
| | 25 | 859 | | Dictionary<object, string> queryFromDocInit = new Dictionary<object, string>(); |
| | | 860 | | |
| | | 861 | | // this collection type provides useful lookup features |
| | 25 | 862 | | WsdlNS.ServiceDescriptionCollection wsdls = CollectWsdls(extension.Metadata); |
| | 25 | 863 | | XmlSchemaSet xsds = CollectXsds(extension.Metadata); |
| | | 864 | | |
| | 25 | 865 | | WsdlNS.ServiceDescription defaultWsdl = null; |
| | 25 | 866 | | WsdlNS.Service someService = GetAnyService(wsdls); |
| | 25 | 867 | | if (someService != null) |
| | | 868 | | { |
| | 25 | 869 | | defaultWsdl = someService.ServiceDescription; |
| | | 870 | | } |
| | | 871 | | |
| | | 872 | | // WSDLs |
| | | 873 | | { |
| | 25 | 874 | | int i = 0; |
| | 100 | 875 | | foreach (WsdlNS.ServiceDescription wsdlDoc in wsdls) |
| | | 876 | | { |
| | 25 | 877 | | string query = WsdlQueryString; |
| | 25 | 878 | | if (wsdlDoc != defaultWsdl) // don't count the WSDL at ?WSDL |
| | | 879 | | { |
| | 0 | 880 | | query += "=wsdl" + (i++).ToString(CultureInfo.InvariantCulture); |
| | | 881 | | } |
| | | 882 | | |
| | 25 | 883 | | docFromQueryInit.Add(query, wsdlDoc); |
| | 25 | 884 | | queryFromDocInit.Add(wsdlDoc, query); |
| | | 885 | | } |
| | | 886 | | } |
| | | 887 | | |
| | | 888 | | // XSDs |
| | | 889 | | { |
| | 25 | 890 | | int i = 0; |
| | 198 | 891 | | foreach (XmlSchema xsdDoc in xsds.Schemas()) |
| | | 892 | | { |
| | 74 | 893 | | string query = XsdQueryString + "=xsd" + (i++).ToString(CultureInfo.InvariantCulture); |
| | 74 | 894 | | docFromQueryInit.Add(query, xsdDoc); |
| | 74 | 895 | | queryFromDocInit.Add(xsdDoc, query); |
| | | 896 | | } |
| | | 897 | | } |
| | | 898 | | |
| | | 899 | | // Disco |
| | 25 | 900 | | if (extension.HelpPageEnabled) |
| | | 901 | | { |
| | 25 | 902 | | string query = DiscoQueryString; |
| | 25 | 903 | | docFromQueryInit.Add(query, DiscoToken); |
| | 25 | 904 | | queryFromDocInit.Add(DiscoToken, query); |
| | | 905 | | } |
| | | 906 | | |
| | 25 | 907 | | InitializationData data = new InitializationData(docFromQueryInit, queryFromDocInit, wsdls, xsds) |
| | 25 | 908 | | { |
| | 25 | 909 | | DefaultWsdl = defaultWsdl, |
| | 25 | 910 | | ServiceName = GetAnyWsdlName(wsdls), |
| | 25 | 911 | | ClientName = ClientClassGenerator.GetClientClassName(GetAnyContractName(wsdls) ?? "IHello") |
| | 25 | 912 | | }; |
| | | 913 | | |
| | 25 | 914 | | return data; |
| | | 915 | | } |
| | | 916 | | |
| | | 917 | | private static WsdlNS.ServiceDescriptionCollection CollectWsdls(MetadataSet metadata) |
| | | 918 | | { |
| | 25 | 919 | | WsdlNS.ServiceDescriptionCollection wsdls = new WsdlNS.ServiceDescriptionCollection(); |
| | 248 | 920 | | foreach (MetadataSection section in metadata.MetadataSections) |
| | | 921 | | { |
| | 99 | 922 | | if (section.Metadata is WsdlNS.ServiceDescription description) |
| | | 923 | | { |
| | 25 | 924 | | wsdls.Add(description); |
| | | 925 | | } |
| | | 926 | | } |
| | | 927 | | |
| | 25 | 928 | | return wsdls; |
| | | 929 | | } |
| | | 930 | | |
| | | 931 | | private static XmlSchemaSet CollectXsds(MetadataSet metadata) |
| | | 932 | | { |
| | 25 | 933 | | XmlSchemaSet xsds = new XmlSchemaSet |
| | 25 | 934 | | { |
| | 25 | 935 | | XmlResolver = null |
| | 25 | 936 | | }; |
| | 248 | 937 | | foreach (MetadataSection section in metadata.MetadataSections) |
| | | 938 | | { |
| | 99 | 939 | | if (section.Metadata is XmlSchema schema) |
| | | 940 | | { |
| | 74 | 941 | | xsds.Add(schema); |
| | | 942 | | } |
| | | 943 | | } |
| | | 944 | | |
| | 25 | 945 | | return xsds; |
| | | 946 | | } |
| | | 947 | | |
| | | 948 | | internal void FixImportAddresses() |
| | | 949 | | { |
| | | 950 | | // fixup imports and includes with addresses |
| | | 951 | | // WSDLs |
| | 8 | 952 | | foreach (WsdlNS.ServiceDescription wsdlDoc in _wsdls) |
| | | 953 | | { |
| | 2 | 954 | | FixImportAddresses(wsdlDoc); |
| | | 955 | | } |
| | | 956 | | // XSDs |
| | 16 | 957 | | foreach (XmlSchema xsdDoc in _xsds.Schemas()) |
| | | 958 | | { |
| | 6 | 959 | | FixImportAddresses(xsdDoc); |
| | | 960 | | } |
| | 2 | 961 | | } |
| | | 962 | | |
| | | 963 | | private void FixImportAddresses(WsdlNS.ServiceDescription wsdlDoc) |
| | | 964 | | { |
| | 4 | 965 | | foreach (WsdlNS.Import import in wsdlDoc.Imports) |
| | | 966 | | { |
| | 0 | 967 | | if (!string.IsNullOrEmpty(import.Location)) |
| | | 968 | | { |
| | | 969 | | continue; |
| | | 970 | | } |
| | | 971 | | |
| | 0 | 972 | | WsdlNS.ServiceDescription targetDoc = _wsdls[import.Namespace ?? string.Empty]; |
| | 0 | 973 | | if (targetDoc != null) |
| | | 974 | | { |
| | 0 | 975 | | string query = _queryFromDoc[targetDoc]; |
| | 0 | 976 | | import.Location = BaseAddressPattern + "?" + query; |
| | | 977 | | } |
| | | 978 | | } |
| | | 979 | | |
| | 2 | 980 | | if (wsdlDoc.Types != null) |
| | | 981 | | { |
| | 8 | 982 | | foreach (XmlSchema xsdDoc in wsdlDoc.Types.Schemas) |
| | | 983 | | { |
| | 2 | 984 | | FixImportAddresses(xsdDoc); |
| | | 985 | | } |
| | | 986 | | } |
| | 2 | 987 | | } |
| | | 988 | | |
| | | 989 | | private void FixImportAddresses(XmlSchema xsdDoc) |
| | | 990 | | { |
| | 32 | 991 | | foreach (XmlSchemaObject o in xsdDoc.Includes) |
| | | 992 | | { |
| | 8 | 993 | | if (!(o is XmlSchemaExternal external) || !string.IsNullOrEmpty(external.SchemaLocation)) |
| | | 994 | | { |
| | | 995 | | continue; |
| | | 996 | | } |
| | | 997 | | |
| | 8 | 998 | | string targetNs = external is XmlSchemaImport import ? import.Namespace : xsdDoc.TargetNamespace |
| | | 999 | | |
| | 24 | 1000 | | foreach (XmlSchema targetXsd in _xsds.Schemas(targetNs ?? string.Empty)) |
| | | 1001 | | { |
| | 8 | 1002 | | if (targetXsd != xsdDoc) |
| | | 1003 | | { |
| | 8 | 1004 | | string query = _queryFromDoc[targetXsd]; |
| | 8 | 1005 | | external.SchemaLocation = BaseAddressPattern + "?" + query; |
| | 8 | 1006 | | break; |
| | | 1007 | | } |
| | | 1008 | | } |
| | | 1009 | | } |
| | 8 | 1010 | | } |
| | | 1011 | | |
| | | 1012 | | private static string GetAnyContractName(WsdlNS.ServiceDescriptionCollection wsdls) |
| | | 1013 | | { |
| | | 1014 | | // try to track down a WSDL portType name using a wsdl:service as a starting point |
| | 75 | 1015 | | foreach (WsdlNS.ServiceDescription wsdl in wsdls) |
| | | 1016 | | { |
| | 75 | 1017 | | foreach (WsdlNS.Service service in wsdl.Services) |
| | | 1018 | | { |
| | 75 | 1019 | | foreach (WsdlNS.Port port in service.Ports) |
| | | 1020 | | { |
| | 25 | 1021 | | if (!port.Binding.IsEmpty) |
| | | 1022 | | { |
| | 25 | 1023 | | WsdlNS.Binding binding = wsdls.GetBinding(port.Binding); |
| | 25 | 1024 | | if (!binding.Type.IsEmpty) |
| | | 1025 | | { |
| | 25 | 1026 | | return binding.Type.Name; |
| | | 1027 | | } |
| | | 1028 | | } |
| | | 1029 | | } |
| | | 1030 | | } |
| | | 1031 | | } |
| | | 1032 | | |
| | 0 | 1033 | | return null; |
| | 25 | 1034 | | } |
| | | 1035 | | |
| | | 1036 | | private static WsdlNS.Service GetAnyService(WsdlNS.ServiceDescriptionCollection wsdls) |
| | | 1037 | | { |
| | | 1038 | | // try to track down a WSDL service |
| | 75 | 1039 | | foreach (WsdlNS.ServiceDescription wsdl in wsdls) |
| | | 1040 | | { |
| | 25 | 1041 | | if (wsdl.Services.Count > 0) |
| | | 1042 | | { |
| | 25 | 1043 | | return wsdl.Services[0]; |
| | | 1044 | | } |
| | | 1045 | | } |
| | | 1046 | | |
| | 0 | 1047 | | return null; |
| | 25 | 1048 | | } |
| | | 1049 | | |
| | | 1050 | | private static string GetAnyWsdlName(WsdlNS.ServiceDescriptionCollection wsdls) |
| | | 1051 | | { |
| | | 1052 | | // try to track down a WSDL name |
| | 75 | 1053 | | foreach (WsdlNS.ServiceDescription wsdl in wsdls) |
| | | 1054 | | { |
| | 25 | 1055 | | if (!string.IsNullOrEmpty(wsdl.Name)) |
| | | 1056 | | { |
| | 25 | 1057 | | return wsdl.Name; |
| | | 1058 | | } |
| | | 1059 | | } |
| | | 1060 | | |
| | 0 | 1061 | | return null; |
| | 25 | 1062 | | } |
| | | 1063 | | } |
| | | 1064 | | |
| | | 1065 | | internal RequestDelegate MetadataMiddleware(RequestDelegate _next) |
| | | 1066 | | { |
| | 34 | 1067 | | string path = _listenUri.AbsolutePath; |
| | 34 | 1068 | | return async context => |
| | 34 | 1069 | | { |
| | 31 | 1070 | | if (context.Request.Path != path || |
| | 31 | 1071 | | !await HandleRequest(context)) |
| | 34 | 1072 | | { |
| | 6 | 1073 | | await _next(context); |
| | 34 | 1074 | | } |
| | 65 | 1075 | | }; |
| | | 1076 | | } |
| | | 1077 | | |
| | | 1078 | | internal async Task<bool> HandleRequest(HttpContext httpContext) |
| | | 1079 | | { |
| | | 1080 | | try |
| | | 1081 | | { |
| | 25 | 1082 | | if (httpContext.Request.IsHttps != _isHttps) // Prevent handling requests on HTTP url's for HTTPS re |
| | | 1083 | | { |
| | 0 | 1084 | | return false; |
| | | 1085 | | } |
| | | 1086 | | |
| | 25 | 1087 | | return await ProcessHttpRequest(httpContext); |
| | | 1088 | | } |
| | | 1089 | | catch (Exception exception) |
| | | 1090 | | { |
| | 0 | 1091 | | var result = new MetadataOnHelpPageResult(SR.SFxDocExt_Error, new ExceptionDetail(exception)); |
| | 0 | 1092 | | SetResponseStatusAndContentType(httpContext.Response, HttpStatusCode.InternalServerError, HtmlConten |
| | 0 | 1093 | | await result.WriteResponseAsync(httpContext.Response); |
| | 0 | 1094 | | return true; |
| | | 1095 | | } |
| | 25 | 1096 | | } |
| | | 1097 | | |
| | | 1098 | | #region static helpers |
| | | 1099 | | private static void SetResponseStatusAndContentType(HttpResponse httpResponse, HttpStatusCode status, string |
| | | 1100 | | { |
| | 25 | 1101 | | httpResponse.StatusCode = (int)status; |
| | 25 | 1102 | | httpResponse.ContentType = contentType; |
| | 25 | 1103 | | } |
| | | 1104 | | |
| | | 1105 | | private static void RespondWithRedirect(HttpResponse httpResponse, string redirectedDestination) |
| | | 1106 | | { |
| | 0 | 1107 | | httpResponse.StatusCode = (int)HttpStatusCode.RedirectKeepVerb; |
| | 0 | 1108 | | httpResponse.Headers["Location"] = redirectedDestination; |
| | 0 | 1109 | | } |
| | | 1110 | | |
| | | 1111 | | private static void SetHttpResponseStatus(HttpResponse response, HttpStatusCode code) |
| | | 1112 | | { |
| | 0 | 1113 | | response.StatusCode = (int)code; |
| | 0 | 1114 | | } |
| | | 1115 | | |
| | | 1116 | | internal static Uri GetUri(Uri baseUri, Uri relativeUri) |
| | | 1117 | | { |
| | 0 | 1118 | | return GetUri(baseUri, relativeUri.OriginalString); |
| | | 1119 | | } |
| | | 1120 | | |
| | | 1121 | | internal static Uri GetUri(Uri baseUri, string path) |
| | | 1122 | | { |
| | 0 | 1123 | | if (path.StartsWith("/", StringComparison.Ordinal) || path.StartsWith("\\", StringComparison.Ordinal)) |
| | | 1124 | | { |
| | 0 | 1125 | | int i = 1; |
| | 0 | 1126 | | for (; i < path.Length; ++i) |
| | | 1127 | | { |
| | 0 | 1128 | | if (path[i] != '/' && path[i] != '\\') |
| | | 1129 | | { |
| | | 1130 | | break; |
| | | 1131 | | } |
| | | 1132 | | } |
| | | 1133 | | |
| | 0 | 1134 | | path = path.Substring(i); |
| | | 1135 | | } |
| | | 1136 | | |
| | 0 | 1137 | | if (path.Length == 0) |
| | | 1138 | | { |
| | 0 | 1139 | | return baseUri; |
| | | 1140 | | } |
| | | 1141 | | |
| | 0 | 1142 | | if (!baseUri.AbsoluteUri.EndsWith("/", StringComparison.Ordinal)) |
| | | 1143 | | { |
| | 0 | 1144 | | baseUri = new Uri(baseUri.AbsoluteUri + "/"); |
| | | 1145 | | } |
| | | 1146 | | |
| | 0 | 1147 | | return new Uri(baseUri, path); |
| | | 1148 | | } |
| | | 1149 | | |
| | | 1150 | | #endregion static helpers |
| | | 1151 | | |
| | | 1152 | | #region Helper Message implementations |
| | | 1153 | | private class DiscoResult : MetadataResult |
| | | 1154 | | { |
| | | 1155 | | private readonly string _wsdlAddress; |
| | | 1156 | | private readonly string _docAddress; |
| | | 1157 | | |
| | 2 | 1158 | | public DiscoResult(string wsdlAddress, string docAddress) |
| | | 1159 | | { |
| | 2 | 1160 | | _wsdlAddress = wsdlAddress; |
| | 2 | 1161 | | _docAddress = docAddress; |
| | 2 | 1162 | | } |
| | | 1163 | | |
| | | 1164 | | protected override void Write(XmlWriter writer) |
| | | 1165 | | { |
| | 2 | 1166 | | writer.WriteStartDocument(); |
| | 2 | 1167 | | writer.WriteStartElement("discovery", "http://schemas.xmlsoap.org/disco/"); |
| | 2 | 1168 | | writer.WriteStartElement("contractRef", "http://schemas.xmlsoap.org/disco/scl/"); |
| | 2 | 1169 | | writer.WriteAttributeString("ref", _wsdlAddress); |
| | 2 | 1170 | | writer.WriteAttributeString("docRef", _docAddress); |
| | 2 | 1171 | | writer.WriteEndElement(); // </contractRef> |
| | 2 | 1172 | | writer.WriteEndElement(); // </discovery> |
| | 2 | 1173 | | writer.WriteEndDocument(); |
| | 2 | 1174 | | } |
| | | 1175 | | } |
| | | 1176 | | |
| | | 1177 | | private class MetadataOnHelpPageResult : MetadataResult |
| | | 1178 | | { |
| | | 1179 | | private readonly string _discoUrl; |
| | | 1180 | | private readonly string _metadataUrl; |
| | | 1181 | | private readonly string _singleWsdlUrl; |
| | | 1182 | | private readonly string _serviceName; |
| | | 1183 | | private readonly string _clientName; |
| | | 1184 | | private readonly bool _linkMetadata; |
| | | 1185 | | private readonly string _errorMessage; |
| | | 1186 | | private readonly ExceptionDetail _exceptionDetail; |
| | | 1187 | | |
| | | 1188 | | public MetadataOnHelpPageResult(string discoUrl, string metadataUrl, string singleWsdlUrl, string servic |
| | 4 | 1189 | | : base() |
| | | 1190 | | { |
| | 4 | 1191 | | _discoUrl = discoUrl; |
| | 4 | 1192 | | _metadataUrl = metadataUrl; |
| | 4 | 1193 | | _singleWsdlUrl = singleWsdlUrl; |
| | 4 | 1194 | | _serviceName = serviceName; |
| | 4 | 1195 | | _clientName = clientName; |
| | 4 | 1196 | | _linkMetadata = linkMetadata; |
| | 4 | 1197 | | } |
| | | 1198 | | |
| | | 1199 | | public MetadataOnHelpPageResult(string errorMessage, ExceptionDetail exceptionDetail) |
| | 0 | 1200 | | : base() |
| | | 1201 | | { |
| | 0 | 1202 | | _errorMessage = errorMessage; |
| | 0 | 1203 | | _exceptionDetail = exceptionDetail; |
| | 0 | 1204 | | } |
| | | 1205 | | |
| | | 1206 | | protected override void Write(XmlWriter writer) |
| | | 1207 | | { |
| | 4 | 1208 | | HelpPageWriter page = new HelpPageWriter(writer); |
| | | 1209 | | |
| | 4 | 1210 | | writer.WriteStartElement("HTML"); |
| | 4 | 1211 | | writer.WriteAttributeString("lang", "en"); // We don't have localized strings so no need to look up |
| | 4 | 1212 | | writer.WriteStartElement("HEAD"); |
| | | 1213 | | |
| | 4 | 1214 | | if (!string.IsNullOrEmpty(_discoUrl)) |
| | | 1215 | | { |
| | 4 | 1216 | | page.WriteDiscoLink(_discoUrl); |
| | | 1217 | | } |
| | | 1218 | | |
| | 4 | 1219 | | page.WriteStyleSheet(); |
| | | 1220 | | |
| | 4 | 1221 | | page.WriteTitle(!string.IsNullOrEmpty(_serviceName) ? SR.Format(SR.SFxDocExt_MainPageTitle, _service |
| | | 1222 | | |
| | 4 | 1223 | | if (!string.IsNullOrEmpty(_errorMessage)) |
| | | 1224 | | { |
| | 0 | 1225 | | page.WriteError(_errorMessage); |
| | | 1226 | | |
| | 0 | 1227 | | if (_exceptionDetail != null) |
| | | 1228 | | { |
| | 0 | 1229 | | page.WriteExceptionDetail(_exceptionDetail); |
| | | 1230 | | } |
| | | 1231 | | } |
| | | 1232 | | else |
| | | 1233 | | { |
| | 4 | 1234 | | page.WriteToolUsage(_metadataUrl, _singleWsdlUrl, _linkMetadata); |
| | 4 | 1235 | | page.WriteSampleCode(_clientName); |
| | | 1236 | | } |
| | | 1237 | | |
| | 4 | 1238 | | writer.WriteEndElement(); // BODY |
| | 4 | 1239 | | writer.WriteEndElement(); // HTML |
| | 4 | 1240 | | } |
| | | 1241 | | |
| | | 1242 | | private struct HelpPageWriter |
| | | 1243 | | { |
| | | 1244 | | private readonly XmlWriter _writer; |
| | | 1245 | | public HelpPageWriter(XmlWriter writer) |
| | | 1246 | | { |
| | 4 | 1247 | | _writer = writer; |
| | 4 | 1248 | | } |
| | | 1249 | | |
| | | 1250 | | internal void WriteClass(string className) |
| | | 1251 | | { |
| | 24 | 1252 | | _writer.WriteStartElement("font"); |
| | 24 | 1253 | | _writer.WriteAttributeString("color", "black"); |
| | 24 | 1254 | | _writer.WriteString(className); |
| | 24 | 1255 | | _writer.WriteEndElement(); // font |
| | 24 | 1256 | | } |
| | | 1257 | | |
| | | 1258 | | internal void WriteComment(string comment) |
| | | 1259 | | { |
| | 16 | 1260 | | _writer.WriteStartElement("font"); |
| | 16 | 1261 | | _writer.WriteAttributeString("color", "darkgreen"); |
| | 16 | 1262 | | _writer.WriteString(comment); |
| | 16 | 1263 | | _writer.WriteEndElement(); // font |
| | 16 | 1264 | | } |
| | | 1265 | | |
| | | 1266 | | internal void WriteDiscoLink(string discoUrl) |
| | | 1267 | | { |
| | 4 | 1268 | | _writer.WriteStartElement("link"); |
| | 4 | 1269 | | _writer.WriteAttributeString("rel", "alternate"); |
| | 4 | 1270 | | _writer.WriteAttributeString("type", "text/xml"); |
| | 4 | 1271 | | _writer.WriteAttributeString("href", discoUrl); |
| | 4 | 1272 | | _writer.WriteEndElement(); // link |
| | 4 | 1273 | | } |
| | | 1274 | | |
| | | 1275 | | internal void WriteError(string message) |
| | | 1276 | | { |
| | 0 | 1277 | | _writer.WriteStartElement("P"); |
| | 0 | 1278 | | _writer.WriteAttributeString("class", "intro"); |
| | 0 | 1279 | | _writer.WriteString(message); |
| | 0 | 1280 | | _writer.WriteEndElement(); // P |
| | | 1281 | | |
| | 0 | 1282 | | } |
| | | 1283 | | |
| | | 1284 | | internal void WriteKeyword(string keyword) |
| | | 1285 | | { |
| | 36 | 1286 | | _writer.WriteStartElement("font"); |
| | 36 | 1287 | | _writer.WriteAttributeString("color", "blue"); |
| | 36 | 1288 | | _writer.WriteString(keyword); |
| | 36 | 1289 | | _writer.WriteEndElement(); // font |
| | 36 | 1290 | | } |
| | | 1291 | | |
| | | 1292 | | internal void WriteSampleCode(string clientName) |
| | | 1293 | | { |
| | 4 | 1294 | | _writer.WriteStartElement("P"); |
| | 4 | 1295 | | _writer.WriteAttributeString("class", "intro"); |
| | 4 | 1296 | | _writer.WriteRaw(SR.SFxDocExt_MainPageIntro2); |
| | 4 | 1297 | | _writer.WriteEndElement(); // P |
| | | 1298 | | |
| | | 1299 | | // C# |
| | 4 | 1300 | | _writer.WriteRaw("<h2 class='intro'>C#</h2><br />"); |
| | 4 | 1301 | | _writer.WriteStartElement("PRE"); |
| | 4 | 1302 | | WriteKeyword("class "); |
| | 4 | 1303 | | WriteClass("Test\n"); |
| | 4 | 1304 | | _writer.WriteString("{\n"); |
| | 4 | 1305 | | WriteKeyword(" static void "); |
| | 4 | 1306 | | _writer.WriteString("Main()\n"); |
| | 4 | 1307 | | _writer.WriteString(" {\n"); |
| | 4 | 1308 | | _writer.WriteString(" "); |
| | 4 | 1309 | | WriteClass(clientName); |
| | 4 | 1310 | | _writer.WriteString(" client = "); |
| | 4 | 1311 | | WriteKeyword("new "); |
| | 4 | 1312 | | WriteClass(clientName); |
| | 4 | 1313 | | _writer.WriteString("();\n\n"); |
| | 4 | 1314 | | WriteComment(" // " + SR.SFxDocExt_MainPageComment+ "\n\n"); |
| | 4 | 1315 | | WriteComment(" // " + SR.SFxDocExt_MainPageComment2+ "\n"); |
| | 4 | 1316 | | _writer.WriteString(" client.Close();\n"); |
| | 4 | 1317 | | _writer.WriteString(" }\n"); |
| | 4 | 1318 | | _writer.WriteString("}\n"); |
| | 4 | 1319 | | _writer.WriteEndElement(); // PRE |
| | 4 | 1320 | | _writer.WriteRaw(HtmlBreak); |
| | | 1321 | | |
| | | 1322 | | |
| | | 1323 | | // VB |
| | 4 | 1324 | | _writer.WriteRaw("<h2 class='intro'>Visual Basic</h2><br />"); |
| | 4 | 1325 | | _writer.WriteStartElement("PRE"); |
| | 4 | 1326 | | WriteKeyword("Class "); |
| | 4 | 1327 | | WriteClass("Test\n"); |
| | 4 | 1328 | | WriteKeyword(" Shared Sub "); |
| | 4 | 1329 | | _writer.WriteString("Main()\n"); |
| | 4 | 1330 | | WriteKeyword(" Dim "); |
| | 4 | 1331 | | _writer.WriteString("client As "); |
| | 4 | 1332 | | WriteClass(clientName); |
| | 4 | 1333 | | _writer.WriteString(" = "); |
| | 4 | 1334 | | WriteKeyword("New "); |
| | 4 | 1335 | | WriteClass(clientName); |
| | 4 | 1336 | | _writer.WriteString("()\n"); |
| | 4 | 1337 | | WriteComment(" ' " + SR.SFxDocExt_MainPageComment+ "\n\n"); |
| | 4 | 1338 | | WriteComment(" ' " + SR.SFxDocExt_MainPageComment2+ "\n"); |
| | 4 | 1339 | | _writer.WriteString(" client.Close()\n"); |
| | 4 | 1340 | | WriteKeyword(" End Sub\n"); |
| | 4 | 1341 | | WriteKeyword("End Class"); |
| | 4 | 1342 | | _writer.WriteEndElement(); // PRE |
| | 4 | 1343 | | } |
| | | 1344 | | |
| | | 1345 | | internal void WriteExceptionDetail(ExceptionDetail exceptionDetail) |
| | | 1346 | | { |
| | 0 | 1347 | | _writer.WriteStartElement("PRE"); |
| | 0 | 1348 | | _writer.WriteString(exceptionDetail.ToString().Replace("\r", "")); |
| | 0 | 1349 | | _writer.WriteEndElement(); // PRE |
| | 0 | 1350 | | } |
| | | 1351 | | |
| | | 1352 | | internal void WriteStyleSheet() |
| | | 1353 | | { |
| | 4 | 1354 | | _writer.WriteStartElement("STYLE"); |
| | 4 | 1355 | | _writer.WriteAttributeString("type", "text/css"); |
| | 4 | 1356 | | _writer.WriteString("#content{ FONT-SIZE: 0.7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px}"); |
| | 4 | 1357 | | _writer.WriteString("BODY{MARGIN-TOP: 0px; MARGIN-LEFT: 0px; COLOR: #000000; FONT-FAMILY: Verdan |
| | 4 | 1358 | | _writer.WriteString("P{MARGIN-TOP: 0px; MARGIN-BOTTOM: 12px; COLOR: #000000; FONT-FAMILY: Verdan |
| | 4 | 1359 | | _writer.WriteString("PRE{BORDER-RIGHT: #f0f0e0 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #f0f0e |
| | 4 | 1360 | | _writer.WriteString(".heading1{MARGIN-TOP: 0px; PADDING-LEFT: 15px; FONT-WEIGHT: normal; FONT-SI |
| | 4 | 1361 | | _writer.WriteString(".intro{display: block; font-size: 1em;}"); |
| | 4 | 1362 | | _writer.WriteEndElement(); |
| | 4 | 1363 | | } |
| | | 1364 | | |
| | | 1365 | | internal void WriteTitle(string title) |
| | | 1366 | | { |
| | 4 | 1367 | | _writer.WriteElementString("TITLE", title); |
| | 4 | 1368 | | _writer.WriteEndElement(); |
| | 4 | 1369 | | _writer.WriteStartElement("BODY"); |
| | 4 | 1370 | | _writer.WriteStartElement("DIV"); |
| | 4 | 1371 | | _writer.WriteAttributeString("id", "content"); |
| | 4 | 1372 | | _writer.WriteAttributeString("role", "main"); |
| | 4 | 1373 | | _writer.WriteStartElement("h1"); |
| | 4 | 1374 | | _writer.WriteAttributeString("class", "heading1"); |
| | 4 | 1375 | | _writer.WriteString(title); |
| | 4 | 1376 | | _writer.WriteEndElement(); |
| | 4 | 1377 | | _writer.WriteRaw(HtmlBreak); |
| | | 1378 | | |
| | 4 | 1379 | | } |
| | | 1380 | | |
| | | 1381 | | internal void WriteToolUsage(string wsdlUrl, string singleWsdlUrl, bool linkMetadata) |
| | | 1382 | | { |
| | 4 | 1383 | | _writer.WriteStartElement("P"); |
| | 4 | 1384 | | _writer.WriteAttributeString("class", "intro"); |
| | | 1385 | | |
| | 4 | 1386 | | if (wsdlUrl != null) |
| | | 1387 | | { |
| | 4 | 1388 | | WriteMetadataAddress(SR.SFxDocExt_MainPageIntro1a, "svcutil.exe ", wsdlUrl, linkMetadata); |
| | 4 | 1389 | | if (singleWsdlUrl != null) |
| | | 1390 | | { |
| | | 1391 | | // ?singleWsdl message |
| | 4 | 1392 | | _writer.WriteStartElement("P"); |
| | 4 | 1393 | | WriteMetadataAddress(SR.SFxDocExt_MainPageIntroSingleWsdl, null, singleWsdlUrl, linkMeta |
| | 4 | 1394 | | _writer.WriteEndElement(); |
| | | 1395 | | } |
| | | 1396 | | } |
| | | 1397 | | else |
| | | 1398 | | { |
| | | 1399 | | // no metadata message |
| | 0 | 1400 | | _writer.WriteRaw(SR.SFxDocExt_MainPageIntro1b); |
| | | 1401 | | } |
| | 4 | 1402 | | _writer.WriteEndElement(); // P |
| | 4 | 1403 | | } |
| | | 1404 | | |
| | | 1405 | | private void WriteMetadataAddress(string introductionText, string clientToolName, string wsdlUrl, bo |
| | | 1406 | | { |
| | 8 | 1407 | | _writer.WriteRaw(introductionText); |
| | 8 | 1408 | | _writer.WriteRaw(HtmlBreak); |
| | 8 | 1409 | | _writer.WriteStartElement("PRE"); |
| | 8 | 1410 | | if (!string.IsNullOrEmpty(clientToolName)) |
| | | 1411 | | { |
| | 4 | 1412 | | _writer.WriteString(clientToolName); |
| | | 1413 | | } |
| | | 1414 | | |
| | 8 | 1415 | | if (linkMetadata) |
| | | 1416 | | { |
| | 8 | 1417 | | _writer.WriteStartElement("A"); |
| | 8 | 1418 | | _writer.WriteAttributeString("HREF", wsdlUrl); |
| | | 1419 | | } |
| | | 1420 | | |
| | 8 | 1421 | | _writer.WriteString(wsdlUrl); |
| | | 1422 | | |
| | 8 | 1423 | | if (linkMetadata) |
| | | 1424 | | { |
| | 8 | 1425 | | _writer.WriteEndElement(); // A |
| | | 1426 | | } |
| | | 1427 | | |
| | 8 | 1428 | | _writer.WriteEndElement(); // PRE |
| | 8 | 1429 | | } |
| | | 1430 | | } |
| | | 1431 | | } |
| | | 1432 | | |
| | | 1433 | | private class MetadataOffHelpPageMessage : MetadataResult |
| | | 1434 | | { |
| | | 1435 | | protected override void Write(XmlWriter writer) |
| | | 1436 | | { |
| | 0 | 1437 | | writer.WriteStartElement("HTML"); |
| | 0 | 1438 | | writer.WriteStartElement("HEAD"); |
| | 0 | 1439 | | writer.WriteRaw(string.Format(CultureInfo.InvariantCulture, |
| | 0 | 1440 | | @"<STYLE type=""text/css"">#content{{ FONT-SIZE: 0.7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px}} |
| | 0 | 1441 | | <TITLE>Service</TITLE>")); |
| | 0 | 1442 | | writer.WriteEndElement(); //HEAD |
| | | 1443 | | |
| | 0 | 1444 | | writer.WriteRaw(string.Format(CultureInfo.InvariantCulture, |
| | 0 | 1445 | | @"<BODY> |
| | 0 | 1446 | | <DIV id=""content""> |
| | 0 | 1447 | | <P class=""heading1"">Service</P> |
| | 0 | 1448 | | <BR/> |
| | 0 | 1449 | | <P class=""intro"">{0}</P> |
| | 0 | 1450 | | <PRE> |
| | 0 | 1451 | | <font color=""blue""><<font color=""darkred"">" + ConfigurationStrings.BehaviorsSectionName + @"</font>></font> |
| | 0 | 1452 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.ServiceBehaviors + @"</font>></font> |
| | 0 | 1453 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.Behavior + @" </font><font color=""red |
| | 0 | 1454 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.ServiceMetadataPublishingSectionNa |
| | 0 | 1455 | | <font color=""blue""> <<font color=""darkred"">/" + ConfigurationStrings.Behavior + @"</font>></font> |
| | 0 | 1456 | | <font color=""blue""> <<font color=""darkred"">/" + ConfigurationStrings.ServiceBehaviors + @"</font>></font> |
| | 0 | 1457 | | <font color=""blue""><<font color=""darkred"">/" + ConfigurationStrings.BehaviorsSectionName + @"</font>></font> |
| | 0 | 1458 | | </PRE> |
| | 0 | 1459 | | <P class=""intro"">{1}</P> |
| | 0 | 1460 | | <PRE> |
| | 0 | 1461 | | <font color=""blue""><<font color=""darkred"">" + ConfigurationStrings.Service + @" </font><font color=""red"">" + Co |
| | 0 | 1462 | | </PRE> |
| | 0 | 1463 | | <P class=""intro"">{2}</P> |
| | 0 | 1464 | | <PRE> |
| | 0 | 1465 | | <font color=""blue""><<font color=""darkred"">" + ConfigurationStrings.Endpoint + @" </font><font color=""red"">" + C |
| | 0 | 1466 | | </PRE> |
| | 0 | 1467 | | |
| | 0 | 1468 | | <P class=""intro"">{3}</P> |
| | 0 | 1469 | | <PRE> |
| | 0 | 1470 | | <font color=""blue""><<font color=""darkred"">configuration</font>></font> |
| | 0 | 1471 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.SectionGroupName + @"</font>></font> |
| | 0 | 1472 | | |
| | 0 | 1473 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.ServicesSectionName + @"</font>></f |
| | 0 | 1474 | | <font color=""blue""> <!-- <font color=""green"">{4}</font> --></font> |
| | 0 | 1475 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.Service + @" </font><font color="" |
| | 0 | 1476 | | <font color=""blue""> <!-- <font color=""green"">{5}</font> --></font> |
| | 0 | 1477 | | <font color=""blue""> <!-- <font color=""green"">{6}</font> --></font> |
| | 0 | 1478 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.Endpoint + @" </font><font col |
| | 0 | 1479 | | <font color=""blue""> <<font color=""darkred"">/" + ConfigurationStrings.Service + @"</font>></font> |
| | 0 | 1480 | | <font color=""blue""> <<font color=""darkred"">/" + ConfigurationStrings.ServicesSectionName + @"</font>></ |
| | 0 | 1481 | | |
| | 0 | 1482 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.BehaviorsSectionName + @"</font>></ |
| | 0 | 1483 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.ServiceBehaviors + @"</font>></ |
| | 0 | 1484 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.Behavior + @" </font><font col |
| | 0 | 1485 | | <font color=""blue""> <!-- <font color=""green"">{7}</font> --></font> |
| | 0 | 1486 | | <font color=""blue""> <<font color=""darkred"">" + ConfigurationStrings.ServiceMetadataPublishingS |
| | 0 | 1487 | | <font color=""blue""> <<font color=""darkred"">/" + ConfigurationStrings.Behavior + @"</font>></fon |
| | 0 | 1488 | | <font color=""blue""> <<font color=""darkred"">/" + ConfigurationStrings.ServiceBehaviors + @"</font>>< |
| | 0 | 1489 | | <font color=""blue""> <<font color=""darkred"">/" + ConfigurationStrings.BehaviorsSectionName + @"</font>>< |
| | 0 | 1490 | | |
| | 0 | 1491 | | <font color=""blue""> <<font color=""darkred"">/" + ConfigurationStrings.SectionGroupName + @"</font>></font> |
| | 0 | 1492 | | <font color=""blue""><<font color=""darkred"">/configuration</font>></font> |
| | 0 | 1493 | | </PRE> |
| | 0 | 1494 | | <P class=""intro"">{8}</P> |
| | 0 | 1495 | | </DIV> |
| | 0 | 1496 | | </BODY>", |
| | 0 | 1497 | | SR.SFxDocExt_NoMetadataSection1, SR.SFxDocExt_NoMetadataSection2, |
| | 0 | 1498 | | SR.SFxDocExt_NoMetadataSection3, SR.SFxDocExt_NoMetadataSection4, |
| | 0 | 1499 | | SR.SFxDocExt_NoMetadataConfigComment1, SR.SFxDocExt_NoMetadataConfigComment2, |
| | 0 | 1500 | | SR.SFxDocExt_NoMetadataConfigComment3, SR.SFxDocExt_NoMetadataConfigComment4, |
| | 0 | 1501 | | SR.SFxDocExt_NoMetadataSection5 )); |
| | | 1502 | | |
| | 0 | 1503 | | writer.WriteEndElement(); //HTML |
| | 0 | 1504 | | } |
| | | 1505 | | } |
| | | 1506 | | |
| | | 1507 | | internal abstract class MetadataResult |
| | | 1508 | | { |
| | | 1509 | | public async Task WriteResponseAsync(HttpResponse response) |
| | | 1510 | | { |
| | | 1511 | | // TODO : Remove use of MemoryStream and write more directly asynchronously |
| | 25 | 1512 | | using (var memoryStream = new MemoryStream()) |
| | | 1513 | | { |
| | 25 | 1514 | | var utf8NoBomEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidB |
| | 25 | 1515 | | using (var xmlTextWriter = XmlDictionaryWriter.CreateTextWriter(memoryStream, utf8NoBomEncoding, |
| | | 1516 | | { |
| | 25 | 1517 | | Write(xmlTextWriter); |
| | 25 | 1518 | | } |
| | 25 | 1519 | | memoryStream.Position = 0; |
| | 25 | 1520 | | await memoryStream.CopyToAsync(response.Body); |
| | 25 | 1521 | | } |
| | 25 | 1522 | | } |
| | | 1523 | | |
| | | 1524 | | protected abstract void Write(XmlWriter writer); |
| | | 1525 | | } |
| | | 1526 | | |
| | | 1527 | | // TODO: Cache the written response in memory and write async |
| | | 1528 | | // Need to accomodate different WriteFilter possibilities. If the first client sends request to http://inter |
| | | 1529 | | // and second client sends request to hhtp://public-hostname/service.svc?wsdl, we need to make sure the inte |
| | | 1530 | | // to an external client. |
| | | 1531 | | private abstract class MetadataFilteredResult : MetadataResult |
| | | 1532 | | { |
| | | 1533 | | private readonly WriteFilter _responseWriter; |
| | | 1534 | | |
| | 19 | 1535 | | protected MetadataFilteredResult(WriteFilter responseWriter) |
| | | 1536 | | { |
| | 19 | 1537 | | _responseWriter = responseWriter; |
| | 19 | 1538 | | } |
| | | 1539 | | |
| | | 1540 | | protected override void Write(XmlWriter xmlWriter) |
| | | 1541 | | { |
| | 19 | 1542 | | _responseWriter.Writer = xmlWriter; |
| | 19 | 1543 | | WriteCore(_responseWriter); |
| | 19 | 1544 | | } |
| | | 1545 | | |
| | | 1546 | | protected abstract void WriteCore(XmlWriter xmlWriter); |
| | | 1547 | | } |
| | | 1548 | | |
| | | 1549 | | private class ServiceDescriptionResult : MetadataFilteredResult |
| | | 1550 | | { |
| | | 1551 | | private readonly WsdlNS.ServiceDescription _description; |
| | | 1552 | | |
| | 19 | 1553 | | public ServiceDescriptionResult(WsdlNS.ServiceDescription description, WriteFilter responseWriter) : bas |
| | | 1554 | | { |
| | 19 | 1555 | | _description = description; |
| | 19 | 1556 | | } |
| | | 1557 | | |
| | 19 | 1558 | | protected override void WriteCore(XmlWriter writer) => _description.Write(writer); |
| | | 1559 | | } |
| | | 1560 | | |
| | | 1561 | | private class XmlSchemaResult : MetadataFilteredResult |
| | | 1562 | | { |
| | | 1563 | | private readonly XmlSchema _schema; |
| | | 1564 | | |
| | 0 | 1565 | | public XmlSchemaResult(XmlSchema schema, WriteFilter responseWriter) : base(responseWriter) |
| | | 1566 | | { |
| | 0 | 1567 | | _schema = schema; |
| | 0 | 1568 | | } |
| | | 1569 | | |
| | 0 | 1570 | | protected override void WriteCore(XmlWriter writer) => _schema.Write(writer); |
| | | 1571 | | } |
| | | 1572 | | #endregion //Helper Message implementations |
| | | 1573 | | } |
| | | 1574 | | |
| | | 1575 | | internal abstract class WriteFilter : XmlDictionaryWriter |
| | | 1576 | | { |
| | 25305 | 1577 | | internal XmlWriter Writer { get; set; } |
| | | 1578 | | public abstract WriteFilter CloneWriteFilter(); |
| | 0 | 1579 | | public override void Close() => Writer.Close(); |
| | 19 | 1580 | | public override void Flush() => Writer.Flush(); |
| | 1913 | 1581 | | public override string LookupPrefix(string ns) => Writer.LookupPrefix(ns); |
| | 0 | 1582 | | public override void WriteBase64(byte[] buffer, int index, int count) => Writer.WriteBase64(buffer, index, c |
| | 0 | 1583 | | public override void WriteCData(string text) => Writer.WriteCData(text); |
| | 0 | 1584 | | public override void WriteCharEntity(char ch) => Writer.WriteCharEntity(ch); |
| | 0 | 1585 | | public override void WriteChars(char[] buffer, int index, int count) => Writer.WriteChars(buffer, index, cou |
| | 0 | 1586 | | public override void WriteComment(string text) => Writer.WriteComment(text); |
| | 0 | 1587 | | public override void WriteDocType(string name, string pubid, string sysid, string subset) => Writer.WriteDoc |
| | 5166 | 1588 | | public override void WriteEndAttribute() => Writer.WriteEndAttribute(); |
| | 0 | 1589 | | public override void WriteEndDocument() => Writer.WriteEndDocument(); |
| | 3501 | 1590 | | public override void WriteEndElement() => Writer.WriteEndElement(); |
| | 0 | 1591 | | public override void WriteEntityRef(string name) => Writer.WriteEntityRef(name); |
| | 399 | 1592 | | public override void WriteFullEndElement() => Writer.WriteFullEndElement(); |
| | 0 | 1593 | | public override void WriteProcessingInstruction(string name, string text) => Writer.WriteProcessingInstructi |
| | 0 | 1594 | | public override void WriteRaw(string data) => Writer.WriteRaw(data); |
| | 0 | 1595 | | public override void WriteRaw(char[] buffer, int index, int count) => Writer.WriteRaw(buffer, index, count); |
| | 5166 | 1596 | | public override void WriteStartAttribute(string prefix, string localName, string ns) => Writer.WriteStartAtt |
| | 0 | 1597 | | public override void WriteStartDocument(bool standalone) => Writer.WriteStartDocument(standalone); |
| | 19 | 1598 | | public override void WriteStartDocument() => Writer.WriteStartDocument(); |
| | 3900 | 1599 | | public override void WriteStartElement(string prefix, string localName, string ns) => Writer.WriteStartEleme |
| | 19 | 1600 | | public override WriteState WriteState => Writer.WriteState; |
| | 5184 | 1601 | | public override void WriteString(string text) => Writer.WriteString(text); |
| | 0 | 1602 | | public override void WriteSurrogateCharEntity(char lowChar, char highChar) => Writer.WriteSurrogateCharEntit |
| | 0 | 1603 | | public override void WriteWhitespace(string ws) => Writer.WriteWhitespace(ws); |
| | | 1604 | | } |
| | | 1605 | | |
| | | 1606 | | private class LocationUpdatingWriter : WriteFilter |
| | | 1607 | | { |
| | | 1608 | | private readonly string _oldValue; |
| | | 1609 | | private readonly string _newValue; |
| | | 1610 | | |
| | | 1611 | | // passing null for newValue filters any string with oldValue as a prefix rather than replacing |
| | 22 | 1612 | | internal LocationUpdatingWriter(string oldValue, string newValue) |
| | | 1613 | | { |
| | 22 | 1614 | | _oldValue = oldValue; |
| | 22 | 1615 | | _newValue = newValue; |
| | 22 | 1616 | | } |
| | | 1617 | | |
| | | 1618 | | public override WriteFilter CloneWriteFilter() |
| | | 1619 | | { |
| | 0 | 1620 | | return new LocationUpdatingWriter(_oldValue, _newValue); |
| | | 1621 | | } |
| | | 1622 | | |
| | | 1623 | | public override void WriteString(string text) |
| | | 1624 | | { |
| | 4443 | 1625 | | if (_newValue != null) |
| | | 1626 | | { |
| | 4443 | 1627 | | text = text.Replace(_oldValue, _newValue); |
| | | 1628 | | } |
| | 0 | 1629 | | else if (text.StartsWith(_oldValue, StringComparison.Ordinal)) |
| | | 1630 | | { |
| | 0 | 1631 | | text = string.Empty; |
| | | 1632 | | } |
| | | 1633 | | |
| | 4443 | 1634 | | base.WriteString(text); |
| | 4443 | 1635 | | } |
| | | 1636 | | } |
| | | 1637 | | |
| | | 1638 | | private class CustomDynamicAddressUpdateWriter : DynamicAddressUpdateWriter |
| | | 1639 | | { |
| | | 1640 | | private readonly Uri _listenUri; |
| | | 1641 | | private readonly string _scheme; |
| | | 1642 | | private readonly string _host; |
| | | 1643 | | private readonly int _port; |
| | | 1644 | | private readonly string _newBaseAddress; |
| | | 1645 | | |
| | 1 | 1646 | | internal CustomDynamicAddressUpdateWriter(Uri listenUri, string scheme, string host, int port) |
| | | 1647 | | { |
| | 1 | 1648 | | _listenUri = listenUri; |
| | 1 | 1649 | | _scheme = scheme; |
| | 1 | 1650 | | _host = host; |
| | 1 | 1651 | | _port = port; |
| | 1 | 1652 | | _newBaseAddress = UpdateUri(listenUri).ToString(); |
| | 1 | 1653 | | } |
| | | 1654 | | |
| | 0 | 1655 | | protected override string NewBaseAddress => _newBaseAddress; |
| | | 1656 | | |
| | 494 | 1657 | | protected override bool RemoveBaseAddress => true; |
| | | 1658 | | |
| | | 1659 | | public override WriteFilter CloneWriteFilter() |
| | | 1660 | | { |
| | 0 | 1661 | | return new CustomDynamicAddressUpdateWriter(_listenUri, _scheme, _host, _port); |
| | | 1662 | | } |
| | | 1663 | | |
| | | 1664 | | protected override Uri UpdateUri(Uri uri, bool updateBaseAddressOnly = false) |
| | | 1665 | | { |
| | 105 | 1666 | | if (uri.Host != _listenUri.Host) |
| | | 1667 | | { |
| | 103 | 1668 | | return null; |
| | | 1669 | | } |
| | | 1670 | | |
| | 2 | 1671 | | UriBuilder result = new UriBuilder(uri) |
| | 2 | 1672 | | { |
| | 2 | 1673 | | Host = _host, |
| | 2 | 1674 | | Port = _port, |
| | 2 | 1675 | | Scheme = _scheme |
| | 2 | 1676 | | }; |
| | 2 | 1677 | | return result.Uri; |
| | | 1678 | | } |
| | | 1679 | | } |
| | | 1680 | | |
| | | 1681 | | private abstract class DynamicAddressUpdateWriter : WriteFilter |
| | | 1682 | | { |
| | | 1683 | | protected abstract string NewBaseAddress { get; } |
| | | 1684 | | protected abstract bool RemoveBaseAddress { get; } |
| | | 1685 | | |
| | | 1686 | | public void UpdateUri(ref Uri uri, bool updateBaseAddressOnly = false) |
| | | 1687 | | { |
| | 0 | 1688 | | Uri newUri = UpdateUri(uri, updateBaseAddressOnly); |
| | 0 | 1689 | | if (newUri != null) |
| | | 1690 | | { |
| | 0 | 1691 | | uri = newUri; |
| | | 1692 | | } |
| | 0 | 1693 | | } |
| | | 1694 | | |
| | | 1695 | | protected abstract Uri UpdateUri(Uri uri, bool updateBaseAddressOnly = false); |
| | | 1696 | | |
| | | 1697 | | public override void WriteString(string text) |
| | | 1698 | | { |
| | 741 | 1699 | | if (RemoveBaseAddress && |
| | 741 | 1700 | | text.StartsWith(BaseAddressPattern, StringComparison.Ordinal)) |
| | | 1701 | | { |
| | 0 | 1702 | | text = string.Empty; |
| | | 1703 | | } |
| | 741 | 1704 | | else if (!RemoveBaseAddress && |
| | 741 | 1705 | | text.Contains(BaseAddressPattern)) |
| | | 1706 | | { |
| | 0 | 1707 | | text = text.Replace(BaseAddressPattern, NewBaseAddress); |
| | | 1708 | | } |
| | 741 | 1709 | | else if (Uri.TryCreate(text, UriKind.Absolute, out Uri uri)) |
| | | 1710 | | { |
| | 312 | 1711 | | Uri newUri = UpdateUri(uri); |
| | 312 | 1712 | | if (newUri != null) |
| | | 1713 | | { |
| | 3 | 1714 | | text = newUri.ToString(); |
| | | 1715 | | } |
| | | 1716 | | } |
| | 741 | 1717 | | base.WriteString(text); |
| | 741 | 1718 | | } |
| | | 1719 | | } |
| | | 1720 | | |
| | | 1721 | | private class RequestHeadersDynamicAddressUpdateWriter : DynamicAddressUpdateWriter |
| | | 1722 | | { |
| | | 1723 | | private readonly string _oldHostName; |
| | | 1724 | | private readonly string _newHostName; |
| | | 1725 | | private readonly string _newBaseAddress; |
| | | 1726 | | private readonly bool _removeBaseAddress; |
| | | 1727 | | private readonly string _requestScheme; |
| | | 1728 | | private readonly int _requestPort; |
| | | 1729 | | private readonly IDictionary<string, int> _updatePortsByScheme; |
| | | 1730 | | |
| | 0 | 1731 | | protected override string NewBaseAddress => _newBaseAddress; |
| | 988 | 1732 | | protected override bool RemoveBaseAddress => _removeBaseAddress; |
| | | 1733 | | |
| | | 1734 | | internal RequestHeadersDynamicAddressUpdateWriter(Uri listenUri, string requestHost, int requestPort, |
| | | 1735 | | IDictionary<string, int> updatePortsByScheme, bool removeBaseAddress) |
| | 2 | 1736 | | : this(listenUri.Host, requestHost, removeBaseAddress, listenUri.Scheme, requestPort, updatePortsBySchem |
| | | 1737 | | { |
| | 2 | 1738 | | _newBaseAddress = UpdateUri(listenUri).ToString(); |
| | 2 | 1739 | | } |
| | | 1740 | | |
| | | 1741 | | private RequestHeadersDynamicAddressUpdateWriter(string oldHostName, string newHostName, string newBaseAddre |
| | | 1742 | | int requestPort, IDictionary<string, int> updatePortsByScheme) |
| | 0 | 1743 | | : this(oldHostName, newHostName, removeBaseAddress, requestScheme, requestPort, updatePortsByScheme) |
| | | 1744 | | { |
| | 0 | 1745 | | _newBaseAddress = newBaseAddress; |
| | 0 | 1746 | | } |
| | | 1747 | | |
| | 2 | 1748 | | private RequestHeadersDynamicAddressUpdateWriter(string oldHostName, string newHostName, bool removeBaseAddr |
| | 2 | 1749 | | int requestPort, IDictionary<string, int> updatePortsByScheme) |
| | | 1750 | | { |
| | 2 | 1751 | | _oldHostName = oldHostName; |
| | 2 | 1752 | | _newHostName = newHostName; |
| | 2 | 1753 | | _removeBaseAddress = removeBaseAddress; |
| | 2 | 1754 | | _requestScheme = requestScheme; |
| | 2 | 1755 | | _requestPort = requestPort; |
| | 2 | 1756 | | _updatePortsByScheme = updatePortsByScheme; |
| | 2 | 1757 | | } |
| | | 1758 | | |
| | | 1759 | | public override WriteFilter CloneWriteFilter() |
| | | 1760 | | { |
| | 0 | 1761 | | return new RequestHeadersDynamicAddressUpdateWriter(_oldHostName, _newHostName, _newBaseAddress, _remove |
| | 0 | 1762 | | _requestScheme, _requestPort, _updatePortsByScheme); |
| | | 1763 | | } |
| | | 1764 | | |
| | | 1765 | | protected override Uri UpdateUri(Uri uri, bool updateBaseAddressOnly = false) |
| | | 1766 | | { |
| | | 1767 | | // Ordinal comparison okay: we're filtering for auto-generated URIs which will |
| | | 1768 | | // always be based off the listenURI, so always match in case |
| | 210 | 1769 | | if (uri.Host != _oldHostName) |
| | | 1770 | | { |
| | 206 | 1771 | | return null; |
| | | 1772 | | } |
| | | 1773 | | |
| | 4 | 1774 | | UriBuilder result = new UriBuilder(uri) |
| | 4 | 1775 | | { |
| | 4 | 1776 | | Host = _newHostName |
| | 4 | 1777 | | }; |
| | | 1778 | | |
| | 4 | 1779 | | if (!updateBaseAddressOnly) |
| | | 1780 | | { |
| | | 1781 | | int port; |
| | 4 | 1782 | | if (uri.Scheme == _requestScheme) |
| | | 1783 | | { |
| | 4 | 1784 | | port = _requestPort; |
| | | 1785 | | } |
| | 0 | 1786 | | else if (!_updatePortsByScheme.TryGetValue(uri.Scheme, out port)) |
| | | 1787 | | { |
| | 0 | 1788 | | return null; |
| | | 1789 | | } |
| | 4 | 1790 | | result.Port = port; |
| | | 1791 | | } |
| | | 1792 | | |
| | 4 | 1793 | | return result.Uri; |
| | | 1794 | | } |
| | | 1795 | | } |
| | | 1796 | | } |
| | | 1797 | | } |