Skip to content

Commit b2aacbf

Browse files
authored
Merge pull request #4723 from aG0aep6G/curl-response-charset
ignore case when looking for "charset"
2 parents 7978ee7 + f87d112 commit b2aacbf

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

std/net/curl.d

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,6 @@ private auto _basicHTTP(T)(const(char)[] url, const(void)[] sendData, HTTP clien
983983
HTTP.StatusLine statusLine;
984984
import std.array : appender;
985985
auto content = appender!(ubyte[])();
986-
string[string] headers;
987986
client.onReceive = (ubyte[] data)
988987
{
989988
content ~= data;
@@ -1025,32 +1024,14 @@ private auto _basicHTTP(T)(const(char)[] url, const(void)[] sendData, HTTP clien
10251024
import std.conv : to;
10261025
content.reserve(value.to!size_t);
10271026
}
1028-
if (auto v = key in headers)
1029-
{
1030-
*v ~= ", ";
1031-
*v ~= value;
1032-
}
1033-
else
1034-
headers[key] = value.idup;
10351027
};
10361028
client.onReceiveStatusLine = (HTTP.StatusLine l) { statusLine = l; };
10371029
client.perform();
10381030
enforce!CurlException(statusLine.code / 100 == 2,
10391031
format("HTTP request returned status code %d (%s)",
10401032
statusLine.code, statusLine.reason));
10411033

1042-
// Default charset defined in HTTP RFC
1043-
auto charset = "ISO-8859-1";
1044-
if (auto v = "content-type" in headers)
1045-
{
1046-
auto m = match(cast(char[]) (*v), regex("charset=([^;,]*)"));
1047-
if (!m.empty && m.captures.length > 1)
1048-
{
1049-
charset = m.captures[1].idup;
1050-
}
1051-
}
1052-
1053-
return _decodeContent!T(content.data, charset);
1034+
return _decodeContent!T(content.data, client.p.charset);
10541035
}
10551036

10561037
unittest
@@ -1088,6 +1069,31 @@ unittest
10881069
assert(res == "TRACERESPONSE");
10891070
}
10901071

1072+
unittest // charset detection and transcoding to T
1073+
{
1074+
testServer.handle((s) {
1075+
s.send("HTTP/1.1 200 OK\r\n"~
1076+
"Content-Length: 4\r\n"~
1077+
"Content-Type: text/plain; charset=utf-8\r\n" ~
1078+
"\r\n" ~
1079+
"äbc");
1080+
});
1081+
auto client = HTTP();
1082+
auto result = _basicHTTP!char(testServer.addr, "", client);
1083+
assert(result == "äbc");
1084+
1085+
testServer.handle((s) {
1086+
s.send("HTTP/1.1 200 OK\r\n"~
1087+
"Content-Length: 3\r\n"~
1088+
"Content-Type: text/plain; charset=iso-8859-1\r\n" ~
1089+
"\r\n" ~
1090+
0xE4 ~ "bc");
1091+
});
1092+
client = HTTP();
1093+
result = _basicHTTP!char(testServer.addr, "", client);
1094+
assert(result == "äbc");
1095+
}
1096+
10911097
/*
10921098
* Helper function for the high level interface.
10931099
*
@@ -2397,7 +2403,7 @@ struct HTTP
23972403
if (fieldName == "content-type")
23982404
{
23992405
auto mct = match(cast(char[]) m.captures[2],
2400-
regex("charset=([^;]*)"));
2406+
regex("charset=([^;]*)", "i"));
24012407
if (!mct.empty && mct.captures.length > 1)
24022408
charset = mct.captures[1].idup;
24032409
}
@@ -3138,6 +3144,26 @@ struct HTTP
31383144

31393145
} // HTTP
31403146

3147+
unittest // charset/Charset/CHARSET/...
3148+
{
3149+
import std.meta : AliasSeq;
3150+
3151+
foreach (c; AliasSeq!("charset", "Charset", "CHARSET", "CharSet", "charSet",
3152+
"ChArSeT", "cHaRsEt"))
3153+
{
3154+
testServer.handle((s) {
3155+
s.send("HTTP/1.1 200 OK\r\n"~
3156+
"Content-Length: 0\r\n"~
3157+
"Content-Type: text/plain; " ~ c ~ "=foo\r\n" ~
3158+
"\r\n");
3159+
});
3160+
3161+
auto http = HTTP(testServer.addr);
3162+
http.perform();
3163+
assert(http.p.charset == "foo");
3164+
}
3165+
}
3166+
31413167
/**
31423168
FTP client functionality.
31433169

0 commit comments

Comments
 (0)