代码之家  ›  专栏  ›  技术社区  ›  jantimon

Perl CGI钩子未按预期工作

  •  0
  • jantimon  · 技术社区  · 16 年前

    我在使用perl cgi钩子时遇到问题。

    似乎在我按下“发送”按钮之后,我的perl脚本不会立即被调用,而是在文件完全上传之后被调用。

    这可能是因为服务器设置。

    以前有人遇到过这个问题吗?

    原因是预装的:Apache安全模块


    Perl源代码

    #!/usr/bin/perl -w
    
    use CGI::Carp 'fatalsToBrowser';
    use CGI qw(:cgi);
    use IO::File;
    use strict;
    
    my $hook_file = "test.txt";
    
    my $hook_handle = new IO::File;
    $hook_handle->open(">> $hook_file") or die("Failed to open $hook_file: $!");
    
    my $hook_query = CGI->new(\&hook, $hook_handle);
    
    #start upload:
    my $query = new CGI;
    
    sub hook{
        my ($current_filename, $buffer, $bytes_read, $hook_handle) = @_;
    
        $hook_handle->print( join(" ",times()) . " -> " . $bytes_read ."\n" );
    }
    
    
    
    print "Content-type: text/html\n\n";
    
    
    1;
    

    要监视上载进度,我使用tail:

    touch test.txt
    tail -f test.txt
    

    使用一个简单的HTML POST表单,我开始上传一个5.5MB的文件。

    输出总是相似的:

    system time / user time / cpu time   ->   bytes transfered
    
    0.03 0 0 0 -> 4037
    ...
    ...
    ...
    0.11 0.01 0 0 -> 5520894
    

    说它在0.1秒内上传了5.5MB。


    use Config qw(myconfig config_sh config_vars);
    
    print myconfig();
    
    print config_sh();
    

    一些差异:(请告诉我是否还有其他有趣的东西)

    第一个值是工作服务器的值。

    第二个值是 正在监听dreamhost服务器。

    // dreamhost uses an older version of perl:
    PERL_API_VERSION='10' -> '8'
    api_versionstring='5.10.0' -> '5.8.0'
    
    // dreamhost uses ByteLoader 
    extensions='B ...'
    extensions='B ByteLoader ...'
    
    // dreamhost uses an older gcc version
    gccversion='4.3.3' -> '4.1.2 20061115 (prerelease) (Debian 4.1.1-21)'
    
    // dreamhost uses an older libc version
    gnulibc_version='2.9' -> '2.3.6'
    
    // the dreamhost server is using fast stdio
    usefaststdio='undef' -> 'define'
    

    CGI( 更新 )

    由于Sinans的建议,我更新了我的CGI版本(但它并没有解决我的问题)

    // dreamhost cgi version
    print $query->version (); -> 3.48
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   Sinan Ünür    16 年前

    我在打印到传递给钩子的文件句柄时遇到了一些困难,可能是因为缓冲问题。我决定用 append_file 从…起 File::Slurp 并传递日志文件的名称。

    我还决定将脚本包装成一个 run sub,以防您在下以注册表脚本的形式运行此脚本 mod_perl . 最后,我不知道在哪里 times() 函数来自,所以我使用 time

    #!/usr/bin/perl
    
    use strict; use warnings;
    
    use CGI;
    use File::Slurp;
    
    run();
    
    sub run {
        my $logfile = 'E:/srv/deploy/app/up.log';
    
        my $cgi = CGI->new(\&hook, $logfile);
    
        print $cgi->header('text/html'),
              $cgi->start_html,
              $cgi->p('Upload done'),
              $cgi->end_html;
        return;
    }
    
    sub hook {
        my ($filename, $buffer, $bytes_read, $logfile) = @_;
        append_file $logfile, \ sprintf("%d: %d\n", time, $bytes_read);
    }
    

    输出:

    1258030571: 4051
    1258030571: 8102
    1258030571: 12153
    ...
    1258030574: 5959021
    1258030574: 5963072
    1258030574: 5963469
    

    CGI版本:3.37 apache版本:2.2.4(Windows) perl版本:5.10.1(ActiveState)