Skip to content

Commit 226edef

Browse files
Fix other tests
1 parent 4e1b471 commit 226edef

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,27 @@ public static IReadOnlyDictionary<string, string> GetHeaders(this OtlpExporterOp
3636

3737
if (nextEqualIndex == -1)
3838
{
39-
throw new ArgumentException("Headers provided in an invalid format.");
39+
throw CreateInvalidHeaderFormatException();
40+
}
41+
42+
// Skip any leading commas.
43+
var leadingCommaIndex = headersSpan.Slice(0, nextEqualIndex).LastIndexOf(',');
44+
if (leadingCommaIndex != -1)
45+
{
46+
headersSpan = headersSpan.Slice(leadingCommaIndex + 1);
47+
nextEqualIndex -= leadingCommaIndex + 1;
4048
}
4149

4250
while (!headersSpan.IsEmpty)
4351
{
4452
var key = headersSpan.Slice(0, nextEqualIndex).Trim().ToString();
4553

54+
// HTTP header field-names can not be empty: https://www.rfc-editor.org/rfc/rfc7230#section-3.2
55+
if (key.Length < 1)
56+
{
57+
throw CreateInvalidHeaderFormatException();
58+
}
59+
4660
headersSpan = headersSpan.Slice(nextEqualIndex + 1);
4761

4862
nextEqualIndex = headersSpan.IndexOf('=');
@@ -51,7 +65,7 @@ public static IReadOnlyDictionary<string, string> GetHeaders(this OtlpExporterOp
5165
if (nextEqualIndex == -1)
5266
{
5367
// Everything until the end of the string can be considered the value.
54-
value = headersSpan.Trim().ToString();
68+
value = headersSpan.TrimEnd(',').Trim().ToString();
5569
headersSpan = [];
5670
}
5771
else
@@ -63,12 +77,12 @@ public static IReadOnlyDictionary<string, string> GetHeaders(this OtlpExporterOp
6377

6478
if (lastComma == -1)
6579
{
66-
throw new ArgumentException("Headers provided in an invalid format.");
80+
throw CreateInvalidHeaderFormatException();
6781
}
6882

6983
potentialValue = potentialValue.Slice(0, lastComma);
7084

71-
value = potentialValue.Trim().ToString();
85+
value = potentialValue.TrimEnd(',').Trim().ToString();
7286
headersSpan = headersSpan.Slice(lastComma + 1);
7387
nextEqualIndex -= potentialValue.Length + 1;
7488
}
@@ -85,6 +99,9 @@ public static IReadOnlyDictionary<string, string> GetHeaders(this OtlpExporterOp
8599
return headers;
86100
}
87101

102+
private static ArgumentException CreateInvalidHeaderFormatException()
103+
=> new("Headers provided in an invalid format. Use: header=value,header=value");
104+
88105
public static OtlpExporterTransmissionHandler GetExportTransmissionHandler(this OtlpExporterOptions options, ExperimentalOptions experimentalOptions, OtlpSignalType otlpSignalType)
89106
{
90107
var exportClient = GetExportClient(options, otlpSignalType);

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsExtensionsTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public void GetHeaders_NoOptionHeaders_ReturnsStandardHeaders(string? optionHead
3636

3737
[Theory]
3838
[InlineData(" ")]
39-
[InlineData(",key1=value1,key2=value2,")]
40-
[InlineData(",,key1=value1,,key2=value2,,")]
39+
[InlineData(",")]
40+
[InlineData("=value1")]
4141
[InlineData("key1")]
4242
public void GetHeaders_InvalidOptionHeaders_ThrowsArgumentException(string inputOptionHeaders)
4343
{
@@ -53,12 +53,13 @@ public void GetHeaders_InvalidOptionHeaders_ThrowsArgumentException(string input
5353
[InlineData("key1=value1,value2,key2=value3", "key1=value1,value2,key2=value3")]
5454
[InlineData(" key1 = value1 , key2=value2 ", "key1=value1,key2=value2")]
5555
[InlineData("key1= value with spaces ,key2=another value", "key1=value with spaces,key2=another value")]
56-
[InlineData("=value1", "=value1")]
5756
[InlineData("key1=", "key1=")]
5857
[InlineData("key1=value1%2Ckey2=value2", "key1=value1,key2=value2")]
5958
[InlineData("key1=value1%2Ckey2=value2%2Ckey3=value3", "key1=value1,key2=value2,key3=value3")]
6059
[InlineData("key1=value1%2Cvalue2", "key1=value1,value2")]
6160
[InlineData("key1=value1%2Cvalue2%2Ckey2=value3", "key1=value1,value2,key2=value3")]
61+
[InlineData(",key1=value1,key2=value2,", "key1=value1,key2=value2")]
62+
[InlineData(",,key1=value1,,key2=value2,,", "key1=value1,key2=value2")]
6263
public void GetHeaders_ValidAndUrlEncodedHeaders_ReturnsCorrectHeaders(string inputOptionHeaders, string expectedNormalizedOptional)
6364
{
6465
VerifyHeaders(inputOptionHeaders, expectedNormalizedOptional);

0 commit comments

Comments
 (0)