Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Add IHttpResponseTrailersFeature and extensions #1043

Merged
merged 1 commit into from
Oct 5, 2018

Conversation

Tratcher
Copy link
Member

@Tratcher Tratcher commented Oct 3, 2018

@Tratcher Tratcher added this to the 2.2.0-preview3 milestone Oct 3, 2018
@Tratcher Tratcher self-assigned this Oct 3, 2018
@Tratcher Tratcher requested a review from halter73 October 3, 2018 21:53
public static void AppendTrailer(this HttpResponse response, string trailerName, StringValues trailerValues)
{
var feature = response.HttpContext.Features.Get<IHttpResponseTrailersFeature>();
feature?.Trailers?.Append(trailerName, trailerValues);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for null when you're already checking for the feature existence seems like it might be a little too defensive. Technically response.Headers could be set to null too, but there's no check in DeclareTrailer.

Is the idea that the server might set the feature but leave the IHeaderDictionary null? Or are you just defending against some middleware setting Trailers to null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just paranoia. We make a lot of assumptions about the request and response features because nothing would work without them, but for these very optional features I try not to assume anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it weird that this can noop? Should it throw if trailers aren't supported?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it threw you'd need some other way to check for support before calling it. Checking for the presence of the feature is the simplest, but negates using an extension like this. I could add another extension here SupportsTrailers to check for the feature.

We also discussed some Http/1.1 scenarios where the feature may be present but the headers may be discarded with a log message (e.g. content-length)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err hmm, I'm not sure if I like that, it's very similar to adding headers after the response body has started. Today we throw. I like that model.

JunTaoLuo
JunTaoLuo previously approved these changes Oct 4, 2018
@JunTaoLuo JunTaoLuo dismissed their stale review October 4, 2018 18:28

Had one question

@jtattermusch
Copy link

CC @jtattermusch

@Tratcher
Copy link
Member Author

Tratcher commented Oct 5, 2018

Updated to throw if Trailers can't be added.

var feature = response.HttpContext.Features.Get<IHttpResponseTrailersFeature>();
if (feature?.Trailers == null || feature.Trailers.IsReadOnly)
{
throw new InvalidOperationException("Trailers are not supported for this response.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could potentially consider adding a test for this.

@Tratcher Tratcher force-pushed the feature/tratcher/trailers branch from 4007f24 to 89b0430 Compare October 5, 2018 18:47
@Tratcher Tratcher merged commit 89b0430 into release/2.2 Oct 5, 2018
@Tratcher Tratcher deleted the feature/tratcher/trailers branch October 5, 2018 18:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants