Skip to content

Commit 2a13b2a

Browse files
viretpPierre Viretrobaho
authored
fix issue robaho#17 - duplicate header count more than 2 causes exception (robaho#18)
* Fix a bug if multiple headers have the same name * make slightly more efficient to avoid array allocation (most likely). change instanceof cases to use pattern matching for consistency --------- Co-authored-by: Pierre Viret <pierre.viret@postfinance.ch> Co-authored-by: robert engels <robaho@users.noreply.github.com>
1 parent 74f1a4a commit 2a13b2a

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/main/java/robaho/net/httpserver/OptimizedHeaders.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package robaho.net.httpserver;
22

33
import java.util.AbstractMap;
4+
import java.util.ArrayList;
45
import java.util.Arrays;
56
import java.util.Collection;
67
import java.util.HashSet;
@@ -35,25 +36,25 @@ public boolean isEmpty() {
3536
@Override
3637
public List<String> get(Object key) {
3738
Object o = map.get(normalize((String)key));
38-
return o == null ? null : (o instanceof String) ? Arrays.asList((String)o) : (List<String>)o;
39+
return o == null ? null : (o instanceof String s) ? List.of(s) : (List<String>)o;
3940
}
4041

4142
@Override
4243
public List<String> put(String key, List<String> value) {
4344
Object o = map.put(normalize(key), value);
44-
return o == null ? null : (o instanceof String) ? Arrays.asList((String)o) : (List<String>)o;
45+
return o == null ? null : (o instanceof String s) ? List.of(s) : (List<String>)o;
4546
}
4647

4748
@Override
4849
public List<String> remove(Object key) {
4950
Object o = map.put(normalize((String)key),null);
50-
return o == null ? null : (o instanceof String) ? Arrays.asList((String)o) : (List<String>)o;
51+
return o == null ? null : (o instanceof String s) ? List.of(s) : (List<String>)o;
5152
}
5253

5354
@Override
5455
public String getFirst(String key) {
5556
Object o = map.get(normalize(key));
56-
return o == null ? null : (o instanceof String) ? (String)o : ((List<String>)o).getFirst();
57+
return o == null ? null : (o instanceof String s) ? s : ((List<String>)o).getFirst();
5758
}
5859

5960
/**
@@ -91,8 +92,8 @@ public void add(String key, String value) {
9192
Object o = map.get(normalized);
9293
if (o == null) {
9394
map.put(normalized, value);
94-
} else if(o instanceof String) {
95-
map.put(normalized, Arrays.asList((String)o,value));
95+
} else if(o instanceof String s) {
96+
map.put(normalized, new ArrayList(List.of(s,value)));
9697
} else {
9798
((List<String>)o).add(value);
9899
}
@@ -146,6 +147,6 @@ public int hashCode() {
146147

147148
@Override
148149
public void forEach(BiConsumer<? super String,? super List<String>> action) {
149-
map.forEach((k,v) -> action.accept(k, (v instanceof String) ? List.of((String)v) : (List<String>)v));
150+
map.forEach((k,v) -> action.accept(k, (v instanceof String s) ? List.of(s) : (List<String>)v));
150151
}
151152
}

src/test/java/robaho/net/httpserver/RequestHeadersTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.ByteArrayInputStream;
44
import java.io.ByteArrayOutputStream;
55
import java.io.IOException;
6+
import java.util.List;
67

78
import static org.testng.Assert.assertEquals;
89
import static org.testng.Assert.assertTrue;
@@ -63,4 +64,14 @@ public void TestWhitespace() throws IOException {
6364
assertEquals(r.headers().getFirst("KEY2"),"VAL2");
6465
}
6566

67+
@Test
68+
public void TestDuplicateHeaders() throws IOException {
69+
String request = "GET blah\r\nKEY : VAL\r\nKEY:VAL2\r\nKEY:VAL3 \r\n\r\nSome Body Data";
70+
var is = new ByteArrayInputStream(request.getBytes());
71+
var os = new ByteArrayOutputStream();
72+
73+
Request r = new Request(is,os);
74+
assertTrue("GET blah".contentEquals(r.requestLine()));
75+
assertEquals(r.headers().get("KEY"), List.of("VAL", "VAL2", "VAL3"));
76+
}
6677
}

0 commit comments

Comments
 (0)