Skip to content

Commit

Permalink
#8885 add EventsHandler API
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <[email protected]>
  • Loading branch information
lorban committed Jun 12, 2023
1 parent 601bd68 commit ef7e4b6
Show file tree
Hide file tree
Showing 76 changed files with 1,446 additions and 470 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.jetty.http2.client.HTTP2Client;
import org.eclipse.jetty.http2.frames.HeadersFrame;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.NanoTime;

@SuppressWarnings("unused")
public class HTTP2Docs
Expand All @@ -44,7 +45,7 @@ public void dataDemanded() throws Exception

HttpFields requestHeaders = HttpFields.build()
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders, NanoTime.now());
HeadersFrame headersFrame = new HeadersFrame(request, null, true);

// tag::dataUnwrap[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.jetty.http2.frames.SettingsFrame;
import org.eclipse.jetty.io.ClientConnector;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.NanoTime;

import static java.lang.System.Logger.Level.INFO;

Expand Down Expand Up @@ -146,7 +147,7 @@ public void newStream() throws Exception
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");

// The request metadata with method, URI and headers.
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders, NanoTime.now());

// The HTTP/2 HEADERS frame, with endStream=true
// to signal that this request has no content.
Expand All @@ -171,7 +172,7 @@ public void newStreamWithData() throws Exception
.put(HttpHeader.CONTENT_TYPE, "application/json");

// The request metadata with method, URI and headers.
MetaData.Request request = new MetaData.Request("POST", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
MetaData.Request request = new MetaData.Request("POST", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders, NanoTime.now());

// The HTTP/2 HEADERS frame, with endStream=false to
// signal that there will be more frames in this stream.
Expand Down Expand Up @@ -210,7 +211,7 @@ public void responseListener() throws Exception

HttpFields requestHeaders = HttpFields.build()
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders, NanoTime.now());
HeadersFrame headersFrame = new HeadersFrame(request, null, true);

// tag::responseListener[]
Expand Down Expand Up @@ -282,7 +283,7 @@ public void reset() throws Exception

HttpFields requestHeaders = HttpFields.build()
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders, NanoTime.now());
HeadersFrame headersFrame = new HeadersFrame(request, null, true);

// tag::reset[]
Expand Down Expand Up @@ -315,7 +316,7 @@ public void push() throws Exception

HttpFields requestHeaders = HttpFields.build()
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders, NanoTime.now());
HeadersFrame headersFrame = new HeadersFrame(request, null, true);

// tag::push[]
Expand Down Expand Up @@ -394,7 +395,7 @@ public void pushReset() throws Exception

HttpFields requestHeaders = HttpFields.build()
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders, NanoTime.now());
HeadersFrame headersFrame = new HeadersFrame(request, null, true);

// tag::pushReset[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.jetty.http3.client.HTTP3Client;
import org.eclipse.jetty.http3.frames.DataFrame;
import org.eclipse.jetty.http3.frames.HeadersFrame;
import org.eclipse.jetty.util.NanoTime;

import static java.lang.System.Logger.Level.INFO;

Expand Down Expand Up @@ -117,7 +118,7 @@ public void newStream() throws Exception
.put(HttpHeader.USER_AGENT, "Jetty HTTP3Client {version}");

// The request metadata with method, URI and headers.
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8444/path"), HttpVersion.HTTP_3, requestHeaders);
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8444/path"), HttpVersion.HTTP_3, requestHeaders, NanoTime.now());

// The HTTP/3 HEADERS frame, with endStream=true
// to signal that this request has no content.
Expand All @@ -142,7 +143,7 @@ public void newStreamWithData() throws Exception
.put(HttpHeader.CONTENT_TYPE, "application/json");

