8000 Initialize Headers Dictionary Only Once by markekraus · Pull Request #4853 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 4 commits into from
Sep 21, 2017

Conversation

markekraus
Copy link
Contributor
@markekraus markekraus commented Sep 16, 2017

Switch the WebResponseObject.Headers Dictionary to initialize once instead of creating a new dictionary on every get. Moved logic to WebResponseHelper.GetHeadersDictionary() as this code will be reused outside of WebResponseObject.

Closes #3842

This changes internal functionality only. Existing tests for the Headers property are sufficient.

@msftclas
Copy link

@markekraus,
Thanks for having 8000 already signed the Contribution License Agreement. Your agreement was validated by Microsoft. We will now review your pull request.
Thanks,
Microsoft Pull Request Bot

}

return headers;
}
}

private Dictionary<string, IEnumerable<string>> headers = null;
Copy link
Collaborator

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

8000 Copy link
Contributor Author

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
@markekraus
Copy link
Contributor Author

I realized in planning for #4845 the headers dictionary logic would need to be available outside of WebResponseObject so I have moved it to WebResponseHelper.

@iSazonov
Copy link
Collaborator

@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;
Copy link
Collaborator

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?

Copy link
Contributor Author
@markekraus markekraus Sep 18, 2017

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.

Copy link
Collaborator

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?

Copy link
Contributor Author
@markekraus markekraus Sep 18, 2017

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.

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comments added.

@markekraus
Copy link
Contributor Author

@iSazonov I have updated the PR description RE: Testing and move to WebResponseHelper.

Copy link
Member
@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@iSazonov iSazonov merged commit 5e24352 into PowerShell:master Sep 21, 2017
@iSazonov
Copy link
Collaborator

@markekraus Thanks for the fix!

@markekraus markekraus deleted the HeadersDictionary branch September 22, 2017 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0