diff --git a/core/src/main/java/hudson/model/FullDuplexHttpChannel.java b/core/src/main/java/hudson/model/FullDuplexHttpChannel.java index 2eb8fa4e25..7265dafda8 100644 --- a/core/src/main/java/hudson/model/FullDuplexHttpChannel.java +++ b/core/src/main/java/hudson/model/FullDuplexHttpChannel.java @@ -28,14 +28,11 @@ import hudson.remoting.PingThread; import hudson.remoting.Channel.Mode; import hudson.util.ChunkedOutputStream; import hudson.util.ChunkedInputStream; -import org.apache.commons.io.IOUtils; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.io.PipedInputStream; -import java.io.PipedOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; @@ -49,11 +46,13 @@ import java.util.logging.Logger; abstract class FullDuplexHttpChannel { private Channel channel; - private final PipedOutputStream pipe = new PipedOutputStream(); + private InputStream upload; private final UUID uuid; private final boolean restricted; + private boolean completed; + public FullDuplexHttpChannel(UUID uuid, boolean restricted) throws IOException { this.uuid = uuid; this.restricted = restricted; @@ -65,7 +64,7 @@ abstract class FullDuplexHttpChannel { *
* If this connection is lost, we'll abort the channel.
*/
- public void download(StaplerRequest req, StaplerResponse rsp) throws InterruptedException, IOException {
+ public synchronized void download(StaplerRequest req, StaplerResponse rsp) throws InterruptedException, IOException {
rsp.setStatus(HttpServletResponse.SC_OK);
// server->client channel.
@@ -74,8 +73,16 @@ abstract class FullDuplexHttpChannel {
OutputStream out = rsp.getOutputStream();
if (DIY_CHUNKING) out = new ChunkedOutputStream(out);
+ // send something out so that the client will see the HTTP headers
+ out.write("Starting HTTP duplex channel".getBytes());
+ out.flush();
+
+ // wait until we have the other channel
+ while(upload==null)
+ wait();
+
channel = new Channel("HTTP full-duplex channel " + uuid,
- Computer.threadPoolForRemoting, Mode.BINARY, new PipedInputStream(pipe), out, null, restricted);
+ Computer.threadPoolForRemoting, Mode.BINARY, upload, out, null, restricted);
// so that we can detect dead clients, periodically send something
PingThread ping = new PingThread(channel) {
@@ -84,7 +91,7 @@ abstract class FullDuplexHttpChannel {
LOGGER.info("Duplex-HTTP session " + uuid + " is terminated");
// this will cause the channel to abort and subsequently clean up
try {
- pipe.close();
+ upload.close();
} catch (IOException e) {
// this can never happen
throw new AssertionError(e);
@@ -95,6 +102,10 @@ abstract class FullDuplexHttpChannel {
main(channel);
channel.join();
ping.interrupt();
+
+ // publish that we are done
+ completed=true;
+ notify();
}
protected abstract void main(Channel channel) throws IOException, InterruptedException;
@@ -102,11 +113,18 @@ abstract class FullDuplexHttpChannel {
/**
* This is where we receive inputs from the client.
*/
- public void upload(StaplerRequest req, StaplerResponse rsp) throws InterruptedException, IOException {
+ public synchronized void upload(StaplerRequest req, StaplerResponse rsp) throws InterruptedException, IOException {
rsp.setStatus(HttpServletResponse.SC_OK);
InputStream in = req.getInputStream();
if(DIY_CHUNKING) in = new ChunkedInputStream(in);
- IOUtils.copy(in,pipe);
+
+ // publish the upload channel
+ upload = in;
+ notify();
+
+ // wait until we are done
+ while (!completed)
+ wait();
}
public Channel getChannel() {
diff --git a/remoting/src/main/java/hudson/remoting/Channel.java b/remoting/src/main/java/hudson/remoting/Channel.java
index d469fb2afb..e671f87115 100644
--- a/remoting/src/main/java/hudson/remoting/Channel.java
+++ b/remoting/src/main/java/hudson/remoting/Channel.java
@@ -793,4 +793,44 @@ public class Channel implements VirtualChannel, IChannel {
private static final ThreadLocal