diff --git a/src/Microsoft.AspNetCore.Http.Abstractions/CookieBuilder.cs b/src/Microsoft.AspNetCore.Http.Abstractions/CookieBuilder.cs index ce89e5b054..5c0db2a46f 100644 --- a/src/Microsoft.AspNetCore.Http.Abstractions/CookieBuilder.cs +++ b/src/Microsoft.AspNetCore.Http.Abstractions/CookieBuilder.cs @@ -107,7 +107,7 @@ namespace Microsoft.AspNetCore.Http Domain = Domain, IsEssential = IsEssential, Secure = SecurePolicy == CookieSecurePolicy.Always || (SecurePolicy == CookieSecurePolicy.SameAsRequest && context.Request.IsHttps), - Expires = Expiration.HasValue ? expiresFrom.Add(Expiration.Value) : default(DateTimeOffset?) + Expires = Expiration.HasValue ? expiresFrom.Add(Expiration.GetValueOrDefault()) : default(DateTimeOffset?) }; } } diff --git a/src/Microsoft.AspNetCore.Http.Abstractions/Internal/ParsingHelpers.cs b/src/Microsoft.AspNetCore.Http.Abstractions/Internal/ParsingHelpers.cs index 185fc40ac7..971447fa44 100644 --- a/src/Microsoft.AspNetCore.Http.Abstractions/Internal/ParsingHelpers.cs +++ b/src/Microsoft.AspNetCore.Http.Abstractions/Internal/ParsingHelpers.cs @@ -103,13 +103,13 @@ namespace Microsoft.AspNetCore.Http.Internal { throw new ArgumentNullException(nameof(key)); } - if (!values.HasValue || StringValues.IsNullOrEmpty(values.Value)) + if (!values.HasValue || StringValues.IsNullOrEmpty(values.GetValueOrDefault())) { headers.Remove(key); } else { - headers[key] = values.Value; + headers[key] = values.GetValueOrDefault(); } } diff --git a/src/Microsoft.AspNetCore.Http.Extensions/HeaderDictionaryTypeExtensions.cs b/src/Microsoft.AspNetCore.Http.Extensions/HeaderDictionaryTypeExtensions.cs index 1723ee6fd5..70f1c4da71 100644 --- a/src/Microsoft.AspNetCore.Http.Extensions/HeaderDictionaryTypeExtensions.cs +++ b/src/Microsoft.AspNetCore.Http.Extensions/HeaderDictionaryTypeExtensions.cs @@ -138,7 +138,7 @@ namespace Microsoft.AspNetCore.Http if (value.HasValue) { - headers[name] = HeaderUtilities.FormatDate(value.Value); + headers[name] = HeaderUtilities.FormatDate(value.GetValueOrDefault()); } else { diff --git a/src/Microsoft.AspNetCore.Http.Extensions/SendFileResponseExtensions.cs b/src/Microsoft.AspNetCore.Http.Extensions/SendFileResponseExtensions.cs index 74c0422ef4..90282bd2df 100644 --- a/src/Microsoft.AspNetCore.Http.Extensions/SendFileResponseExtensions.cs +++ b/src/Microsoft.AspNetCore.Http.Extensions/SendFileResponseExtensions.cs @@ -172,7 +172,7 @@ namespace Microsoft.AspNetCore.Http throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty); } if (count.HasValue && - (count.Value < 0 || count.Value > fileLength - offset)) + (count.GetValueOrDefault() < 0 || count.GetValueOrDefault() > fileLength - offset)) { throw new ArgumentOutOfRangeException(nameof(count), count, string.Empty); } diff --git a/src/Microsoft.AspNetCore.Http.Extensions/StreamCopyOperation.cs b/src/Microsoft.AspNetCore.Http.Extensions/StreamCopyOperation.cs index 12067fef65..e0a301f4eb 100644 --- a/src/Microsoft.AspNetCore.Http.Extensions/StreamCopyOperation.cs +++ b/src/Microsoft.AspNetCore.Http.Extensions/StreamCopyOperation.cs @@ -42,13 +42,13 @@ namespace Microsoft.AspNetCore.Http.Extensions { Debug.Assert(source != null); Debug.Assert(destination != null); - Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.Value >= 0); + Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.GetValueOrDefault() >= 0); Debug.Assert(buffer != null); while (true) { // The natural end of the range. - if (bytesRemaining.HasValue && bytesRemaining.Value <= 0) + if (bytesRemaining.HasValue && bytesRemaining.GetValueOrDefault() <= 0) { return; } @@ -58,7 +58,7 @@ namespace Microsoft.AspNetCore.Http.Extensions int readLength = buffer.Length; if (bytesRemaining.HasValue) { - readLength = (int)Math.Min(bytesRemaining.Value, (long)readLength); + readLength = (int)Math.Min(bytesRemaining.GetValueOrDefault(), (long)readLength); } int read = await source.ReadAsync(buffer, 0, readLength, cancel); diff --git a/src/Microsoft.AspNetCore.Http/Features/FormFeature.cs b/src/Microsoft.AspNetCore.Http/Features/FormFeature.cs index 865e183f76..d93234d08b 100644 --- a/src/Microsoft.AspNetCore.Http/Features/FormFeature.cs +++ b/src/Microsoft.AspNetCore.Http/Features/FormFeature.cs @@ -199,7 +199,7 @@ namespace Microsoft.AspNetCore.Http.Features if (section.BaseStreamOffset.HasValue) { // Relative reference to buffered request body - file = new FormFile(_request.Body, section.BaseStreamOffset.Value, section.Body.Length, name, fileName); + file = new FormFile(_request.Body, section.BaseStreamOffset.GetValueOrDefault(), section.Body.Length, name, fileName); } else { diff --git a/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs b/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs index bc0b7a26ce..2113204823 100644 --- a/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs +++ b/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs @@ -121,7 +121,7 @@ namespace Microsoft.AspNetCore.Http ThrowIfReadOnly(); if (value.HasValue) { - this[HeaderNames.ContentLength] = HeaderUtilities.FormatNonNegativeInt64(value.Value); + this[HeaderNames.ContentLength] = HeaderUtilities.FormatNonNegativeInt64(value.GetValueOrDefault()); } else { diff --git a/src/Microsoft.AspNetCore.Owin/DictionaryStringValuesWrapper.cs b/src/Microsoft.AspNetCore.Owin/DictionaryStringValuesWrapper.cs index b31c7e9790..c590a86789 100644 --- a/src/Microsoft.AspNetCore.Owin/DictionaryStringValuesWrapper.cs +++ b/src/Microsoft.AspNetCore.Owin/DictionaryStringValuesWrapper.cs @@ -68,7 +68,7 @@ namespace Microsoft.AspNetCore.Owin { if (value.HasValue) { - Inner[HeaderNames.ContentLength] = (StringValues)HeaderUtilities.FormatNonNegativeInt64(value.Value); + Inner[HeaderNames.ContentLength] = (StringValues)HeaderUtilities.FormatNonNegativeInt64(value.GetValueOrDefault()); } else { diff --git a/src/Microsoft.AspNetCore.WebUtilities/MultipartReaderStream.cs b/src/Microsoft.AspNetCore.WebUtilities/MultipartReaderStream.cs index 7952bd34b2..e1c4f642c8 100644 --- a/src/Microsoft.AspNetCore.WebUtilities/MultipartReaderStream.cs +++ b/src/Microsoft.AspNetCore.WebUtilities/MultipartReaderStream.cs @@ -151,9 +151,9 @@ namespace Microsoft.AspNetCore.WebUtilities if (_observedLength < _position) { _observedLength = _position; - if (LengthLimit.HasValue && _observedLength > LengthLimit.Value) + if (LengthLimit.HasValue && _observedLength > LengthLimit.GetValueOrDefault()) { - throw new InvalidDataException($"Multipart body length limit {LengthLimit.Value} exceeded."); + throw new InvalidDataException($"Multipart body length limit {LengthLimit.GetValueOrDefault()} exceeded."); } } return read; diff --git a/src/Microsoft.AspNetCore.WebUtilities/StreamHelperExtensions.cs b/src/Microsoft.AspNetCore.WebUtilities/StreamHelperExtensions.cs index e2c16a9cf2..f4ec558aaa 100644 --- a/src/Microsoft.AspNetCore.WebUtilities/StreamHelperExtensions.cs +++ b/src/Microsoft.AspNetCore.WebUtilities/StreamHelperExtensions.cs @@ -34,9 +34,9 @@ namespace Microsoft.AspNetCore.WebUtilities { // Not all streams support cancellation directly. cancellationToken.ThrowIfCancellationRequested(); - if (limit.HasValue && limit.Value - total < read) + if (limit.HasValue && limit.GetValueOrDefault() - total < read) { - throw new InvalidDataException($"The stream exceeded the data limit {limit.Value}."); + throw new InvalidDataException($"The stream exceeded the data limit {limit.GetValueOrDefault()}."); } total += read; read = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken); diff --git a/src/Microsoft.Net.Http.Headers/CacheControlHeaderValue.cs b/src/Microsoft.Net.Http.Headers/CacheControlHeaderValue.cs index 81e18faf47..655d9447ce 100644 --- a/src/Microsoft.Net.Http.Headers/CacheControlHeaderValue.cs +++ b/src/Microsoft.Net.Http.Headers/CacheControlHeaderValue.cs @@ -197,14 +197,14 @@ namespace Microsoft.Net.Http.Headers { AppendValueWithSeparatorIfRequired(sb, MaxAgeString); sb.Append('='); - sb.Append(((int)_maxAge.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(((int)_maxAge.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)); } if (_sharedMaxAge.HasValue) { AppendValueWithSeparatorIfRequired(sb, SharedMaxAgeString); sb.Append('='); - sb.Append(((int)_sharedMaxAge.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(((int)_sharedMaxAge.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)); } if (_maxStale) @@ -213,7 +213,7 @@ namespace Microsoft.Net.Http.Headers if (_maxStaleLimit.HasValue) { sb.Append('='); - sb.Append(((int)_maxStaleLimit.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(((int)_maxStaleLimit.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)); } } @@ -221,7 +221,7 @@ namespace Microsoft.Net.Http.Headers { AppendValueWithSeparatorIfRequired(sb, MinFreshString); sb.Append('='); - sb.Append(((int)_minFresh.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(((int)_minFresh.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo)); } if (_private) @@ -291,10 +291,10 @@ namespace Microsoft.Net.Http.Headers // XOR the hashcode of timespan values with different numbers to make sure two instances with the same // timespan set on different fields result in different hashcodes. - result = result ^ (_maxAge.HasValue ? _maxAge.Value.GetHashCode() ^ 1 : 0) ^ - (_sharedMaxAge.HasValue ? _sharedMaxAge.Value.GetHashCode() ^ 2 : 0) ^ - (_maxStaleLimit.HasValue ? _maxStaleLimit.Value.GetHashCode() ^ 4 : 0) ^ - (_minFresh.HasValue ? _minFresh.Value.GetHashCode() ^ 8 : 0); + result = result ^ (_maxAge.HasValue ? _maxAge.GetValueOrDefault().GetHashCode() ^ 1 : 0) ^ + (_sharedMaxAge.HasValue ? _sharedMaxAge.GetValueOrDefault().GetHashCode() ^ 2 : 0) ^ + (_maxStaleLimit.HasValue ? _maxStaleLimit.GetValueOrDefault().GetHashCode() ^ 4 : 0) ^ + (_minFresh.HasValue ? _minFresh.GetValueOrDefault().GetHashCode() ^ 8 : 0); if ((_noCacheHeaders != null) && (_noCacheHeaders.Count > 0)) { diff --git a/src/Microsoft.Net.Http.Headers/ContentDispositionHeaderValue.cs b/src/Microsoft.Net.Http.Headers/ContentDispositionHeaderValue.cs index 392a441733..5084f8ea06 100644 --- a/src/Microsoft.Net.Http.Headers/ContentDispositionHeaderValue.cs +++ b/src/Microsoft.Net.Http.Headers/ContentDispositionHeaderValue.cs @@ -137,11 +137,11 @@ namespace Microsoft.Net.Http.Headers } else if (sizeParameter != null) { - sizeParameter.Value = value.Value.ToString(CultureInfo.InvariantCulture); + sizeParameter.Value = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture); } else { - string sizeString = value.Value.ToString(CultureInfo.InvariantCulture); + string sizeString = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture); _parameters.Add(new NameValueHeaderValue(SizeString, sizeString)); } } @@ -324,7 +324,7 @@ namespace Microsoft.Net.Http.Headers else { // Must always be quoted - var dateString = HeaderUtilities.FormatDate(date.Value, quoted: true); + var dateString = HeaderUtilities.FormatDate(date.GetValueOrDefault(), quoted: true); if (dateParameter != null) { dateParameter.Value = dateString; diff --git a/src/Microsoft.Net.Http.Headers/ContentRangeHeaderValue.cs b/src/Microsoft.Net.Http.Headers/ContentRangeHeaderValue.cs index 99583cdf47..1149c33865 100644 --- a/src/Microsoft.Net.Http.Headers/ContentRangeHeaderValue.cs +++ b/src/Microsoft.Net.Http.Headers/ContentRangeHeaderValue.cs @@ -151,9 +151,9 @@ namespace Microsoft.Net.Http.Headers if (HasRange) { - sb.Append(_from.Value.ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(_from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo)); sb.Append('-'); - sb.Append(_to.Value.ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(_to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo)); } else { @@ -163,7 +163,7 @@ namespace Microsoft.Net.Http.Headers sb.Append('/'); if (HasLength) { - sb.Append(_length.Value.ToString(NumberFormatInfo.InvariantInfo)); + sb.Append(_length.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo)); } else { diff --git a/src/Microsoft.Net.Http.Headers/RangeConditionHeaderValue.cs b/src/Microsoft.Net.Http.Headers/RangeConditionHeaderValue.cs index f1ebee276c..b1d6513e2e 100644 --- a/src/Microsoft.Net.Http.Headers/RangeConditionHeaderValue.cs +++ b/src/Microsoft.Net.Http.Headers/RangeConditionHeaderValue.cs @@ -54,7 +54,7 @@ namespace Microsoft.Net.Http.Headers { if (_entityTag == null) { - return HeaderUtilities.FormatDate(_lastModified.Value); + return HeaderUtilities.FormatDate(_lastModified.GetValueOrDefault()); } return _entityTag.ToString(); } @@ -70,7 +70,7 @@ namespace Microsoft.Net.Http.Headers if (_entityTag == null) { - return (other._lastModified != null) && (_lastModified.Value == other._lastModified.Value); + return (other._lastModified != null) && (_lastModified.GetValueOrDefault() == other._lastModified.GetValueOrDefault()); } return _entityTag.Equals(other._entityTag); @@ -80,7 +80,7 @@ namespace Microsoft.Net.Http.Headers { if (_entityTag == null) { - return _lastModified.Value.GetHashCode(); + return _lastModified.GetValueOrDefault().GetHashCode(); } return _entityTag.GetHashCode(); diff --git a/src/Microsoft.Net.Http.Headers/RangeItemHeaderValue.cs b/src/Microsoft.Net.Http.Headers/RangeItemHeaderValue.cs index 3b177f6e9a..010f2da1e0 100644 --- a/src/Microsoft.Net.Http.Headers/RangeItemHeaderValue.cs +++ b/src/Microsoft.Net.Http.Headers/RangeItemHeaderValue.cs @@ -20,15 +20,15 @@ namespace Microsoft.Net.Http.Headers { throw new ArgumentException("Invalid header range."); } - if (from.HasValue && (from.Value < 0)) + if (from.HasValue && (from.GetValueOrDefault() < 0)) { throw new ArgumentOutOfRangeException(nameof(from)); } - if (to.HasValue && (to.Value < 0)) + if (to.HasValue && (to.GetValueOrDefault() < 0)) { throw new ArgumentOutOfRangeException(nameof(to)); } - if (from.HasValue && to.HasValue && (from.Value > to.Value)) + if (from.HasValue && to.HasValue && (from.GetValueOrDefault() > to.GetValueOrDefault())) { throw new ArgumentOutOfRangeException(nameof(from)); } @@ -51,38 +51,37 @@ namespace Microsoft.Net.Http.Headers { if (!_from.HasValue) { - return "-" + _to.Value.ToString(NumberFormatInfo.InvariantInfo); + return "-" + _to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo); } else if (!_to.HasValue) { - return _from.Value.ToString(NumberFormatInfo.InvariantInfo) + "-"; + return _from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo) + "-"; } - return _from.Value.ToString(NumberFormatInfo.InvariantInfo) + "-" + - _to.Value.ToString(NumberFormatInfo.InvariantInfo); + return _from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo) + "-" + + _to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo); } public override bool Equals(object obj) { - var other = obj as RangeItemHeaderValue; - - if (other == null) + if (obj is RangeItemHeaderValue other) { - return false; + return ((_from == other._from) && (_to == other._to)); } - return ((_from == other._from) && (_to == other._to)); + + return false; } public override int GetHashCode() { if (!_from.HasValue) { - return _to.GetHashCode(); + return _to.GetValueOrDefault().GetHashCode(); } else if (!_to.HasValue) { - return _from.GetHashCode(); + return _from.GetValueOrDefault().GetHashCode(); } - return _from.GetHashCode() ^ _to.GetHashCode(); + return _from.GetValueOrDefault().GetHashCode() ^ _to.GetValueOrDefault().GetHashCode(); } // Returns the length of a range list. E.g. "1-2, 3-4, 5-6" adds 3 ranges to 'rangeCollection'. Note that empty diff --git a/src/Microsoft.Net.Http.Headers/SetCookieHeaderValue.cs b/src/Microsoft.Net.Http.Headers/SetCookieHeaderValue.cs index 74b5c6c48c..3070a6e1f9 100644 --- a/src/Microsoft.Net.Http.Headers/SetCookieHeaderValue.cs +++ b/src/Microsoft.Net.Http.Headers/SetCookieHeaderValue.cs @@ -105,13 +105,13 @@ namespace Microsoft.Net.Http.Headers if (Expires.HasValue) { - expires = HeaderUtilities.FormatDate(Expires.Value); + expires = HeaderUtilities.FormatDate(Expires.GetValueOrDefault()); length += SeparatorToken.Length + ExpiresToken.Length + EqualsToken.Length + expires.Length; } if (MaxAge.HasValue) { - maxAge = HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.Value.TotalSeconds); + maxAge = HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.GetValueOrDefault().TotalSeconds); length += SeparatorToken.Length + MaxAgeToken.Length + EqualsToken.Length + maxAge.Length; } @@ -212,12 +212,12 @@ namespace Microsoft.Net.Http.Headers if (Expires.HasValue) { - AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.Value)); + AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.GetValueOrDefault())); } if (MaxAge.HasValue) { - AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.Value.TotalSeconds)); + AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.GetValueOrDefault().TotalSeconds)); } if (Domain != null) diff --git a/src/Microsoft.Net.Http.Headers/StringWithQualityHeaderValue.cs b/src/Microsoft.Net.Http.Headers/StringWithQualityHeaderValue.cs index deba2d2697..153643b361 100644 --- a/src/Microsoft.Net.Http.Headers/StringWithQualityHeaderValue.cs +++ b/src/Microsoft.Net.Http.Headers/StringWithQualityHeaderValue.cs @@ -58,7 +58,7 @@ namespace Microsoft.Net.Http.Headers { if (_quality.HasValue) { - return _value + "; q=" + _quality.Value.ToString("0.0##", NumberFormatInfo.InvariantInfo); + return _value + "; q=" + _quality.GetValueOrDefault().ToString("0.0##", NumberFormatInfo.InvariantInfo); } return _value.ToString(); @@ -83,7 +83,7 @@ namespace Microsoft.Net.Http.Headers // Note that we don't consider double.Epsilon here. We really consider two values equal if they're // actually equal. This makes sure that we also get the same hashcode for two values considered equal // by Equals(). - return other._quality.HasValue && (_quality.Value == other._quality.Value); + return other._quality.HasValue && (_quality.GetValueOrDefault() == other._quality.Value); } // If we don't have a quality value, then 'other' must also have no quality assigned in order to be @@ -97,7 +97,7 @@ namespace Microsoft.Net.Http.Headers if (_quality.HasValue) { - result = result ^ _quality.Value.GetHashCode(); + result = result ^ _quality.GetValueOrDefault().GetHashCode(); } return result;