// The request metadata with method, URI and headers.
MetaData.Request request = new MetaData.Request("POST", HttpURI.from("http://localhost:8444/path"), HttpVersion.HTTP_3, requestHeaders);
MetaData.Request request = new MetaData.Request("POST", HttpURI.from("http://localhost:8444/path"), HttpVersion.HTTP_3, requestHeaders, NanoTime.now());

// The HTTP/3 HEADERS frame, with endStream=false to
// signal that there will be more frames in this stream.
Expand Down Expand Up @@ -181,7 +182,7 @@ public void responseListener() throws Exception

HttpFields requestHeaders = HttpFields.build()
.put(HttpHeader.USER_AGENT, "Jetty HTTP3Client {version}");
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8444/path"), HttpVersion.HTTP_3, requestHeaders);
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8444/path"), HttpVersion.HTTP_3, requestHeaders, NanoTime.now());
HeadersFrame headersFrame = new HeadersFrame(request, true);

// tag::responseListener[]
Expand Down Expand Up @@ -239,7 +240,7 @@ public void reset() throws Exception

HttpFields requestHeaders = HttpFields.build()
.put(HttpHeader.USER_AGENT, "Jetty HTTP3Client {version}");
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders, NanoTime.now());
HeadersFrame headersFrame = new HeadersFrame(request, true);

// tag::reset[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.NanoTime;
import org.eclipse.jetty.util.resource.ResourceFactory;

