Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add init #1

Merged
merged 36 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f8e0d22
add init
BenjaminAbt May 15, 2021
de35be3
add bot tests
BenjaminAbt May 15, 2021
f5fd2ad
add regex map
BenjaminAbt May 15, 2021
0f313b9
Apply suggestions from code review
BenjaminAbt May 16, 2021
98cdb64
add memory cache implementation
BenjaminAbt May 16, 2021
7e597e1
add regex
BenjaminAbt May 16, 2021
398e922
remove custom
BenjaminAbt May 16, 2021
e2612ce
add factories
BenjaminAbt May 16, 2021
530468f
remove redirect
BenjaminAbt May 16, 2021
7c5146a
fix naming
BenjaminAbt May 16, 2021
1079944
add cache feedback
BenjaminAbt May 16, 2021
e9016f8
add feedback
BenjaminAbt May 16, 2021
98d6883
remove doubled ctor
BenjaminAbt May 16, 2021
50ec686
add feedback
BenjaminAbt May 16, 2021
76cb8bd
add docs
BenjaminAbt May 16, 2021
6506c43
add feedback
BenjaminAbt May 16, 2021
bfd72a4
add code cleanup
BenjaminAbt May 16, 2021
8da5e86
add docs
BenjaminAbt May 16, 2021
b46a8c8
add docs
BenjaminAbt May 16, 2021
fd79a9f
add feedback
BenjaminAbt May 16, 2021
32f99f4
add docs
BenjaminAbt May 16, 2021
d5c5760
add auto cleanup editconfig
BenjaminAbt May 16, 2021
7401c0e
add feedback
BenjaminAbt May 16, 2021
ed4f6be
add tests
BenjaminAbt May 16, 2021
b268088
add tests
BenjaminAbt May 16, 2021
59a9a5c
add tests
BenjaminAbt May 16, 2021
c412b2d
add tests
BenjaminAbt May 16, 2021
9dfc771
add tests
BenjaminAbt May 16, 2021
59700e3
add cicd
BenjaminAbt May 17, 2021
40fb27e
add feedback
BenjaminAbt May 17, 2021
eaa8a22
add nuget stuff
BenjaminAbt May 17, 2021
06e50e4
add docs
BenjaminAbt May 17, 2021
95593cb
add benchmark
BenjaminAbt May 17, 2021
e0da0a3
add benchmark
BenjaminAbt May 17, 2021
c5ab071
add benchmarks
BenjaminAbt May 17, 2021
8ab6364
fix doc
BenjaminAbt May 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 85 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,107 @@
# MyCSharp.HttpUserAgentParser

## Usage
Parsing HTTP User Agents with .NET

text here
## NuGet

