mirror of
https://github.com/dlang/phobos.git
synced 2025-05-07 03:27:03 +03:00
Merge pull request #1842 from blackwhale/issue-11350
Fix issue 11350 ibphobos2 regex match segfaults when a rare HTTP header is received
This commit is contained in:
commit
84dbc9934d
1 changed files with 42 additions and 34 deletions
|
@ -2063,48 +2063,56 @@ struct HTTP
|
||||||
// status lines. The last one is the one recorded.
|
// status lines. The last one is the one recorded.
|
||||||
auto dg = (in char[] header)
|
auto dg = (in char[] header)
|
||||||
{
|
{
|
||||||
if (header.empty)
|
import std.utf : UTFException;
|
||||||
|
try
|
||||||
{
|
{
|
||||||
// header delimiter
|
if (header.empty)
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (header.startsWith("HTTP/"))
|
|
||||||
{
|
|
||||||
string[string] empty;
|
|
||||||
headersIn = empty; // clear
|
|
||||||
|
|
||||||
auto m = match(header, regex(r"^HTTP/(\d+)\.(\d+) (\d+) (.*)$"));
|
|
||||||
if (m.empty)
|
|
||||||
{
|
{
|
||||||
// Invalid status line
|
// header delimiter
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else
|
if (header.startsWith("HTTP/"))
|
||||||
{
|
{
|
||||||
status.majorVersion = to!ushort(m.captures[1]);
|
string[string] empty;
|
||||||
status.minorVersion = to!ushort(m.captures[2]);
|
headersIn = empty; // clear
|
||||||
status.code = to!ushort(m.captures[3]);
|
|
||||||
status.reason = m.captures[4].idup;
|
auto m = match(header, regex(r"^HTTP/(\d+)\.(\d+) (\d+) (.*)$"));
|
||||||
if (onReceiveStatusLine != null)
|
if (m.empty)
|
||||||
onReceiveStatusLine(status);
|
{
|
||||||
|
// Invalid status line
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
status.majorVersion = to!ushort(m.captures[1]);
|
||||||
|
status.minorVersion = to!ushort(m.captures[2]);
|
||||||
|
status.code = to!ushort(m.captures[3]);
|
||||||
|
status.reason = m.captures[4].idup;
|
||||||
|
if (onReceiveStatusLine != null)
|
||||||
|
onReceiveStatusLine(status);
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
|
// Normal http header
|
||||||
|
auto m = match(cast(char[]) header, regex("(.*?): (.*)$"));
|
||||||
|
|
||||||
|
auto fieldName = m.captures[1].toLower().idup;
|
||||||
|
if (fieldName == "content-type")
|
||||||
|
{
|
||||||
|
auto mct = match(cast(char[]) m.captures[2],
|
||||||
|
regex("charset=([^;]*)"));
|
||||||
|
if (!mct.empty && mct.captures.length > 1)
|
||||||
|
charset = mct.captures[1].idup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!m.empty && callback !is null)
|
||||||
|
callback(fieldName, m.captures[2]);
|
||||||
|
headersIn[fieldName] = m.captures[2].idup;
|
||||||
}
|
}
|
||||||
|
catch(UTFException e)
|
||||||
// Normal http header
|
|
||||||
auto m = match(cast(char[]) header, regex("(.*?): (.*)$"));
|
|
||||||
|
|
||||||
auto fieldName = m.captures[1].toLower().idup;
|
|
||||||
if (fieldName == "content-type")
|
|
||||||
{
|
{
|
||||||
auto mct = match(cast(char[]) m.captures[2],
|
//munch it - a header should be all ASCII, any "wrong UTF" is broken header
|
||||||
regex("charset=([^;]*)"));
|
|
||||||
if (!mct.empty && mct.captures.length > 1)
|
|
||||||
charset = mct.captures[1].idup;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m.empty && callback !is null)
|
|
||||||
callback(fieldName, m.captures[2]);
|
|
||||||
headersIn[fieldName] = m.captures[2].idup;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
curl.onReceiveHeader = dg;
|
curl.onReceiveHeader = dg;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue