Skip to content

Commit dc6c0fc

Browse files
committed
Added HTTPContext to HTTPRequest (and all intermediate classes as necessary).
1 parent 4f86776 commit dc6c0fc

5 files changed

Lines changed: 25 additions & 10 deletions

File tree

project.latte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ restifyVersion = "4.2.1"
77
slf4jVersion = "2.0.17"
88
testngVersion = "7.11.0"
99

10-
project(group: "org.lattejava", name: "http", version: "0.1.3", licenses: ["MIT"]) {
10+
project(group: "org.lattejava", name: "http", version: "0.1.4", licenses: ["MIT"]) {
1111
workflow {
1212
standard()
1313
}

src/main/java/org/lattejava/http/server/HTTPRequest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public class HTTPRequest implements Buildable<HTTPRequest> {
6060

6161
private String contentType;
6262

63+
private HTTPContext context;
64+
6365
private String contextPath;
6466

6567
private Charset encoding = StandardCharsets.UTF_8;
@@ -104,9 +106,10 @@ public HTTPRequest(String contextPath, @Deprecated int multipartBufferSize, Stri
104106
this.multipartStreamProcessor.getMultiPartConfiguration().withMultipartBufferSize(multipartBufferSize);
105107
}
106108

107-
public HTTPRequest(String contextPath, String scheme, int port, String ipAddress) {
109+
public HTTPRequest(HTTPContext context, String contextPath, String scheme, int port, String ipAddress) {
108110
Objects.requireNonNull(contextPath);
109111
Objects.requireNonNull(scheme);
112+
this.context = context;
110113
this.contextPath = contextPath;
111114
this.scheme = scheme;
112115
this.port = port;
@@ -304,6 +307,10 @@ public void setContentType(String contentType) {
304307
this.contentType = contentType;
305308
}
306309

310+
public HTTPContext getContext() {
311+
return context;
312+
}
313+
307314
public String getContextPath() {
308315
return contextPath;
309316
}

src/main/java/org/lattejava/http/server/HTTPServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public HTTPServer start() {
9393

9494
try {
9595
for (HTTPListenerConfiguration listener : configuration.getListeners()) {
96-
HTTPServerThread server = new HTTPServerThread(configuration, listener);
96+
HTTPServerThread server = new HTTPServerThread(configuration, context, listener);
9797
servers.add(server);
9898
server.start();
9999
logger.info("HTTP server listening on port [{}]", listener.getPort());

src/main/java/org/lattejava/http/server/internal/HTTPServerThread.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import org.lattejava.http.log.Logger;
3232
import org.lattejava.http.security.SecurityTools;
33+
import org.lattejava.http.server.HTTPContext;
3334
import org.lattejava.http.server.HTTPListenerConfiguration;
3435
import org.lattejava.http.server.HTTPServerConfiguration;
3536
import org.lattejava.http.server.Instrumenter;
@@ -48,6 +49,8 @@ public class HTTPServerThread extends Thread {
4849

4950
private final HTTPServerConfiguration configuration;
5051

52+
private final HTTPContext context;
53+
5154
private final Instrumenter instrumenter;
5255

5356
private final HTTPListenerConfiguration listener;
@@ -62,11 +65,12 @@ public class HTTPServerThread extends Thread {
6265

6366
private volatile boolean running;
6467

65-
public HTTPServerThread(HTTPServerConfiguration configuration, HTTPListenerConfiguration listener)
68+
public HTTPServerThread(HTTPServerConfiguration configuration, HTTPContext context, HTTPListenerConfiguration listener)
6669
throws IOException, GeneralSecurityException {
6770
super("HTTP server [" + listener.getBindAddress().toString() + ":" + listener.getPort() + "]");
6871

6972
this.configuration = configuration;
73+
this.context = context;
7074
this.listener = listener;
7175
this.instrumenter = configuration.getInstrumenter();
7276
this.logger = configuration.getLoggerFactory().getLogger(HTTPServerThread.class);
@@ -75,8 +79,8 @@ public HTTPServerThread(HTTPServerConfiguration configuration, HTTPListenerConfi
7579
this.cleaner = new HTTPServerCleanerThread();
7680

7781
if (listener.isTLS()) {
78-
SSLContext context = SecurityTools.serverContext(listener.getCertificateChain(), listener.getPrivateKey());
79-
this.socket = context.getServerSocketFactory().createServerSocket();
82+
SSLContext sslContext = SecurityTools.serverContext(listener.getCertificateChain(), listener.getPrivateKey());
83+
this.socket = sslContext.getServerSocketFactory().createServerSocket();
8084
} else {
8185
this.socket = new ServerSocket();
8286
}
@@ -112,7 +116,7 @@ public void run() {
112116
}
113117

114118
Throughput throughput = new Throughput(configuration.getReadThroughputCalculationDelay().toMillis(), configuration.getWriteThroughputCalculationDelay().toMillis());
115-
HTTPWorker runnable = new HTTPWorker(clientSocket, configuration, instrumenter, listener, throughput);
119+
HTTPWorker runnable = new HTTPWorker(clientSocket, configuration, context, instrumenter, listener, throughput);
116120
Thread client = Thread.ofVirtual()
117121
.name("HTTP client [" + clientSocket.getRemoteSocketAddress() + "]")
118122
.start(runnable);

src/main/java/org/lattejava/http/server/internal/HTTPWorker.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.lattejava.http.io.PushbackInputStream;
3535
import org.lattejava.http.log.Logger;
3636
import org.lattejava.http.server.ExceptionHandlerContext;
37+
import org.lattejava.http.server.HTTPContext;
3738
import org.lattejava.http.server.HTTPHandler;
3839
import org.lattejava.http.server.HTTPListenerConfiguration;
3940
import org.lattejava.http.server.HTTPRequest;
@@ -59,6 +60,8 @@ public class HTTPWorker implements Runnable {
5960

6061
private final HTTPServerConfiguration configuration;
6162

63+
private final HTTPContext context;
64+
6265
private final PushbackInputStream inputStream;
6366

6467
private final Instrumenter instrumenter;
@@ -77,10 +80,11 @@ public class HTTPWorker implements Runnable {
7780

7881
private volatile State state;
7982

80-
public HTTPWorker(Socket socket, HTTPServerConfiguration configuration, Instrumenter instrumenter, HTTPListenerConfiguration listener,
81-
Throughput throughput) throws IOException {
83+
public HTTPWorker(Socket socket, HTTPServerConfiguration configuration, HTTPContext context, Instrumenter instrumenter,
84+
HTTPListenerConfiguration listener, Throughput throughput) throws IOException {
8285
this.socket = socket;
8386
this.configuration = configuration;
87+
this.context = context;
8488
this.instrumenter = instrumenter;
8589
this.listener = listener;
8690
this.throughput = throughput;
@@ -117,7 +121,7 @@ public void run() {
117121

118122
while (true) {
119123
logger.trace("[{}] Running HTTP worker. Block while we wait to read the preamble", Thread.currentThread().threadId());
120-
request = new HTTPRequest(configuration.getContextPath(), listener.getCertificate() != null ? "https" : "http", listener.getPort(), socket.getInetAddress().getHostAddress());
124+
request = new HTTPRequest(context, configuration.getContextPath(), listener.getCertificate() != null ? "https" : "http", listener.getPort(), socket.getInetAddress().getHostAddress());
121125

122126
// Create a deep copy of the MultipartConfiguration so that the request may optionally modify the configuration on a per-request basis.
123127
request.getMultiPartStreamProcessor().setMultipartConfiguration(new MultipartConfiguration(configuration.getMultipartConfiguration()));

0 commit comments

Comments
 (0)