Given a target URI, how can I programmatically determine whether an HTTP GET of that URI would be making a request to the local machine?
Context: There are two reasons I need to do this. One is that I have a mod_perl2 application that responds to HTTP requests. In doing so, it sometimes needs to make an HTTP request to retrieve some data from a target URI. To avoid an infinite recursion of HTTP requests, I need to avoid making the HTTP request if the target URI would actually resolve to the current machine. This is to prevent users from accidentally shooting themselves in the foot. It is not intended as a security check.
The second reason is that, if my application receives an HTTP request, I need to look up some metadata using the request URI as key. The problem is that any of several URI synonyms could have been used as keys in creating the metadata, so I need a way to resolve the synonyms, but only for URIs on the local host machine.
The problem is not as simple as looking at the URI to see if the domain is "localhost", or its IP address is 127.0.0.1 (or 127.0.1.1 or 127.*), because: (a) the target URI might use a fully qualified domain name (e.g., foo.example.com) that resolves to an IP address on the current machine; and (b) a machine can have several IP addresses.
The OS must have the information needed to figure this out, since it has to know the IP addresses and ports on which it listens. This post discusses the problem of trying to determine the local machine's IP address (or addresses, since it may have several). Maybe I could do that to determine the local machine's IP addresses, and then perhaps I could compare those IP addresses against the IP address in the target URI (or the IP address returned by gethostbyname of the URI's domain). Do I really need to do that? Are there problems with that approach? Is there a better way?
This post indicates that C# has a function HttpContext.Current.Request.IsLocal to do what I need, but I have been unable to find anything similar in perl.
I previously asked this question on perlmonks.org (because I'm using perl) but found no satisfactory answer. If there is a solution available in some other programming language that is commonly available on Linux, such as C, bash or python, that would be adequate also. I do not need a solution that is guaranteed to work in every possible case, but it would be nice if it would work for most cases.