Skip to content
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 “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

Freeze HttpFields #10339

Merged
merged 18 commits into from
Aug 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,13 @@ public Mutable add(HttpField field)
return this;
}

@Override
public Mutable clear()
{
_fields.clear();
return this;
}

@Override
public ListIterator<HttpField> listIterator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ public class HttpGenerator

private static final byte[] __colon_space = new byte[]{':', ' '};
public static final MetaData.Response CONTINUE_100_INFO = new MetaData.Response(100, null, HttpVersion.HTTP_1_1, HttpFields.EMPTY);
public static final MetaData.Response PROGRESS_102_INFO = new MetaData.Response(102, null, HttpVersion.HTTP_1_1, HttpFields.EMPTY);
public static final MetaData.Response RESPONSE_400_INFO =
new MetaData.Response(HttpStatus.BAD_REQUEST_400, null, HttpVersion.HTTP_1_1, HttpFields.build().add(HttpFields.CONNECTION_CLOSE), 0);
public static final MetaData.Response RESPONSE_500_INFO =
new MetaData.Response(INTERNAL_SERVER_ERROR_500, null, HttpVersion.HTTP_1_1, HttpFields.build().add(HttpFields.CONNECTION_CLOSE), 0);

// states
public enum State
Expand Down Expand Up @@ -808,12 +803,6 @@ public String toString()
private static final byte[] CONNECTION_CLOSE = StringUtil.getBytes("Connection: close\r\n");
private static final byte[] HTTP_1_1_SPACE = StringUtil.getBytes(HttpVersion.HTTP_1_1 + " ");
private static final byte[] TRANSFER_ENCODING_CHUNKED = StringUtil.getBytes("Transfer-Encoding: chunked\r\n");
private static final byte[][] SEND = new byte[][]{
new byte[0],
StringUtil.getBytes("Server: Jetty(12.x.x)\r\n"),
StringUtil.getBytes("X-Powered-By: Jetty(12.x.x)\r\n"),
StringUtil.getBytes("Server: Jetty(12.x.x)\r\nX-Powered-By: Jetty(12.x.x)\r\n")
};

// Build cache of response lines for status
private static class PreparedResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.http.PreEncodedHttpField;
import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ErrorHandler;
import org.eclipse.jetty.server.internal.ResponseHttpFields;
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.DecoratedObjectFactory;
Expand Down Expand Up @@ -474,6 +474,11 @@ public void setDumpBeforeStop(boolean dumpBeforeStop)
_dumpBeforeStop = dumpBeforeStop;
}

