Er perlen i orden?
HejsaJeg mangler lidt flere øjne til at se på et lille cgi-script på en ~120 linier.
Jeg tror jeg kan gøre nogle af tingene bedre, men kender ikke perl godt nok til at vide hvordan/hvor.
Er det helt hen i vejret "slam" eller kan du se en mening med galskaben?
PS: Det er til brug med Googiespell (baseret på Google) på en unix server og skal inkludere mulighed for simpel logging af requests.
spellcheck.cgi:
#!/local/bin/perl
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
#-----------------------------------------------------------------------------
my $log = 1; # global option
my $log_folder = "/data/www/test.domain.dk/cgi-bin/spellcheck/data/";
my $log_brief = 0;
my $log_all = 1; # log _all_ requests
my $log_file = "common.xml";
my $log_users = 1; # log specific users
my %log_userA = ("testuser");
my $log_ips = 1; # log specific ips
my %log_ipA = ("192.168.1.1");
#-----------------------------------------------------------------------------
#testLog();
main();
#-----------------------------------------------------------------------------
sub main {
my $ua = LWP::UserAgent->new(agent => 'GoogieSpell Client');
my $reqXML = "";
read (STDIN, $reqXML, $ENV{'CONTENT_LENGTH'});
logSpellcheck($reqXML); # log before sending request
my $url = "https://www.google.com/tbproxy/spell?$ENV{QUERY_STRING}";
my $res = $ua->request(POST $url, Content_Type => 'text/xml', Content => $reqXML);
print "Content-Type: text/xml\n\n";
print $res->{_content};
exit 0;
}
#-----------------------------------------------------------------------------
sub testLog {
# skipping all other - test w. bogus xml
logSpellcheck("<xml><option>hoho</option></xml>");
print "Content-Type: text/html\n\n";
print "finished-->". $ENV{REMOTE_USER} ."--". $ENV{REMOTE_ADDR} ."<--";
exit 0;
}
#-----------------------------------------------------------------------------
sub logSpellcheck {
my ($xml) = @_;
return 0 unless $log && $xml; # save processing if logging is off or no input
my $timestamp = getTimestamp();
{ # log to files
if ($log_all == 1) {
writeLog($xml, $log_folder . $log_file);
}
if ($log_users == 1 && exists($log_userA{$ENV{REMOTE_USER}})) {
writeLog($xml, $log_folder . "user_" . $ENV{REMOTE_USER} . ".xml");
}
if ($log_ips == 1 && exists($log_ipA{$ENV{REMOTE_ADDR}})) {
writeLog($xml, $log_folder . "ip_" . $ENV{REMOTE_ADDR} . ".xml");
}
}
}
#-----------------------------------------------------------------------------
sub writeLog {
my ($msg, $file) = @_;
$msg = buildXML($msg);
{ # specific for this "xml" logging - surround with global usage tag and xml tag
unless ( -e $file ) {
$msg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><usage>\n" . $msg;
}
else {
# delete last line of existing file (</usage>)
my $addr;
open (FH, "+< $file");
while ( <FH> ) {
$addr = tell(FH) unless eof(FH);
}
truncate(FH, $addr);
close FH;
}
$msg .= "\n</usage>";
}
open(LOG, ">>$file") || return;
print LOG "$msg";
close(LOG);
}
#-----------------------------------------------------------------------------
sub buildXML {
my ($xml) = @_;
{ # replace input if brief logging
if ($log_brief == 1) {
$xml = "<bogus>";
}
}
{ # build and insert log entry to xml
my $pos = index($xml, ">");
if ($pos > 0) {
substr($xml, 0, $pos + 1, "<spellcheck><log><time>". $timestamp ."</time><ip>". $ENV{REMOTE_ADDR} ."</ip><user>". $ENV{REMOTE_USER} ."</user><ref>". $ENV{'HTTP_REFERER'} ."</ref><length>". $ENV{'CONTENT_LENGTH'} ."</length></log>\n");
$xml .= "</spellcheck>";
}
}
return $xml;
}
#-----------------------------------------------------------------------------
sub getTimestamp() {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
return sprintf("%02d:%02d:%02d %02d-%02d-%4d", $hour, $min, $sec, $mday, $mon+1, $year+1900);
}
#-----------------------------------------------------------------------------