Wrong Server Port

Discussion in 'Perl Misc' started by FeelLikeANut@gmail.com, Feb 14, 2007.

  1. Guest

    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.
     
    , Feb 14, 2007
    #1
    1. Advertising

  2. Mumia W. Guest

    On 02/14/2007 01:49 PM, wrote:
    > 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__


    --
    Windows Vista and your freedom in conflict:
    http://www.securityfocus.com/columnists/420/2
     
    Mumia W., Feb 14, 2007
    #2
    1. Advertising

  3. Guest

    On Feb 14, 6:01 pm, "Mumia W." <paduille.4060.mumia.w
    > wrote:
    > On 02/14/2007 01:49 PM, wrote:
    >
    > > 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__
    >
    > --
    > Windows Vista and your freedom in conflict:http://www.securityfocus.com/columnists/420/2


    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.)
     
    , Feb 15, 2007
    #3
  4. Guest

    On Feb 14, 9:04 pm, ""
    <> wrote:
    > On Feb 14, 4:53 pm, wrote:
    >
    > > 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
    >
    > --
    > Hope this helps,
    > Steven


    Thanks, Steven. That helps.
     
    , Feb 15, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John T. Goodman

    Overhead of 4-port over 2-port SRAM

    John T. Goodman, Jan 25, 2005, in forum: VHDL
    Replies:
    0
    Views:
    608
    John T. Goodman
    Jan 25, 2005
  2. Sean Wolfe
    Replies:
    1
    Views:
    2,263
    Joerg Jooss
    Apr 28, 2005
  3. b3ny
    Replies:
    11
    Views:
    929
    Babu Kalakrishnan
    Nov 20, 2004
  4. Gerald Klix
    Replies:
    0
    Views:
    1,278
    Gerald Klix
    Oct 26, 2005
  5. stewart
    Replies:
    1
    Views:
    4,215
    willvv
    Mar 12, 2008
Loading...

Share This Page