// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; namespace Microsoft.HttpRepl { public class DirectoryStructure : IDirectoryStructure { private readonly Dictionary _childDirectories = new Dictionary(StringComparer.OrdinalIgnoreCase); public DirectoryStructure(IDirectoryStructure parent) { Parent = parent; } public IEnumerable DirectoryNames => _childDirectories.Keys; public IDirectoryStructure Parent { get; } public DirectoryStructure DeclareDirectory(string name) { if (_childDirectories.TryGetValue(name, out DirectoryStructure existing)) { return existing; } return _childDirectories[name] = new DirectoryStructure(this); } public IDirectoryStructure GetChildDirectory(string name) { if (_childDirectories.TryGetValue(name, out DirectoryStructure result)) { return result; } IDirectoryStructure parameterizedTarget = _childDirectories.FirstOrDefault(x => x.Key.StartsWith('{') && x.Key.EndsWith('}')).Value; if (!(parameterizedTarget is null)) { return parameterizedTarget; } return new DirectoryStructure(this); } public IRequestInfo RequestInfo { get; set; } } public class RequestInfo : IRequestInfo { private readonly HashSet _methods = new HashSet(StringComparer.OrdinalIgnoreCase); private readonly Dictionary> _requestBodiesByMethodByContentType = new Dictionary>(StringComparer.OrdinalIgnoreCase); private readonly Dictionary _fallbackBodyStringsByMethod = new Dictionary(StringComparer.OrdinalIgnoreCase); private readonly Dictionary> _contentTypesByMethod = new Dictionary>(StringComparer.OrdinalIgnoreCase); public IReadOnlyList Methods => _methods.ToList(); public IReadOnlyDictionary> ContentTypesByMethod => _contentTypesByMethod; public string GetRequestBodyForContentType(string contentType, string method) { if (_requestBodiesByMethodByContentType.TryGetValue(method, out Dictionary bodiesByContentType) && bodiesByContentType.TryGetValue(contentType, out string body)) { return body; } if (_fallbackBodyStringsByMethod.TryGetValue(method, out body)) { return body; } return null; } public void SetRequestBody(string method, string contentType, string body) { if (!_requestBodiesByMethodByContentType.TryGetValue(method, out Dictionary bodiesByContentType)) { _requestBodiesByMethodByContentType[method] = bodiesByContentType = new Dictionary(StringComparer.OrdinalIgnoreCase); } if (!_contentTypesByMethod.TryGetValue(method, out IReadOnlyList contentTypesRaw)) { _contentTypesByMethod[method] = contentTypesRaw = new List(); } List contentTypes = (List)contentTypesRaw; contentTypes.Add(contentType); bodiesByContentType[contentType] = body; } public void AddMethod(string method) { _methods.Add(method); } public void SetFallbackRequestBody(string method, string fallbackBodyString) { _fallbackBodyStringsByMethod[method] = fallbackBodyString; } } }