From 7a24045953df16e0ee69173a05078c51764ba720 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 13 Sep 2015 22:07:17 -0700 Subject: [PATCH] Split ParsingHelpers classes into their own files #397 --- .../Internal/HeaderSegment.cs | 62 ++++ .../HeaderSegmentCollection.cs} | 329 +----------------- .../Internal/ParsingHelpers.cs | 137 ++++++++ .../Internal/StringSegment.cs | 132 +++++++ 4 files changed, 332 insertions(+), 328 deletions(-) create mode 100644 src/Microsoft.AspNet.Http.Extensions/Internal/HeaderSegment.cs rename src/Microsoft.AspNet.Http.Extensions/{ParsingHelpers.cs => Internal/HeaderSegmentCollection.cs} (52%) create mode 100644 src/Microsoft.AspNet.Http.Extensions/Internal/ParsingHelpers.cs create mode 100644 src/Microsoft.AspNet.Http.Extensions/Internal/StringSegment.cs diff --git a/src/Microsoft.AspNet.Http.Extensions/Internal/HeaderSegment.cs b/src/Microsoft.AspNet.Http.Extensions/Internal/HeaderSegment.cs new file mode 100644 index 0000000000..72e94d3ef5 --- /dev/null +++ b/src/Microsoft.AspNet.Http.Extensions/Internal/HeaderSegment.cs @@ -0,0 +1,62 @@ +using System; + +namespace Microsoft.AspNet.Http.Internal +{ + internal struct HeaderSegment : IEquatable + { + private readonly StringSegment _formatting; + private readonly StringSegment _data; + + // + // Initializes a new instance of the class. + // + public HeaderSegment(StringSegment formatting, StringSegment data) + { + _formatting = formatting; + _data = data; + } + + public StringSegment Formatting + { + get { return _formatting; } + } + + public StringSegment Data + { + get { return _data; } + } + + public bool Equals(HeaderSegment other) + { + return _formatting.Equals(other._formatting) && _data.Equals(other._data); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) + { + return false; + } + + return obj is HeaderSegment && Equals((HeaderSegment)obj); + } + + public override int GetHashCode() + { + unchecked + { + return (_formatting.GetHashCode() * 397) ^ _data.GetHashCode(); + } + } + + public static bool operator ==(HeaderSegment left, HeaderSegment right) + { + return left.Equals(right); + } + + public static bool operator !=(HeaderSegment left, HeaderSegment right) + { + return !left.Equals(right); + } + } +} diff --git a/src/Microsoft.AspNet.Http.Extensions/ParsingHelpers.cs b/src/Microsoft.AspNet.Http.Extensions/Internal/HeaderSegmentCollection.cs similarity index 52% rename from src/Microsoft.AspNet.Http.Extensions/ParsingHelpers.cs rename to src/Microsoft.AspNet.Http.Extensions/Internal/HeaderSegmentCollection.cs index fcce4dd404..85e51f3782 100644 --- a/src/Microsoft.AspNet.Http.Extensions/ParsingHelpers.cs +++ b/src/Microsoft.AspNet.Http.Extensions/Internal/HeaderSegmentCollection.cs @@ -1,78 +1,10 @@ -// 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 System; using System.Collections; using System.Collections.Generic; -using System.Linq; -using Microsoft.Framework.Internal; using Microsoft.Framework.Primitives; namespace Microsoft.AspNet.Http.Internal { - internal struct HeaderSegment : IEquatable - { - private readonly StringSegment _formatting; - private readonly StringSegment _data; - - // - // Initializes a new instance of the class. - // - public HeaderSegment(StringSegment formatting, StringSegment data) - { - _formatting = formatting; - _data = data; - } - - public StringSegment Formatting - { - get { return _formatting; } - } - - public StringSegment Data - { - get { return _data; } - } - - #region Equality members - - public bool Equals(HeaderSegment other) - { - return _formatting.Equals(other._formatting) && _data.Equals(other._data); - } - - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) - { - return false; - } - - return obj is HeaderSegment && Equals((HeaderSegment)obj); - } - - public override int GetHashCode() - { - unchecked - { - return (_formatting.GetHashCode() * 397) ^ _data.GetHashCode(); - } - } - - public static bool operator ==(HeaderSegment left, HeaderSegment right) - { - return left.Equals(right); - } - - public static bool operator !=(HeaderSegment left, HeaderSegment right) - { - return !left.Equals(right); - } - - #endregion - } - - [System.CodeDom.Compiler.GeneratedCode("App_Packages", "")] internal struct HeaderSegmentCollection : IEnumerable, IEquatable { private readonly StringValues _headers; @@ -82,8 +14,6 @@ namespace Microsoft.AspNet.Http.Internal _headers = headers; } - #region Equality members - public bool Equals(HeaderSegmentCollection other) { return Equals(_headers, other._headers); @@ -114,8 +44,6 @@ namespace Microsoft.AspNet.Http.Internal return !left.Equals(right); } - #endregion - public Enumerator GetEnumerator() { return new Enumerator(_headers); @@ -360,259 +288,4 @@ namespace Microsoft.AspNet.Http.Internal } } - [System.CodeDom.Compiler.GeneratedCode("App_Packages", "")] - internal struct StringSegment : IEquatable - { - private readonly string _buffer; - private readonly int _offset; - private readonly int _count; - - // - // Initializes a new instance of the class. - // - public StringSegment(string buffer, int offset, int count) - { - _buffer = buffer; - _offset = offset; - _count = count; - } - - public string Buffer - { - get { return _buffer; } - } - - public int Offset - { - get { return _offset; } - } - - public int Count - { - get { return _count; } - } - - public string Value - { - get { return _offset == -1 ? null : _buffer.Substring(_offset, _count); } - } - - public bool HasValue - { - get { return _offset != -1 && _count != 0 && _buffer != null; } - } - - #region Equality members - - public bool Equals(StringSegment other) - { - return string.Equals(_buffer, other._buffer) && _offset == other._offset && _count == other._count; - } - - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) - { - return false; - } - - return obj is StringSegment && Equals((StringSegment)obj); - } - - public override int GetHashCode() - { - unchecked - { - int hashCode = (_buffer != null ? _buffer.GetHashCode() : 0); - hashCode = (hashCode * 397) ^ _offset; - hashCode = (hashCode * 397) ^ _count; - return hashCode; - } - } - - public static bool operator ==(StringSegment left, StringSegment right) - { - return left.Equals(right); - } - - public static bool operator !=(StringSegment left, StringSegment right) - { - return !left.Equals(right); - } - - #endregion - - public bool StartsWith([NotNull] string text, StringComparison comparisonType) - { - int textLength = text.Length; - if (!HasValue || _count < textLength) - { - return false; - } - - return string.Compare(_buffer, _offset, text, 0, textLength, comparisonType) == 0; - } - - public bool EndsWith([NotNull] string text, StringComparison comparisonType) - { - int textLength = text.Length; - if (!HasValue || _count < textLength) - { - return false; - } - - return string.Compare(_buffer, _offset + _count - textLength, text, 0, textLength, comparisonType) == 0; - } - - public bool Equals([NotNull] string text, StringComparison comparisonType) - { - int textLength = text.Length; - if (!HasValue || _count != textLength) - { - return false; - } - - return string.Compare(_buffer, _offset, text, 0, textLength, comparisonType) == 0; - } - - public string Substring(int offset, int length) - { - return _buffer.Substring(_offset + offset, length); - } - - public StringSegment Subsegment(int offset, int length) - { - return new StringSegment(_buffer, _offset + offset, length); - } - - public override string ToString() - { - return Value ?? string.Empty; - } - } - - internal static class ParsingHelpers - { - public static StringValues GetHeader(IDictionary headers, string key) - { - StringValues value; - return headers.TryGetValue(key, out value) ? value : StringValues.Empty; - } - - public static StringValues GetHeaderSplit(IDictionary headers, string key) - { - var values = GetHeaderUnmodified(headers, key); - return new StringValues(GetHeaderSplitImplementation(values).ToArray()); - } - - private static IEnumerable GetHeaderSplitImplementation(StringValues values) - { - foreach (var segment in new HeaderSegmentCollection(values)) - { - if (segment.Data.HasValue) - { - yield return DeQuote(segment.Data.Value); - } - } - } - - public static StringValues GetHeaderUnmodified([NotNull] IDictionary headers, string key) - { - StringValues values; - return headers.TryGetValue(key, out values) ? values : StringValues.Empty; - } - - public static void SetHeaderJoined([NotNull] IDictionary headers, [NotNull] string key, StringValues value) - { - if (string.IsNullOrWhiteSpace(key)) - { - throw new ArgumentNullException(nameof(key)); - } - if (StringValues.IsNullOrEmpty(value)) - { - headers.Remove(key); - } - else - { - headers[key] = string.Join(",", value.Select(QuoteIfNeeded)); - } - } - - // Quote items that contain comas and are not already quoted. - private static string QuoteIfNeeded(string value) - { - if (string.IsNullOrWhiteSpace(value)) - { - // Ignore - } - else if (value.Contains(',')) - { - if (value[0] != '"' || value[value.Length - 1] != '"') - { - value = '"' + value + '"'; - } - } - - return value; - } - - private static string DeQuote(string value) - { - if (string.IsNullOrWhiteSpace(value)) - { - // Ignore - } - else if (value.Length > 1 && value[0] == '"' && value[value.Length - 1] == '"') - { - value = value.Substring(1, value.Length - 2); - } - - return value; - } - - public static void SetHeaderUnmodified([NotNull] IDictionary headers, [NotNull] string key, StringValues? values) - { - if (string.IsNullOrWhiteSpace(key)) - { - throw new ArgumentNullException(nameof(key)); - } - if (!values.HasValue || StringValues.IsNullOrEmpty(values.Value)) - { - headers.Remove(key); - } - else - { - headers[key] = values.Value; - } - } - - public static void AppendHeaderJoined([NotNull] IDictionary headers, [NotNull] string key, params string[] values) - { - if (values == null || values.Length == 0) - { - return; - } - - string existing = GetHeader(headers, key); - if (existing == null) - { - SetHeaderJoined(headers, key, values); - } - else - { - headers[key] = existing + "," + string.Join(",", values.Select(value => QuoteIfNeeded(value))); - } - } - - public static void AppendHeaderUnmodified([NotNull] IDictionary headers, [NotNull] string key, StringValues values) - { - if (values.Count == 0) - { - return; - } - - var existing = GetHeaderUnmodified(headers, key); - SetHeaderUnmodified(headers, key, StringValues.Concat(existing, values)); - } - } } diff --git a/src/Microsoft.AspNet.Http.Extensions/Internal/ParsingHelpers.cs b/src/Microsoft.AspNet.Http.Extensions/Internal/ParsingHelpers.cs new file mode 100644 index 0000000000..0959adbca0 --- /dev/null +++ b/src/Microsoft.AspNet.Http.Extensions/Internal/ParsingHelpers.cs @@ -0,0 +1,137 @@ +// 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 System.Collections; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Framework.Internal; +using Microsoft.Framework.Primitives; + +namespace Microsoft.AspNet.Http.Internal +{ + internal static class ParsingHelpers + { + public static StringValues GetHeader(IDictionary headers, string key) + { + StringValues value; + return headers.TryGetValue(key, out value) ? value : StringValues.Empty; + } + + public static StringValues GetHeaderSplit(IDictionary headers, string key) + { + var values = GetHeaderUnmodified(headers, key); + return new StringValues(GetHeaderSplitImplementation(values).ToArray()); + } + + private static IEnumerable GetHeaderSplitImplementation(StringValues values) + { + foreach (var segment in new HeaderSegmentCollection(values)) + { + if (segment.Data.HasValue) + { + yield return DeQuote(segment.Data.Value); + } + } + } + + public static StringValues GetHeaderUnmodified([NotNull] IDictionary headers, string key) + { + StringValues values; + return headers.TryGetValue(key, out values) ? values : StringValues.Empty; + } + + public static void SetHeaderJoined([NotNull] IDictionary headers, [NotNull] string key, StringValues value) + { + if (string.IsNullOrWhiteSpace(key)) + { + throw new ArgumentNullException(nameof(key)); + } + if (StringValues.IsNullOrEmpty(value)) + { + headers.Remove(key); + } + else + { + headers[key] = string.Join(",", value.Select(QuoteIfNeeded)); + } + } + + // Quote items that contain comas and are not already quoted. + private static string QuoteIfNeeded(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + // Ignore + } + else if (value.Contains(',')) + { + if (value[0] != '"' || value[value.Length - 1] != '"') + { + value = '"' + value + '"'; + } + } + + return value; + } + + private static string DeQuote(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + // Ignore + } + else if (value.Length > 1 && value[0] == '"' && value[value.Length - 1] == '"') + { + value = value.Substring(1, value.Length - 2); + } + + return value; + } + + public static void SetHeaderUnmodified([NotNull] IDictionary headers, [NotNull] string key, StringValues? values) + { + if (string.IsNullOrWhiteSpace(key)) + { + throw new ArgumentNullException(nameof(key)); + } + if (!values.HasValue || StringValues.IsNullOrEmpty(values.Value)) + { + headers.Remove(key); + } + else + { + headers[key] = values.Value; + } + } + + public static void AppendHeaderJoined([NotNull] IDictionary headers, [NotNull] string key, params string[] values) + { + if (values == null || values.Length == 0) + { + return; + } + + string existing = GetHeader(headers, key); + if (existing == null) + { + SetHeaderJoined(headers, key, values); + } + else + { + headers[key] = existing + "," + string.Join(",", values.Select(value => QuoteIfNeeded(value))); + } + } + + public static void AppendHeaderUnmodified([NotNull] IDictionary headers, [NotNull] string key, StringValues values) + { + if (values.Count == 0) + { + return; + } + + var existing = GetHeaderUnmodified(headers, key); + SetHeaderUnmodified(headers, key, StringValues.Concat(existing, values)); + } + } +} diff --git a/src/Microsoft.AspNet.Http.Extensions/Internal/StringSegment.cs b/src/Microsoft.AspNet.Http.Extensions/Internal/StringSegment.cs new file mode 100644 index 0000000000..2b7fa93020 --- /dev/null +++ b/src/Microsoft.AspNet.Http.Extensions/Internal/StringSegment.cs @@ -0,0 +1,132 @@ +using System; +using Microsoft.Framework.Internal; + +namespace Microsoft.AspNet.Http.Internal +{ + internal struct StringSegment : IEquatable + { + private readonly string _buffer; + private readonly int _offset; + private readonly int _count; + + // + // Initializes a new instance of the class. + // + public StringSegment(string buffer, int offset, int count) + { + _buffer = buffer; + _offset = offset; + _count = count; + } + + public string Buffer + { + get { return _buffer; } + } + + public int Offset + { + get { return _offset; } + } + + public int Count + { + get { return _count; } + } + + public string Value + { + get { return _offset == -1 ? null : _buffer.Substring(_offset, _count); } + } + + public bool HasValue + { + get { return _offset != -1 && _count != 0 && _buffer != null; } + } + + public bool Equals(StringSegment other) + { + return string.Equals(_buffer, other._buffer) && _offset == other._offset && _count == other._count; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) + { + return false; + } + + return obj is StringSegment && Equals((StringSegment)obj); + } + + public override int GetHashCode() + { + unchecked + { + int hashCode = (_buffer != null ? _buffer.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ _offset; + hashCode = (hashCode * 397) ^ _count; + return hashCode; + } + } + + public static bool operator ==(StringSegment left, StringSegment right) + { + return left.Equals(right); + } + + public static bool operator !=(StringSegment left, StringSegment right) + { + return !left.Equals(right); + } + + public bool StartsWith([NotNull] string text, StringComparison comparisonType) + { + int textLength = text.Length; + if (!HasValue || _count < textLength) + { + return false; + } + + return string.Compare(_buffer, _offset, text, 0, textLength, comparisonType) == 0; + } + + public bool EndsWith([NotNull] string text, StringComparison comparisonType) + { + int textLength = text.Length; + if (!HasValue || _count < textLength) + { + return false; + } + + return string.Compare(_buffer, _offset + _count - textLength, text, 0, textLength, comparisonType) == 0; + } + + public bool Equals([NotNull] string text, StringComparison comparisonType) + { + int textLength = text.Length; + if (!HasValue || _count != textLength) + { + return false; + } + + return string.Compare(_buffer, _offset, text, 0, textLength, comparisonType) == 0; + } + + public string Substring(int offset, int length) + { + return _buffer.Substring(_offset + offset, length); + } + + public StringSegment Subsegment(int offset, int length) + { + return new StringSegment(_buffer, _offset + offset, length); + } + + public override string ToString() + { + return Value ?? string.Empty; + } + } + +}