Wrong Server Port

F

FeelLikeANut

I have a Web server on my comp set to use a port other than 80 for my
own testing purposes. This is making a problem for creating a self-
referencing URL, but I don't understand why. The CGI module works. I
looked at its source, and it seems to retrieve the port number through
$ENV{SERVER_PORT}, but when I access this environment variable in the
main body of my program, it is always incorrectly reporting port 80.

I'm sure someone will ask me why I don't just use the CGI module. It's
because I'm using CGI::Minimal instead, and because accessing this one
environment variable really should not be a big deal.

Can someone help me understand why $ENV{SERVER_PORT} reports
differently in the CGI module and in my main program? Thanks.
 
M

Mumia W.

I have a Web server on my comp set to use a port other than 80 for my
own testing purposes. This is making a problem for creating a self-
referencing URL, but I don't understand why. The CGI module works. I
looked at its source, and it seems to retrieve the port number through
$ENV{SERVER_PORT}, but when I access this environment variable in the
main body of my program, it is always incorrectly reporting port 80.

I'm sure someone will ask me why I don't just use the CGI module. It's
because I'm using CGI::Minimal instead, and because accessing this one
environment variable really should not be a big deal.

Can someone help me understand why $ENV{SERVER_PORT} reports
differently in the CGI module and in my main program? Thanks.

use strict;
use warnings;
use Data::Dumper;

print "Content-Type: text/plain\n\n";

my $selfuri = "http://$ENV{HTTP_HOST}$ENV{REQUEST_URI}";

print "Self: $selfuri\n";
print '--------------------------------',"\n";
print "Why not look at ALL of the environment variables?\n";
print '--------------------------------',"\n";
print Dumper(\%ENV);

__HTH__
 
F

FeelLikeANut

use strict;
use warnings;
use Data::Dumper;

print "Content-Type: text/plain\n\n";

my $selfuri = "http://$ENV{HTTP_HOST}$ENV{REQUEST_URI}";

print "Self: $selfuri\n";
print '--------------------------------',"\n";
print "Why not look at ALL of the environment variables?\n";
print '--------------------------------',"\n";
print Dumper(\%ENV);

__HTH__

I don't want to use HTTP_HOST or HTTP_X_FORWARDED_HOST or
HTTP_*anything*, because those are set by the browser, not the server,
and I'd much rather not rely on them.

The CGI module uses SERVER_PORT, and the CGI module works. I was
hoping people could help inform me why this is. (The CGI module, btw,
is a whole lot of Perl code. Just because the code targets another
kind of technology does not magically remove all relatedness to Perl.)
 
F

FeelLikeANut

On Feb 14, 6:01 pm, "Mumia W." <paduille.4060.mumia.w
(snipped)

I don't want to use HTTP_HOST or HTTP_X_FORWARDED_HOST or
HTTP_*anything*, because those are set by the browser, not the server,
and I'd much rather not rely on them.
The CGI module uses SERVER_PORT, and the CGI module works. I was
hoping people could help inform me why this is. (The CGI module, btw,
is a whole lot of Perl code. Just because the code targets another
kind of technology does not magically remove all relatedness to Perl.)

Take another look at CGI.pm. It does check for HTTP_HOST, etc.
The 'url' method calls the 'http' method and uses
$ENV{HTTP_HOST} under certain conditions. In fact it
seems to prefer HTTP_HOST (if found) over SERVER_PORT:

'url' => <<'END_OF_FUNC',
sub url {
my($self,@p) = self_or_default(@_);
my ($relative,$absolute,$full,$path_info,$query,$base,$rewrite) =
rearrange(['RELATIVE','ABSOLUTE','FULL',['PATH','PATH_INFO'],
['QUERY','QUERY_STRING'],'BASE','REWRITE'],@p);
my $url = '';
$full++ if $base || !($relative || $absolute);
$rewrite++ unless defined $rewrite;

my $path = $self->path_info;
my $script_name = $self->script_name;
my $request_uri = unescape($self->request_uri) || '';
my $query_str = $self->query_string;

my $rewrite_in_use = $request_uri && $request_uri !~ /^
$script_name/; undef $path if $rewrite_in_use && $rewrite; # path
not valid when rewriting active
my $uri = $rewrite && $request_uri ? $request_uri :
$script_name;

if ($full) {
my $protocol = $self->protocol();
$url = "$protocol://";
my $vh = http('x_forwarded_host') || http('host'); # <<<<<<<
LOOK HERE <<<<<<
if ($vh) {
$url .= $vh;
} else {
$url .= server_name();
my $port = $self->server_port;

# ...

'http' => <<'END_OF_FUNC',
sub http {
my ($self,$parameter) = self_or_CGI(@_);
return $ENV{$parameter} if $parameter=~/^HTTP/;
$parameter =~ tr/-/_/;
return $ENV{"HTTP_\U$parameter\E"} if $parameter; # <<<<<< LOOK
HERE <<<<<<<
my(@p);
foreach (keys %ENV) {
push(@p,$_) if /^HTTP/;
}
return @p;}

END_OF_FUNC

Thanks, Steven. That helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top