### Parse
| NuGet |
|-|
| [![MyCSharp.HttpUserAgentParser](https://img.shields.io/nuget/v/MyCSharp.HttpUserAgentParser.svg?logo=nuget&label=MyCSharp.HttpUserAgentParser)](https://www.nuget.org/packages/MyCSharp.HttpUserAgentParser) |
| [![MyCSharp.HttpUserAgentParser](https://img.shields.io/nuget/v/MyCSharp.HttpUserAgentParser.MemoryCache.svg?logo=nuget&label=MyCSharp.HttpUserAgentParser.MemoryCache)](https://www.nuget.org/packages/MyCSharp.HttpUserAgentParser.MemoryCache)| `dotnet add package MyCSharp.HttpUserAgentParser.MemoryCach.MemoryCache` |
| [![MyCSharp.HttpUserAgentParser.AspNetCore](https://img.shields.io/nuget/v/MyCSharp.HttpUserAgentParser.AspNetCore.svg?logo=nuget&label=MyCSharp.HttpUserAgentParser.AspNetCore)](https://www.nuget.org/packages/MyCSharp.HttpUserAgentParser.AspNetCore) | `dotnet add package MyCSharp.HttpUserAgentParser.AspNetCore` |

text here

### Dependency Injection Configuration
## Usage

text here
```csharp
string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
HttpUserAgentInformation info = HttpUserAgentParser.Parse(userAgent); // alias HttpUserAgentInformation.Parse()
```
returns
```csharp
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
Type = HttpUserAgentType.Browser
Platform = {
Name = "Windows 10",
PlatformType = HttpUserAgentPlatformType.Windows
}
Name = "Chrome"
Version = "90.0.4430.212"
MobileDeviceType = null
```

### Caching

text here
If no cache is required but dependency injection is still desired, the default cache provider can simply be used. This registers the dummy class `HttpUserAgentParserDefaultProvider`, which does not cache at all.
BenjaminAbt marked this conversation as resolved.
Show resolved Hide resolved

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpUserAgentParser(); // uses HttpUserAgentParserDefaultProvider and does not cache
}
```

Likewise, an In Process Cache mechanism is provided, based on a `ConcurrentDictionary`.

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpUserAgentCachedParser(); // uses `HttpUserAgentParserCachedProvider`
// or
// services.AddHttpUserAgentParser<HttpUserAgentParserCachedProvider>();
}
```

This is especially recommended for tests. For web applications, the `IMemoryCache` implementation should be used, which offers a timed expiration of the entries.

The package [MyCSharp.HttpUserAgentParser.MemoryCache](https://www.nuget.org/packages/MyCSharp.HttpUserAgentParser.MemoryCache) is required to use the IMemoryCache. This enables the registration of the `IMemoryCache` implementation:


```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpUserAgentMemoryCachedParser();

// or use options

services.AddHttpUserAgentMemoryCachedParser(options =>
{
options.CacheEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(60); // default is 1 day

options.CacheOptions.SizeLimit = 1024; // default is 256
BenjaminAbt marked this conversation as resolved.
Show resolved Hide resolved
});
}
```

### ASP.NET Core

For ASP.NET Core applications, an accessor pattern (`IHttpUserAgentParserAccessor`) implementation can be registered additionally that independently retrieves the user agent based on the `HttpContextAccessor`. This requires the package [MyCSharp.HttpUserAgentParser.AspNetCore](https://www.nuget.org/packages/MyCSharp.HttpUserAgentParser.AspNetCore)

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpUserAgentParserAccessor(); // registers IHttpUserAgentParserAccessor
}
```

Now you can use

```csharp
public void MyMethod(IHttpUserAgentParserAccessor parserAccessor)
{
HttpUserAgentInformation info = parserAccessor.Get();
}
```

## Disclaimer

This library is inspired by [UserAgentService by DannyBoyNg](https://github.com/DannyBoyNg/UserAgentService) and contains optimizations for our requirements on myCSharp.de.
BenjaminAbt marked this conversation as resolved.
Show resolved Hide resolved
We decided to fork the project, because we want a general restructuring with corresponding breaking changes.

## Maintained

by [@gfoidl](https://github.com/gfoidl) and [@BenjaminAbt](https://github.com/BenjaminAbt)
BenjaminAbt marked this conversation as resolved.
Show resolved Hide resolved

## License

MIT License
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace MyCSharp.HttpUserAgentParser.AspNetCore
public interface IHttpUserAgentParserAccessor
{
string HttpContextUserAgent { get; }
HttpUserAgentInformation ParseFromHttpContext();
HttpUserAgentInformation Get();
}

public class HttpUserAgentParserAccessor : IHttpUserAgentParserAccessor
Expand All @@ -23,7 +23,7 @@ public HttpUserAgentParserAccessor(IHttpContextAccessor httpContextAccessor, IHt
public string HttpContextUserAgent =>
_httpContextAccessor?.HttpContext?.Request?.Headers["User-Agent"].ToString()!;
BenjaminAbt marked this conversation as resolved.
Show resolved Hide resolved

public HttpUserAgentInformation ParseFromHttpContext()
public HttpUserAgentInformation Get()
=> _httpUserAgentParser.Parse(HttpContextUserAgent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class HttpUserAgentParserMemoryCacheServiceCollectionExtensions
/// <summary>
/// Registers <see cref="HttpUserAgentParserCachedProvider"/> as singleton to <see cref="IHttpUserAgentParserProvider"/>
/// </summary>
public static HttpUserAgentParserDependencyInjectionOptions AddMemoryCachedHttpUserAgentParser(
public static HttpUserAgentParserDependencyInjectionOptions AddHttpUserAgentMemoryCachedParser(
this IServiceCollection services, Action<HttpUserAgentParserMemoryCachedProviderOptions>? options = null)
{
HttpUserAgentParserMemoryCachedProviderOptions providerOptions = new();
Expand Down