fix WebSocket HttpFieldsWrapper

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2022-05-05 08:59:29 +10:00
parent 1c1847be6f
commit 5fb45f7273
3 changed files with 116 additions and 66 deletions

View File

@ -13,10 +13,14 @@
package org.eclipse.jetty.websocket.core.server.internal; package org.eclipse.jetty.websocket.core.server.internal;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.ListIterator; import java.util.ListIterator;
import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
public class HttpFieldsWrapper implements HttpFields.Mutable public class HttpFieldsWrapper implements HttpFields.Mutable
{ {
@ -27,8 +31,6 @@ public class HttpFieldsWrapper implements HttpFields.Mutable
_fields = fields; _fields = fields;
} }
// TODO a signature that took HttpField would be better.
// TODO Do we need Put? Could it just be done as a onRemoveField then an onAddField?
public boolean onPutField(String name, String value) public boolean onPutField(String name, String value)
{ {
return true; return true;
@ -44,72 +46,121 @@ public class HttpFieldsWrapper implements HttpFields.Mutable
return true; return true;
} }
@Override
public Mutable add(String name, String value)
{
if (onAddField(name, value))
return _fields.add(name, value);
return this;
}
@Override
public Mutable add(HttpHeader header, HttpHeaderValue value)
{
if (onAddField(header.name(), value.asString()))
return _fields.add(header, value);
return this;
}
@Override
public Mutable add(HttpHeader header, String value)
{
if (onAddField(header.name(), value))
return _fields.add(header, value);
return this;
}
@Override
public Mutable add(HttpField field)
{
if (onAddField(field.getName(), field.getValue()))
return _fields.add(field);
return this;
}
@Override
public Mutable add(HttpFields fields)
{
for (HttpField field : fields)
{
add(field);
}
return this;
}
@Override
public Mutable clear()
{
return _fields.clear();
}
@Override
public Iterator<HttpField> iterator()
{
return _fields.iterator();
}
@Override @Override
public ListIterator<HttpField> listIterator() public ListIterator<HttpField> listIterator()
{ {
return new ListIterator<>() return _fields.listIterator();
{
final ListIterator<HttpField> _list = _fields.listIterator();
HttpField _last;
@Override
public boolean hasNext()
{
return _list.hasNext();
} }
@Override @Override
public HttpField next() public Mutable put(HttpField field)
{ {
return _last = _list.next(); if (onPutField(field.getName(), field.getValue()))
return _fields.put(field);
return this;
} }
@Override @Override
public boolean hasPrevious() public Mutable put(String name, String value)
{ {
return _list.hasPrevious(); if (onPutField(name, value))
return _fields.put(name, value);
return this;
} }
@Override @Override
public HttpField previous() public Mutable put(HttpHeader header, HttpHeaderValue value)
{ {
return _last = _list.previous(); if (onPutField(header.name(), value.asString()))
return _fields.put(header, value);
return this;
} }
@Override @Override
public int nextIndex() public Mutable put(HttpHeader header, String value)
{ {
return _list.nextIndex(); if (onPutField(header.name(), value))
return _fields.put(header, value);
return this;
} }
@Override @Override
public int previousIndex() public Mutable remove(HttpHeader header)
{ {
return _list.previousIndex(); if (onRemoveField(header.name()))
return _fields.remove(header);
return this;
} }
@Override @Override
public void remove() public Mutable remove(EnumSet<HttpHeader> fields)
{ {
if (_last != null && HttpFieldsWrapper.this.onRemoveField(_last.getName())) for (HttpHeader header : fields)
_list.remove(); {
remove(header);
}
return this;
} }
@Override @Override
public void set(HttpField httpField) public Mutable remove(String name)
{ {
if (_last != null && HttpFieldsWrapper.this.onPutField(_last.getName(), _last.getValue())) if (onRemoveField(name))
_list.set(httpField); return _fields.remove(name);
} return this;
@Override
public void add(HttpField httpField)
{
// TODO: we don't know if this resulted from a put or an add.
// if (httpField != null && HttpFieldsWrapper.this.onAddField(httpField.getName(), httpField.getValue()))
if (_last != null && HttpFieldsWrapper.this.onAddField(_last.getName(), _last.getValue()))
_list.add(httpField);
}
};
} }
} }

View File

@ -79,6 +79,7 @@ public class WebSocketHttpFieldsWrapper extends HttpFieldsWrapper
if (HttpHeader.SEC_WEBSOCKET_EXTENSIONS.is(name)) if (HttpHeader.SEC_WEBSOCKET_EXTENSIONS.is(name))
{ {
// TODO: why add extensions??
_response.addExtensions(Collections.emptyList()); _response.addExtensions(Collections.emptyList());
return false; return false;
} }

View File

@ -34,7 +34,6 @@ import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.internal.HttpChannelState; import org.eclipse.jetty.server.internal.HttpChannelState;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
@ -112,7 +111,6 @@ public class JettyWebSocketNegotiationTest
} }
} }
@Disabled
@Test @Test
public void testManualNegotiationInCreator() throws Exception public void testManualNegotiationInCreator() throws Exception
{ {