-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move http context to correct namespace * fix test namespaces * update code style * add roslyn and fix some hints
- Loading branch information
1 parent
2a344bc
commit 6c18506
Showing
33 changed files
with
950 additions
and
876 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,55 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
</PropertyGroup> | ||
<ItemGroup Label=".NET Dependencies"> | ||
<PackageVersion Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" /> | ||
<PackageVersion Include="NaughtyStrings" Version="2.4.1" /> | ||
</ItemGroup> | ||
<ItemGroup Label="ASP.NET Core Dependencies"> | ||
<PackageVersion Include="Microsoft.AspNetCore.Http" Version="2.2.2" /> | ||
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" /> | ||
</ItemGroup> | ||
<ItemGroup Label="Libraries for comparison"> | ||
<PackageVersion Include="UAParser" Version="3.1.47" /> | ||
<PackageVersion Include="DeviceDetector.NET" Version="4.3.0" /> | ||
<PackageVersion Include="Ng.UserAgentService" Version="1.1.0" /> | ||
</ItemGroup> | ||
<ItemGroup Label="Tests / Performance"> | ||
<PackageVersion Include="BenchmarkDotNet" Version="0.13.11" /> | ||
<PackageVersion Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.11" /> | ||
<PackageVersion Include="NSubstitute" Version="5.1.0" /> | ||
<PackageVersion Include="FluentAssertions" Version="7.0.0-alpha.2" /> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0-preview-23503-02" /> | ||
<PackageVersion Include="xunit" Version="2.6.3" /> | ||
<PackageVersion Include="xunit.runner.console" Version="2.6.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageVersion> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.5"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageVersion> | ||
<PackageVersion Include="coverlet.msbuild" Version="6.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageVersion> | ||
</ItemGroup> | ||
</Project> | ||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Label=".NET Dependencies"> | ||
<PackageVersion Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" /> | ||
<PackageVersion Include="NaughtyStrings" Version="2.4.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Label="Libraries for comparison"> | ||
<PackageVersion Include="UAParser" Version="3.1.47" /> | ||
<PackageVersion Include="DeviceDetector.NET" Version="6.3.3" /> | ||
<PackageVersion Include="Ng.UserAgentService" Version="3.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Label="Tests / Performance"> | ||
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" /> | ||
<PackageVersion Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.12" /> | ||
<PackageVersion Include="NSubstitute" Version="5.1.0" /> | ||
<PackageVersion Include="FluentAssertions" Version="7.0.0-alpha.2" /> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> | ||
<PackageVersion Include="xunit" Version="2.7.0" /> | ||
<PackageVersion Include="xunit.runner.console" Version="2.7.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageVersion> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageVersion> | ||
<PackageVersion Include="coverlet.msbuild" Version="6.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageVersion> | ||
</ItemGroup> | ||
|
||
<ItemGroup Label="Roslyn"> | ||
<PackageVersion Include="Roslynator.Analyzers" Version="4.12.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageVersion> | ||
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="4.12.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageVersion> | ||
<PackageVersion Include="Roslynator.CodeAnalysis.Analyzers" Version="4.12.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageVersion> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/MyCSharp.HttpUserAgentParser.AspNetCore/IHttpUserAgentParserAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright © myCSharp.de - all rights reserved | ||
|
||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MyCSharp.HttpUserAgentParser.AspNetCore; | ||
|
||
/// <summary> | ||
/// User Agent parser accessor | ||
/// </summary> | ||
public interface IHttpUserAgentParserAccessor | ||
{ | ||
/// <summary> | ||
/// User agent value | ||
/// </summary> | ||
string? GetHttpContextUserAgent(HttpContext httpContext); | ||
|
||
/// <summary> | ||
/// Returns current <see cref="HttpUserAgentInformation"/> | ||
/// </summary> | ||
HttpUserAgentInformation? Get(HttpContext httpContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 37 additions & 44 deletions
81
src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,58 @@ | ||
// Copyright © myCSharp.de - all rights reserved | ||
|
||
using System; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using MyCSharp.HttpUserAgentParser.Providers; | ||
|
||
namespace MyCSharp.HttpUserAgentParser.MemoryCache | ||
namespace MyCSharp.HttpUserAgentParser.MemoryCache; | ||
|
||
/// <inheritdoc/> | ||
/// <summary> | ||
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProvider"/>. | ||
/// </summary> | ||
/// <param name="options">The options used to set expiration and size limit</param> | ||
public class HttpUserAgentParserMemoryCachedProvider( | ||
HttpUserAgentParserMemoryCachedProviderOptions options) : IHttpUserAgentParserProvider | ||
{ | ||
private readonly Microsoft.Extensions.Caching.Memory.MemoryCache _memoryCache = new(options.CacheOptions); | ||
private readonly HttpUserAgentParserMemoryCachedProviderOptions _options = options; | ||
|
||
/// <inheritdoc/> | ||
public class HttpUserAgentParserMemoryCachedProvider : IHttpUserAgentParserProvider | ||
public HttpUserAgentInformation Parse(string userAgent) | ||
{ | ||
private readonly Microsoft.Extensions.Caching.Memory.MemoryCache _memoryCache; | ||
private readonly HttpUserAgentParserMemoryCachedProviderOptions _options; | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProvider"/>. | ||
/// </summary> | ||
/// <param name="options">The options used to set expiration and size limit</param> | ||
public HttpUserAgentParserMemoryCachedProvider(HttpUserAgentParserMemoryCachedProviderOptions options) | ||
{ | ||
_memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(options.CacheOptions); | ||
_options = options; | ||
} | ||
CacheKey key = this.GetKey(userAgent); | ||
|
||
/// <inheritdoc/> | ||
public HttpUserAgentInformation Parse(string userAgent) | ||
return _memoryCache.GetOrCreate(key, static entry => | ||
{ | ||
CacheKey key = this.GetKey(userAgent); | ||
|
||
return _memoryCache.GetOrCreate(key, static entry => | ||
{ | ||
CacheKey key = (entry.Key as CacheKey)!; | ||
entry.SlidingExpiration = key.Options.CacheEntryOptions.SlidingExpiration; | ||
entry.SetSize(1); | ||
CacheKey key = (entry.Key as CacheKey)!; | ||
entry.SlidingExpiration = key.Options.CacheEntryOptions.SlidingExpiration; | ||
entry.SetSize(1); | ||
return HttpUserAgentParser.Parse(key.UserAgent); | ||
}); | ||
} | ||
return HttpUserAgentParser.Parse(key.UserAgent); | ||
}); | ||
} | ||
|
||
[ThreadStatic] | ||
private static CacheKey? s_tKey; | ||
[ThreadStatic] | ||
private static CacheKey? s_tKey; | ||
|
||
private CacheKey GetKey(string userAgent) | ||
{ | ||
CacheKey key = s_tKey ??= new CacheKey(); | ||
private CacheKey GetKey(string userAgent) | ||
{ | ||
CacheKey key = s_tKey ??= new CacheKey(); | ||
|
||
key.UserAgent = userAgent; | ||
key.Options = _options; | ||
key.UserAgent = userAgent; | ||
key.Options = _options; | ||
|
||
return key; | ||
} | ||
return key; | ||
} | ||
|
||
private class CacheKey : IEquatable<CacheKey> // required for IMemoryCache | ||
{ | ||
public string UserAgent { get; set; } = null!; | ||
private class CacheKey : IEquatable<CacheKey> // required for IMemoryCache | ||
{ | ||
public string UserAgent { get; set; } = null!; | ||
|
||
public HttpUserAgentParserMemoryCachedProviderOptions Options { get; set; } = null!; | ||
public HttpUserAgentParserMemoryCachedProviderOptions Options { get; set; } = null!; | ||
|
||
public bool Equals(CacheKey? other) => this.UserAgent == other?.UserAgent; | ||
public override bool Equals(object? obj) => this.Equals(obj as CacheKey); | ||
public bool Equals(CacheKey? other) => this.UserAgent == other?.UserAgent; | ||
public override bool Equals(object? obj) => this.Equals(obj as CacheKey); | ||
|
||
public override int GetHashCode() => this.UserAgent.GetHashCode(); | ||
} | ||
public override int GetHashCode() => this.UserAgent.GetHashCode(); | ||
} | ||
} |
Oops, something went wrong.