Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions 15 src/main/java/robaho/net/httpserver/OptimizedHeaders.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package robaho.net.httpserver;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -35,25 +36,25 @@ public boolean isEmpty() {
@Override
public List<String> get(Object key) {
Object o = map.get(normalize((String)key));
return o == null ? null : (o instanceof String) ? Arrays.asList((String)o) : (List<String>)o;
return o == null ? null : (o instanceof String s) ? List.of(s) : (List<String>)o;
}

@Override
public List<String> put(String key, List<String> value) {
Object o = map.put(normalize(key), value);
return o == null ? null : (o instanceof String) ? Arrays.asList((String)o) : (List<String>)o;
return o == null ? null : (o instanceof String s) ? List.of(s) : (List<String>)o;
}

@Override
public List<String> remove(Object key) {
Object o = map.put(normalize((String)key),null);
return o == null ? null : (o instanceof String) ? Arrays.asList((String)o) : (List<String>)o;
return o == null ? null : (o instanceof String s) ? List.of(s) : (List<String>)o;
}

@Override
public String getFirst(String key) {
Object o = map.get(normalize(key));
return o == null ? null : (o instanceof String) ? (String)o : ((List<String>)o).getFirst();
return o == null ? null : (o instanceof String s) ? s : ((List<String>)o).getFirst();
}

/**
Expand Down Expand Up @@ -91,8 +92,8 @@ public void add(String key, String value) {
Object o = map.get(normalized);
if (o == null) {
map.put(normalized, value);
} else if(o instanceof String) {
map.put(normalized, Arrays.asList((String)o,value));
} else if(o instanceof String s) {
map.put(normalized, new ArrayList(List.of(s,value)));
} else {
((List<String>)o).add(value);
}
Expand Down Expand Up @@ -146,6 +147,6 @@ public int hashCode() {

@Override
public void forEach(BiConsumer<? super String,? super List<String>> action) {
map.forEach((k,v) -> action.accept(k, (v instanceof String) ? List.of((String)v) : (List<String>)v));
map.forEach((k,v) -> action.accept(k, (v instanceof String s) ? List.of(s) : (List<String>)v));
}
}
11 changes: 11 additions & 0 deletions 11 src/test/java/robaho/net/httpserver/RequestHeadersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

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

@Test
public void TestDuplicateHeaders() throws IOException {
String request = "GET blah\r\nKEY : VAL\r\nKEY:VAL2\r\nKEY:VAL3 \r\n\r\nSome Body Data";
var is = new ByteArrayInputStream(request.getBytes());
var os = new ByteArrayOutputStream();

Request r = new Request(is,os);
assertTrue("GET blah".contentEquals(r.requestLine()));
assertEquals(r.headers().get("KEY"), List.of("VAL", "VAL2", "VAL3"));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.