Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: presenting and serving unicode file names
  • Loading branch information
rejetto committed May 3, 2020
1 parent b654228 commit 384d2d1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Expand Up @@ -2,12 +2,15 @@ tmp/
__history/
__recovery/
win32/
.vscode/
*.vfs
*.dcu
*.exe
*.map
hfs.ini
*.tmp
*.bak
*.*-
*.corrupted
*.corrupted
hfs.ini
hfs.identcache

Binary file removed hfs.identcache
Binary file not shown.
Binary file removed hfs.vrc
Binary file not shown.
9 changes: 6 additions & 3 deletions hslib.pas
Expand Up @@ -102,6 +102,7 @@ ThttpReply = record
RBM_STREAM // refer to bodyStream
);
body: ansistring; // specifies reply body according to bodyMode
bodyFile: string;
bodyStream: Tstream; // note: the stream is automatically freed
firstByte, lastByte: int64; // body interval for partial replies (206)
realm: ansistring; // this will appear in the authentication dialog
Expand Down Expand Up @@ -528,7 +529,9 @@ function decodeURL(url:ansistring; utf8:boolean=TRUE):string;
try
url[j]:=ansichar(strToInt( '$'+url[i+1]+url[i+2] ));
inc(i,2); // three chars for one
except url[j]:='_' end;
except url[j]:='_' end
else if i>j then
url[j]:=url[i];
end;
setLength(url, j);
if utf8 then
Expand Down Expand Up @@ -1425,7 +1428,7 @@ procedure ThttpConn.datasent(sender:Tobject; error:word);
reply.bodyMode:=RBM_STRING;
reply.body:=HRM2BODY[reply.mode];
if reply.mode in [HRM_REDIRECT, HRM_MOVED] then
reply.body:=stringReplace(reply.body, '%url%', reply.url, [rfReplaceAll]);
reply.body:=replaceStr(reply.body, '%url%', reply.url);
initInputStream();
end;
end;
Expand Down Expand Up @@ -1502,7 +1505,7 @@ function ThttpConn.initInputStream():boolean;
end;
RBM_FILE:
begin
i:=fileopen(reply.body, fmOpenRead+fmShareDenyNone);
i:=fileopen(reply.bodyFile, fmOpenRead+fmShareDenyNone);
if i = -1 then exit;
stream:=TFileStream.Create(i);
end;
Expand Down
21 changes: 13 additions & 8 deletions main.pas
Expand Up @@ -1330,7 +1330,7 @@ function hasRightAttributes(attr:integer):boolean; overload;
end; // hasRightAttributes

function hasRightAttributes(fn:string):boolean; overload;
begin result:=hasRightAttributes(GetFileAttributesA(pAnsiChar(ansiString(fn)))) end;
begin result:=hasRightAttributes(GetFileAttributes(pChar(fn))) end;

function isAnyMacroIn(s:ansistring):boolean; inline;
begin result:=pos(MARKER_OPEN, s) > 0 end;
Expand Down Expand Up @@ -3242,7 +3242,7 @@ function Tmainfrm.findFilebyURL(url:string; parent:Tfile=NIL; allowTemp:boolean=
n:=cur.getFirstChild();
while assigned(n) do
begin
found:=stringExists(n.text, s) or sameText(n.text, UTF8toString(s));
found:=sameText(n.text, s);
if found then break;
n:=n.getNextSibling();
end;
Expand Down Expand Up @@ -3556,7 +3556,7 @@ function Tmainfrm.getFolderPage(folder:Tfile; cd:TconnData; otpl:Tobject):string
diffTpl.fullText:=optUTF8(diffTpl.over, folder.getRecursiveDiffTplAsStr());

isDMbrowser:= otpl = dmBrowserTpl;
fullEncode:=not isDMbrowser;
fullEncode:=FALSE;
ofsRelUrl:=length(folder.url(fullEncode))+1;
ofsRelItemUrl:=length(optUTF8(diffTpl, folder.pathTill()))+1;
// pathTill() is '/' for root, and 'just/folder', so we must accordingly consider a starting and trailing '/' for the latter case (bugfix by mars)
Expand Down Expand Up @@ -4478,7 +4478,7 @@ procedure Tmainfrm.httpEvent(event:ThttpEvent; conn:ThttpConn);
var
data: TconnData;
f: Tfile;
url: ansistring;
url: string;

procedure switchToDefaultFile();
var
Expand Down Expand Up @@ -4801,6 +4801,11 @@ procedure Tmainfrm.httpEvent(event:ThttpEvent; conn:ThttpConn);
assignFile(data.f^, data.uploadDest);
end; // getUploadDestinationFileName

procedure addContentDisposition(attach:boolean=TRUE);
begin
conn.addHeader( 'Content-Disposition: '+if_(attach, 'attachment; ')+'filename*=UTF-8''"'+UTF8encode(data.lastFN)+'";');
end;

procedure sessionSetup();
begin
if (data = NIL) or assigned(data.session) then exit;
Expand Down Expand Up @@ -4948,7 +4953,7 @@ procedure Tmainfrm.httpEvent(event:ThttpEvent; conn:ThttpConn);
'%archive-size%', intToStr(tar.size)
]), data.lastFN);
if not noContentdispositionChk.checked then
conn.addHeader('Content-Disposition: attachment; filename="'+data.lastFN+'";');
addContentDisposition();
except tar.free end;
end; // serveTar

Expand Down Expand Up @@ -5029,7 +5034,7 @@ procedure Tmainfrm.httpEvent(event:ThttpEvent; conn:ThttpConn);
conn.reply.contentType:=if_(trim(getTill('<', s))='', 'text/html', 'text/plain');
conn.reply.mode:=HRM_REPLY;
conn.reply.bodyMode:=RBM_STRING;
conn.reply.body:=s;
conn.reply.body:=UTF8encode(s);
compressReply(data);
end; // replyWithString

Expand Down Expand Up @@ -5474,7 +5479,7 @@ procedure Tmainfrm.httpEvent(event:ThttpEvent; conn:ThttpConn);
data.eta.idx:=0;
conn.reply.contentType:=name2mimetype(f.name, DEFAULT_MIME);
conn.reply.bodyMode:=RBM_FILE;
conn.reply.body:=f.resource;
conn.reply.bodyFile:=f.resource;
data.downloadingWhat:=DW_FILE;
{ I guess this would not help in any way for files since we are already handling the 'if-modified-since' field
try
Expand All @@ -5493,7 +5498,7 @@ procedure Tmainfrm.httpEvent(event:ThttpEvent; conn:ThttpConn);
if (data.agent = 'MSIE') and (conn.getHeader('Accept') = '*/*') then
s:=replaceStr(s, ' ','%20');
if not noContentdispositionChk.checked or not b then
conn.addHeader( 'Content-Disposition: '+if_(not b, 'attachment; ')+'filename="'+s+'";' );
addContentDisposition(not b);
end; // handleRequest

procedure lastByte();
Expand Down

0 comments on commit 384d2d1

Please sign in to comment.