/**
* @return A {@link HttpField} instance efficiently recording the current time to a second resolution,
* that cannot be cleared from a {@link ResponseHttpFields} instance.
gregw marked this conversation as resolved.
Show resolved Hide resolved
* @see ResponseHttpFields.PersistentPreEncodedHttpField
*/
public HttpField getDateField()
{
long now = System.currentTimeMillis();
Expand All @@ -487,7 +492,7 @@ public HttpField getDateField()
df = _dateField;
if (df == null || df._seconds != seconds)
{
HttpField field = new PreEncodedHttpField(HttpHeader.DATE, DateGenerator.formatDate(now));
HttpField field = new ResponseHttpFields.PersistentPreEncodedHttpField(HttpHeader.DATE, DateGenerator.formatDate(now));
_dateField = new DateField(seconds, field);
return field;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.http.MultiPartFormData.Parts;
import org.eclipse.jetty.http.PreEncodedHttpField;
import org.eclipse.jetty.http.Trailers;
import org.eclipse.jetty.http.UriCompliance;
import org.eclipse.jetty.io.ByteBufferPool;
Expand Down Expand Up @@ -97,8 +96,8 @@ private enum StreamSendState

private static final Logger LOG = LoggerFactory.getLogger(HttpChannelState.class);
private static final Throwable DO_NOT_SEND = new Throwable("No Send");
private static final HttpField SERVER_VERSION = new PreEncodedHttpField(HttpHeader.SERVER, HttpConfiguration.SERVER_VERSION);
private static final HttpField POWERED_BY = new PreEncodedHttpField(HttpHeader.X_POWERED_BY, HttpConfiguration.SERVER_VERSION);
private static final HttpField SERVER_VERSION = new ResponseHttpFields.PersistentPreEncodedHttpField(HttpHeader.SERVER, HttpConfiguration.SERVER_VERSION);
private static final HttpField POWERED_BY = new ResponseHttpFields.PersistentPreEncodedHttpField(HttpHeader.X_POWERED_BY, HttpConfiguration.SERVER_VERSION);

private final AutoLock _lock = new AutoLock();
private final HandlerInvoker _handlerInvoker = new HandlerInvoker();
Expand Down Expand Up @@ -273,8 +272,8 @@ public Runnable onRequest(MetaData.Request request)
_request = new ChannelRequest(this, request);
_response = new ChannelResponse(_request);

HttpFields.Mutable responseHeaders = _response.getHeaders();
HttpConfiguration httpConfiguration = getHttpConfiguration();
HttpFields.Mutable responseHeaders = _response.getHeaders();
if (httpConfiguration.getSendServerVersion())
responseHeaders.add(SERVER_VERSION);
if (httpConfiguration.getSendXPoweredBy())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@

import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.PreEncodedHttpField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.eclipse.jetty.server.internal.ResponseHttpFields.PersistentField.isPersistent;

public class ResponseHttpFields implements HttpFields.Mutable
{
private static final Logger LOG = LoggerFactory.getLogger(ResponseHttpFields.class);
Expand Down Expand Up @@ -88,7 +92,17 @@ public HttpFields asImmutable()
@Override
public Mutable clear()
{
return _committed.get() ? this : _fields.clear();
if (!_committed.get())
{
// TODO iterate backwards when the list iterator of that form is available
for (Iterator<HttpField> iterator = _fields.iterator(); iterator.hasNext();)
{
HttpField field = iterator.next();
if (!PersistentField.isPersistent(field))
iterator.remove();
}
}
return this;
}

@Override
Expand All @@ -104,6 +118,8 @@ public Iterator<HttpField> iterator()
Iterator<HttpField> i = _fields.iterator();
return new Iterator<>()
{
HttpField _current;

@Override
public boolean hasNext()
{
Expand All @@ -113,15 +129,19 @@ public boolean hasNext()
@Override
public HttpField next()
{
return i.next();
_current = i.next();
return _current;
}

@Override
public void remove()
{
if (_committed.get())
throw new UnsupportedOperationException("Read Only");
if (isPersistent(_current))
throw new IllegalStateException("Persistent field");
Copy link
Contributor

Choose a reason for hiding this comment

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

My interpretation of the Iterator.remove() javadoc makes me think UnsupportedOperationException should be thrown here.

i.remove();
_current = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

You should also throw ISE when _current is null according to the Iterator.remove() javadoc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The i.remove() on the previous line will do that. But I will add an explicit check

}
};
}
Expand All @@ -132,6 +152,8 @@ public ListIterator<HttpField> listIterator()
ListIterator<HttpField> i = _fields.listIterator();
return new ListIterator<>()
{
HttpField _current;

@Override
public boolean hasNext()
{
Expand All @@ -141,7 +163,8 @@ public boolean hasNext()
@Override
public HttpField next()
{
return i.next();
_current = i.next();
return _current;
}

@Override
Expand All @@ -153,7 +176,8 @@ public boolean hasPrevious()
@Override
public HttpField previous()
{
return i.previous();
_current = i.previous();
return _current;
}

@Override
Expand All @@ -173,18 +197,34 @@ public void remove()
{
if (_committed.get())
throw new UnsupportedOperationException("Read Only");
if (isPersistent(_current))
throw new IllegalStateException("Persistent field");
gregw marked this conversation as resolved.
Show resolved Hide resolved
i.remove();
_current = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

You should also throw ISE when _current is null according to the Iterator.remove() javadoc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe the i.remove() call above will do that. I can explicitly do the check if you really want ?

}

@Override
public void set(HttpField field)
{
if (_committed.get())
throw new UnsupportedOperationException("Read Only");
if (isPersistent(_current))
{
// cannot change the field name
if (field == null || !field.isSameName(_current))
throw new IllegalStateException("Persistent field");

// new field must also be persistent
if (!isPersistent(field))
field = (field instanceof PreEncodedHttpField)
? new PersistentPreEncodedHttpField(field.getHeader(), field.getValue())
: new PersistentHttpField(field);
}
if (field == null)
i.remove();
else
i.set(field);
_current = field;
}

@Override
Expand All @@ -203,4 +243,59 @@ public String toString()
{
return _fields.toString();
}

/**
* A marker interface for {@link HttpField}s that cannot be {@link #remove(HttpHeader) removed} or {@link #clear() cleared}
* from a {@link ResponseHttpFields} instance. Persistent fields are not immutable in the {@link ResponseHttpFields}
gregw marked this conversation as resolved.
Show resolved Hide resolved
* and may be replaced with a different value.
*/
gregw marked this conversation as resolved.
Show resolved Hide resolved
public interface PersistentField
gregw marked this conversation as resolved.
Show resolved Hide resolved
{
static boolean isPersistent(HttpField field)
{
return field instanceof PersistentField;
}
}

/**
* A {@link HttpField} that is a {@link PersistentField}.
*/
public static class PersistentHttpField extends HttpField implements PersistentField
{
private final HttpField _field;

public PersistentHttpField(HttpField field)
{
super(field.getHeader(), field.getName(), field.getValue());
_field = field;
}

@Override
public int getIntValue()
{
return _field.getIntValue();
}

@Override
public long getLongValue()
{
return _field.getIntValue();
}
}

/**
* A {@link PreEncodedHttpField} that is a {@link PersistentField}.
*/
public static class PersistentPreEncodedHttpField extends PreEncodedHttpField implements PersistentField
{
public PersistentPreEncodedHttpField(HttpHeader header, String value)
{
this(header, value, true);
}

public PersistentPreEncodedHttpField(HttpHeader header, String value, boolean immutable)
{
super(header, value);
}
}
}
Loading