Skip to content

Commit ff2f78d

Browse files
JamesNKpranavkm
authored andcommitted
Add nullable annotations to Http.Abstractions, Http.Features, Connections.Abstractions
1 parent bcd4fc0 commit ff2f78d

File tree

123 files changed

+1037
-1022
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+1037
-1022
lines changed

src/Http/Headers/ref/Microsoft.Net.Http.Headers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
5+
<Nullable>annotations</Nullable>
56
</PropertyGroup>
67
<ItemGroup Condition="'$(TargetFramework)' == '$(DefaultNetCoreTargetFramework)'">
78
<Compile Include="Microsoft.Net.Http.Headers.netcoreapp.cs" />

src/Http/Headers/ref/Microsoft.Net.Http.Headers.netcoreapp.cs

Lines changed: 47 additions & 47 deletions
Large diffs are not rendered by default.

src/Http/Headers/src/BaseHeaderParser.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using Microsoft.Extensions.Primitives;
56

67
namespace Microsoft.Net.Http.Headers
@@ -12,11 +13,11 @@ protected BaseHeaderParser(bool supportsMultipleValues)
1213
{
1314
}
1415

15-
protected abstract int GetParsedValueLength(StringSegment value, int startIndex, out T parsedValue);
16+
protected abstract int GetParsedValueLength(StringSegment value, int startIndex, [MaybeNull] out T parsedValue);
1617

