Skip to content

Commit

Permalink
DRY: the HTTP struct already detects the charset
Browse files Browse the repository at this point in the history
  • Loading branch information
aG0aep6G committed Aug 8, 2016
1 parent 4a634ed commit 9d65668
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions std/net/curl.d
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,6 @@ private auto _basicHTTP(T)(const(char)[] url, const(void)[] sendData, HTTP clien
HTTP.StatusLine statusLine;
import std.array : appender;
auto content = appender!(ubyte[])();
string[string] headers;
client.onReceive = (ubyte[] data)
{
content ~= data;
Expand Down Expand Up @@ -1025,32 +1024,14 @@ private auto _basicHTTP(T)(const(char)[] url, const(void)[] sendData, HTTP clien
import std.conv : to;
content.reserve(value.to!size_t);
}
if (auto v = key in headers)
{
*v ~= ", ";
*v ~= value;
}
else
headers[key] = value.idup;
};
client.onReceiveStatusLine = (HTTP.StatusLine l) { statusLine = l; };
client.perform();
enforce!CurlException(statusLine.code / 100 == 2,
format("HTTP request returned status code %d (%s)",
statusLine.code, statusLine.reason));

// Default charset defined in HTTP RFC
auto charset = "ISO-8859-1";
if (auto v = "content-type" in headers)
{
auto m = match(cast(char[]) (*v), regex("charset=([^;,]*)"));
if (!m.empty && m.captures.length > 1)
{
charset = m.captures[1].idup;
}
}

return _decodeContent!T(content.data, charset);
return _decodeContent!T(content.data, client.p.charset);
}

unittest
Expand Down Expand Up @@ -1088,6 +1069,31 @@ unittest
assert(res == "TRACERESPONSE");
}

unittest // charset detection and transcoding to T
{
testServer.handle((s) {
s.send("HTTP/1.1 200 OK\r\n"~
"Content-Length: 4\r\n"~
"Content-Type: text/plain; charset=utf-8\r\n" ~
"\r\n" ~
"äbc");
});
auto client = HTTP();
auto result = _basicHTTP!char(testServer.addr, "", client);
assert(result == "äbc");

testServer.handle((s) {
s.send("HTTP/1.1 200 OK\r\n"~
"Content-Length: 3\r\n"~
"Content-Type: text/plain; charset=iso-8859-1\r\n" ~
"\r\n" ~
0xE4 ~ "bc");
});
client = HTTP();
result = _basicHTTP!char(testServer.addr, "", client);
assert(result == "äbc");
}

/*
* Helper function for the high level interface.
*
Expand Down

0 comments on commit 9d65668

Please sign in to comment.