提交 f473997b authored 作者: Jonas Gauffin's avatar Jonas Gauffin

Bugfixes and a fix when FS reports incorrect content-length.

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk/contrib@16212 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 5a7e1f39
...@@ -79,25 +79,21 @@ namespace FreeSwitch.EventSocket ...@@ -79,25 +79,21 @@ namespace FreeSwitch.EventSocket
_text.Append(_piecesToAppend.Dequeue()); _text.Append(_piecesToAppend.Dequeue());
} }
if (_text.Length == 0)
return null;
PlainEventMsg plainEvent; PlainEventMsg plainEvent;
lock (_text) lock (_text)
{ {
int i = 0; int i = 0;
IgnoreLineBreaks(ref i);
_text.Remove(0, i);
// find complete header i = FindEmptyLine(i);
bool found = false; if (i == -1)
for (; i < _text.Length - 1; ++i) return null;
{
if (_text[i] == '\n' && _text[i + 1] == '\n')
{
found = true;
break;
}
}
if (!found)
return null;
// extract header // extract header
char[] chars = new char[i]; char[] chars = new char[i];
_text.CopyTo(0, chars, 0, i); _text.CopyTo(0, chars, 0, i);
string headers = new string(chars); string headers = new string(chars);
...@@ -114,7 +110,7 @@ namespace FreeSwitch.EventSocket ...@@ -114,7 +110,7 @@ namespace FreeSwitch.EventSocket
// this will eat that kind of header. // this will eat that kind of header.
if (_text.Length >= BugWorkaround.Length + i) if (_text.Length >= BugWorkaround.Length + i)
{ {
found = true; bool found = true;
for (int index = 0; index < BugWorkaround.Length; ++index) for (int index = 0; index < BugWorkaround.Length; ++index)
{ {
if (_text[index + i] != BugWorkaround[index]) if (_text[index + i] != BugWorkaround[index])
...@@ -137,8 +133,18 @@ namespace FreeSwitch.EventSocket ...@@ -137,8 +133,18 @@ namespace FreeSwitch.EventSocket
return null; return null;
// extract body // extract body
chars = new char[plainEvent.ContentLength];
_text.CopyTo(i, chars, 0, plainEvent.ContentLength); // since Freeswitch report false sizes sometimes,
// we need to go through all lines to find contents.
int endOfBody = FindEmptyLine(i) + 2; // include the first and last \n
if (endOfBody-i != plainEvent.ContentLength)
{
Console.WriteLine("Invalid content length, actual length: " + (endOfBody - i) + ", reported length: " +
plainEvent.ContentLength);
plainEvent.ContentLength = endOfBody - i;
}
chars = new char[plainEvent.ContentLength];
_text.CopyTo(i, chars, 0, plainEvent.ContentLength);
plainEvent.Body = new string(chars); plainEvent.Body = new string(chars);
// api/response is buggy for originate, no \n\n are appended at the end. // api/response is buggy for originate, no \n\n are appended at the end.
...@@ -175,8 +181,18 @@ namespace FreeSwitch.EventSocket ...@@ -175,8 +181,18 @@ namespace FreeSwitch.EventSocket
return plainEvent; return plainEvent;
} }
private int FindEmptyLine(int i)
{
for (; i < _text.Length - 1; ++i)
{
if (_text[i] == '\n' && _text[i + 1] == '\n')
return i;
}
return -1;
}
public void Append(string text) public void Append(string text)
{ {
lock (_piecesToAppend) lock (_piecesToAppend)
_piecesToAppend.Enqueue(text); _piecesToAppend.Enqueue(text);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论