From c3e17fb185afddefe05accaa713cba7e0cb52e00 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Thu, 30 Aug 2012 13:33:06 -0400 Subject: [PATCH] Fix broken file upload in mobile-spec tests (CB-1290). The explicit transfer-encoding: chunk that was added breaks in fixed-length streaming mode. It is, however, still required to work-around an OOM bug in HTTPS mode. The new logic works for both the mobile-spec and the HTTPS large-file test that I used before. Commit adding the header: 999c548e6e29c9253403dbfc9f340c1c52c89871 --- framework/src/org/apache/cordova/FileTransfer.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/framework/src/org/apache/cordova/FileTransfer.java b/framework/src/org/apache/cordova/FileTransfer.java index b64bc501..ce7120b1 100644 --- a/framework/src/org/apache/cordova/FileTransfer.java +++ b/framework/src/org/apache/cordova/FileTransfer.java @@ -146,9 +146,9 @@ public class FileTransfer extends Plugin { //------------------ CLIENT REQUEST // open a URL connection to the server URL url = new URL(target); - + boolean useHttps = url.getProtocol().toLowerCase().equals("https"); // Open a HTTP connection to the URL based on protocol - if (url.getProtocol().toLowerCase().equals("https")) { + if (useHttps) { // Using standard HTTPS connection. Will not allow self signed certificate if (!trustEveryone) { conn = (HttpsURLConnection) url.openConnection(); @@ -246,12 +246,17 @@ public class FileTransfer extends Plugin { Log.d(LOG_TAG, "Content Length: " + fixedLength); // setFixedLengthStreamingMode causes and OutOfMemoryException on pre-Froyo devices. // http://code.google.com/p/android/issues/detail?id=3164 - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO && chunkedMode) { + // It also causes OOM if HTTPS is used, even on newer devices. + chunkedMode = chunkedMode && (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO || useHttps); + + if (chunkedMode) { conn.setChunkedStreamingMode(maxBufferSize); + // Although setChunkedStreamingMode sets this header, setting it explicitly here works + // around an OutOfMemoryException when using https. + conn.setRequestProperty("Transfer-Encoding", "chunked"); } else { conn.setFixedLengthStreamingMode(fixedLength); } - conn.setRequestProperty("Transfer-Encoding", "chunked"); dos = new DataOutputStream( conn.getOutputStream() ); //We don't want to change encoding, we just want this to write for all Unicode.