Created
December 19, 2022 18:05
-
-
Save dweiss/9703cfa2713a7fc211a0018a72267d90 to your computer and use it in GitHub Desktop.
http client buffer repro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.sun.net.httpserver.HttpExchange; | |
import com.sun.net.httpserver.HttpHandler; | |
import com.sun.net.httpserver.HttpServer; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.net.InetSocketAddress; | |
import java.net.URI; | |
import java.net.http.HttpClient; | |
import java.net.http.HttpRequest; | |
import java.net.http.HttpResponse; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class HttpClientBufTest { | |
static int LINES = 10_000; | |
public static void main(String[] args) throws Exception { | |
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); | |
server.createContext("/test", new MyHandler()); | |
server.setExecutor(null); // creates a default executor | |
server.start(); | |
Thread.sleep(1000); | |
var request = HttpRequest.newBuilder(URI.create("http://localhost:8080/test")).GET().build(); | |
var httpClient = HttpClient.newHttpClient(); | |
var tempFile = Files.createTempFile("tempFile-", ".txt"); | |
try { | |
httpClient.send(request, HttpResponse.BodyHandlers.ofFile(tempFile)); | |
boolean received[] = new boolean[LINES]; | |
Pattern p = Pattern.compile("This is line: (?<num>[0-9]+)"); | |
for (String line : Files.readAllLines(tempFile, StandardCharsets.UTF_8)) { | |
Matcher matcher = p.matcher(line); | |
if (matcher.matches()) { | |
received[Integer.parseInt(matcher.group("num"))] = true; | |
} else { | |
System.out.println("Corrupted line received: " + line); | |
} | |
} | |
for (int i = 0; i < received.length; i++) { | |
if (!received[i]) { | |
int j = i; | |
while (j < received.length && !received[j]) { | |
j++; | |
} | |
System.out.println("Missed line range: " + i + "-" + (j - 1)); | |
i = j; | |
} | |
} | |
} finally { | |
Files.delete(tempFile); | |
} | |
server.stop(0); | |
} | |
static class MyHandler implements HttpHandler { | |
@Override | |
public void handle(HttpExchange t) throws IOException { | |
t.sendResponseHeaders(200, 0); | |
try (OutputStream os = t.getResponseBody()) { | |
for (int i = 0; i < LINES; i++) { | |
String line = "This is line: " + i + "\n"; | |
os.write(line.getBytes(StandardCharsets.UTF_8)); | |
os.flush(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment