42 lines
1.8 KiB
Java
42 lines
1.8 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
import java.net.InetAddress;
|
|
import java.net.ServerSocket;
|
|
import java.net.Socket;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
public final class SeaVHostSmoke {
|
|
private SeaVHostSmoke() {}
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
String portValue = System.getenv("PORT");
|
|
if (portValue == null || portValue.isEmpty()) {
|
|
for (String argument : args) {
|
|
if (argument.startsWith("--server.port=")) {
|
|
portValue = argument.substring("--server.port=".length());
|
|
}
|
|
}
|
|
}
|
|
if (portValue == null || portValue.isEmpty()) {
|
|
throw new IllegalArgumentException("missing runtime port");
|
|
}
|
|
int port = Integer.parseInt(portValue);
|
|
byte[] body = "sea-vhost-java-artifact".getBytes(StandardCharsets.UTF_8);
|
|
try (ServerSocket server = new ServerSocket(port, 50, InetAddress.getLoopbackAddress())) {
|
|
while (true) {
|
|
try (Socket socket = server.accept()) {
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.US_ASCII));
|
|
for (String line; (line = reader.readLine()) != null && !line.isEmpty();) {
|
|
// Consume one HTTP request before returning the fixed marker.
|
|
}
|
|
OutputStream output = socket.getOutputStream();
|
|
output.write(("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: " + body.length + "\r\nConnection: close\r\n\r\n").getBytes(StandardCharsets.US_ASCII));
|
|
output.write(body);
|
|
output.flush();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|