1

I searching the way how to detect the img-file was not found(404... or 50x)

so I did figure out what img-file is, like below steps..

  1. parse the html and css code, then catch the URL
  2. make img tag temporally, assign src=URI
  3. check the attribute(width, height), if it is 0 value, it would be not found.

finally, I wanna catch the remote address, if so I can figure out what server has problem !

so I thought that if i can handle http-header at front-end, it could be awesome !

and I also considered using Ajax, but it require additional running time, then can make bad web-performance.....

So ! In short, it is my real question. Is it possible to handle http-header info at front-end? (using JavaScript, not using any tools like chrome-dev-tool, wiresharks and so on..)

![How can I handle this data?(status, remote-address and so on...)][1]

  • images have an error event, no need to side-load. and 0 is not the width of missing img btw... – dandavis Dec 16 '14 at 01:24
  • Thanks for rapid comment! yeah, but final purpose is detecting server has trouble, so i need it... actually it doesn't need to only be a img-file, any resources from web-servers are okay. it is just a case of one.(img) Main issue is how to handle http-header data. – Sewon Kyle Park Dec 16 '14 at 01:31
  • you can use a HEAD request to get headers, but i don't see how.why you need headers; img.src will reveal the address of the server, even after a re-direct. – dandavis Dec 16 '14 at 01:33
  • For example, when we use 'youtube.com', many resources are received(html, css, vedie clips, img... etc), but, the server's ip-addresses are different on every entering to 'youtube.com'.. sometime, ip-addr will be 1.1.1.1, sometime, will be 2.2.2.2, ... and referd resources has different server-ip also. . so, http-header which contain remote address, can distinguish. – Sewon Kyle Park Dec 16 '14 at 01:49
  • 'img.src' not always has ip-address. sometime, it has like 'www.foo.com/img/blah/blah2' sometime, '0.0.0.0/blah/blah2' so I need remote address, from http-header – Sewon Kyle Park Dec 16 '14 at 01:51
  • if in case of 'www.foo.com/blah' , also can has different address, because of having many web-server and considering network traffic. – Sewon Kyle Park Dec 16 '14 at 01:54

1 Answers1

1

No, it is not possible to retrieve the IP address from the HTTP headers. Reason being, the remote address is not part of the HTTP request or response headers. Your only option is making an ajax request to a DNS resolution API, and use that to resolve the host-name to an IP address. Here is an example using Statdns.com:

function hostToIp(host, callback) {
    function onComplete() {
        var res = JSON.parse(this.responseText);
        var ip = res.answer[0].rdata;
        callback(ip);
    }
    var oReq = new XMLHttpRequest();
    oReq.onload = onComplete;
    oReq.open("GET", "http://api.statdns.com/" + host + "/a", true);
    oReq.send();
}

console.time('resolve ip');
hostToIp('google.com', function(res){
    document.write(res);
    console.timeEnd('resolve ip');
});

As you can see, the request typically resolves in under 300ms, and is non-blocking. So I don't think it will negatively impact performance.

levi
  • 23,693
  • 18
  • 59
  • 73
  • great! it is almost what i wanted, but, i want to check the each resource's address. It seems only in case of host, like google.com or youtube.com etc.. Is it also possible? like 'http:/www.google.com/images/A.png' ?? – Sewon Kyle Park Dec 16 '14 at 06:18
  • Yes. Just first extract the hostname from the URL. See here - http://stackoverflow.com/questions/8498592/extract-root-domain-name-from-string – levi Dec 16 '14 at 14:24
  • Oh, sorry to forget it. Thx. – Sewon Kyle Park Dec 18 '14 at 02:28