View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2016, University of Amsterdam,
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(http_host,
   37          [ http_public_url/2,          % +Request, -PublicURL
   38            http_public_host_url/2,     % +Request, -HostURL
   39            http_public_host/4,         % +Request, -Host, -Port, +Options
   40                                        % deprecated
   41            http_current_host/4         % +Request, -Host, -Port, +Options
   42          ]).   43:- use_module(library(http/thread_httpd)).   44:- use_module(library(http/http_wrapper)).   45:- use_module(library(socket)).   46:- use_module(library(option)).   47:- use_module(library(settings)).   48
   49:- setting(http:public_host, atom, '',
   50           'Name the outside world can use to contact me').   51:- setting(http:public_port, integer, 80,
   52           'Port on the public server').   53:- setting(http:public_scheme, oneof([http,https]), http,
   54           'Default URL scheme to use').   55
   56:- predicate_options(http_public_host/4, 4, [global(boolean)]).   57:- predicate_options(http_current_host/4, 4,
   58                     [pass_to(http_public_host/4, 4)]).

Obtain public server location

This library finds the public address of the running server. This can be used to construct URLs that are visible from anywhere on the internet. This module was introduced to deal with OpenID, where a request is redirected to the OpenID server, which in turn redirects to our server (see http_openid.pl).

The address is established from the settings http:public_host and http:public_port if provided. Otherwise it is deduced from the request. */

 http_public_url(+Request, -URL) is det
True when URL is an absolute URL for the current request. Typically, the login page should redirect to this URL to avoid losing the session.
   78http_public_url(Request, URL) :-
   79    http_public_host_url(Request, HostURL),
   80    option(request_uri(RequestURI), Request),
   81    atomic_list_concat([HostURL, RequestURI], URL).
 http_public_host_url(+Request, -URL) is det
True when URL is the public URL at which this server can be contacted. This value is not easy to obtain. See http_public_host/4 for the hardest part: find the host and port.
   91http_public_host_url(Request, URL) :-
   92    http_public_host(Request, Host, Port,
   93                      [ global(true)
   94                      ]),
   95    setting(http:public_scheme, Scheme),
   96    (   scheme_port(Scheme, Port)
   97    ->  format(atom(URL), '~w://~w', [Scheme, Host])
   98    ;   format(atom(URL), '~w://~w:~w', [Scheme, Host, Port])
   99    ).
  100
  101scheme_port(http, 80).
  102scheme_port(https, 443).
 http_public_host(?Request, -Hostname, -Port, +Options) is det
Current global host and port of the HTTP server. This is the basis to form absolute address, which we need for redirection based interaction such as the OpenID protocol. Options are:
global(+Bool)
If true (default false), try to replace a local hostname by a world-wide accessible name.

This predicate performs the following steps to find the host and port:

  1. Use the settings http:public_host and http:public_port
  2. Use X-Forwarded-Host header, which applies if this server runs behind a proxy.
  3. Use the Host header, which applies for HTTP 1.1 if we are contacted directly.
  4. Use gethostname/1 to find the host and http_current_server/2 to find the port.
Arguments:
Request- is the current request. If it is left unbound, and the request is needed, it is obtained with http_current_request/1.
  129http_public_host(_Request, Host, Port, _) :-
  130    setting(http:public_host, PublicHost), PublicHost \== '',
  131    !,
  132    Host = PublicHost,
  133    setting(http:public_port, Port).
  134http_public_host(Request, Host, Port, Options) :-
  135    (   var(Request)
  136    ->  http_current_request(Request)
  137    ;   true
  138    ),
  139    (   memberchk(x_forwarded_host(Forwarded), Request)
  140    ->  Port = 80,
  141        primary_forwarded_host(Forwarded, Host)
  142    ;   memberchk(host(Host0), Request),
  143        (   option(global(true), Options, false)
  144        ->  global_host(Host0, Host)
  145        ;   Host = Host0
  146        ),
  147        option(port(Port), Request, 80)
  148    ),
  149    !.
  150http_public_host(_Request, Host, Port, _Options) :-
  151    gethostname(Host),
  152    http_current_server(_:_Pred, Port).
 http_current_host(?Request, -Hostname, -Port, +Options) is det
deprecated
- Use http_public_host/4 (same semantics)
  158http_current_host(Request, Hostname, Port, Options) :-
  159    http_public_host(Request, Hostname, Port, Options).
 primary_forwarded_host(+Spec, -Host) is det
x_forwarded host contains multiple hosts seperated by ', ' if there are multiple proxy servers in between. The first one is the one the user's browser knows about.
  167primary_forwarded_host(Spec, Host) :-
  168    sub_atom(Spec, B, _, _, ','),
  169    !,
  170    sub_atom(Spec, 0, B, _, Host).
  171primary_forwarded_host(Host, Host).
 global_host(+HostIn, -Host)
Globalize a hostname. Used if we need to pass our hostname to a client and expect the client to be able to contact us. In this case we cannot use a name such as `localhost' or the plain hostname of the machine. We assume (possibly wrongly) that if the host contains a '.', it is globally accessible.

If the heuristics used by this predicate do not suffice, the setting http:public_host can be used to override.

  185global_host(_, Host) :-
  186    setting(http:public_host, PublicHost), PublicHost \== '',
  187    !,
  188    Host = PublicHost.
  189global_host(localhost, Host) :-
  190    !,
  191    gethostname(Host).
  192global_host(Local, Host) :-
  193    sub_atom(Local, _, _, _, '.'),
  194    !,
  195    Host = Local.
  196global_host(Local, Host) :-
  197    tcp_host_to_address(Local, IP),
  198    tcp_host_to_address(Host, IP)