/// <param name="headerValue">A raw string header value.</param> /// <returns>A collection of header values with quality.</returns> public static HeaderValuesWithQualityCollection Parse(string headerValue) { var result = new HeaderValuesWithQualityCollection(); if (string.IsNullOrEmpty(headerValue)) { return(result); } if (!ValueFormat.IsMatch(headerValue)) { throw new FormatException($"Invalid header value: '{headerValue}'."); } var headerValueParts = headerValue.Split(ValueSeparator, StringSplitOptions.RemoveEmptyEntries); foreach (var valueWithQuality in headerValueParts) { var valueQuality = valueWithQuality.Split(QualitySeparator, StringSplitOptions.RemoveEmptyEntries); if (valueQuality.Length < 1 || valueQuality.Length > 2) { throw new FormatException($"Invalid header value format at '{valueWithQuality}'"); } var value = valueQuality.FirstOrDefault(); if (valueQuality.Length < 2 || !decimal.TryParse(valueQuality[1].Split(QualityValueSeparator, StringSplitOptions.RemoveEmptyEntries).LastOrDefault(), NumberStyles.Number, NumberFormatInfo.InvariantInfo, out var quality)) { quality = 1m; } result.Add(value, quality); } return(result); }
public static Request AppendToHeaderWithQuality([NotNull] this Request request, [NotNull] string name, [NotNull] string value, decimal quality = 1) { var currentValue = request.Headers?[name]; if (string.IsNullOrEmpty(currentValue)) { return(request.WithHeader(name, new HeaderValueWithQuality(value, quality))); } var valueCollection = HeaderValuesWithQualityCollection.Parse(currentValue); valueCollection.Add(value, quality); return(request.WithHeader(name, valueCollection.ToString())); }