Perl   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用Perl以一些安全性和最少的资源提供映像?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在这里发现了一些相关的帖子,但没有任何关于..
我需要“正确地”提供图像(humm)并尽可能少地使用资源.
我正在研究一个子(下面)但是,由于我使用CGI这个事实并不太资源友好.这只是我的假设.我是一个Perl新手但是,我比它更喜欢它.

该查询将由“somescript.pl?img=image.png”生成

#!/usr/bin/perl -Tw
use Strict;
use warnings;
use CGI;

#I should drop warnings after all is said and done. Also name my vars generically. Right?
#I dont know if this query method will work or is even the best method.
$query = new CGI;
my @img = $query->param;
if ( $_ eq "img" ) { my $file = $query->param($_); }
if ( $_ ne "img" ) {    ## I will send to an error sub that serves up a error image
}

# Prob a one liner to take care of the above. Not within my ability though.
# Still figuring all this out here.. Very verbose sorry...
# I will Strip everything but lowercase alpha and the "."
# with s =~ /[something like A-Z] I will look it up //g;
# Still.. prob all above will fit in a one liner by a PERL guru!

# Below is related to -Taint,I was told this is important to use the -T.
$ENV{PATH} = "bin:/usr/bin";
delete( $ENV{qw(IFS CDPATH BASH_ENV ENV)} );

# now I will grab the images extension.
my $ext = ( $file =~ m/[^.]+$/ )[0];

#I was informed to use the "three" but,I am unsure what that means.
# My attempt based on my reading many posts here.

my $length = ( stat($filE) )[10];
my $image  = do {
    local $/ = undef;
    print "Content-type: image/$ext\n";
    print "Content-length: $length \n\n";
    binmode STDOUT;
    open( FH,"<",$file ) || die "Could not find $file: $!";
    my $buffer = "";
    while ( read( FH,$buffer,10240 ) ) {
        print $buffer;
    }
    close(FH);
};

正如你所看到的,我在这里的尝试显然是初学者.

我在堆栈溢出中找到了很好的建议.我感谢过去和现在的所有人.

解决方法

>如果您要使用扩展名作为MIME类型的替代品,那么您最好将所有JPEG图像命名为.jpeg而不是.jpg! File::MMagicFile::MimeInfo将提供更好的通用解决方案.
>(stat $filE)[10]不是内容长度,它是ctime,对你来说毫无价值. (stat $filE)[7]有效,但-s $文件也可以正常工作,对任何Perl程序员来说都很明显,无需参stat手册. (2a:一旦打开文件句柄而不是文件名,就在文件句柄上使用-s,以避免因文件替换而竞争.)
>我可以访问文件系统上的任何文件,这些文件系统可由CGI运行的用户读取,例如,image.pl?image=../../../../../../../etc/passwd.我建议特别提到images目录,这样你就不依赖于getcwd,并使用 File::Spec-> no_upWARDs和File :: Spec-> catfile来构建一个只能在images目录中的路径名. >如果CGI可以避免死亡,那么它就不是真正好的形式.如果找不到该文件,则返回404状态.如果请求是非法的,则返回400或403状态等. >如果您使用path_info来允许image.PL/foo.png而不是image.pl?img=foo.png,那么您的网址会更好. >除非您添加更多逻辑,否则您提供的图像将不会被客户端缓存. >伙计,这些正在堆积起来.您是否虑过找到一些已经为此目的编写的代码而不是编写自己的代码?

大佬总结

以上是大佬教程为你收集整理的如何使用Perl以一些安全性和最少的资源提供映像?全部内容,希望文章能够帮你解决如何使用Perl以一些安全性和最少的资源提供映像?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。