Skip to content

HttpListenerRequest.UserAgent should be attributed as nullable #93033

@wsheemsft

Description

@wsheemsft

Description

HttpListenerRequest.UserAgent has a return type of string but it returns null if the request does not have a User-Agent header. Other public properties, such as ContentType and UrlReferrer are correctly attributed.

UserAgent uses the null-forgiving operator when reading the value:

public string UserAgent => Headers[HttpKnownHeaderNames.UserAgent]!;

The same file also checks it for null:

if (UserAgent != null && CultureInfo.InvariantCulture.CompareInfo.IsPrefix(UserAgent, "UP"))

Can UserAgent return string? instead of string? It matches the behavior both internally (above) and externally (requests missing User-Agent header).

It's especially confusing because other properties are attributed.

Reproduction Steps

Create an HTTP listener and send it a request without a User-Agent header. The UserAgent property will be null even though the return type is not attributed as such.

Expected behavior

UserAgent property is attributed as nullable because it can easily be null.

Actual behavior

UserAgent property is not attributed as nullable.

Regression?

N/A. I think it was overlooked during #41189

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions