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

Revisit concurrency in JavaNetReverseProxy2#service #913

Merged
merged 1 commit into from
Feb 19, 2025
Merged
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
45 changes: 29 additions & 16 deletions src/main/java/org/jvnet/hudson/test/JavaNetReverseProxy2.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package org.jvnet.hudson.test;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Util;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import org.eclipse.jetty.ee9.servlet.ServletContextHandler;
import org.eclipse.jetty.ee9.servlet.ServletHolder;
import org.eclipse.jetty.server.Server;
Expand All @@ -34,16 +38,6 @@
public JavaNetReverseProxy2(File cacheFolder) throws Exception {
this.cacheFolder = cacheFolder;
cacheFolder.mkdirs();
Runtime.getRuntime().addShutdownHook(new Thread("delete " + cacheFolder) {
@Override
public void run() {
try {
Util.deleteRecursive(cacheFolder);
} catch (IOException x) {
x.printStackTrace();
}
}
});

QueuedThreadPool qtp = new QueuedThreadPool();
qtp.setName("Jetty (JavaNetReverseProxy)");
Expand All @@ -67,20 +61,39 @@
}

@Override
@SuppressFBWarnings(value = "URLCONNECTION_SSRF_FD", justification = "TODO needs triage")

Check warning on line 64 in src/main/java/org/jvnet/hudson/test/JavaNetReverseProxy2.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: needs triage")
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Path parent = cacheFolder.toPath();
Files.createDirectories(parent);

String path = req.getServletPath();
String d = Util.getDigestOf(path);
Path cache = parent.resolve(d);

File cache = new File(cacheFolder, d);
synchronized (this) {
if (!cache.exists()) {
if (!Files.exists(cache)) {
Path tmp = Files.createTempFile(parent, d, null);
try {
URL url = new URL("https://updates.jenkins.io/" + path);
FileUtils.copyURLToFile(url, cache);
try (InputStream is = url.openStream()) {
Files.copy(is, tmp, StandardCopyOption.REPLACE_EXISTING);
}
try {
Files.move(tmp, cache, StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException e) {
try {
Files.move(tmp, cache, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e2) {
e2.addSuppressed(e);
throw e2;
}
}
} finally {
Files.deleteIfExists(tmp);
}
}

resp.setContentType(getMimeType(path));
Files.copy(cache.toPath(), resp.getOutputStream());
Files.copy(cache, resp.getOutputStream());
}

private String getMimeType(String path) {
Expand Down