感谢您在链接Parse::ABNF perl usage发布的问题输入。我仍然面临解决问题的困难。请求检查下面的问题并提供解决方案的指示。
现在我已经在一个文件中创建了ABNF格式的sip语法(将其命名为sip.abnf
)。
我在下面的文件(recd_message.txt
)中点了带有标题的消息:
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.
注意:目前我有C程序生成SIP标头,我试图通过这个perl函数验证标头的内容。我正在尝试使用链接Parse::RecDescent grammar not working as expected
中给出的类似用法我必须在你的脚本中稍微修改一下以包含Parse :: ABNF并处理输入文件,然后收到的输出出现在路径https://drive.google.com/file/d/0B8KDQDXsCKR_ZERzV3IyY1M2NW8/edit?usp=sharing
中答案 0 :(得分: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";
}
}