set content-length header on post requests in order to fix issue #701

This commit is contained in:
Sebastian Sdorra
2015-04-13 13:42:39 +02:00
parent 451490e95a
commit 11c8c1994f
2 changed files with 74 additions and 24 deletions

View File

@@ -79,6 +79,12 @@ public final class HttpUtil
*/ */
public static final String HEADER_LOCATION = "Location"; public static final String HEADER_LOCATION = "Location";
/**
* content-length header
* @since 1.46
*/
public static final String HEADER_CONTENT_LENGTH = "Content-Length";
/** /**
* header for identifying the scm-manager client * header for identifying the scm-manager client
* @since 1.19 * @since 1.19

View File

@@ -48,8 +48,10 @@ import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import com.sun.jersey.core.util.Base64; import com.sun.jersey.core.util.Base64;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@@ -67,6 +69,7 @@ import java.util.Map;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import sonia.scm.util.HttpUtil;
/** /**
* *
@@ -152,6 +155,9 @@ public class URLHttpClient implements HttpClient
url); url);
connection.setRequestMethod(METHOD_POST); connection.setRequestMethod(METHOD_POST);
// send empty content-length
// see issue #701 http://goo.gl/oyTdrA
setContentLength(connection, 0);
return new URLHttpResponse(connection); return new URLHttpResponse(connection);
} }
@@ -347,16 +353,60 @@ public class URLHttpClient implements HttpClient
{ {
if (Util.isNotEmpty(parameters)) if (Util.isNotEmpty(parameters))
{ {
// use a ByteArrayOutputStream in order to get the final content-length
// see issue #701 http://goo.gl/oyTdrA
connection.setDoOutput(true); connection.setDoOutput(true);
OutputStreamWriter writer = null; OutputStreamWriter writer = null;
ByteArrayOutputStream baos = null;
try try
{ {
writer = new OutputStreamWriter(connection.getOutputStream()); baos = new ByteArrayOutputStream();
writer = new OutputStreamWriter(baos);
Iterator<Map.Entry<String, List<String>>> it = appendPostParameters(writer, parameters);
parameters.entrySet().iterator(); }
finally
{
IOUtil.close(writer);
IOUtil.close(baos);
}
if ( baos != null ){
byte[] data = baos.toByteArray();
appendBody(connection, data);
}
}
else
{
setContentLength(connection, 0);
}
}
private void appendBody(HttpURLConnection connection, byte[] body) throws IOException
{
int length = body.length;
logger.trace("write {} bytes to output", length);
setContentLength(connection, length);
connection.setFixedLengthStreamingMode(length);
OutputStream os = null;
try {
os = connection.getOutputStream();
os.write(body);
} finally {
IOUtil.close(os);
}
}
private void setContentLength(HttpURLConnection connection, int length)
{
connection.setRequestProperty(HttpUtil.HEADER_CONTENT_LENGTH, String.valueOf(length));
}
private void appendPostParameters(OutputStreamWriter writer, Map<String, List<String>> parameters) throws IOException
{
Iterator<Map.Entry<String, List<String>>> it = parameters.entrySet().iterator();
while (it.hasNext()) while (it.hasNext())
{ {
@@ -379,12 +429,6 @@ public class URLHttpClient implements HttpClient
} }
} }
} }
finally
{
IOUtil.close(writer);
}
}
}
/** /**
* Method description * Method description