17-
public sealed override bool TryParseValue(StringSegment value, ref int index, out T parsedValue)
18+
public sealed override bool TryParseValue(StringSegment value, ref int index, [MaybeNull] out T parsedValue)
1819
{
19-
parsedValue = default(T);
20+
parsedValue = default;
2021

2122
// If multiple values are supported (i.e. list of values), then accept an empty string: The header may
2223
// be added multiple times to the request/response message. E.g.

src/Http/Headers/src/CacheControlHeaderValue.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public class CacheControlHeaderValue
3131
// Cache-Control headers, only one instance of CacheControlHeaderValue is created (if all headers contain valid
3232
// values, otherwise we may have multiple strings containing the invalid values).
3333
private static readonly HttpHeaderParser<CacheControlHeaderValue> Parser
34-
= new GenericHeaderParser<CacheControlHeaderValue>(true, GetCacheControlLength);
34+
= new GenericHeaderParser<CacheControlHeaderValue>(true, GetCacheControlLength!);
3535

3636
private static readonly Action<StringSegment> CheckIsValidTokenAction = CheckIsValidToken;
3737

3838
private bool _noCache;
39-
private ICollection<StringSegment> _noCacheHeaders;
39+
private ICollection<StringSegment>? _noCacheHeaders;
4040
private bool _noStore;
4141
private TimeSpan? _maxAge;
4242
private TimeSpan? _sharedMaxAge;
@@ -47,10 +47,10 @@ private static readonly HttpHeaderParser<CacheControlHeaderValue> Parser
4747
private bool _onlyIfCached;
4848
private bool _public;
4949
private bool _private;
50-
private ICollection<StringSegment> _privateHeaders;
50+
private ICollection<StringSegment>? _privateHeaders;
5151
private bool _mustRevalidate;
5252
private bool _proxyRevalidate;
53-
private IList<NameValueHeaderValue> _extensions;
53+
private IList<NameValueHeaderValue>? _extensions;
5454

5555
public CacheControlHeaderValue()
5656
{
@@ -240,7 +240,7 @@ public override string ToString()
240240
return sb.ToString();
241241
}
242242

243-
public override bool Equals(object obj)
243+
public override bool Equals(object? obj)
244244
{
245245
var other = obj as CacheControlHeaderValue;
246246

@@ -335,7 +335,7 @@ public static CacheControlHeaderValue Parse(StringSegment input)
335335
return result;
336336
}
337337

338-
public static bool TryParse(StringSegment input, out CacheControlHeaderValue parsedValue)
338+
public static bool TryParse(StringSegment input, out CacheControlHeaderValue? parsedValue)
339339
{
340340
int index = 0;
341341
// Cache-Control is unusual because there are no required values so the parser will succeed for an empty string, but still return null.
@@ -347,7 +347,7 @@ public static bool TryParse(StringSegment input, out CacheControlHeaderValue par
347347
return false;
348348
}
349349

350-
private static int GetCacheControlLength(StringSegment input, int startIndex, out CacheControlHeaderValue parsedValue)
350+
private static int GetCacheControlLength(StringSegment input, int startIndex, out CacheControlHeaderValue? parsedValue)
351351
{
352352
Contract.Requires(startIndex >= 0);
353353

@@ -361,16 +361,18 @@ private static int GetCacheControlLength(StringSegment input, int startIndex, ou
361361
// Cache-Control header consists of a list of name/value pairs, where the value is optional. So use an
362362
// instance of NameValueHeaderParser to parse the string.
363363
var current = startIndex;
364-
NameValueHeaderValue nameValue = null;
365364
var nameValueList = new List<NameValueHeaderValue>();
366365
while (current < input.Length)
367366
{
368-
if (!NameValueHeaderValue.MultipleValueParser.TryParseValue(input, ref current, out nameValue))
367+
if (!NameValueHeaderValue.MultipleValueParser.TryParseValue(input, ref current, out var nameValue))
369368
{
370369
return 0;
371370
}
372371

373-
nameValueList.Add(nameValue);
372+
if (nameValue != null)
373+
{
374+
nameValueList.Add(nameValue);
375+
}
374376
}
375377

376378
// If we get here, we were able to successfully parse the string as list of name/value pairs. Now analyze
@@ -539,10 +541,8 @@ private static bool TrySetTokenOnlyValue(NameValueHeaderValue nameValue, ref boo
539541
private static bool TrySetOptionalTokenList(
540542
NameValueHeaderValue nameValue,
541543
ref bool boolField,
542-
ref ICollection<StringSegment> destination)
544+
ref ICollection<StringSegment>? destination)
543545
{
544-
Contract.Requires(nameValue != null);
545-
546546
if (nameValue.Value == null)
547547
{
548548
boolField = true;
@@ -603,8 +603,6 @@ private static bool TrySetOptionalTokenList(
603603

604604
private static bool TrySetTimeSpan(NameValueHeaderValue nameValue, ref TimeSpan? timeSpan)
605605
{
606-
Contract.Requires(nameValue != null);
607-
608606
if (nameValue.Value == null)
609607
{
610608
return false;

src/Http/Headers/src/ContentDispositionHeaderValue.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Buffers;
66
using System.Collections.Generic;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Diagnostics.Contracts;
89
using System.Globalization;
910
using System.Linq;
@@ -26,10 +27,10 @@ public class ContentDispositionHeaderValue
2627
private static readonly char[] SingleQuote = new char[] { '\'' };
2728

2829
private static readonly HttpHeaderParser<ContentDispositionHeaderValue> Parser
29-
= new GenericHeaderParser<ContentDispositionHeaderValue>(false, GetDispositionTypeLength);
30+
= new GenericHeaderParser<ContentDispositionHeaderValue>(false, GetDispositionTypeLength!);
3031

3132
// Use list instead of dictionary since we may have multiple parameters with the same name.
32-
private ObjectCollection<NameValueHeaderValue> _parameters;
33+
private ObjectCollection<NameValueHeaderValue>? _parameters;
3334
private StringSegment _dispositionType;
3435

3536
private ContentDispositionHeaderValue()
@@ -128,7 +129,7 @@ public long? Size
128129
// Remove parameter
129130
if (sizeParameter != null)
130131
{
131-
_parameters.Remove(sizeParameter);
132+
_parameters!.Remove(sizeParameter);
132133
}
133134
}
134135
else if (value < 0)
@@ -142,7 +143,7 @@ public long? Size
142143
else
143144
{
144145
string sizeString = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture);
145-
_parameters.Add(new NameValueHeaderValue(SizeString, sizeString));
146+
_parameters!.Add(new NameValueHeaderValue(SizeString, sizeString));
146147
}
147148
}
148149
}
@@ -180,7 +181,7 @@ public override string ToString()
180181
return _dispositionType + NameValueHeaderValue.ToString(_parameters, ';', true);
181182
}
182183

183-
public override bool Equals(object obj)
184+
public override bool Equals(object? obj)
184185
{
185186
var other = obj as ContentDispositionHeaderValue;
186187

@@ -202,16 +203,16 @@ public override int GetHashCode()
202203
public static ContentDispositionHeaderValue Parse(StringSegment input)
203204
{
204205
var index = 0;
205-
return Parser.ParseValue(input, ref index);
206+
return Parser.ParseValue(input, ref index)!;
206207
}
207208

208-
public static bool TryParse(StringSegment input, out ContentDispositionHeaderValue parsedValue)
209+
public static bool TryParse(StringSegment input, [NotNullWhen(true)] out ContentDispositionHeaderValue? parsedValue)
209210
{
210211
var index = 0;
211-
return Parser.TryParseValue(input, ref index, out parsedValue);
212+
return Parser.TryParseValue(input, ref index, out parsedValue!);
212213
}
213214

214-
private static int GetDispositionTypeLength(StringSegment input, int startIndex, out ContentDispositionHeaderValue parsedValue)
215+
private static int GetDispositionTypeLength(StringSegment input, int startIndex, out ContentDispositionHeaderValue? parsedValue)
215216
{
216217
Contract.Requires(startIndex >= 0);
217218

@@ -253,7 +254,7 @@ private static int GetDispositionTypeLength(StringSegment input, int startIndex,
253254

254255
private static int GetDispositionTypeExpressionLength(StringSegment input, int startIndex, out StringSegment dispositionType)
255256
{
256-
Contract.Requires((input != null) && (input.Length > 0) && (startIndex < input.Length));
257+
Contract.Requires((input.Length > 0) && (startIndex < input.Length));
257258

258259
// This method just parses the disposition type string, it does not parse parameters.
259260
dispositionType = null;
@@ -318,7 +319,7 @@ private void SetDate(string parameter, DateTimeOffset? date)
318319
// Remove parameter
319320
if (dateParameter != null)
320321
{
321-
_parameters.Remove(dateParameter);
322+
_parameters!.Remove(dateParameter);
322323
}
323324
}
324325
else
@@ -343,7 +344,7 @@ private StringSegment GetName(string parameter)
343344
var nameParameter = NameValueHeaderValue.Find(_parameters, parameter);
344345
if (nameParameter != null)
345346
{
346-
string result;
347+
string? result;
347348
// filename*=utf-8'lang'%7FMyString
348349
if (parameter.EndsWith("*", StringComparison.Ordinal))
349350
{
@@ -375,7 +376,7 @@ private void SetName(StringSegment parameter, StringSegment value)
375376
// Remove parameter
376377
if (nameParameter != null)
377378
{
378-
_parameters.Remove(nameParameter);
379+
_parameters!.Remove(nameParameter);
379380
}
380381
}
381382
else
@@ -497,7 +498,7 @@ private unsafe string EncodeMime(StringSegment input)
497498
}
498499

499500
// Attempt to decode MIME encoded strings
500-
private bool TryDecodeMime(StringSegment input, out string output)
501+
private bool TryDecodeMime(StringSegment input, [NotNullWhen(true)] out string? output)
501502
{
502503
Contract.Assert(input != null);
503504

@@ -582,7 +583,7 @@ private static void HexEscape(StringBuilder builder, char c)
582583

583584
// Attempt to decode using RFC 5987 encoding.
584585
// encoding'language'my%20string
585-
private bool TryDecode5987(StringSegment input, out string output)
586+
private bool TryDecode5987(StringSegment input, [NotNullWhen(true)] out string? output)
586587
{
587588
output = null;
588589

@@ -593,7 +594,7 @@ private bool TryDecode5987(StringSegment input, out string output)
593594
}
594595

595596
var decoded = new StringBuilder();
596-
byte[] unescapedBytes = null;
597+
byte[]? unescapedBytes = null;
597598
try
598599
{
599600
var encoding = Encoding.GetEncoding(parts[0].ToString());

src/Http/Headers/src/ContentRangeHeaderValue.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Diagnostics.Contracts;
67
using System.Globalization;
78
using System.Text;
@@ -12,7 +13,7 @@ namespace Microsoft.Net.Http.Headers
1213
public class ContentRangeHeaderValue
1314
{
1415
private static readonly HttpHeaderParser<ContentRangeHeaderValue> Parser
15-
= new GenericHeaderParser<ContentRangeHeaderValue>(false, GetContentRangeLength);
16+
= new GenericHeaderParser<ContentRangeHeaderValue>(false, GetContentRangeLength!);
1617

1718
private StringSegment _unit;
1819
private long? _from;
@@ -113,7 +114,7 @@ public long? Length
113114
get { return _from != null; }
114115
}
115116

116-
public override bool Equals(object obj)
117+
public override bool Equals(object? obj)
117118
{
118119
var other = obj as ContentRangeHeaderValue;
119120

@@ -176,16 +177,16 @@ public override string ToString()
176177
public static ContentRangeHeaderValue Parse(StringSegment input)
177178
{
178179
var index = 0;
179-
return Parser.ParseValue(input, ref index);
180+
return Parser.ParseValue(input, ref index)!;
180181
}
181182

182183
public static bool TryParse(StringSegment input, out ContentRangeHeaderValue parsedValue)
183184
{
184185
var index = 0;
185-
return Parser.TryParseValue(input, ref index, out parsedValue);
186+
return Parser.TryParseValue(input, ref index, out parsedValue!);
186187
}
187188

188-
private static int GetContentRangeLength(StringSegment input, int startIndex, out ContentRangeHeaderValue parsedValue)
189+
private static int GetContentRangeLength(StringSegment input, int startIndex, out ContentRangeHeaderValue? parsedValue)
189190
{
190191
Contract.Requires(startIndex >= 0);
191192

@@ -351,7 +352,7 @@ private static bool TryCreateContentRange(
351352
int toLength,
352353
int lengthStartIndex,
353354
int lengthLength,
354-
out ContentRangeHeaderValue parsedValue)
355+
[NotNullWhen(true)]out ContentRangeHeaderValue? parsedValue)
355356
{
356357
parsedValue = null;
357358

src/Http/Headers/src/CookieHeaderParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Diagnostics.Contracts;
56
using Microsoft.Extensions.Primitives;
67

@@ -13,7 +14,7 @@ internal CookieHeaderParser(bool supportsMultipleValues)
1314
{
1415
}
1516

16-
public override bool TryParseValue(StringSegment value, ref int index, out CookieHeaderValue parsedValue)
17+
public override bool TryParseValue(StringSegment value, ref int index, [MaybeNull] out CookieHeaderValue? parsedValue)
1718
{
1819
parsedValue = null;
1920

@@ -43,7 +44,7 @@ public override bool TryParseValue(StringSegment value, ref int index, out Cooki
4344
return SupportsMultipleValues;
4445
}
4546

46-
CookieHeaderValue result = null;
47+
CookieHeaderValue? result = null;
4748
if (!CookieHeaderValue.TryGetCookieLength(value, ref current, out result))
4849
{
4950
return false;
@@ -64,7 +65,6 @@ public override bool TryParseValue(StringSegment value, ref int index, out Cooki
6465

6566
private static int GetNextNonEmptyOrWhitespaceIndex(StringSegment input, int startIndex, bool skipEmptyValues, out bool separatorFound)
6667
{
67-
Contract.Requires(input != null);
6868
Contract.Requires(startIndex <= input.Length); // it's OK if index == value.Length.
6969

7070
separatorFound = false;

0 commit comments

Comments
 (0)