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 CURRENT = new ThreadLocal(); private static final Logger logger = Logger.getLogger(Channel.class.getName()); + +// static { +// ConsoleHandler h = new ConsoleHandler(); +// h.setFormatter(new Formatter(){ +// public synchronized String format(LogRecord record) { +// StringBuilder sb = new StringBuilder(); +// sb.append((record.getMillis()%100000)+100000); +// sb.append(" "); +// if (record.getSourceClassName() != null) { +// sb.append(record.getSourceClassName()); +// } else { +// sb.append(record.getLoggerName()); +// } +// if (record.getSourceMethodName() != null) { +// sb.append(" "); +// sb.append(record.getSourceMethodName()); +// } +// sb.append('\n'); +// String message = formatMessage(record); +// sb.append(record.getLevel().getLocalizedName()); +// sb.append(": "); +// sb.append(message); +// sb.append('\n'); +// if (record.getThrown() != null) { +// try { +// StringWriter sw = new StringWriter(); +// PrintWriter pw = new PrintWriter(sw); +// record.getThrown().printStackTrace(pw); +// pw.close(); +// sb.append(sw.toString()); +// } catch (Exception ex) { +// } +// } +// return sb.toString(); +// } +// }); +// h.setLevel(Level.FINE); +// logger.addHandler(h); +// logger.setLevel(Level.FINE); +// } }