Skip to content

Commit

Permalink
fix: utf-8 decoding for {.load.}
Browse files Browse the repository at this point in the history
  • Loading branch information
rejetto committed May 30, 2020
1 parent 469acc3 commit df5fa09
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions hslib.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ procedure ThttpConn.processInputBuffer();
if state = HCS_IDLE then
begin
state:=HCS_REQUESTING;
reply.contentType:='text/html';
reply.contentType:='text/html; charset=utf-8';
notify(HE_REQUESTING);
end;
case state of
Expand All @@ -1384,7 +1384,7 @@ procedure ThttpConn.processInputBuffer();
if not initInputStream() then
begin
reply.mode:=HRM_INTERNAL_ERROR;
reply.contentType:='text/html';
reply.contentType:='text/html; charset=utf-8';
notify(HE_CANT_OPEN_FILE);
end;
notify(HE_STREAM_READY);
Expand Down
4 changes: 2 additions & 2 deletions main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3899,7 +3899,7 @@ procedure Tmainfrm.getPage(sectionName:string; data:TconnData; f:Tfile=NIL; tpl2
else s:=first(data.banReason, data.disconnectReason);
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.tpl:=tpl2use;
Expand Down Expand Up @@ -5130,7 +5130,7 @@ procedure Tmainfrm.httpEvent(event:ThttpEvent; conn:ThttpConn);
end;

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.bodyMode:=RBM_STRING;
conn.reply.body:=UTF8encode(s);
Expand Down
18 changes: 13 additions & 5 deletions scriptLib.pas
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ function cbMacros(fullMacro:string; pars:Tstrings; cbData:pointer):string;
vars: Tstrings;
s: string;
begin
if not satisfied(md.cd) then exit;
if not satisfied(md.cd) then
exit;
try
result:=md.cd.conn.request.url;
if pars.count < 2 then exit;
Expand Down Expand Up @@ -666,7 +667,7 @@ function cbMacros(fullMacro:string; pars:Tstrings; cbData:pointer):string;
procedure convert();
begin
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
result:=utf8ToAnsi(ansistring(par(2)))
end; // convert
Expand Down Expand Up @@ -1077,6 +1078,7 @@ function cbMacros(fullMacro:string; pars:Tstrings; cbData:pointer):string;
procedure load(fn:string; varname:string='');
var
from, size: int64;
s: ansistring;
begin
result:='';
from:=parI('from', 0);
Expand All @@ -1095,11 +1097,17 @@ function cbMacros(fullMacro:string; pars:Tstrings; cbData:pointer):string;
try result:=httpGet(fn, from, size)
except result:='' end
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
begin
if anyCharIn('/\',fn) then result:=macroQuote(result);
if anyCharIn('/\',fn) then
result:=macroQuote(result);
exit;
end;
if ansiStartsStr(ENCODED_TABLE_HEADER, result) then
Expand Down Expand Up @@ -1962,7 +1970,7 @@ function cbMacros(fullMacro:string; pars:Tstrings; cbData:pointer):string;
result:=jsEncode(p, first(par(1),'''"'));

if name = 'base64' then
result:=base64encode(UTF8encode(p));
result:=string(base64encode(UTF8encode(p)));
if name = 'base64decode' then
result:=utf8ToString(base64decode(ansistring(p)));
if name = 'md5' then
Expand Down
2 changes: 2 additions & 0 deletions utillib.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,8 @@ function httpGet(url:string; from:int64=0; size:int64=-1):string;
contentRangeEnd:=intToStr(from+size-1);
get();
result:=reply.dataString;
if sameText('utf-8', reGet(ContentType, '; *charset=(.+) *($|;)')) then
Result:=UTF8ToString(result);
finally
reply.free;
Free;
Expand Down

0 comments on commit df5fa09

Please sign in to comment.