Gossamer Forum
Home : General : Perl Programming :

Fetching HTTP Status Codes

Quote Reply
Fetching HTTP Status Codes
I've been playing with a CGI-Perl (non-mod_perl) script to fetch the Apache HTTP Status Code using the HTTP::Status module, but with no luck. The purpose of this ordeal is to allow the script to determine if it was called from the browser (code=200), or from the server (as redirect) via .htaccess RewriteRule (code=302). Any suggestions?

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] Fetching HTTP Status Codes In reply to
Why not use something like this?

Code:
my $url = "http://www.site.com/folder/page.html";

# Create a request
my ($host, $port, $path, $sock, $line, $status, $protocol);
my $cr = "\015\012";

($url =~ m,^http://([^:/]+):?(\d*)(.*),i) and (($host, $port, $path) = ($1, $2, $3));
$path ||= '/';
$port ||= 80;
$path =~ s/#.*//;

$host;
use IO::Socket;
$sock = new IO::Socket::INET ( PeerAddr => $host,
PeerPort => 80,
Proto => 'tcp',
Type => SOCK_STREAM,
) or die undef, $@;
$sock->autoflush(1);
$sock->timeout(5);
print $sock "GET $path HTTP/1.0$cr";
print $sock "Host: $host$cr";
print $sock "User-Agent: Links SQL $cr$cr";
my $response = <$sock>;
($protocol, $status) = split / /, $response;
close $sock;

print $status;

$status should hold the value of the page that was called.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Fetching HTTP Status Codes In reply to
Nope. Perhaps I shouldn't have used the term fetch in this context. Here's the chain of events (and they all occur on the same server):

1. RewriteRule redirects user to script.cgi (HTTP Status Code=302)

2. Then script.cgi pulls HTTP Status Code. If 200 (i.e., user called script directly from the browser), script aborts. If 302, script executes.

Any GT staffers want to take a stab at it.

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] Fetching HTTP Status Codes In reply to
*gentle bump*

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln