Skip to content

Commit cd81d48

Browse files
authored
Add nullable to WebUtilities (#22543)
1 parent aeb28d4 commit cd81d48

32 files changed

+154
-140
lines changed

src/Http/WebUtilities/ref/Microsoft.AspNetCore.WebUtilities.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.AspNetCore.WebUtilities.netcoreapp.cs" />

src/Http/WebUtilities/ref/Microsoft.AspNetCore.WebUtilities.netcoreapp.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public FileBufferingReadStream(System.IO.Stream inner, int memoryThreshold, long
5252
public bool InMemory { get { throw null; } }
5353
public override long Length { get { throw null; } }
5454
public override long Position { get { throw null; } set { } }
55-
public string TempFileName { get { throw null; } }
55+
public string? TempFileName { get { throw null; } }
5656
protected override void Dispose(bool disposing) { }
5757
[System.Diagnostics.DebuggerStepThroughAttribute]
5858
public override System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
@@ -67,7 +67,7 @@ public override void Write(byte[] buffer, int offset, int count) { }
6767
}
6868
public sealed partial class FileBufferingWriteStream : System.IO.Stream
6969
{
70-
public FileBufferingWriteStream(int memoryThreshold = 32768, long? bufferLimit = default(long?), System.Func<string> tempFileDirectoryAccessor = null) { }
70+
public FileBufferingWriteStream(int memoryThreshold = 32768, long? bufferLimit = default(long?), System.Func<string>? tempFileDirectoryAccessor = null) { }
7171
public override bool CanRead { get { throw null; } }
7272
public override bool CanSeek { get { throw null; } }
7373
public override bool CanWrite { get { throw null; } }
@@ -91,16 +91,16 @@ public override void Write(byte[] buffer, int offset, int count) { }
9191
public partial class FileMultipartSection
9292
{
9393
public FileMultipartSection(Microsoft.AspNetCore.WebUtilities.MultipartSection section) { }
94-
public FileMultipartSection(Microsoft.AspNetCore.WebUtilities.MultipartSection section, Microsoft.Net.Http.Headers.ContentDispositionHeaderValue header) { }
94+
public FileMultipartSection(Microsoft.AspNetCore.WebUtilities.MultipartSection section, Microsoft.Net.Http.Headers.ContentDispositionHeaderValue? header) { }
9595
public string FileName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
96-
public System.IO.Stream FileStream { get { throw null; } }
96+
public System.IO.Stream? FileStream { get { throw null; } }
9797
public string Name { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
9898
public Microsoft.AspNetCore.WebUtilities.MultipartSection Section { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
9999
}
100100
public partial class FormMultipartSection
101101
{
102102
public FormMultipartSection(Microsoft.AspNetCore.WebUtilities.MultipartSection section) { }
103-
public FormMultipartSection(Microsoft.AspNetCore.WebUtilities.MultipartSection section, Microsoft.Net.Http.Headers.ContentDispositionHeaderValue header) { }
103+
public FormMultipartSection(Microsoft.AspNetCore.WebUtilities.MultipartSection section, Microsoft.Net.Http.Headers.ContentDispositionHeaderValue? header) { }
104104
public string Name { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
105105
public Microsoft.AspNetCore.WebUtilities.MultipartSection Section { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
106106
public System.Threading.Tasks.Task<string> GetValueAsync() { throw null; }
@@ -149,9 +149,9 @@ protected override void Dispose(bool disposing) { }
149149
public override System.Threading.Tasks.Task<int> ReadAsync(char[] buffer, int index, int count) { throw null; }
150150
[System.Diagnostics.DebuggerStepThroughAttribute]
151151
public override System.Threading.Tasks.ValueTask<int> ReadAsync(System.Memory<char> buffer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
152-
public override string ReadLine() { throw null; }
152+
public override string? ReadLine() { throw null; }
153153
[System.Diagnostics.DebuggerStepThroughAttribute]
154-
public override System.Threading.Tasks.Task<string> ReadLineAsync() { throw null; }
154+
public override System.Threading.Tasks.Task<string?> ReadLineAsync() { throw null; }
155155
[System.Diagnostics.DebuggerStepThroughAttribute]
156156
public override System.Threading.Tasks.Task<string> ReadToEndAsync() { throw null; }
157157
}
@@ -169,11 +169,11 @@ public override void Flush() { }
169169
public override void Write(char value) { }
170170
public override void Write(char[] values, int index, int count) { }
171171
public override void Write(System.ReadOnlySpan<char> value) { }
172-
public override void Write(string value) { }
172+
public override void Write(string? value) { }
173173
public override System.Threading.Tasks.Task WriteAsync(char value) { throw null; }
174174
public override System.Threading.Tasks.Task WriteAsync(char[] values, int index, int count) { throw null; }
175175
public override System.Threading.Tasks.Task WriteAsync(System.ReadOnlyMemory<char> value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
176-
public override System.Threading.Tasks.Task WriteAsync(string value) { throw null; }
176+
public override System.Threading.Tasks.Task WriteAsync(string? value) { throw null; }
177177
public override void WriteLine(System.ReadOnlySpan<char> value) { }
178178
public override System.Threading.Tasks.Task WriteLineAsync(System.ReadOnlyMemory<char> value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
179179
}
@@ -198,22 +198,22 @@ public MultipartReader(string boundary, System.IO.Stream stream, int bufferSize)
198198
public int HeadersCountLimit { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
199199
public int HeadersLengthLimit { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
200200
[System.Diagnostics.DebuggerStepThroughAttribute]
201-
public System.Threading.Tasks.Task<Microsoft.AspNetCore.WebUtilities.MultipartSection> ReadNextSectionAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
201+
public System.Threading.Tasks.Task<Microsoft.AspNetCore.WebUtilities.MultipartSection?> ReadNextSectionAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
202202
}
203203
public partial class MultipartSection
204204
{
205205
public MultipartSection() { }
206206
public long? BaseStreamOffset { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
207-
public System.IO.Stream Body { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
208-
public string ContentDisposition { get { throw null; } }
209-
public string ContentType { get { throw null; } }
210-
public System.Collections.Generic.Dictionary<string, Microsoft.Extensions.Primitives.StringValues> Headers { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
207+
public System.IO.Stream? Body { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
208+
public string? ContentDisposition { get { throw null; } }
209+
public string? ContentType { get { throw null; } }
210+
public System.Collections.Generic.Dictionary<string, Microsoft.Extensions.Primitives.StringValues>? Headers { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
211211
}
212212
public static partial class MultipartSectionConverterExtensions
213213
{
214-
public static Microsoft.AspNetCore.WebUtilities.FileMultipartSection AsFileSection(this Microsoft.AspNetCore.WebUtilities.MultipartSection section) { throw null; }
215-
public static Microsoft.AspNetCore.WebUtilities.FormMultipartSection AsFormDataSection(this Microsoft.AspNetCore.WebUtilities.MultipartSection section) { throw null; }
216-
public static Microsoft.Net.Http.Headers.ContentDispositionHeaderValue GetContentDispositionHeader(this Microsoft.AspNetCore.WebUtilities.MultipartSection section) { throw null; }
214+
public static Microsoft.AspNetCore.WebUtilities.FileMultipartSection? AsFileSection(this Microsoft.AspNetCore.WebUtilities.MultipartSection section) { throw null; }
215+
public static Microsoft.AspNetCore.WebUtilities.FormMultipartSection? AsFormDataSection(this Microsoft.AspNetCore.WebUtilities.MultipartSection section) { throw null; }
216+
public static Microsoft.Net.Http.Headers.ContentDispositionHeaderValue? GetContentDispositionHeader(this Microsoft.AspNetCore.WebUtilities.MultipartSection section) { throw null; }
217217
}
218218
public static partial class MultipartSectionStreamExtensions
219219
{
@@ -222,11 +222,11 @@ public static partial class MultipartSectionStreamExtensions
222222
}
223223
public static partial class QueryHelpers
224224
{
225-
public static string AddQueryString(string uri, System.Collections.Generic.IDictionary<string, string> queryString) { throw null; }
225+
public static string AddQueryString(string uri, System.Collections.Generic.IDictionary<string, string?> queryString) { throw null; }
226226
public static string AddQueryString(string uri, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, Microsoft.Extensions.Primitives.StringValues>> queryString) { throw null; }
227-
public static string AddQueryString(string uri, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> queryString) { throw null; }
227+
public static string AddQueryString(string uri, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string?>> queryString) { throw null; }
228228
public static string AddQueryString(string uri, string name, string value) { throw null; }
229-
public static System.Collections.Generic.Dictionary<string, Microsoft.Extensions.Primitives.StringValues> ParseNullableQuery(string queryString) { throw null; }
229+
public static System.Collections.Generic.Dictionary<string, Microsoft.Extensions.Primitives.StringValues>? ParseNullableQuery(string queryString) { throw null; }
230230
public static System.Collections.Generic.Dictionary<string, Microsoft.Extensions.Primitives.StringValues> ParseQuery(string queryString) { throw null; }
231231
}
232232
public static partial class ReasonPhrases

src/Http/WebUtilities/src/AspNetCoreTempDirectory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
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+
#nullable enable
5+
46
using System;
57
using System.IO;
68

79
namespace Microsoft.AspNetCore.Internal
810
{
911
internal static class AspNetCoreTempDirectory
1012
{
11-
private static string _tempDirectory;
13+
private static string? _tempDirectory;
1214

1315
public static string TempDirectory
1416
{

src/Http/WebUtilities/src/FileBufferingReadStream.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public class FileBufferingReadStream : Stream
2323
private readonly ArrayPool<byte> _bytePool;
2424
private readonly int _memoryThreshold;
2525
private readonly long? _bufferLimit;
26-
private string _tempFileDirectory;
27-
private readonly Func<string> _tempFileDirectoryAccessor;
28-
private string _tempFileName;
26+
private string? _tempFileDirectory;
27+
private readonly Func<string>? _tempFileDirectoryAccessor;
28+
private string? _tempFileName;
2929

3030
private Stream _buffer;
31-
private byte[] _rentedBuffer;
31+
private byte[]? _rentedBuffer;
3232
private bool _inMemory = true;
3333
private bool _completelyBuffered;
3434

@@ -137,7 +137,7 @@ public bool InMemory
137137
get { return _inMemory; }
138138
}
139139

140-
public string TempFileName
140+
public string? TempFileName
141141
{
142142
get { return _tempFileName; }
143143
}

src/Http/WebUtilities/src/FileBufferingWriteStream.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
using System;
55
using System.Buffers;
66
using System.Diagnostics;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.IO;
8-
using System.IO.Pipelines;
9-
using System.Security.Cryptography;
109
using System.Threading;
1110
using System.Threading.Tasks;
1211
using Microsoft.AspNetCore.Internal;
@@ -42,7 +41,7 @@ public sealed class FileBufferingWriteStream : Stream
4241
public FileBufferingWriteStream(
4342
int memoryThreshold = DefaultMemoryThreshold,
4443
long? bufferLimit = null,
45-
Func<string> tempFileDirectoryAccessor = null)
44+
Func<string>? tempFileDirectoryAccessor = null)
4645
{
4746
if (memoryThreshold < 0)
4847
{
@@ -82,7 +81,7 @@ public override long Position
8281

8382
internal PagedByteBuffer PagedByteBuffer { get; }
8483

85-
internal FileStream FileStream { get; private set; }
84+
internal FileStream? FileStream { get; private set; }
8685

8786
internal bool Disposed { get; private set; }
8887

@@ -225,6 +224,7 @@ public override async ValueTask DisposeAsync()
225224
}
226225
}
227226

227+
[MemberNotNull(nameof(FileStream))]
228228
private void EnsureFileStream()
229229
{
230230
if (FileStream == null)

src/Http/WebUtilities/src/FileMultipartSection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// 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

44
using System;
@@ -29,9 +29,9 @@ public FileMultipartSection(MultipartSection section)
2929
/// </summary>
3030
/// <param name="section">The section from which to create the <see cref="FileMultipartSection"/></param>
3131
/// <param name="header">An already parsed content disposition header</param>
32-
public FileMultipartSection(MultipartSection section, ContentDispositionHeaderValue header)
32+
public FileMultipartSection(MultipartSection section, ContentDispositionHeaderValue? header)
3333
{
34-
if (!header.IsFileDisposition())
34+
if (header is null || !header.IsFileDisposition())
3535
{
3636
throw new ArgumentException($"Argument must be a file section", nameof(section));
3737
}
@@ -54,7 +54,7 @@ public FileMultipartSection(MultipartSection section, ContentDispositionHeaderVa
5454
/// <summary>
5555
/// Gets the file stream from the section body
5656
/// </summary>
57-
public Stream FileStream => Section.Body;
57+
public Stream? FileStream => Section.Body;
5858

5959
/// <summary>
6060
/// Gets the name of the section

src/Http/WebUtilities/src/FormMultipartSection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// 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

44
using System;
@@ -29,7 +29,7 @@ public FormMultipartSection(MultipartSection section)
2929
/// </summary>
3030
/// <param name="section">The section from which to create the <see cref="FormMultipartSection"/></param>
3131
/// <param name="header">An already parsed content disposition header</param>
32-
public FormMultipartSection(MultipartSection section, ContentDispositionHeaderValue header)
32+
public FormMultipartSection(MultipartSection section, ContentDispositionHeaderValue? header)
3333
{
3434
if (header == null || !header.IsFormDisposition())
3535
{

src/Http/WebUtilities/src/FormPipeReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class FormPipeReader
3434
private static ReadOnlySpan<byte> UTF8AndEncoded => new byte[] { (byte)'&' };
3535

3636
// Used for other encodings
37-
private byte[] _otherEqualEncoding;
38-
private byte[] _otherAndEncoding;
37+
private byte[]? _otherEqualEncoding;
38+
private byte[]? _otherAndEncoding;
3939

4040
private readonly PipeReader _pipeReader;
4141
private readonly Encoding _encoding;

src/Http/WebUtilities/src/FormReader.cs

Lines changed: 6 additions & 4 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.IO;
89
using System.Text;
910
using System.Threading;
@@ -28,8 +29,8 @@ public class FormReader : IDisposable
2829
private readonly StringBuilder _builder = new StringBuilder();
2930
private int _bufferOffset;
3031
private int _bufferCount;
31-
private string _currentKey;
32-
private string _currentValue;
32+
private string? _currentKey;
33+
private string? _currentValue;
3334
private bool _endOfStream;
3435
private bool _disposed;
3536

@@ -189,7 +190,7 @@ private bool TryReadNextPair()
189190
return true;
190191
}
191192

192-
private bool TryReadWord(char separator, int limit, out string value)
193+
private bool TryReadWord(char separator, int limit, [NotNullWhen(true)] out string? value)
193194
{
194195
do
195196
{
@@ -201,7 +202,7 @@ private bool TryReadWord(char separator, int limit, out string value)
201202
return false;
202203
}
203204

204-
private bool ReadChar(char separator, int limit, out string word)
205+
private bool ReadChar(char separator, int limit, [NotNullWhen(true)] out string? word)
205206
{
206207
// End
207208
if (_bufferCount == 0)
@@ -283,6 +284,7 @@ public Dictionary<string, StringValues> ReadForm()
283284
return accumulator.GetResults();
284285
}
285286

287+
[MemberNotNullWhen(true, nameof(_currentKey), nameof(_currentValue))]
286288
private bool ReadSucceeded()
287289
{
288290
return _currentKey != null && _currentValue != null;

0 commit comments

Comments
 (0)