Skip to content

Commit e659715

Browse files
Sergey KanzhelevSergey Kanzhelev
authored andcommitted
changes complete. Tests not yet
1 parent 0ff7bdd commit e659715

22 files changed

+658
-20
lines changed

Microsoft.AspNet.TelemetryCorrelation.sln

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{CE6B50B2-34A
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{258D5057-81B9-40EC-A872-D21E27452749}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.TelemetryCorrelation", "src\Microsoft.AspNet.TelemetryCorrelation\Microsoft.AspNet.TelemetryCorrelation.csproj", "{4C8E592C-C532-4CF2-80EF-3BDD0D788D12}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNet.TelemetryCorrelation", "src\Microsoft.AspNet.TelemetryCorrelation\Microsoft.AspNet.TelemetryCorrelation.csproj", "{4C8E592C-C532-4CF2-80EF-3BDD0D788D12}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.TelemetryCorrelation.Tests", "test\Microsoft.AspNet.TelemetryCorrelation.Tests\Microsoft.AspNet.TelemetryCorrelation.Tests.csproj", "{9FAE5C43-F56C-4D87-A23C-6D2D57B4ABED}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNet.TelemetryCorrelation.Tests", "test\Microsoft.AspNet.TelemetryCorrelation.Tests\Microsoft.AspNet.TelemetryCorrelation.Tests.csproj", "{9FAE5C43-F56C-4D87-A23C-6D2D57B4ABED}"
1313
EndProject
1414
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{504D7010-38CC-4B07-BC57-D7030209D631}"
1515
ProjectSection(SolutionItems) = preProject
16+
tools\Common.props = tools\Common.props
1617
README.md = README.md
1718
EndProjectSection
1819
EndProject

src/Microsoft.AspNet.TelemetryCorrelation/ActivityExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Specialized;
66
using System.ComponentModel;
77
using System.Diagnostics;
8-
using System.Net.Http.Headers;
98

109
namespace Microsoft.AspNet.TelemetryCorrelation
1110
{
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNet.TelemetryCorrelation
5+
{
6+
internal abstract class BaseHeaderParser<T> : HttpHeaderParser<T>
7+
{
8+
protected BaseHeaderParser(bool supportsMultipleValues)
9+
: base(supportsMultipleValues)
10+
{
11+
}
12+
13+
public sealed override bool TryParseValue(string value, ref int index, out T parsedValue)
14+
{
15+
parsedValue = default(T);
16+
17+
// If multiple values are supported (i.e. list of values), then accept an empty string: The header may
18+
// be added multiple times to the request/response message. E.g.
19+
// Accept: text/xml; q=1
20+
// Accept:
21+
// Accept: text/plain; q=0.2
22+
if (string.IsNullOrEmpty(value) || (index == value.Length))
23+
{
24+
return SupportsMultipleValues;
25+
}
26+
27+
var separatorFound = false;
28+
var current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, index, SupportsMultipleValues, out separatorFound);
29+
30+
if (separatorFound && !SupportsMultipleValues)
31+
{
32+
return false; // leading separators not allowed if we don't support multiple values.
33+
}
34+
35+
if (current == value.Length)
36+
{
37+
if (SupportsMultipleValues)
38+
{
39+
index = current;
40+
}
41+
42+
return SupportsMultipleValues;
43+
}
44+
45+
T result;
46+
var length = GetParsedValueLength(value, current, out result);
47+
48+
if (length == 0)
49+
{
50+
return false;
51+
}
52+
53+
current = current + length;
54+
current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, current, SupportsMultipleValues, out separatorFound);
55+
56+
// If we support multiple values and we've not reached the end of the string, then we must have a separator.
57+
if ((separatorFound && !SupportsMultipleValues) || (!separatorFound && (current < value.Length)))
58+
{
59+
return false;
60+
}
61+
62+
index = current;
63+
parsedValue = result;
64+
return true;
65+
}
66+
67+
protected abstract int GetParsedValueLength(string value, int startIndex, out T parsedValue);
68+
}
69+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.AspNet.TelemetryCorrelation
7+
{
8+
internal sealed class GenericHeaderParser<T> : BaseHeaderParser<T>
9+
{
10+
private GetParsedValueLengthDelegate getParsedValueLength;
11+
12+
internal GenericHeaderParser(bool supportsMultipleValues, GetParsedValueLengthDelegate getParsedValueLength)
13+
: base(supportsMultipleValues)
14+
{
15+
if (getParsedValueLength == null)
16+
{
17+
throw new ArgumentNullException(nameof(getParsedValueLength));
18+
}
19+
20+
this.getParsedValueLength = getParsedValueLength;
21+
}
22+
23+
internal delegate int GetParsedValueLengthDelegate(string value, int startIndex, out T parsedValue);
24+
25+
protected override int GetParsedValueLength(string value, int startIndex, out T parsedValue)
26+
{
27+
return getParsedValueLength(value, startIndex, out parsedValue);
28+
}
29+
}
30+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Diagnostics.Contracts;
5+
6+
namespace Microsoft.AspNet.TelemetryCorrelation
7+
{
8+
internal static class HeaderUtilities
9+
{
10+
internal static int GetNextNonEmptyOrWhitespaceIndex(
11+
string input,
12+
int startIndex,
13+
bool skipEmptyValues,
14+
out bool separatorFound)
15+
{
16+
Contract.Requires(input != null);
17+
Contract.Requires(startIndex <= input.Length); // it's OK if index == value.Length.
18+
19+
separatorFound = false;
20+
var current = startIndex + HttpRuleParser.GetWhitespaceLength(input, startIndex);
21+
22+
if ((current == input.Length) || (input[current] != ','))
23+
{
24+
return current;
25+
}
26+
27+
// If we have a separator, skip the separator and all following whitespaces. If we support
28+
// empty values, continue until the current character is neither a separator nor a whitespace.
29+
separatorFound = true;
30+
current++; // skip delimiter.
31+
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
32+
33+
if (skipEmptyValues)
34+
{
35+
while ((current < input.Length) && (input[current] == ','))
36+
{
37+
current++; // skip delimiter.
38+
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
39+
}
40+
}
41+
42+
return current;
43+
}
44+
}
45+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNet.TelemetryCorrelation
5+
{
6+
internal abstract class HttpHeaderParser<T>
7+
{
8+
private bool supportsMultipleValues;
9+
10+
protected HttpHeaderParser(bool supportsMultipleValues)
11+
{
12+
this.supportsMultipleValues = supportsMultipleValues;
13+
}
14+
15+
public bool SupportsMultipleValues
16+
{
17+
get { return supportsMultipleValues; }
18+
}
19+
20+
// If a parser supports multiple values, a call to ParseValue/TryParseValue should return a value for 'index'
21+
// pointing to the next non-whitespace character after a delimiter. E.g. if called with a start index of 0
22+
// for string "value , second_value", then after the call completes, 'index' must point to 's', i.e. the first
23+
// non-whitespace after the separator ','.
24+
public abstract bool TryParseValue(string value, ref int index, out T parsedValue);
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNet.TelemetryCorrelation
5+
{
6+
internal enum HttpParseResult
7+
{
8+
/// <summary>
9+
/// Parsed succesfully.
10+
/// </summary>
11+
Parsed,
12+
13+
/// <summary>
14+
/// Was not parsed.
15+
/// </summary>
16+
NotParsed,
17+
18+
/// <summary>
19+
/// Invalid format.
20+
/// </summary>
21+
InvalidFormat,
22+
}
23+
}

0 commit comments

Comments
 (0)