diff --git a/std/net/curl.d b/std/net/curl.d index 3589c6621..add2f9ead 100644 --- a/std/net/curl.d +++ b/std/net/curl.d @@ -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; @@ -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) + if (parseStatusLine(header, 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); } @@ -2452,6 +2441,37 @@ struct HTTP private RefCounted!Impl p; import etc.c.curl : CurlTimeCond; + /// Parse status line, as received from / generated by cURL. + private static bool parseStatusLine(in char[] header, out StatusLine status) @safe + { + 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; + } + } + + @safe 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)); + } + /** Time condition enumeration as an alias of $(REF CurlTimeCond, etc,c,curl) $(HTTP www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25, _RFC2616 Section 14.25)