Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AddressBinder.cs « Internal « src « Core « Kestrel « Servers « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 448f8458218b9e7cf311d6ce8c6b5f4e4cb9d448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal;

internal sealed class AddressBinder
{
    public static async Task BindAsync(IEnumerable<ListenOptions> listenOptions, AddressBindContext context, CancellationToken cancellationToken)
    {
        var strategy = CreateStrategy(
            listenOptions.ToArray(),
            context.Addresses.ToArray(),
            context.ServerAddressesFeature.PreferHostingUrls);

        // reset options. The actual used options and addresses will be populated
        // by the address binding feature
        context.ServerOptions.OptionsInUse.Clear();
        context.Addresses.Clear();

        await strategy.BindAsync(context, cancellationToken).ConfigureAwait(false);
    }

    private static IStrategy CreateStrategy(ListenOptions[] listenOptions, string[] addresses, bool preferAddresses)
    {
        var hasListenOptions = listenOptions.Length > 0;
        var hasAddresses = addresses.Length > 0;

        if (preferAddresses && hasAddresses)
        {
            if (hasListenOptions)
            {
                return new OverrideWithAddressesStrategy(addresses);
            }

            return new AddressesStrategy(addresses);
        }
        else if (hasListenOptions)
        {
            if (hasAddresses)
            {
                return new OverrideWithEndpointsStrategy(listenOptions, addresses);
            }

            return new EndpointsStrategy(listenOptions);
        }
        else if (hasAddresses)
        {
            // If no endpoints are configured directly using KestrelServerOptions, use those configured via the IServerAddressesFeature.
            return new AddressesStrategy(addresses);
        }
        else
        {
            // "localhost" for both IPv4 and IPv6 can't be represented as an IPEndPoint.
            return new DefaultAddressStrategy();
        }
    }

    /// <summary>
    /// Returns an <see cref="IPEndPoint"/> for the given host an port.
    /// If the host parameter isn't "localhost" or an IP address, use IPAddress.Any.
    /// </summary>
    internal static bool TryCreateIPEndPoint(BindingAddress address, [NotNullWhen(true)] out IPEndPoint? endpoint)
    {
        if (!IPAddress.TryParse(address.Host, out var ip))
        {
            endpoint = null;
            return false;
        }

        endpoint = new IPEndPoint(ip, address.Port);
        return true;
    }

    internal static async Task BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
    {
        try
        {
            await context.CreateBinding(endpoint, cancellationToken).ConfigureAwait(false);
        }
        catch (AddressInUseException ex)
        {
            throw new IOException(CoreStrings.FormatEndpointAlreadyInUse(endpoint), ex);
        }

        context.ServerOptions.OptionsInUse.Add(endpoint);
    }

    internal static ListenOptions ParseAddress(string address, out bool https)
    {
        var parsedAddress = BindingAddress.Parse(address);
        https = false;

        if (parsedAddress.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
        {
            https = true;
        }
        else if (!parsedAddress.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException(CoreStrings.FormatUnsupportedAddressScheme(address));
        }

        if (!string.IsNullOrEmpty(parsedAddress.PathBase))
        {
            throw new InvalidOperationException(CoreStrings.FormatConfigurePathBaseFromMethodCall($"{nameof(IApplicationBuilder)}.UsePathBase()"));
        }

        ListenOptions? options = null;
        if (parsedAddress.IsUnixPipe)
        {
            options = new ListenOptions(parsedAddress.UnixPipePath);
        }
        else if (parsedAddress.IsNamedPipe)
        {
            options = new ListenOptions(new NamedPipeEndPoint(parsedAddress.NamedPipePath));
        }
        else if (string.Equals(parsedAddress.Host, "localhost", StringComparison.OrdinalIgnoreCase))
        {
            // "localhost" for both IPv4 and IPv6 can't be represented as an IPEndPoint.
            options = new LocalhostListenOptions(parsedAddress.Port);
        }
        else if (TryCreateIPEndPoint(parsedAddress, out var endpoint))
        {
            options = new ListenOptions(endpoint);
        }
        else
        {
            // when address is 'http://hostname:port', 'http://*:port', or 'http://+:port'
            options = new AnyIPListenOptions(parsedAddress.Port);
        }

        return options;
    }

    private interface IStrategy
    {
        Task BindAsync(AddressBindContext context, CancellationToken cancellationToken);
    }

    private sealed class DefaultAddressStrategy : IStrategy
    {
        public async Task BindAsync(AddressBindContext context, CancellationToken cancellationToken)
        {
            var httpDefault = ParseAddress(Constants.DefaultServerAddress, out _);
            context.ServerOptions.ApplyEndpointDefaults(httpDefault);
            await httpDefault.BindAsync(context, cancellationToken).ConfigureAwait(false);

            context.Logger.LogDebug(CoreStrings.BindingToDefaultAddress, Constants.DefaultServerAddress);
        }
    }

    private sealed class OverrideWithAddressesStrategy : AddressesStrategy
    {
        public OverrideWithAddressesStrategy(IReadOnlyCollection<string> addresses)
            : base(addresses)
        {
        }

        public override Task BindAsync(AddressBindContext context, CancellationToken cancellationToken)
        {
            var joined = string.Join(", ", _addresses);
            context.Logger.LogInformation(CoreStrings.OverridingWithPreferHostingUrls, nameof(IServerAddressesFeature.PreferHostingUrls), joined);

            return base.BindAsync(context, cancellationToken);
        }
    }

    private sealed class OverrideWithEndpointsStrategy : EndpointsStrategy
    {
        private readonly string[] _originalAddresses;

        public OverrideWithEndpointsStrategy(IReadOnlyCollection<ListenOptions> endpoints, string[] originalAddresses)
            : base(endpoints)
        {
            _originalAddresses = originalAddresses;
        }

        public override Task BindAsync(AddressBindContext context, CancellationToken cancellationToken)
        {
            var joined = string.Join(", ", _originalAddresses);
            context.Logger.LogWarning(CoreStrings.OverridingWithKestrelOptions, joined);

            return base.BindAsync(context, cancellationToken);
        }
    }

    private class EndpointsStrategy : IStrategy
    {
        private readonly IReadOnlyCollection<ListenOptions> _endpoints;

        public EndpointsStrategy(IReadOnlyCollection<ListenOptions> endpoints)
        {
            _endpoints = endpoints;
        }

        public virtual async Task BindAsync(AddressBindContext context, CancellationToken cancellationToken)
        {
            foreach (var endpoint in _endpoints)
            {
                await endpoint.BindAsync(context, cancellationToken).ConfigureAwait(false);
            }
        }
    }

    private class AddressesStrategy : IStrategy
    {
        protected readonly IReadOnlyCollection<string> _addresses;

        public AddressesStrategy(IReadOnlyCollection<string> addresses)
        {
            _addresses = addresses;
        }

        public virtual async Task BindAsync(AddressBindContext context, CancellationToken cancellationToken)
        {
            foreach (var address in _addresses)
            {
                var options = ParseAddress(address, out var https);
                context.ServerOptions.ApplyEndpointDefaults(options);

                if (https && !options.IsTls)
                {
                    options.UseHttps();
                }

                await options.BindAsync(context, cancellationToken).ConfigureAwait(false);
            }
        }
    }
}