1

感谢您在链接Parse::ABNF perl usage上发布的问题输入。我仍然面临解决问题的困难。请求在下面检查我的问题并提供解决方案的指示。

现在我已经在一个文件中创建了 ABNF 格式的 sip 语法(命名为sip.abnf)。我在如下文件(recd_message.txt)中有带有标题的 sip 消息:

From: <sip:07455900064@212.136.178.216:5060;user=phone>;tag=1526438727-1338998848384-
To: "govoice-lab2-aokanlawon"<sip:441127653485@isf.fire.ipgcom.com>
Contact: <sip:07455900064@10.12.33.29:5070;transport=udp>
Allow: ACK,BYE,CANCEL,INFO,INVITE,OPTIONS,PRACK,REFER,NOTIFY
Accept: multipart/mixed,application/media_control+xml,application/sdp

我创建了一个 Perl 程序来使用 ABNF 语法来验证上述标题消息,并将其命名为testSipHeader.pl以下内​​容:

use strict; use warnings;
use File::Slurp;
use Parse::ABNF ;
use Data::Dumper;

my $grammar_file = shift;
my $messages = shift;
my @header_name;
my $header_status;

my $grammar = scalar read_file( $grammar_file );
$grammar =~ s/\x0d\x0a/\n/g;
$grammar =~ s/^\s+(?=[\w-]+\s*=)//mg;

$grammar = Parse::ABNF->new->parse(  scalar read_file( $grammar_file ) );
if (defined $grammar) {
    print "Grammar is now defined...\n";
    my $header;
    open ( $header , "<", $messages) or die "Could not open $messages file";
    while(<$header>) {
        print "Processing the message $_\n";
        @header_name = split(': ', $_);
        if ($header_name[0] eq "From") {
            $header_status = $grammar->From($_) ;
        } elsif ($header_name[0] eq "To") {
            $header_status = $grammar->To($_) ;
        } elsif ($header_name[0] eq "Contact") {
            $header_status = $grammar->Contact($_) ;
        } elsif ($header_name[0] eq "Allow") {
            $header_status = $grammar->Allow($_) ;
        } elsif ($header_name[0] eq "Accept") {
            $header_status = $grammar->Accept($_) ;
        } else {
            print "Error: Unsupported header $header_name[0] received\n";
        }
    }
} else {
        print "Error: grammar is not defined\n";
}

Perl 调用和输出/错误如下

$> perl -I. testSipHeader.pl sip.abnf recd_messages.txt
Grammar is now defined...
Processing the message From: <sip:07455900064@212.136.178.216:5060;user=phone>;tag=1526438727-

Can't call method "From" on unblessed reference at testSipHeader.pl line 21, <$header> line 1.

注意:目前我有生成 SIP 标头的 C 程序,我正在尝试通过这个 perl 函数验证标头的内容。我正在尝试使用类似于链接Parse::RecDescent 语法未按预期工作的用法

我不得不在您的脚本中进行一些修改以包含 Parse::ABNF 并处理输入文件,然后收到的输出出现在路径https://drive.google.com/file/d/0B8KDQDXsCKR_ZERzV3IyY1M2NW8/edit?usp=sharing中

4

1 回答 1

0

我已经重写了你的脚本,输出是什么?

use Data::Dumper;
my $grammar_rules;
{
    local $/=undef;
    open(my $fh,'<',$grammar_file) or die $grammar_file,$!;
    $grammar_rules = <$fh>;
    $grammar_rules =~ s/\x0d\x0a/\n/g;
    $grammar_rules =~ s/^\s+(?=[\w-]+\s*=)//mg;
}
print Dumper('rules',$grammar_rules);
my $grammar = Parse::ABNF->new->parse(  $grammar_rules );
print Dumper('grammar',$grammar);
die "Error: grammar is not defined" if ! defined $grammer;
print "Grammar is now defined...\n";

open ( my $header_fh , "<", $messages) or die "Could not open $messages file, $!";
while(my $line = <$header_fh>) {
    chomp($line);
    print "Processing the message $line\n";
    @header_name = split(': ', $line);
    if ($header_name[0] eq "From") {
        $header_status = $grammar->From($line) ;
    } elsif ($header_name[0] eq "To") {
        $header_status = $grammar->To($line) ;
    } elsif ($header_name[0] eq "Contact") {
        $header_status = $grammar->Contact($line) ;
    } elsif ($header_name[0] eq "Allow") {
        $header_status = $grammar->Allow($line) ;
    } elsif ($header_name[0] eq "Accept") {
        $header_status = $grammar->Accept($line) ;
    } else {
        print "Error: Unsupported header $header_name[0] received\n";
    }
}
于 2014-02-25T13:44:52.637 回答