fix: utf-8 decoding for {.load.}

This commit is contained in:
Massimo Melina 2020-05-30 17:10:59 +02:00
parent 469acc3fb7
commit df5fa09ba9
4 changed files with 19 additions and 9 deletions

View File

@ -1366,7 +1366,7 @@ if buffer = '' then exit;
if state = HCS_IDLE then if state = HCS_IDLE then
begin begin
state:=HCS_REQUESTING; state:=HCS_REQUESTING;
reply.contentType:='text/html'; reply.contentType:='text/html; charset=utf-8';
notify(HE_REQUESTING); notify(HE_REQUESTING);
end; end;
case state of case state of
@ -1384,7 +1384,7 @@ notify(HE_REQUESTED);
if not initInputStream() then if not initInputStream() then
begin begin
reply.mode:=HRM_INTERNAL_ERROR; reply.mode:=HRM_INTERNAL_ERROR;
reply.contentType:='text/html'; reply.contentType:='text/html; charset=utf-8';
notify(HE_CANT_OPEN_FILE); notify(HE_CANT_OPEN_FILE);
end; end;
notify(HE_STREAM_READY); notify(HE_STREAM_READY);

View File

@ -3899,7 +3899,7 @@ try
else s:=first(data.banReason, data.disconnectReason); else s:=first(data.banReason, data.disconnectReason);
addArray(md.table, ['%reason%', s]); addArray(md.table, ['%reason%', s]);
data.conn.reply.contentType:=ansistring(name2mimetype(sectionName, 'text/html')); data.conn.reply.contentType:=ansistring(name2mimetype(sectionName, 'text/html; charset=utf-8'));
md.cd:=data; md.cd:=data;
md.tpl:=tpl2use; md.tpl:=tpl2use;
@ -5130,7 +5130,7 @@ var
end; end;
if conn.reply.contentType = '' then if conn.reply.contentType = '' then
conn.reply.contentType:=ansistring(if_(trim(getTill('<', s))='', 'text/html', 'text/plain')); conn.reply.contentType:=ansistring(if_(trim(getTill('<', s))='', 'text/html', 'text/plain'))+'; charset=utf-8';
conn.reply.mode:=HRM_REPLY; conn.reply.mode:=HRM_REPLY;
conn.reply.bodyMode:=RBM_STRING; conn.reply.bodyMode:=RBM_STRING;
conn.reply.body:=UTF8encode(s); conn.reply.body:=UTF8encode(s);

View File

@ -457,7 +457,8 @@ var
vars: Tstrings; vars: Tstrings;
s: string; s: string;
begin begin
if not satisfied(md.cd) then exit; if not satisfied(md.cd) then
exit;
try try
result:=md.cd.conn.request.url; result:=md.cd.conn.request.url;
if pars.count < 2 then exit; if pars.count < 2 then exit;
@ -666,7 +667,7 @@ var
procedure convert(); procedure convert();
begin begin
if sameText(p, 'ansi') and sameText(par(1), 'utf-8') then if sameText(p, 'ansi') and sameText(par(1), 'utf-8') then
result:=ansiToUTF8(ansistring(par(2))) result:=string(ansiToUTF8(ansistring(par(2))))
else if sameText(p, 'utf-8') and sameText(par(1), 'ansi') then else if sameText(p, 'utf-8') and sameText(par(1), 'ansi') then
result:=utf8ToAnsi(ansistring(par(2))) result:=utf8ToAnsi(ansistring(par(2)))
end; // convert end; // convert
@ -1077,6 +1078,7 @@ var
procedure load(fn:string; varname:string=''); procedure load(fn:string; varname:string='');
var var
from, size: int64; from, size: int64;
s: ansistring;
begin begin
result:=''; result:='';
from:=parI('from', 0); from:=parI('from', 0);
@ -1095,11 +1097,17 @@ var
try result:=httpGet(fn, from, size) try result:=httpGet(fn, from, size)
except result:='' end except result:='' end
else else
result:=loadFile(uri2diskMaybe(fn), from, size); begin
s:=loadFile(uri2diskMaybe(fn), from, size);
result:=UTF8ToString(s);
if result = '' then
result:=s;
end;
if varname = '' then if varname = '' then
begin begin
if anyCharIn('/\',fn) then result:=macroQuote(result); if anyCharIn('/\',fn) then
result:=macroQuote(result);
exit; exit;
end; end;
if ansiStartsStr(ENCODED_TABLE_HEADER, result) then if ansiStartsStr(ENCODED_TABLE_HEADER, result) then
@ -1962,7 +1970,7 @@ try
result:=jsEncode(p, first(par(1),'''"')); result:=jsEncode(p, first(par(1),'''"'));
if name = 'base64' then if name = 'base64' then
result:=base64encode(UTF8encode(p)); result:=string(base64encode(UTF8encode(p)));
if name = 'base64decode' then if name = 'base64decode' then
result:=utf8ToString(base64decode(ansistring(p))); result:=utf8ToString(base64decode(ansistring(p)));
if name = 'md5' then if name = 'md5' then

View File

@ -1707,6 +1707,8 @@ with ThttpClient.createURL(url) do
contentRangeEnd:=intToStr(from+size-1); contentRangeEnd:=intToStr(from+size-1);
get(); get();
result:=reply.dataString; result:=reply.dataString;
if sameText('utf-8', reGet(ContentType, '; *charset=(.+) *($|;)')) then
Result:=UTF8ToString(result);
finally finally
reply.free; reply.free;
Free; Free;