Project: titanium
Issue TIMOB-15612: Android: HttpClient.abort() does not stop file upload
Currently, it's not possible to stop an upload with HttpClient on Android. Calling abort() does abort the transfer, but the transfer starts over from 0%, and runs to completion, somewhat hidden - even calling the onload() callback when it's done. On the server-side, it's apparant that the socket connection is closed when abort() is called, but a new socket connection is instantly opened, where the upload is restarted. Here's some source (app.js) to reproduce the issue - it starts an upload, then abort() is called after 1 second: {code:js} var win = Ti.UI.createWindow({ exitOnClose: true,}); win.open(); var percent_done = 0; var xhr = Ti.Network.createHTTPClient({ onload : function(e) { alert("onload: "+this.responseText); }, onerror : function(e) { alert("e.error: "+e.error); }, onreadystatechange: function(e){ console.log("onreadystatechange: "+this.readyState); }, onsendstream : function(e){ var curr_percent_done = parseInt(e.progress*100); if (percent_done == curr_percent_done) return; percent_done = curr_percent_done; console.log(percent_done+"% done. readyState: "+this.readyState); }, timeout: 10000 }); function upload(){ console.log("STARTING UPLOAD"); var url = "http://bitfabrikken.dk/upload"; //using this server for simplicity's sake var file = Ti.Filesystem.getFile(Ti.Filesystem.getResourcesDirectory()+"500k_test.mp4"); //just use any large-ish file, 500kb+ or so xhr.open("POST", url); xhr.send({ file: file }); } upload(); setTimeout(function(){ console.log("ABORTING UPLOAD - calling xhr.abort"); xhr.abort(); },1000); {code} The console output looks this, where it's clear to see that somehow the transfer is re-initiated at 0%, when abort() is called: {code} [INFO][TiAPI ( 3108)] STARTING UPLOAD [INFO][ActivityManager( 1015)] Displayed dk.bitfabrikken.nettest/.NettestActivity: +636ms [INFO][TiAPI ( 3108)] onreadystatechange: 1 [INFO][TiAPI ( 3108)] 1% done. readyState: 1 [INFO][TiAPI ( 3108)] 2% done. readyState: 1 [INFO][TiAPI ( 3108)] 3% done. readyState: 1 [INFO][TiAPI ( 3108)] 4% done. readyState: 1 [INFO][TiAPI ( 3108)] 5% done. readyState: 1 [INFO][TiAPI ( 3108)] 6% done. readyState: 1 [INFO][TiAPI ( 3108)] 7% done. readyState: 1 [INFO][TiAPI ( 3108)] 8% done. readyState: 1 [INFO][TiAPI ( 3108)] 9% done. readyState: 1 [INFO][TiAPI ( 3108)] ABORTING UPLOAD - calling xhr.abort [INFO][TiAPI ( 3108)] 0% done. readyState: 1 [INFO][TiAPI ( 3108)] 1% done. readyState: 1 [INFO][TiAPI ( 3108)] 2% done. readyState: 1 [INFO][TiAPI ( 3108)] 3% done. readyState: 1 [INFO][TiAPI ( 3108)] 4% done. readyState: 1 [INFO][TiAPI ( 3108)] 5% done. readyState: 1 [INFO][TiAPI ( 3108)] 6% done. readyState: 1 [INFO][TiAPI ( 3108)] 7% done. readyState: 1 [INFO][TiAPI ( 3108)] 8% done. readyState: 1 [INFO][TiAPI ( 3108)] 9% done. readyState: 1 [INFO][TiAPI ( 3108)] 10% done. readyState: 1 [INFO][TiAPI ( 3108)] 11% done. readyState: 1 [INFO][TiAPI ( 3108)] 12% done. readyState: 1 [INFO][TiAPI ( 3108)] 13% done. readyState: 1 [INFO][TiAPI ( 3108)] 14% done. readyState: 1 -----SNIP - LOTS OF REPETITION----- [INFO][TiAPI ( 3108)] 93% done. readyState: 1 [INFO][TiAPI ( 3108)] 94% done. readyState: 1 [INFO][TiAPI ( 3108)] 95% done. readyState: 1 [INFO][TiAPI ( 3108)] 96% done. readyState: 1 [INFO][TiAPI ( 3108)] 97% done. readyState: 1 [INFO][TiAPI ( 3108)] 98% done. readyState: 1 [INFO][TiAPI ( 3108)] 99% done. readyState: 1 [INFO][TiAPI ( 3213)] 100% done. readyState: 1 [INFO][TiAPI ( 3213)] onreadystatechange: 2 [INFO][TiAPI ( 3213)] onreadystatechange: 3 [INFO][TiAPI ( 3213)] onreadystatechange: 4 [INFO][ALERT ( 3213)] (KrollRuntimeThread) [7533,8510] onload: null {code} Previously I've side-stepped this issue by using TCPSocket to handle uploading. However now my client wants SSL, and since that's not supported with TCPSocket, I'm back to being stuck on this error. Links to a few Q&A threads where this bug is the problem: http://developer.appcelerator.com/question/144392/android-httpclientabort-does-not-stop-file-upload http://developer.appcelerator.com/question/146555/how-to-abort-an-upload http://developer.appcelerator.com/question/158669/how-to-abort-a-file-upload-via-httpclient-android https://developer.appcelerator.com/question/154693/httpclient---how-to-stop-an-upload-in-progress---abort https://developer.appcelerator.com/question/152916/abort-of-sending-by-http-client-show-progress-of-send--receive-characters-stream
Story points: 5