| | | 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.Collections.ObjectModel; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | using Microsoft.AspNetCore.Http; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Description |
| | | 11 | | { |
| | | 12 | | public class UseRequestHeadersForMetadataAddressBehavior : IServiceBehavior |
| | | 13 | | { |
| | | 14 | | internal class UseHostHeaderMetadataEndpointAddressProvider : IMetadataEndpointAddressProvider |
| | | 15 | | { |
| | | 16 | | public Uri GetEndpointAddress(HttpRequest httpRequest) |
| | | 17 | | { |
| | 3 | 18 | | Uri listenUri = httpRequest.HttpContext.Items["CoreWCF.Description.ServiceMetadataExtension.HttpGetImpl. |
| | | 19 | | |
| | 3 | 20 | | string host = null; |
| | 3 | 21 | | int port = 0; |
| | | 22 | | |
| | | 23 | | // Get the host header |
| | 3 | 24 | | HostString hostString = httpRequest.Host; |
| | 3 | 25 | | if (!hostString.HasValue) |
| | | 26 | | { |
| | 0 | 27 | | return null; |
| | | 28 | | } |
| | | 29 | | |
| | 3 | 30 | | host = hostString.Host; |
| | 3 | 31 | | if (hostString.Port.HasValue) |
| | | 32 | | { |
| | 2 | 33 | | port = hostString.Port.Value; |
| | | 34 | | } |
| | | 35 | | else |
| | | 36 | | { |
| | 1 | 37 | | string hostUriString = string.Concat(listenUri.Scheme, "://", host); |
| | 1 | 38 | | if (!Uri.TryCreate(hostUriString, UriKind.Absolute, out Uri hostUri)) |
| | | 39 | | { |
| | 0 | 40 | | return null; |
| | | 41 | | } |
| | | 42 | | |
| | 1 | 43 | | port = hostUri.Port; |
| | | 44 | | } |
| | | 45 | | |
| | 3 | 46 | | return new UriBuilder(listenUri.Scheme, host, port).Uri; |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | private Dictionary<string, int> _defaultPortsByScheme; |
| | | 51 | | |
| | | 52 | | public IDictionary<string, int> DefaultPortsByScheme |
| | | 53 | | { |
| | | 54 | | get |
| | | 55 | | { |
| | 3 | 56 | | if (_defaultPortsByScheme == null) |
| | | 57 | | { |
| | 3 | 58 | | _defaultPortsByScheme = new Dictionary<string, int>(); |
| | | 59 | | } |
| | | 60 | | |
| | 3 | 61 | | return _defaultPortsByScheme; |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | 3 | 65 | | void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { } |
| | 6 | 66 | | void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBas |
| | | 67 | | |
| | | 68 | | void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, |
| | | 69 | | ServiceHostBase serviceHostBase) |
| | | 70 | | { |
| | 3 | 71 | | var mex = ServiceMetadataExtension.EnsureServiceMetadataExtension(serviceHostBase); |
| | 3 | 72 | | mex.DynamicMetadataEndpointAddressProvider = new UseHostHeaderMetadataEndpointAddressProvider(); |
| | 3 | 73 | | mex.UpdatePortsByScheme = new ReadOnlyDictionary<string, int>(DefaultPortsByScheme); |
| | 3 | 74 | | } |
| | | 75 | | } |
| | | 76 | | } |