This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathQueryFeature.cs
93 lines (80 loc) · 2.95 KB
/
QueryFeature.cs
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
// 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 Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.WebUtilities;
namespace Microsoft.AspNetCore.Http.Features
{
public class QueryFeature : IQueryFeature
{
// Lambda hoisted to static readonly field to improve inlining https://github.com/dotnet/roslyn/issues/13624
private readonly static Func<IFeatureCollection, IHttpRequestFeature> _nullRequestFeature = f => null;
private FeatureReferences<IHttpRequestFeature> _features;
private string _original;
private IQueryCollection _parsedValues;
public QueryFeature(IQueryCollection query)
{
if (query == null)
{
throw new ArgumentNullException(nameof(query));
}
_parsedValues = query;
}
public QueryFeature(IFeatureCollection features)
{
if (features == null)
{
throw new ArgumentNullException(nameof(features));
}
_features = new FeatureReferences<IHttpRequestFeature>(features);
}
private IHttpRequestFeature HttpRequestFeature =>
_features.Fetch(ref _features.Cache, _nullRequestFeature);
public IQueryCollection Query
{
get
{
if (_features.Collection == null)
{
if (_parsedValues == null)
{
_parsedValues = QueryCollection.Empty;
}
return _parsedValues;
}
var current = HttpRequestFeature.QueryString;
if (_parsedValues == null || !string.Equals(_original, current, StringComparison.Ordinal))
{
_original = current;
var result = QueryHelpers.ParseNullableQuery(current);
if (result == null)
{
_parsedValues = QueryCollection.Empty;
}
else
{
_parsedValues = new QueryCollection(result);
}
}
return _parsedValues;
}
set
{
_parsedValues = value;
if (_features.Collection != null)
{
if (value == null)
{
_original = string.Empty;
HttpRequestFeature.QueryString = string.Empty;
}
else
{
_original = QueryString.Create(_parsedValues).ToString();
HttpRequestFeature.QueryString = _original;
}
}
}
}
}
}