Skip to content

HttpHeaders.with should replace existing header #108

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Nyquest
Copy link
Contributor

@Nyquest Nyquest commented Mar 28, 2023

The current implementation of the method HttpHeaders.with causes undefined behavior or an error when adding an existing header, depending on the case of the header name.

var i = 0;
for (var stringListEntry : this) {
entries[i++] = stringListEntry;
Map<String, List<String>> newValues = new HashMap<>(this.size());
Copy link
Contributor

Choose a reason for hiding this comment

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

new HashMap<>(this.size()) не является корректной initialCapacity, можно подсмотреть реализацию у Maps.newHashMapWithExpectedSize либо оставить массив Map.Entry, просто проверять через equals в цикле stringListEntry.getKey() с key из входных параметров

Copy link
Contributor Author

Choose a reason for hiding this comment

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

а если так переписать:

Map<String, List<String>> newValues = new HashMap<>(this.size() + 1)

Copy link
Contributor

@slutmaker slutmaker Apr 25, 2023

Choose a reason for hiding this comment

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

Лучше оставить вариант с entries и доработать проверку по equals, он будет оптимальней

entries[i++] = stringListEntry;
Map<String, List<String>> newValues = new HashMap<>(this.size());
for (var entry : this) {
newValues.put(entry.getKey().toLowerCase(), entry.getValue());
Copy link
Contributor

Choose a reason for hiding this comment

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

lowerCase здесь бтв излишен

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.

None yet

2 participants