André Warnier wrote:

André Warnier wrote:


John ORourke wrote:
Hi folks,

I can't seem to find a way to retrieve the inbound port number during requests.

I have a server listening on multiple ports, and need to know which one the request came in on. Here's the setup:

apache config:
   Listen 127.0.0.1:81
   Listen 127.0.0.1:82
   NameVirtualHost *:*
   <VirtualHost *:*>
      ......
   </VirtualHost>

I believe you could resolve this as follows (although it is really a "brute force" method) : Instead of one <VirtualHost *:*>, you could define 2 sections, entirely copy of eachother except for :

<VirtualHost *:81>
  PerlSetVar ConnType "HTTP"
  ....
</VirtualHost>

<VirtualHost *:82>
  PerlSetVar ConnType "HTTPS"
  ....
</VirtualHost>

and then in your script/handler get the ConnType config value to tell the difference.


Addendum :
Apart from he brute force method above, I believe there must be numerous other ways to achieve best what you really need. For instance, I would have a look at the Apache SetEnvIf configuration directive, which may be used to set an environment variable later retrieved by your script/module. I would also imagine that a HTTPS request already includes some specific HTTP headers which a HTTP request does not have, and you could test for that (either in the script/module or with SetEnvIf). Which method really works best in your case, and which is the most efficient, is left as an exercise to the reader.


Addendum # 2 :

Here is a more mod_perl-ish solution :

  use Apache2::RequestRec ();
  use Apache2::Connection ();
  use APR::SockAddr ();

sub handler {

my $r = shift;
my $c = $r->connection;
my $serverport = $c->local_addr->port;

# ...  and now you know

}

Refs :
http://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html#C_connection_
http://perl.apache.org/docs/2.0/api/Apache2/Connection.html#C_local_addr_
http://perl.apache.org/docs/2.0/api/APR/SockAddr.html

Note : I am deriving this purely from the documentation, and haven't tried it myself yet.

Reply via email to