Postpone writing to output stream in diff command

Without this, the gzip filter would write the first bytes to the
response output stream, before the diff command is triggered and
potential exceptions may be thrown. When exceptions are thrown too late,
filters like the GZip filter may already have sent bytes to the response
stream. Afterwards this cannot be undone and the response created by an
exception mapper may not be valid anymore.
This commit is contained in:
Rene Pfeuffer
2019-09-25 12:15:30 +02:00
parent eb1812dea6
commit 3d27938e80
11 changed files with 110 additions and 127 deletions

View File

@@ -39,13 +39,12 @@ import com.google.common.base.Strings;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.DiffCommandBuilder;
import sonia.scm.repository.api.DiffFormat;
import sonia.scm.repository.spi.javahg.HgDiffInternalCommand;
import sonia.scm.web.HgUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
//~--- JDK imports ------------------------------------------------------------
@@ -71,8 +70,7 @@ public class HgDiffCommand extends AbstractCommand implements DiffCommand
//~--- get methods ----------------------------------------------------------
@Override
public void getDiffResult(DiffCommandRequest request, OutputStream output)
throws IOException
public DiffCommandBuilder.OutputStreamConsumer getDiffResult(DiffCommandRequest request)
{
com.aragost.javahg.Repository hgRepo = open();
@@ -86,26 +84,22 @@ public class HgDiffCommand extends AbstractCommand implements DiffCommand
cmd.change(HgUtil.getRevision(request.getRevision()));
InputStream inputStream = null;
return output -> {
InputStream inputStream = null;
try
{
try {
if (!Strings.isNullOrEmpty(request.getPath()))
{
inputStream = cmd.stream(hgRepo.file(request.getPath()));
if (!Strings.isNullOrEmpty(request.getPath())) {
inputStream = cmd.stream(hgRepo.file(request.getPath()));
} else {
inputStream = cmd.stream();
}
ByteStreams.copy(inputStream, output);
} finally {
Closeables.close(inputStream, true);
}
else
{
inputStream = cmd.stream();
}
ByteStreams.copy(inputStream, output);
}
finally
{
Closeables.close(inputStream, true);
}
};
}
}