aboutsummaryrefslogtreecommitdiff
path: root/apps/VNLib.WebServer/src/VirtualHosts/JsonWebConfigBuilder.cs
blob: 7f3b488c609243ce836e6b2ee95da6374373b3e1 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright (c) 2024 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.WebServer
* File: JsonWebConfigBuilder.cs 
*
* JsonWebConfigBuilder.cs is part of VNLib.WebServer which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.WebServer is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* VNLib.WebServer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/.
*/

using System;
using System.IO;
using System.Net;
using System.Data;
using System.Linq;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Text.RegularExpressions;

using VNLib.Utils.Logging;
using VNLib.Utils.Extensions;

using VNLib.WebServer.Config;
using VNLib.WebServer.Config.Model;

namespace VNLib.WebServer
{   

    internal sealed partial class JsonWebConfigBuilder(VirtualHostServerConfig VhConfig, ILogProvider logger) 
        : IVirtualHostConfigBuilder
    {
        //Use pre-compiled default regex
        private static readonly Regex DefaultRootRegex = MyRegex();
       
        ///<inheritdoc/>
        public VirtualHostConfig GetBaseConfig()
        {
            //Declare the vh config
            return new()
            {
                //File root is required
                RootDir                 = new(VhConfig.DirPath!),
                LogProvider             = logger,
                ExecutionTimeout        = GetExecutionTimeout(VhConfig),
                WhiteList               = GetIpWhitelist(VhConfig),
                DownStreamServers       = GetDownStreamServers(VhConfig),
                ExcludedExtensions      = GetExlcudedExtensions(VhConfig),
                DefaultFiles            = GetDefaultFiles(VhConfig),
                PathFilter              = GetPathFilter(VhConfig),
                CacheDefault            = TimeSpan.FromSeconds(VhConfig.CacheDefaultTimeSeconds),
                AdditionalHeaders       = GetConfigHeaders(VhConfig),
                SpecialHeaders          = GetSpecialHeaders(VhConfig),
                FailureFiles            = GetFailureFiles(VhConfig),
                FilePathCacheMaxAge     = TimeSpan.MaxValue,
                Hostnames               = GetHostnames(VhConfig),
                Transports              = GetInterfaces(VhConfig),
                BlackList               = GetIpBlacklist(VhConfig),
            };
        }

        private static string[] GetHostnames(VirtualHostServerConfig conf)
        {
            Validate.EnsureNotNull(conf.Hostnames, "Hostnames array was set to null, you must define at least one hostname");

            foreach (string hostname in conf.Hostnames)
            {
                Validate.EnsureNotNull(hostname, "Hostname is null, all hostnames must be defined");
            }

            return conf.Hostnames;
        }

        private static TransportInterface[] GetInterfaces(VirtualHostServerConfig conf)
        {
            Validate.EnsureNotNull(conf.Interfaces, "Interfaces array was set to null, you must define at least one network interface");
            Validate.Assert(conf.Interfaces.Length > 0, $"You must define at least one interface for host");

            for(int i = 0; i < conf.Interfaces.Length; i++)
            {
                TransportInterface iFace = conf.Interfaces[i];

                Validate.EnsureNotNull(iFace, $"Vrtual host interface [{i}] is undefined");

                Validate.EnsureNotNull(iFace.Address, $"The interface IP address is required for interface [{i}]");
                Validate.EnsureValidIp(iFace.Address, $"The interface IP address is invalid for interface [{i}]");
                Validate.EnsureRange(iFace.Port, 1, 65535, "Interface port");
            }

            return conf.Interfaces;
        }

        private static Regex GetPathFilter(VirtualHostServerConfig conf)
        {
            //Allow site to define a regex filter pattern
            return conf.PathFilter is not null ? new(conf.PathFilter!) : DefaultRootRegex;
        }

        private FrozenDictionary<HttpStatusCode, FileCache> GetFailureFiles(VirtualHostServerConfig conf)
        {
            //if a failure file array is specified, capure all files and
            if (conf.ErrorFiles is null || conf.ErrorFiles.Length < 1)
            {
                return new Dictionary<HttpStatusCode, FileCache>().ToFrozenDictionary();
            }

            //Get the error files
            IEnumerable<KeyValuePair<HttpStatusCode, string>> ffs = conf.ErrorFiles
                        .Select(static f => new KeyValuePair<HttpStatusCode, string>((HttpStatusCode)f.Code, f.Path!));

            //Create the file cache dictionary
            (HttpStatusCode, string, FileCache?)[] loadCache = ffs.Select(static kv =>
                {
                    FileCache? cached = FileCache.Create(kv.Key, kv.Value);
                    return (kv.Key, kv.Value, cached);

                }).ToArray();

            //Only include files that exist and were loaded
            int loadedFiles = loadCache
                .Where(static loadCache => loadCache.Item3 != null)
                .Count();

            string[] notFoundFiles = loadCache
                .Where(static loadCache => loadCache.Item3 == null)
                .Select(static l => Path.GetFileName(l.Item2))
                .ToArray();

            if (notFoundFiles.Length > 0)
            {
                logger.Warn("Failed to load error files {files} for host {hosts}", notFoundFiles, conf.Hostnames);
            }

            //init frozen dictionary from valid cached files
            return loadCache
                .Where(static kv => kv.Item3 != null)
                .ToDictionary(static kv => kv.Item1, static kv => kv.Item3!)
                .ToFrozenDictionary();
        }

        private static FrozenSet<IPAddress> GetDownStreamServers(VirtualHostServerConfig conf)
        {
            //Find downstream servers
            HashSet<IPAddress>? downstreamServers = null;

            //See if element is set
            if (conf.DownstreamServers is not null)
            {
                //hash addresses, make is distinct
                downstreamServers = conf.DownstreamServers
                    .Where(static addr => !string.IsNullOrWhiteSpace(addr))
                    .Select(static addr => IPAddress.Parse(addr))
                    .Distinct()
                    .ToHashSet();
            }

            return (downstreamServers ?? []).ToFrozenSet();
        }

        private static TimeSpan GetExecutionTimeout(VirtualHostServerConfig conf)
        {
            //Get the execution timeout
            return TimeSpan.FromMilliseconds(conf.MaxExecutionTimeMs);
        }

        private static FrozenSet<IPAddress>? GetIpWhitelist(VirtualHostServerConfig conf)
        {
            if(conf.Whitelist is null)
            {
                return null;
            }

            //See if whitelist is defined, if so, get a distinct list of addresses
            return conf.Whitelist
                    .Where(static addr => !string.IsNullOrWhiteSpace(addr))
                    .Select(static addr => IPAddress.Parse(addr))
                    .Distinct()
                    .ToHashSet()
                    .ToFrozenSet();
        }

        private static FrozenSet<IPAddress>? GetIpBlacklist(VirtualHostServerConfig conf)
        {
            if (conf.Blacklist is null)
            {
                return null;
            }

            //See if whitelist is defined, if so, get a distinct list of addresses
            return conf.Blacklist
                    .Where(static addr => !string.IsNullOrWhiteSpace(addr))
                    .Select(static addr => IPAddress.Parse(addr))
                    .Distinct()
                    .ToHashSet()
                    .ToFrozenSet();
        }

        private static FrozenSet<string> GetExlcudedExtensions(VirtualHostServerConfig conf)
        {
            //Get exlucded/denied extensions from config, ignore null strings
            if (conf.DenyExtensions is not null)
            {
                return conf.DenyExtensions
                    .Where(static s => !string.IsNullOrWhiteSpace(s))
                    .Distinct()
                    .ToHashSet()
                    .ToFrozenSet(StringComparer.OrdinalIgnoreCase);
            }
            else
            {
                return new HashSet<string>().ToFrozenSet();
            }
        }

        private static IReadOnlyCollection<string> GetDefaultFiles(VirtualHostServerConfig conf)
        {
            if(conf.DefaultFiles is null)
            {
                return Array.Empty<string>();
            }

            //Get blocked extensions for the root
            return conf.DefaultFiles
                        .Where(static s => !string.IsNullOrWhiteSpace(s))
                        .Distinct()
                        .ToList();
        }

        private static KeyValuePair<string, string>[] GetConfigHeaders(VirtualHostServerConfig conf)
        {
            if (conf.Headers is null)
            {
                return [];
            }

            //Enumerate kv headers
            return conf.Headers
                    //Ignore empty keys or values                        
                    .Where(static p => !string.IsNullOrWhiteSpace(p.Key) && string.IsNullOrWhiteSpace(p.Value))
                    //Exclude special headers
                    .Where(static p => !SpecialHeaders.SpecialHeader.Contains(p.Key, StringComparer.OrdinalIgnoreCase))
                    .Select(static p => new KeyValuePair<string, string>(p.Key!, p.Value))
                    .ToArray();
        }

        private static FrozenDictionary<string, string> GetSpecialHeaders(VirtualHostServerConfig conf)
        {
            //get the headers array property
            if (conf.Headers is null)
            {
                return new Dictionary<string, string>().ToFrozenDictionary();
            }

            //Enumerate kv header
            return conf.Headers
                    //Ignore empty keys or values
                    .Where(static p => !string.IsNullOrWhiteSpace(p.Key) && !string.IsNullOrWhiteSpace(p.Value))
                    //Only include special headers
                    .Where(static p => SpecialHeaders.SpecialHeader.Contains(p.Key, StringComparer.OrdinalIgnoreCase))
                    //Create the special dictionary
                    .ToDictionary(static k => k.Key, static k => k.Value, StringComparer.OrdinalIgnoreCase)
                    .ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
        }


        [GeneratedRegex(@"(\/\.\.)|(\\\.\.)|[\[\]^*<>|`~'\n\r\t\n]|(\s$)|^(\s)", RegexOptions.Compiled)]
        private static partial Regex MyRegex();
    }
}