@@ -36,13 +36,27 @@ public static IReadOnlyDictionary<string, string> GetHeaders(this OtlpExporterOp
36
36
37
37
if ( nextEqualIndex == - 1 )
38
38
{
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 ;
40
48
}
41
49
42
50
while ( ! headersSpan . IsEmpty )
43
51
{
44
52
var key = headersSpan . Slice ( 0 , nextEqualIndex ) . Trim ( ) . ToString ( ) ;
45
53
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
+
46
60
headersSpan = headersSpan . Slice ( nextEqualIndex + 1 ) ;
47
61
48
62
nextEqualIndex = headersSpan . IndexOf ( '=' ) ;
@@ -51,7 +65,7 @@ public static IReadOnlyDictionary<string, string> GetHeaders(this OtlpExporterOp
51
65
if ( nextEqualIndex == - 1 )
52
66
{
53
67
// Everything until the end of the string can be considered the value.
54
- value = headersSpan . Trim ( ) . ToString ( ) ;
68
+ value = headersSpan . TrimEnd ( ',' ) . Trim ( ) . ToString ( ) ;
55
69
headersSpan = [ ] ;
56
70
}
57
71
else
@@ -63,12 +77,12 @@ public static IReadOnlyDictionary<string, string> GetHeaders(this OtlpExporterOp
63
77
64
78
if ( lastComma == - 1 )
65
79
{
66
- throw new ArgumentException ( "Headers provided in an invalid format." ) ;
80
+ throw CreateInvalidHeaderFormatException ( ) ;
67
81
}
68
82
69
83
potentialValue = potentialValue . Slice ( 0 , lastComma ) ;
70
84
71
- value = potentialValue . Trim ( ) . ToString ( ) ;
85
+ value = potentialValue . TrimEnd ( ',' ) . Trim ( ) . ToString ( ) ;
72
86
headersSpan = headersSpan . Slice ( lastComma + 1 ) ;
73
87
nextEqualIndex -= potentialValue . Length + 1 ;
74
88
}
@@ -85,6 +99,9 @@ public static IReadOnlyDictionary<string, string> GetHeaders(this OtlpExporterOp
85
99
return headers ;
86
100
}
87
101
102
+ private static ArgumentException CreateInvalidHeaderFormatException ( )
103
+ => new ( "Headers provided in an invalid format. Use: header=value,header=value" ) ;
104
+
88
105
public static OtlpExporterTransmissionHandler GetExportTransmissionHandler ( this OtlpExporterOptions options , ExperimentalOptions experimentalOptions , OtlpSignalType otlpSignalType )
89
106
{
90
107
var exportClient = GetExportClient ( options , otlpSignalType ) ;
0 commit comments