Rav has asked for the wisdom of the Perl Monks concerning the following question:

Greets all, I've been trying to knock this one for a while, but no luck yet... Any help you all could give would be totally appreciated!

I am building a perl-based web client that retrieves web pages in the same manner a browser retrieves web pages, but my program is started from the linux command line. The problem is, I also want to display the current values of the HTTP Environment/Session Variables when each page is retrieved and display them with the page (e.g. If the page I am retrieving comes from server 192.241.212.14, I would like to read the SERVER_ADDR Environment variable to see that). I have tried accessing it directly through the %Env hash, as I would if this were a CGI program:
foreach $var (sort(keys(%ENV))) { $val = $ENV{$var}; $val =~ s|\n|\\n|g; $val =~ s|"|\\"|g; print "${var}=\"${val}\"\n";
But instead of HTTP Environment Variables, I get Linux Shell Variables. Any light the divine perlmonks could shed on this issue is, again, received with complete gratitude :) Thanks!
-Rav

Replies are listed 'Best First'.
Re: Reading HTTP Environment Variables from web Clients point of view
by archon (Monk) on Feb 21, 2001 at 10:33 UTC
    You are confused about the meanings of %ENV and 'Environment.' %ENV contains the contents of your current environment. The 'Linux Shell Variables' you mentioned are your shell environment variables. The reason you see different contents when you run the script as a CGI is because the entire environment is different and the web server sets those values for you. Environment variables are just what the name implies: variables particular to whatever environment you currently reside in.

    HTH, HAND.

Re: Reading HTTP Environment Variables from web Clients point of view
by dws (Chancellor) on Feb 21, 2001 at 11:35 UTC
    If by "HTTP Environment/Session Variables" you mean those that are available to CGI scripts, then you're going to need to set those up yourself. A web server sets these up for a CGI script in part by mapping HTTP tags to environment variables (where they'll be availabe in %ENV).

    This mapping isn't done automatically on the client side. You're writing the client, so you're going to have to do this yourself (by mapping the HTTP tags you see in a response). And a web server won't respond with the same HTTP tags that a client sends (e.g., a web server won't send If-modified-since:), so you won't have quite the same information to work with.

    If this isn't clear, consider the question of how the OS knows to set up environment variable based on a stream of data that your client is reading through a socket. The answer is, it doesn't.

Re: Reading HTTP Environment Variables from web Clients point of view
by sierrathedog04 (Hermit) on Feb 21, 2001 at 21:06 UTC
    I am building a perl-based web client that retrieves web pages in the same manner a browser retrieves web pages, but my program is started from the linux command line.
    You may be already aware of it, but the Lynx browser does exactly what you describe above. Moreover, there would be a variety of ways to insert the additional info you seek without redoing Lynx's functionality. (For instance, you could create a local static web page with frames. The top frame would display your info, the bottom whatever URL the user wanted)

    If you are running Red Hat 5.1 Linux, you will find that right out of the box RH installs Lynx. (RH also installs an instance of Apache that starts automatically at bootup and is visible from Lynx as http://localhost.) Others can get Lynx here.