Skip to content

Commit

Permalink
Fix Issue 19367 - std.net.curl does not understand HTTP/2 status lines
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberShadow committed Nov 5, 2018
1 parent a7485d9 commit 7c00a82
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions std/net/curl.d
Expand Up @@ -2385,7 +2385,6 @@ struct HTTP
in char[] value) callback)
{
import std.algorithm.searching : startsWith;
import std.conv : to;
import std.regex : regex, match;
import std.uni : toLower;

Expand All @@ -2405,18 +2404,8 @@ struct HTTP
if (header.startsWith("HTTP/"))
{
headersIn.clear();

const m = match(header, regex(r"^HTTP/(\d+)\.(\d+) (\d+) (.*)$"));
if (m.empty)
{
// Invalid status line
}
else
if (parseStatusLine(header, status))
{
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);
}
Expand Down Expand Up @@ -2447,6 +2436,34 @@ struct HTTP

curl.onReceiveHeader = dg;
}

/// Parse status line, as received from / generated by cURL.
private static bool parseStatusLine(in char[] header, out StatusLine status)
{
import std.conv : to;
import std.regex : regex, match;

const m = match(header, regex(r"^HTTP/(\d+)(?:\.(\d+))? (\d+)(?: (.*))?$"));
if (m.empty)
return false; // Invalid status line
else
{
status.majorVersion = to!ushort(m.captures[1]);
status.minorVersion = m.captures[2].length ? to!ushort(m.captures[2]) : 0;
status.code = to!ushort(m.captures[3]);
status.reason = m.captures[4].idup;
return true;
}
}

unittest
{
StatusLine status;
assert(parseStatusLine("HTTP/1.1 200 OK", status) && status == StatusLine(1, 1, 200, "OK"));
assert(parseStatusLine("HTTP/1.0 304 Not Modified", status) && status == StatusLine(1, 0, 304, "Not Modified"));
// The HTTP2 protocol is binary; cURL generates this fake text header.
assert(parseStatusLine("HTTP/2 200", status) && status == StatusLine(2, 0, 200, null));
}
}

private RefCounted!Impl p;
Expand Down

0 comments on commit 7c00a82

Please sign in to comment.