1
- // Copyright (c) Cloud Native Foundation.
1
+ // Copyright (c) Cloud Native Foundation.
2
2
// Licensed under the Apache 2.0 license.
3
3
// See LICENSE file in the project root for full license information.
4
4
5
5
using CloudNative . CloudEvents . Core ;
6
6
using System ;
7
7
using System . Collections . Generic ;
8
+ using System . Diagnostics . CodeAnalysis ;
8
9
using System . Linq ;
9
10
using System . Net . Http ;
10
11
using System . Net . Http . Headers ;
@@ -130,7 +131,7 @@ public static Task<CloudEvent> ToCloudEventAsync(
130
131
return ToCloudEventInternalAsync ( httpRequestMessage . Headers , httpRequestMessage . Content , formatter , extensionAttributes , nameof ( httpRequestMessage ) ) ;
131
132
}
132
133
133
- private static async Task < CloudEvent > ToCloudEventInternalAsync ( HttpHeaders headers , HttpContent content ,
134
+ private static async Task < CloudEvent > ToCloudEventInternalAsync ( HttpHeaders headers , HttpContent ? content ,
134
135
CloudEventFormatter formatter , IEnumerable < CloudEventAttribute > ? extensionAttributes , string paramName )
135
136
{
136
137
Validation . CheckNotNull ( formatter , nameof ( formatter ) ) ;
@@ -142,7 +143,7 @@ private static async Task<CloudEvent> ToCloudEventInternalAsync(HttpHeaders head
142
143
}
143
144
else
144
145
{
145
- string ? versionId = MaybeGetVersionId ( headers ) ?? MaybeGetVersionId ( content . Headers ) ;
146
+ string ? versionId = MaybeGetVersionId ( headers ) ?? MaybeGetVersionId ( content ? . Headers ) ;
146
147
if ( versionId is null )
147
148
{
148
149
throw new ArgumentException ( $ "Request does not represent a CloudEvent. It has neither a { HttpUtilities . SpecVersionHttpHeader } header, nor a suitable content type.", nameof ( paramName ) ) ;
@@ -151,7 +152,8 @@ private static async Task<CloudEvent> ToCloudEventInternalAsync(HttpHeaders head
151
152
?? throw new ArgumentException ( $ "Unknown CloudEvents spec version '{ versionId } '", paramName ) ;
152
153
153
154
var cloudEvent = new CloudEvent ( version , extensionAttributes ) ;
154
- foreach ( var header in headers . Concat ( content . Headers ) )
155
+ var allHeaders = content is null ? headers : headers . Concat ( content . Headers ) ;
156
+ foreach ( var header in allHeaders )
155
157
{
156
158
string ? attributeName = HttpUtilities . GetAttributeNameFromHeaderName ( header . Key ) ;
157
159
if ( attributeName is null || attributeName == CloudEventsSpecVersion . SpecVersionAttribute . Name )
@@ -231,7 +233,7 @@ public static Task<IReadOnlyList<CloudEvent>> ToCloudEventBatchAsync(
231
233
return ToCloudEventBatchInternalAsync ( httpRequestMessage . Content , formatter , extensionAttributes , nameof ( httpRequestMessage ) ) ;
232
234
}
233
235
234
- private static async Task < IReadOnlyList < CloudEvent > > ToCloudEventBatchInternalAsync ( HttpContent content ,
236
+ private static async Task < IReadOnlyList < CloudEvent > > ToCloudEventBatchInternalAsync ( HttpContent ? content ,
235
237
CloudEventFormatter formatter , IEnumerable < CloudEventAttribute > ? extensionAttributes , string paramName )
236
238
{
237
239
Validation . CheckNotNull ( formatter , nameof ( formatter ) ) ;
@@ -331,21 +333,21 @@ public static HttpContent ToHttpContent(this IReadOnlyList<CloudEvent> cloudEven
331
333
}
332
334
333
335
private static ByteArrayContent ToByteArrayContent ( ReadOnlyMemory < byte > content ) =>
334
- MemoryMarshal . TryGetArray ( content , out var segment )
336
+ MemoryMarshal . TryGetArray ( content , out var segment ) && segment . Array is not null
335
337
? new ByteArrayContent ( segment . Array , segment . Offset , segment . Count )
336
338
// TODO: Just throw?
337
339
: new ByteArrayContent ( content . ToArray ( ) ) ;
338
340
339
341
// TODO: This would include "application/cloudeventsarerubbish" for example...
340
- private static bool HasCloudEventsContentType ( HttpContent content ) =>
342
+ private static bool HasCloudEventsContentType ( [ NotNullWhen ( true ) ] HttpContent ? content ) =>
341
343
MimeUtilities . IsCloudEventsContentType ( content ? . Headers ? . ContentType ? . MediaType ) ;
342
344
343
- private static bool HasCloudEventsBatchContentType ( HttpContent content ) =>
345
+ private static bool HasCloudEventsBatchContentType ( [ NotNullWhen ( true ) ] HttpContent ? content ) =>
344
346
MimeUtilities . IsCloudEventsBatchContentType ( content ? . Headers ? . ContentType ? . MediaType ) ;
345
347
346
348
private static string ? MaybeGetVersionId ( HttpHeaders ? headers ) =>
347
349
headers is not null && headers . Contains ( HttpUtilities . SpecVersionHttpHeader )
348
350
? headers . GetValues ( HttpUtilities . SpecVersionHttpHeader ) . First ( )
349
351
: null ;
350
352
}
351
- }
353
+ }
0 commit comments