Gossamer Forum
Home : General : Perl Programming :

Am I going mad?

Quote Reply
Am I going mad?
Ok... am I going mad here? Basically, the following routine *should* check to see the output of the website grabbed. In this case, it is an image grabbed from Links SQL, via jump.cgi. If the file doesn't exist, the page should contain 'Unknown' ... which the regex should pick up. For some reason though, it always returns 1! Can anyone see my boo-boo?

Code:
# routine to see if image exists...
sub check_image_exists {

my $id = $_[1];
my $field = $_[0];

my $grabbed = get("http://www.site.com/cgi-bin/vehicles/jump.cgi?ID=$id&view=$field");
$grabbed =~ s/\n//g;

my $got = 1;
if ($grabbed =~ /Unknown/si) { $got = 0; }

$got ? return 1 : return 0;

}

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] Am I going mad? In reply to
Hi Andy

What returns 'Unkown'? What's the code behind 'get'?

Haven't the LWP set of modules got an if $exists; or something similar that return actual HTTP error codes and/or success codes?

Code:
my $id = $_[1];
my $field = $_[0];

I would re-write that as:

Code:
my $field = shift;
my $id = shift;

Cheers

- wil
Quote Reply
Re: [Wil] Am I going mad? In reply to
Thinking about this logic again, surely this will always return a success? Because jump.cgi exists, therefore whenever you request jump.cgi?random-variables-here it will always return as a success, no matter what.

I think you need to resolve where jump.cgi?ID=x points to, check that actual URL and then go from there.

- wil
Quote Reply
Re: [Wil] Am I going mad? In reply to
Oh, and remember to use print statemements while you're debugging. That is, maybe try and print $grabbed and see what is actually stored in that variable.

- wil
Quote Reply
Re: [Wil] Am I going mad? In reply to
Sorry, I should have explained it a bit better. The get() command is using LWP::Simple. I am grabbing the page, and trying to see if it includes the 'Unknown' string in it. The desired output is an image, so if it doesn't return a page with 'Unknown', then the image must exist.

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: [Wil] Am I going mad? In reply to
Quote:
I would re-write that as:

my $field = shift;
my $id = shift;

That would work too, but Andy's code is slightly quicker as it simply copies one variable to another whereas your example is calling the shift() function twice. Another possible example would be:

Code:
my ($field, $id) = @_;

...which works in the same way as Andy's code but is just a little shorter.

Andy:

The first thing to do is print the output from your GET request to see what *actually* is being returned and then work from there.
Quote Reply
Re: [Andy] Am I going mad? In reply to
Where do you get the idea that the page will return an 'Unkown'?

Don't you want to use the LWP::Simple built-in function if_success to check for success or not? Probably a lote more reliable than a custom regex?

- wil
Quote Reply
Re: [Mizuno] Am I going mad? In reply to
Yes, I just 'prefer' my example as I find it easier to follow what's going on. Although your second example is probably the best practice for this case.

- wil
Quote Reply
Re: [Wil] Am I going mad? In reply to
>>>Where do you get the idea that the page will return an 'Unkown'? <<<

That won't work. Either way, its going to return true...because the page *is* grabbed.... This is Links SQL... so jump.cgi will show an error page (which is where I'm getting the 'Unknown' string from). If the file doesn't exist, it will show something like;

Quote:
There was an error.
Unknown field for ID 25

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] Am I going mad? In reply to
Code:
my $got;

if ($grabbed =~ /Unknown/g) {
$got = 0;
}
else {
$got = 1;
}

?

- wil

Last edited by:

Wil: Sep 1, 2003, 4:12 AM
Quote Reply
Re: [Wil] Am I going mad? In reply to
Quote:
Don't you want to use the LWP::Simple built-in function if_success to check for success or not? Probably a lote more reliable than a custom regex?

That may not work as jump.cgi will return html whether the request fails or succeeds (apart from more serious errors such as 400, 403, 500 etc) and so is_success will always be true.

Even though jump.cgi may return an error page, it will still be considered a successful request.
Quote Reply
Re: [Mizuno] Am I going mad? In reply to
Yes, I have very little knowledge of how jump.cgi works and has assumed that jump.cgi will always refer the call to the URL in the database regardles of if the URL exists or not.

- wil
Quote Reply
Re: [Mizuno] Am I going mad? In reply to
Ok...maybe the page isn't being grabbed.