import static java.lang.System.Logger.Level.INFO;
Expand Down Expand Up @@ -326,7 +327,7 @@ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame)
{
// Push the favicon.
HttpURI pushedURI = HttpURI.build(request.getHttpURI()).path("/favicon.ico");
MetaData.Request pushedRequest = new MetaData.Request("GET", pushedURI, HttpVersion.HTTP_2, HttpFields.EMPTY);
MetaData.Request pushedRequest = new MetaData.Request("GET", pushedURI, HttpVersion.HTTP_2, HttpFields.EMPTY, NanoTime.now());
PushPromiseFrame promiseFrame = new PushPromiseFrame(stream.getId(), 0, pushedRequest);
stream.push(promiseFrame, null)
.thenCompose(pushedStream ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.jetty.http2.frames.HeadersFrame;
import org.eclipse.jetty.util.FuturePromise;
import org.eclipse.jetty.util.Jetty;
import org.eclipse.jetty.util.NanoTime;
import org.eclipse.jetty.util.Promise;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.jupiter.api.Assumptions;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void testConscryptHTTP2Client() throws Exception
Session session = sessionPromise.get(15, TimeUnit.SECONDS);

HttpFields requestFields = HttpFields.build().put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
MetaData.Request metaData = new MetaData.Request("GET", HttpURI.from("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
MetaData.Request metaData = new MetaData.Request("GET", HttpURI.from("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields, NanoTime.now());
HeadersFrame headersFrame = new HeadersFrame(metaData, null, true);
CountDownLatch latch = new CountDownLatch(1);
session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testJDK9HTTP2Client() throws Exception

HttpFields.Mutable requestFields = HttpFields.build();
requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
MetaData.Request metaData = new MetaData.Request("GET", HttpURI.from("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
MetaData.Request metaData = new MetaData.Request("GET", HttpURI.from("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields, 0);
HeadersFrame headersFrame = new HeadersFrame(metaData, null, true);
CountDownLatch latch = new CountDownLatch(1);
session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.IteratingCallback;
import org.eclipse.jetty.util.NanoTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -74,7 +75,7 @@ protected void sendHeaders(HttpExchange exchange, ByteBuffer contentBuffer, bool
String query = request.getQuery();
if (query != null)
path += "?" + query;
metaData = new MetaData.Request(request.getMethod(), HttpURI.from(path), request.getVersion(), request.getHeaders(), contentLength, request.getTrailersSupplier());
metaData = new MetaData.Request(request.getMethod(), HttpURI.from(path), request.getVersion(), request.getHeaders(), contentLength, request.getTrailersSupplier(), NanoTime.now());
if (LOG.isDebugEnabled())
LOG.debug("Sending headers with content {} last={} for {}", BufferUtil.toDetailString(contentBuffer), lastContent, exchange.getRequest());
headersCallback.iterate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class HttpStreamOverFCGI implements HttpStream
private final ServerGenerator _generator;
private final HttpChannel _httpChannel;
private final int _id;
private final long _nanoTime;
private String _method;
private HostPortHttpField hostPort;
private String _path;
Expand All @@ -70,7 +69,6 @@ public HttpStreamOverFCGI(ServerFCGIConnection connection, ServerGenerator gener
_generator = generator;
_httpChannel = httpChannel;
_id = id;
_nanoTime = NanoTime.now();
}

public HttpChannel getHttpChannel()
Expand All @@ -84,12 +82,6 @@ public String getId()
return String.valueOf(_id);
}

@Override
public long getNanoTime()
{
return _nanoTime;
}

public void onHeader(HttpField field)
{
String name = field.getName();
Expand All @@ -113,7 +105,7 @@ public void onHeaders()
{
String pathQuery = URIUtil.addPathQuery(_path, _query);
HttpScheme scheme = StringUtil.isEmpty(_secure) ? HttpScheme.HTTP : HttpScheme.HTTPS;
MetaData.Request request = new MetaData.Request(_method, scheme.asString(), hostPort, pathQuery, HttpVersion.fromString(_version), _headers, -1);
MetaData.Request request = new MetaData.Request(_method, scheme.asString(), hostPort, pathQuery, HttpVersion.fromString(_version), _headers, -1, NanoTime.now()); // TODO #9900 make beginNanoTime accurate
Runnable task = _httpChannel.onRequest(request);
_allHeaders.forEach(field -> _httpChannel.getRequest().setAttribute(field.getName(), field.getValue()));
// TODO: here we just execute the task.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.HostPort;
import org.eclipse.jetty.util.Index;
import org.eclipse.jetty.util.NanoTime;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.slf4j.Logger;
Expand Down Expand Up @@ -235,6 +236,7 @@ public enum State
private final FieldCache _fieldCache = new FieldCache();
private HttpField _field;
private HttpHeader _header;
private long _startNanoTime;
private String _headerString;
private String _valueString;
private int _responseStatus;
Expand Down Expand Up @@ -310,6 +312,11 @@ private HttpParser(RequestHandler requestHandler, ResponseHandler responseHandle
_complianceListener = (ComplianceViolation.Listener)(_handler instanceof ComplianceViolation.Listener ? _handler : null);
}

public long getStartNanoTime()
{
return _startNanoTime;
}

public HttpHandler getHandler()
{
return _handler;
Expand Down Expand Up @@ -1519,6 +1526,8 @@ public boolean parseNext(ByteBuffer buffer)
_methodString = null;
_endOfContent = EndOfContent.UNKNOWN_CONTENT;
_header = null;
if (buffer.hasRemaining())
_startNanoTime = NanoTime.now();
if (quickStart(buffer))
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.charset.StandardCharsets;

import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.NanoTime;
import org.eclipse.jetty.util.StringUtil;

/**
Expand Down Expand Up @@ -567,7 +568,7 @@ public void setURI(String uri)
@Override
public MetaData.Request getMetaData()
{
return new MetaData.Request(_method, HttpURI.from(_uri), _version, this, _content == null ? 0 : _content.size());
return new MetaData.Request(_method, HttpURI.from(_uri), _version, this, _content == null ? 0 : _content.size(), NanoTime.now());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,31 @@ public static class Request extends MetaData
{
private final String _method;
private final HttpURI _uri;
private final long _beginNanoTime;

public Request(String method, HttpURI uri, HttpVersion version, HttpFields headers)
public Request(String method, HttpURI uri, HttpVersion version, HttpFields headers, long beginNanoTime)
{
this(method, uri, version, headers, -1);
this(method, uri, version, headers, -1, beginNanoTime);
}

public Request(String method, HttpURI uri, HttpVersion version, HttpFields headers, long contentLength)
public Request(String method, HttpURI uri, HttpVersion version, HttpFields headers, long contentLength, long beginNanoTime)
{
this(method, uri, version, headers, contentLength, null);
this(method, uri, version, headers, contentLength, null, beginNanoTime);
}

public Request(String method, String scheme, HostPortHttpField authority, String uri, HttpVersion version, HttpFields headers, long contentLength)
public Request(String method, String scheme, HostPortHttpField authority, String uri, HttpVersion version, HttpFields headers, long contentLength, long beginNanoTime)
{
this(method,
HttpURI.build().scheme(scheme).host(authority == null ? null : authority.getHost()).port(authority == null ? -1 : authority.getPort()).pathQuery(uri),
version, headers, contentLength);
version, headers, contentLength, beginNanoTime);
}

public Request(String method, HttpURI uri, HttpVersion version, HttpFields headers, long contentLength, Supplier<HttpFields> trailers)
public Request(String method, HttpURI uri, HttpVersion version, HttpFields headers, long contentLength, Supplier<HttpFields> trailers, long beginNanoTime)
{
super(version, headers, contentLength, trailers);
_method = Objects.requireNonNull(method);
_uri = Objects.requireNonNull(uri);
_beginNanoTime = beginNanoTime;
}

@Override
Expand All @@ -159,6 +161,11 @@ public boolean isRequest()
return true;
}

public long getBeginNanoTime()
{
return _beginNanoTime;
}

/**
* @return the HTTP method
*/
Expand Down Expand Up @@ -199,16 +206,16 @@ public static class ConnectRequest extends Request
{
private final String _protocol;

public ConnectRequest(HttpScheme scheme, HostPortHttpField authority, String path, HttpFields headers, String protocol)
public ConnectRequest(HttpScheme scheme, HostPortHttpField authority, String path, HttpFields headers, String protocol, long beginNanoTime)
{
this(scheme == null ? null : scheme.asString(), authority, path, headers, protocol);
this(scheme == null ? null : scheme.asString(), authority, path, headers, protocol, beginNanoTime);
}

public ConnectRequest(String scheme, HostPortHttpField authority, String path, HttpFields headers, String protocol)
public ConnectRequest(String scheme, HostPortHttpField authority, String path, HttpFields headers, String protocol, long beginNanoTime)
{
super(HttpMethod.CONNECT.asString(),
HttpURI.build().scheme(scheme).host(authority == null ? null : authority.getHost()).port(authority == null ? -1 : authority.getPort()).pathQuery(path),
HttpVersion.HTTP_2, headers, -1, null);
HttpVersion.HTTP_2, headers, -1, null, beginNanoTime);
_protocol = protocol;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.nio.ByteBuffer;

import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.NanoTime;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

Expand All @@ -32,17 +33,17 @@ class RequestInfo extends MetaData.Request
{
RequestInfo(String method, String uri, HttpFields fields)
{
super(method, HttpURI.from(method, uri), HttpVersion.HTTP_1_1, fields);
super(method, HttpURI.from(method, uri), HttpVersion.HTTP_1_1, fields, NanoTime.now());
}

RequestInfo(String method, String uri, HttpVersion version, HttpFields fields)
{
super(method, HttpURI.from(method, uri), version, fields);
super(method, HttpURI.from(method, uri), version, fields, NanoTime.now());
}

RequestInfo(String method, String uri, int contentLength, HttpFields fields)
{
super(method, HttpURI.from(method, uri), HttpVersion.HTTP_1_1, fields, contentLength);
super(method, HttpURI.from(method, uri), HttpVersion.HTTP_1_1, fields, contentLength, NanoTime.now());
}
}

Expand Down
Loading

0 comments on commit ef7e4b6

Please sign in to comment.