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

I'm using the Image:Magick Read() to grab images off a remote server.

$image = Image::Magick->new; $url = "$Variable1/$Variable2/Filename.jpg"; $image->Read($url);

When I do this to an image within a publicly visible folder, everything works wonderfully: I am able to use Get() to assign the attributes of that remote image to variables.

However, when I try to do this to an image within a folder that is within a protected area requiring htpasswd authentication (such as a Member's Area), the Read($url) fails.

What I am looking for is a means to provide my own username and password to authenticate my script to access these protected files remotely

I have been scouring the internet to find a solution to this. Unfortunately, I am not sure exactly what approach or solution I need to be searching for. Is there a separate package that allows me to authenticate as a user, or something that allows Image::Magick to define user/pass access, or something entirely different altogether.

Any help on this matter would be GREATLY appreciated!

Thanks in advance, perlmonks community.

Replies are listed 'Best First'.
Re: Trying to allow cgi script to access htpasswd protected files.
by kschwab (Vicar) on Nov 14, 2013 at 03:21 UTC
    Try this type of url maybe?
    http://username:password@site.com/rest/of/url
    If that doesn't work, LWP can fetch basic auth pages into a string, then you can do:
    $image->Read(blob=>$string);
Re: Trying to allow cgi script to access htpasswd protected files.
by tangent (Parson) on Nov 14, 2013 at 10:57 UTC
    As others have suggested, you could use LWP::UserAgent. This allows you to set the authorization header via HTTP::Headers. You could then transfer the image to a local directory and feed that to Image::Magick.
    use LWP::UserAgent; use Image::Magick; my $url = 'path/to/remotefile'; my $localfile = 'path/to/localfile'; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new; $req->authorization_basic('username', 'pass'); $ua->get( $url, :content_file => $localfile ); # do some checking here my $image = Image::Magick->new; $image->Read($localfile);
    - Untested -
Re: Trying to allow cgi script to access htpasswd protected files.
by taint (Chaplain) on Nov 14, 2013 at 04:07 UTC
    I've given this some more thought. I'm afraid I'm at a bit of a loss, as I'm not privy to your entire script. So based on the info I have. I can only best suggest. That you use a session-based auth setup. One that performs a re-direct, if not already logged in. That way, if you attempt to initiate your Image::Magick script when not already loggen in. You get a redirect to the logon page. Then, after being looged on, the page provides a link to your Image::Magick script.

    Not the most specific answer/solution. But without a bit more info. That's the best I'm prepared to offer.

    HTH

    --Chris

    #!/usr/bin/perl -Tw
    use Perl::Always or die;
    my $perl_version = (5.12.5);
    print $perl_version;

      kschwab’s answer was on point and probably right/workable; though without https:// also a bit dangerous. That style of URL works fine with HTTP Basic Authentication. Without LWP::UserAgent or WWW::Mechanize or other code samples, your answer is pretty hand-wavy, as they say on the innertubes.

Re: Trying to allow cgi script to access htpasswd protected files.
by Anonymous Monk on Nov 14, 2013 at 22:40 UTC

    Thank you for your responses!

    I haven't had any time to work on this script today, but will be doing so again tomorrow. Hopefully by the end of tomorrow and early next week at the latest I'll be able to provide feedback to elaborate on my issue/situations I'm still encountering, or hopefully display a solution that worked for me!

    Look forward to sitting down and trying to implement what you guys have suggested. I know I've already tried the 'http://username:password@site.com/rest/of/url' approach that kschwab suggested and it seemed to not work. Perhaps I implemented it wrong, but it seemed fairly straightforward. Later on I'll copy some direct code in here to show you what failed for me.

    I've also poked at LWP but probably not to the extents I may have needed. Furthermore, tangent, you reference a $localfile. I'm afraid I'm not sure what the localfile is supposed to be pointing to.

    Thanks again for the ideas and comments - it's my first time posting here and I'm happy to see there is an active and HELPFUL community.

      In case it wasn't obvious - I wasn't logged in when I posted this.

Re: Trying to allow cgi script to access htpasswd protected files.
by taint (Chaplain) on Nov 14, 2013 at 00:41 UTC
    Greetings, Aquilae.

    While I don't have an immediate cure. I'm pretty sure you're dealing with an HTTP header issue.

    In other words; Apache is intervening with an authentication type header, when you were attempting an image type header.

    HTH, if I can think of something more concrete (a solution), I'll post back.

    --Chris

    #!/usr/bin/perl -Tw
    use Perl::Always or die;
    my $perl_version = (5.12.5);
    print $perl_version;