Skip to content

Commit

Permalink
Fix broken file upload in mobile-spec tests (CB-1290).
Browse files Browse the repository at this point in the history
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: 999c548
  • Loading branch information
agrieve committed Aug 30, 2012
1 parent f7ae7fe commit c3e17fb
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions framework/src/org/apache/cordova/FileTransfer.java
Expand Up @@ -146,9 +146,9 @@ private PluginResult upload(String source, String target, JSONArray args) {
//------------------ 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();
Expand Down Expand Up @@ -246,12 +246,17 @@ private PluginResult upload(String source, String target, JSONArray args) {
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.
Expand Down

0 comments on commit c3e17fb

Please sign in to comment.