I've been doing a lot of research and could not find a way to handle this. I'm trying to perform a jQuery ajax call from an https server to a locahost https server running jetty with a custom self signed certificate. My problem is that I cannot determine whether the response is a connection refused or a insecure response (due to the lack of the certificate acceptance). Is there a way to determine the difference between both scenarios? The responseText
, and statusCode
are always the same in both cases, even though in the chrome console I can see a difference:
net::ERR_INSECURE_RESPONSE
net::ERR_CONNECTION_REFUSED
responseText
is always "" and statusCode
is always "0" for both cases.
My question is, how can I determine if a jQuery ajax call failed due to ERR_INSECURE_RESPONSE
or due to ERR_CONNECTION_REFUSED
?
Once the certificate is accepted everything works fine, but I want to know whether the localhost server is shut down, or its up and running but the certificate has not yet been accepted.
$.ajax({
type: 'GET',
url: "https://localhost/custom/server/",
dataType: "json",
async: true,
success: function (response) {
//do something
},
error: function (xhr, textStatus, errorThrown) {
console.log(xhr, textStatus, errorThrown); //always the same for refused and insecure responses.
}
});
Even performing manually the request I get the same result:
var request = new XMLHttpRequest();
request.open('GET', "https://localhost/custom/server/", true);
request.onload = function () {
console.log(request.responseText);
};
request.onerror = function () {
console.log(request.responseText);
};
request.send();
Check out jQuery.ajaxError() Taken reference from : jQuery AJAX Error Handling (HTTP Status Codes) It catches global Ajax errors which you can handle in any number of ways over HTTP or HTTPS:
Unfortunately the present-day browser XHR API does not provide an explicit indication for when the browser refuses to connect due to an "insecure response", and also when it does not trust the website's HTTP/SSL certificate.
But there are ways around this problem.
One solution I came up with to determine when the browser does not trust the HTTP/SSL certificate, is to first detect if an XHR error has occurred (using the jQuery
error()
callback for instance), then check if the XHR call is to an 'https://' URL, and then check if the XHRreadyState
is 0, which means that the XHR connection has not even been opened (which is what happens when the browser does not like the certificate).Here's the code where I do this: https://github.com/maratbn/RainbowPayPress/blob/e9e9472a36ced747a0f9e5ca9fa7d96959aeaf8a/rainbowpaypress/js/le_requirejs/public/model_info__transaction_details.js#L88
@Schultzie's answer is pretty close, but clearly
http
- in general - will not work fromhttps
in the browser environment.What you can do though is to use an intermediate server (proxy) to make the request on your behalf. The proxy should allow either to forward
http
request fromhttps
origin or to load content from self-signed origins.Having your own server with proper certificate is probably an overkill in your case -- as you could use this setting instead of the machine with self-signed certificate -- but there is a plenty of anonymous open proxy services out there.
So two approaches that come to my mind are:
.parentWindow
. If your window received a message you can be sure the server is running (or more precisely was running a fraction of second before).If you are only interested in your local environment you can try to run chrome with
--disable-web-security
flag.Another suggestion: did you try to load an image programatically to find out if more info is present there?
There is no way to differentiate it from newest Web Browsers.
W3C Specification:
As you can read, network errors does not include HTTP response that include errors, that is why you will get always 0 as status code, and "" as error.
Source
Note: The following examples were made using Google Chrome Version 43.0.2357.130 and against an environment that I've created to emulate OP one. Code to the set it up is at the bottom of the answer.
I though that an approach To work around this would be make a secondary request over HTTP instead of HTTPS as This answer but I've remembered that is not possible due that newer versions of browsers block mixed content.
That means that the Web Browser will not allow a request over HTTP if you are using HTTPS and vice versa.
This has been like this since few years ago but older Web Browser versions like Mozilla Firefox below it versions 23 allow it.
Evidence about it:
Making a HTTP request from HTTPS usign Web Broser console
will result in the following error:
Same error will appear in the browser console if you try to do this in other ways as adding an Iframe.
Using Socket connection was also Posted as an answer, I was pretty sure that the result will be the same / similar but I've give it a try.
Trying to Open a socket connection from the Web Broswer using HTTPS to a non Secure socket endpoint will end in mixed content errors.
Then I've tried to connect to a wss endpoint too see If I could read some information about network connection errors:
Executing snippet above with Server turned off results in:
Executing snippet above with Server turned On
But again, the error that the "onerror function" output to the console have not any tip to differentiate one error of the other.
Using a proxy as this answer suggest could work but only if the "target" server has public access.
This was not the case here, so trying to implement a proxy in this scenario will lead Us to the same problem.
Code to create Node.js HTTPS server:
I've created two Nodejs HTTPS servers, that use self signed certificates:
targetServer.js:
applicationServer.js:
To make it work you need to have Nodejs Installed, Need to generate separated certificates for each server and store it in the folders certs and certs2 accordingly.
To Run it just execute
node applicationServer.js
andnode targetServer.js
in a terminal (ubuntu example).As of now: There is no way to differentiate this event between browers. As the browsers do not provide an event for developers to access. (July 2015)
This answer merely seeks to provide ideas for a potential, albiet hacky and incomplete, solution.
Disclaimer: this answer is incomplete as it doesn't completely solve OP's issues (due to cross-origin policies). However the idea itself does has some merit that is further expanded upon by: @artur grzesiak here, using a proxy and ajax.
After quite a bit of research myself, there doesn't seem to be any form of error checking for the difference between connection refused and an insecure response, at least as far as javascript providing a response for the difference between the two.
The general consensus of my research being that SSL certificates are handled by the browser, so until a self-signed certificate is accepted by the user, the browser locks down all requests, including those for a status code. The browser could (if coded to) send back it's own status code for an insecure response, but that doesn't really help anything, and even then, you'd have issues with browser compatibility (chrome/firefox/IE having different standards... once again)
Since your original question was for checking the status of your server between being up versus having an unaccepted certificate, could you not make a standard HTTP request like so?
Granted this does require an extra request to verify server connectivity, but it should get the job done by process of elimination. Still feels hacky though, and I'm not a big fan of the doubled request.
I don't think there's currently a way to detect these error messages, but a hack that you can do is to use a server like nginx in front of your application server, so that if the application server is down you'll get a bad gateway error from nginx with
502
status code which you can detect in JS. Otherwise, if the certificate is invalid you'll still get the same generic error withstatusCode = 0
.