我正在尝试解析SIP REQUEST消息。当我调用函数解析消息时,它从中间开始没有任何理由。
输入是:
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 127.0.0.1:5060;branch=z9hG4bK1448207213;received=127.0.0.1;rport=52447
From: <sip:croolman@127.0.0.1>;tag=glitchy1448207213
To: <sip:croolman@127.0.0.1>;tag=as4589087f
Call-ID: croolman1448207213
CSeq: 1 REGISTER
Server: Asterisk PBX 11.7.0~dfsg-1ubuntu1
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH
Supported: replaces, timer
WWW-Authenticate: Digest algorithm=MD5, realm="asterisk", nonce="3adf9b5b"
Content-Length: 0
解析函数:
void generateOutput(string input, int response){
string help = "";
cout << "***********EXAMPLE - INPUT *************" << endl;
cout << input << endl;
int i = 0;
cout << to_string(global_iterator) << " "; // prints the number at the start of the output
global_iterator++;
size_t pozic;
if(response == 1){
while(input[i] != ' '){ // get to 401
i++;
}
i++;
while(input[i] != ' '){ // print 401
cout << input[i];
i++;
}
i++;
cout << " ";
while(input[i] != '\n'){ // print Unauthorized
cout << input[i];
i++;
}
pozic = input.find("From:"); // find From:
pozic += 5; // get to position where adress starts
while(input[pozic] != ':'){
pozic++;
}
pozic++;
while(input[pozic] != '>'){
cout << input[pozic];
pozic++;
}
cout << " ";
pozic = input.find("To:");
pozic += 3;
while(input[pozic] != ':'){
pozic++;
}
pozic++;
while(input[pozic] != '>'){
cout << input[pozic];
pozic++;
}
cout << endl;
}else{
while(input[i] != ' '){
cout << input[i];
i++;
}
cout << " ";
pozic = input.find("From:");
pozic += 5;
while(input[pozic] != ':'){
pozic++;
}
pozic++;
while(input[pozic] != '>'){
cout << input[pozic];
pozic++;
}
cout << " ";
pozic = input.find("To:");
pozic += 3;
while(input[pozic] != ':'){
pozic++;
}
pozic++;
while(input[pozic] != '>'){
cout << input[pozic];
pozic++;
}
cout << endl;
}
全局迭代器是计算消息的全局变量。 预期的输出是:
1 401 Unauthorized croolman@127.0.0.1 croolman@127.0.0.1
我得到的结果:
1 REGISTER croolman@127.0.0.1 croolman@127.0.0.1
***********EXAMPLE - INPUT *************
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 127.0.0.1:5060;branch=z9hG4bK1448215167;received=127.0.0.1;rport=53191
From: <sip:croolman@127.0.0.1>;tag=glitchy1448215167
To: <sip:croolman@127.0.0.1>;tag=as017834f4
Call-ID: croolman1448215167
CSeq: 1 REGISTER
Server: Asterisk PBX 11.7.0~dfsg-1ubuntu1
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH
Supported: replaces, timer
WWW-Authenticate: Digest algorithm=MD5, realm="asterisk", nonce="28af07e9"
Content-Length: 0
croolman@127.0.0.1 croolman@127.0.0.1