-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Initialize Headers Dictionary Only Once #4853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking &l 8000 dquo;Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@markekraus, |
dd128ba
to
ec28224
Compare
} | ||
|
||
return headers; | ||
} | ||
} | ||
|
||
private Dictionary<string, IEnumerable<string>> headers = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be _headers
? naming-conventions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks.
This code has potential reuse in InvokeRestMethodCommand. Moving it to WebResponseHelper
I realized in planning for #4845 the headers dictionary logic would need to be available outside of |
@markekraus Have we already tests? If so please add the comment in PR description. And about WebResponseHelper too. |
{ | ||
foreach (var entry in response.Content.Headers) | ||
{ | ||
headers[entry.Key] = entry.Value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, can there be duplicate headers in response.Headers
and response.Content.Headers
? Have second ones a high ptiority?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. The headers in Response.Content.Headers
will never exist in response.Headers
. #4494 added this to address the lack of Content-Type
in the WebResponseObject.Headers
. I should also say that HttpResponseMessage
has it's own conflict resolution for the headers, so we shouldn't have to do anything here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clarify - my understanding from #4494 is that headers
already contains response.Content.Headers
, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was added in #4494. Previous to that, WebResponseObject.Headers
did not contain any of the headers in HttpResponseMessage.Content.Headers
. CoreFX has split the response headers so that those related to content are always in HttpResponseMessage.Content.Headers
and the rest of the response headers are in HttpResponseMessage.Headers
.
To clarify, I'm not adding or changing any of the logic in this PR for the creation of the Dictionary. Just moving it so that a new dictionary is not created on every Get to Headers
and to make the logic available outside of WebResponseObject
. The existing logic is there on purpose and we have existing tests to ensure it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarify.
So foreach (var entry in response.Content.Headers)
is duplicate code and could we remove it?
Sorry for late question - if CoreFX split the headers why we join them again? Only for backward compatibility?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are joining them for backwards compat. In 5.1 and earlier all headers are in WebResponseObject.Headers
. In 6.0 the content headers are buried in WebResponseObject.BaseResponse.Content.Headers
unless we promote them to the WebResponseObject.Headers
dictionary. One would reasonably expect the Headers
property to contain all headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@markekraus can you please add a comment to the code to clarify that there won't be duplicate headers in response.Headers
and response.Content.Headers
? It would be very helpful to other people who look at the code later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daxian-dbw should I also include a note about the content headers being added to the headers dictionary for backwards compatibility? or is the note about the distinct headers sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would also be helpful to mention the backward compatibility issue. Thanks @markekraus!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments added.
@iSazonov I have updated the PR description RE: Testing and move to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
@markekraus Thanks for the fix! |
Switch the
WebResponseObject.Headers
Dictionary to initialize once instead of creating a new dictionary on every get. Moved logic toWebResponseHelper.GetHeadersDictionary()
as this code will be reused outside ofWebResponseObject
.Closes #3842
This changes internal functionality only. Existing tests for the
Headers
property are sufficient.