Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($httpBackend): Allow status code 0 from any protocol
Browse files Browse the repository at this point in the history
Android 4.1 stock browser also returns status code 0 when
a template is loaded via `http` and the application is cached using
appcache.

Fixes #1356.
Closes #5547.
  • Loading branch information
rafallo authored and tbosch committed Jan 9, 2014
1 parent b6c42d5 commit 28fc80b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
}

function completeRequest(callback, status, response, headersString) {
var protocol = urlResolve(url).protocol;

// cancel timeout and subsequent timeout promise resolution
timeoutId && $browserDefer.cancel(timeoutId);
jsonpDone = xhr = null;

// fix status code for file protocol (it's always 0)
status = (protocol == 'file' && status === 0) ? (response ? 200 : 404) : status;
// fix status code when it is 0 (0 status is undocumented).
// Occurs when accessing file resources.
// On Android 4.1 stock browser it occurs while retrieving files from application cache.
status = (status === 0) ? (response ? 200 : 404) : status;

// normalize IE bug (http://bugs.jquery.com/ticket/1450)
status = status == 1223 ? 204 : status;
Expand Down
10 changes: 5 additions & 5 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ describe('$httpBackend', function() {
// TODO(vojta): test whether it fires "async-end" on both success and error
});

describe('file protocol', function() {
describe('protocols that return 0 status code', function() {

function respond(status, content) {
xhr = MockXhr.$$lastInstance;
Expand All @@ -435,7 +435,7 @@ describe('$httpBackend', function() {
it('should convert 0 to 200 if content', function() {
$backend = createHttpBackend($browser, createMockXhr);

$backend('GET', 'file:///whatever/index.html', null, callback);
$backend('GET', 'someProtocol:///whatever/index.html', null, callback);
respond(0, 'SOME CONTENT');

expect(callback).toHaveBeenCalled();
Expand All @@ -446,7 +446,7 @@ describe('$httpBackend', function() {
it('should convert 0 to 404 if no content', function() {
$backend = createHttpBackend($browser, createMockXhr);

$backend('GET', 'file:///whatever/index.html', null, callback);
$backend('GET', 'someProtocol:///whatever/index.html', null, callback);
respond(0, '');

expect(callback).toHaveBeenCalled();
Expand All @@ -462,10 +462,10 @@ describe('$httpBackend', function() {
hash : "#/C:/",
host : "",
hostname : "",
href : "file:///C:/base#!/C:/foo",
href : "someProtocol:///C:/base#!/C:/foo",
pathname : "/C:/foo",
port : "",
protocol : "file:",
protocol : "someProtocol:",
search : "",
setAttribute: angular.noop
};
Expand Down

3 comments on commit 28fc80b

@geirgp
Copy link

@geirgp geirgp commented on 28fc80b Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Altering status 0 to 404 may seem like the right thing to do in some scenarios but it is not a good solution overall as 404 is a HTTP response code and indicates that the server did actually respond. Status 0 should, according to w3.org definition, be considered "some type of network error or fetch termination" (http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute) - very different to the meaning of status 404 - "Not found"

I discovered this code change after upgrading to latest version of angularjs - my code which handles network/connection problems based on status code 0 stopped working since status 0 is now mapped to 404.

@tbosch
Copy link
Contributor

@tbosch tbosch commented on 28fc80b Feb 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you mean we should leave statusCode 0 as it is if it represents an error, right? However, in the success case we do want to convert it into a statusCode 200 (e.g. needed for reading a file which will always set statusCode 0, or when using the appCache on Android).

Could you create an issue for this?

@swallez
Copy link

@swallez swallez commented on 28fc80b Feb 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.