Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-15871] HTTPClient: setting autoRedirect off for 302s on Android calls onerror

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2014-08-29T00:18:08.000+0000
Affected Version/sn/a
Fix Version/sRelease 3.4.0
ComponentsAndroid
Labelsalloy, android, error, httpclient, module_network, onerror, qe-manualtest, qe-testadded, redirect
ReporterAdam Lynch
AssigneeAllen Yeung
Created2013-11-13T11:35:53.000+0000
Updated2014-10-14T23:20:44.000+0000

Description

If you set httpclient.autoRedirect to false and then send a GET which returns a 302 (or anything but a 200 I'm guessing), then onerror is called instead of onload. 1. 301, 302, etc. responses shouldn't be deemed an error, but if you're really set on that: 2. Make it consistent. On iOS, onload is called and on Android, onerror is called. Even if you don't, at least... 3. Give us all the information. Provide more than {error:'...'} to onerror. Give us the headers, the status, the responseText, responseData, etc. if they're there in the response. This really reduces confidence in what you're doing and requires so much more testing.

Attachments

FileDateSize
workaroundApp.js2014-08-27T22:34:00.000+00001725

Comments

  1. Mauro Parra-Miranda 2013-11-21

    Hello Adam! Do you have any test case that shows this issue? TIA! Best, Mauro
  2. Mostafizur Rahman 2013-11-25

    Hello, We have tested this issue with my test code. I got this problem. But we checked on iOS then its return success alert message.

    Testing environment:

    OS: MAC OS X 10.8.5 Ti SDK: 3.1.3 GA Ti CLI: 3.2.0 Android SDK 4.2.2 Device Android SDK 4.0.3 in Emulator

    My test code

       var win1 = Titanium.UI.createWindow({
       	 
       });
       
       // Create a Button.
       var aButton = Ti.UI.createButton({
       	title : 'aButton',
       });
       
       // Listen for click events.
       aButton.addEventListener('click', function() {
       
       	var url = "http://www.example.com/redirect.php";
       	var client = Ti.Network.createHTTPClient({
       		// function called when the response data is available
       		onload : function(e) {
       			Ti.API.info('e ' + JSON.stringify(e));
       			Ti.API.info("Received HEADERS_RECEIVED: " + this.HEADERS_RECEIVED);
       			Ti.API.info("Received status: " + this.status);
       			Ti.API.info("Received statusText: " + this.statusText);
       			Ti.API.info("Received responseText: " + this.responseText);
       
       			alert('success');
       		},
       		// function called when an error occurs, including a timeout
       		onerror : function(e) {
       			Ti.API.debug(e.error);
       			Ti.API.info("Received HEADERS_RECEIVED: " + this.HEADERS_RECEIVED);
       			Ti.API.info("Received status: " + this.status);
       			Ti.API.info("Received statusText: " + this.statusText);
       			Ti.API.info("Received responseText: " + this.responseText);
       
       			alert('error');
       		},
       		timeout : 5000, // in milliseconds
       		autoRedirect : false,
       		autoEncodeUrl : false,
       
       	});
       	// Prepare the connection.
       
       	client.open("GET", url);
       	// Send the request.
       	client.send();
       
       });
       
       // Add to the parent view.
       win1.add(aButton);
       
       win1.open();
       
       <?php
       
       error_reporting(1);
       Header( "HTTP/1.1 301 Moved Permanently" );
       //Header( "Location: http://www.example.com" );
       ?>
       

    Step to reproduces

    1. Create a new project 2. Paste test code in app.js 3. Now run on android device/emulator 4. Click on button its return error Thanks
  3. Israel 2014-05-23

    Hi I have exactly the same problem, but the problem occurs when the header code is 302 or 301 which are redirecting. And android is only in the case of self-identification does not recover or cookie. Please any solution. regards
  4. Nick Milner 2014-08-12

    This is really a bit poor IMHO. Correctly responding to the http specification would seem to be the most basic requirement for any http implementation, especially in mobile. I just saw http://developer.appcelerator.com/question/176965/incorrect-status-returned-by-the-http-client-in-the-330-sdk and in the tests I describe there it seems Android is in a right mess. I was wondering why this is a 'low' priority ?
  5. Matthew Delmarter 2014-08-12

    Yes this should be an extremely high priority. Please fix asap - this bug has hosed critical features of my app. I can confirm that the above code sample also returns a "200" instead of a "301" when pointed to the URL of "http://google.co.nz" using the 3.3.0 SDK. Switching back to the 3.2.3 SDK fixed the issue. I am using OS X Yosemite, with Xcode 5.1.1, iOS Simulator 7.1. Interesting that this ticket was created in a much older environment. This ticket is a likely duplicate of TC-4562 just added.
  6. Ingo Muschenetz 2014-08-12

    The description of this ticket refers to "onerror" being called. Is that just the side effect of the wrong response code being returned? Note that this would not explain it working in 3.2.3 and not in 3.3.0. I'm not sure TC-4562 is a duplicate.
  7. Ewan Harris 2014-08-26

    Issue can be reproduced on 3.2.3.GA and 3.3.0.GA. Due to TIMOB-17566 setting the autoRedirect in createHTTPClient() to false does not work and it defaults to true, therefore .status was always returning 200 instead of 302. This caused difficulties in being able to reproduce the issue and therefore the workaround of setting the properties via:
       client.setAutoRedirect(false);
       client.setAutoEncodeUrl(false);
       
    was used. After using the workaround I was able to reproduce the issue using the workaroundApp.js I have attached to the ticket. Tested on: Mac OSX 10.9.4 Appcelerator Studio, build: 3.4.0.201408270900 Titanium SDK build: 3.3.0.GA, 3.2.3.GA Titanium CLI, build: 3.3.0 Alloy: 1.3.1
  8. Allen Yeung 2014-08-28

    https://github.com/appcelerator/titanium_mobile/pull/5997
  9. Allen Yeung 2014-08-28

    Test case:
       var win1 = Titanium.UI.createWindow({
       
       });
       
       // Create a Button.
       var aButton = Ti.UI.createButton({
       	title : 'aButton',
       });
       
       // Listen for click events.
       aButton.addEventListener('click', function() {
       
       	var url = "http://httpbin.org/redirect/2";
       	var client = Ti.Network.createHTTPClient({
       		// function called when the response data is available
       		onload : function(e) {
       			Ti.API.debug('########################## ONLOAD');
       
       			Ti.API.info('e ' + JSON.stringify(e));
       			Ti.API.info("Received HEADERS_RECEIVED: " + this.HEADERS_RECEIVED);
       			Ti.API.info("Received status: " + this.status);
       			alert(this.status);
       			Ti.API.info("Received statusText: " + this.statusText);
       			Ti.API.info("Received responseText: " + this.responseText);
       
       			alert('success');
       		},
       		// function called when an error occurs, including a timeout
       		onerror : function(e) {
       			Ti.API.debug('########################## ERROR');
       
       			Ti.API.debug(e.error);
       			Ti.API.info("Received HEADERS_RECEIVED: " + this.HEADERS_RECEIVED);
       			Ti.API.info("Received status: " + this.status);
       			alert(this.status);
       			Ti.API.info("Received statusText: " + this.statusText);
       			Ti.API.info("Received responseText: " + this.responseText);
       
       			alert('error');
       		},
       		timeout : 5000, // in milliseconds*
       
       		autoRedirect : false,
       		autoEncodeUrl : false,
       
       	});
       
       	// Prepare the connection.
       	client.setAutoRedirect(false);
       	client.setAutoEncodeUrl(false);
       	client.open("GET", url);
       	// Send the request.
       	client.send();
       
       });
       
       // Add to the parent view.
       win1.add(aButton);
       
       win1.open(); 
       
  10. Lokesh Choudhary 2014-09-03

    Verified the fix. Onload is called instead of onerror. Closing The response for the test case above is:
        [INFO] :   e {"code":0,"source":{"location":"http://httpbin.org/redirect/2","status":302,"autoEncodeUrl":false,"timeout":5000,"domain":null,"responseText":"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>Redirecting...</title>\n<h1>Redirecting...</h1>\n<p>You should be redirected automatically to target URL: <a href=\"/redirect/1\">/redirect/1</a>.  If not click the link.","autoRedirect":false,"connectionType":"GET","validatesSecureCertificate":false,"statusText":"FOUND","password":null,"allResponseHeaders":"Access-Control-Allow-Credentials:true\nAccess-Control-Allow-Origin:*\nContent-Type:text/html; charset=utf-8\nDate:Wed, 03 Sep 2014 17:21:00 GMT\nLocation:/redirect/1\nServer:gunicorn/18.0\nContent-Length:229\nConnection:keep-alive\n","readyState":4,"responseXML":null,"responseData":{"file":null,"nativePath":null,"height":0,"length":229,"width":0,"mimeType":"text/html","apiName":"Ti.Blob","text":"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>Redirecting...</title>\n<h1>Redirecting...</h1>\n<p>You should be redirected automatically to target URL: <a href=\"/redirect/1\">/redirect/1</a>.  If not click the link.","type":2,"bubbleParent":true},"apiName":"Ti.Network.HTTPClient","username":null,"connected":false,"bubbleParent":true,"_events":{"disposehandle":{}}},"success":true}
        [INFO] :   Received HEADERS_RECEIVED: 2
        [INFO] :   Received status: 302
        [INFO] :   ALERT: (KrollRuntimeThread) [6,2604] 302
        [INFO] :   Received statusText: FOUND
        [INFO] :   Received responseText: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
        [INFO] :   <title>Redirecting...</title>
        [INFO] :   <h1>Redirecting...</h1>
        [INFO] :   <p>You should be redirected automatically to target URL: <a href="/redirect/1">/redirect/1</a>.  If not click the link.
        [INFO] :   ALERT: (KrollRuntimeThread) [4,2608] success
        
    Environment: Appc Studio : 3.4.0.201408291834 Ti SDK : 3.4.0.v20140829184521 Mac OSX : 10.9.4 Alloy : 1.5.0-dev CLI - 3.4.0-dev Code Processor: 1.1.1 Nexus - android 4.2.2

JSON Source