Code:
# routine to see if image exists...
sub check_image_exists {

my $id = $_[1];
my $field = $_[0];

my $url = "http://www.site.com/cgi-bin/vehicles/jump.cgi?ID=131&view=Image2_thumbnail";
my $grabbed = get($url);
$grabbed =~ s/\n//g;

print $IN->header();
print "HTML: " . $grabbed;

}

prints;

Quote:
HTML: HTML:

I've tried;

$grabbed = LWP::Simple::get($url);

and

$grabbed = get($url);

Wonder why its not grabbing the page :/ If I manually enter the URL in a browser... it shows fine.

Eugh!

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: [Wil] Am I going mad? In reply to
You won't need the /g in the regex as you are only searching for one match and aren't doing anything such as a global serach and replace.

If the case of the word is constant then using index() will be quicker than loading the regex engine...eg....

Code:
$got = 0 unless (index($grabbed, 'Unknown') > -1);
Quote Reply
Re: [Andy] Am I going mad? In reply to
I'd use LWP::UserAgent as it is more advanced. You can then use $response->code to see the status of your request, which helps significantly with debugging.
Quote Reply
Re: [Mizuno] Am I going mad? In reply to
Getting there. I'm now using;

Code:
my $url = "http://www.site.com/cgi-bin/vehicles/jump.cgi?ID=$id&view=$field";

my ($grabbed,$type);

use LWP;
my $browser = LWP::UserAgent->new;
my $response = $browser->get( $url );
if ($response->content_type eq 'image/gif' || $response->content_type eq 'image/jpg') {
$type = "Image";
} else {
$type = "None";
}

print $IN->header();
print "HTML: " . $response->content_type;

... the problem is that it always returns text/html ... even if the output is image/gif Unsure Any ideas? Preferaby I would like to grab the page, and do the check with it... to see if the string exists.

TIA

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: [Mizuno] Am I going mad? In reply to
Quote:
You won't need the /g in the regex as you are only searching for one match and aren't doing anything such as a global serach and replace.

Yeah, I know. I was just putting it in there to see if it matched anything. It didn't change anything Frown

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] Am I going mad? In reply to
THere must be something amiss. This code prints image/gif for me....

Code:

use strict;
use LWP::UserAgent;
use HTTP::Request::Common qw(GET);

my $url = 'http://us.i1.yimg.com/us.yimg.com/i/ww/m6v9b.gif';
my $uag = LWP::UserAgent->new(timeout => 30);
my $res = $uag->request(GET $url);

print "Content-type: text/html\n\n";
print $res->content_type;
Quote Reply
Re: [Mizuno] Am I going mad? In reply to
Don't forget its going through jump.cgi ... maybe that prints a text/html header first?

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: [Mizuno] Am I going mad? In reply to
In admin/Links/User/Jump.pm, there is;

Code:
if ($IN->param('v') or $IN->param('view')) { # Viewing
print $IN->header(
'-type' => $fh->File_MimeType,
'-Content-Length' => $fh->File_Size,
'-Content-Disposition' => \("inline; filename=" . $IN->escape($fh->File_Name))
);
}


... should the following not change the header type to image/gif (or whatever the image type i);

Code:
'-type' => $fh->File_MimeType,

Unsure

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: [Mizuno] Am I going mad? In reply to
Got it working Smile

Code:
my $id = $_[1];
my $field = $_[0];

my ($request,$response,$ua);
my $thisurl = 'http://www.site.co.uk/cgi-bin/vehicles/jump.cgi';

$ua = new LWP::UserAgent;
$ua->agent("andys/1.1");
$ua->env_proxy;
$response = $ua->request(POST $thisurl, [view => $field, ID => $id]);
my $grabbed = $response->as_string();

my $got = 1;
$got = 0 unless (index($grabbed, 'Unknown') > -1);

$got ? return 1 : return 0;

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] Am I going mad? In reply to
You could write this:

>>
$got ? return 1 : return 0;
<<

as:

return $got ? 1 : 0;

Are you sure you need to call env_proxy?
Quote Reply
Re: [Mizuno] Am I going mad? In reply to
Cool... I didn't know that. Not that it would make it that much faster Tongue

>>>Are you sure you need to call env_proxy? <<<

Nope. I've already removed that in the final version.

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] Am I going mad? In reply to
Just looks neater though =)