Browse Source

* Enhanced patch from Michalis Kamburelis (bug 24324)

git-svn-id: trunk@24321 -
michael 12 years ago
parent
commit
4f69d1046a
1 changed files with 13 additions and 7 deletions
  1. 13 7
      packages/fcl-base/src/uriparser.pp

+ 13 - 7
packages/fcl-base/src/uriparser.pp

@@ -47,7 +47,7 @@ function ResolveRelativeURI(const BaseUri, RelUri: AnsiString;
   out ResultUri: AnsiString): Boolean; overload;
 
 function URIToFilename(const URI: string; out Filename: string): Boolean;
-function FilenameToURI(const Filename: string): string;
+function FilenameToURI(const Filename: string; Encode : Boolean = True): string;
 
 function IsAbsoluteURI(const UriReference: string): Boolean;
 
@@ -332,7 +332,6 @@ begin
   end;
 end;
 
-// TODO: this probably must NOT percent-encode the result...
 function ResolveRelativeURI(const BaseUri, RelUri: AnsiString;
   out ResultUri: AnsiString): Boolean;
 var
@@ -377,6 +376,8 @@ begin
       RemoveDotSegments(Path);
     end;
   end; // with
+  
+  // EncodeUri percent-encodes the result, and that's good
   ResultUri := EncodeUri(Rel);
 end;
 
@@ -423,10 +424,11 @@ begin
   end;
 end;
 
-function FilenameToURI(const Filename: string): string;
+function FilenameToURI(const Filename: string; Encode : Boolean = True): string;
 var
   I: Integer;
   IsAbsFilename: Boolean;
+  FilenamePart: string;
 begin
   IsAbsFilename := ((Filename <> '') and (Filename[1] = PathDelim)) or
     ((Length(Filename) > 2) and (Filename[1] in ['A'..'Z', 'a'..'z']) and (Filename[2] = ':'));
@@ -440,17 +442,21 @@ begin
       Result := Result + '//';
   end;
 
-  Result := Result + Filename;
+  FilenamePart := Filename;
   { unreachable code warning is ok here }
   if PathDelim <> '/' then
   begin
-    I := Pos(PathDelim, Result);
+    I := Pos(PathDelim, FilenamePart);
     while I <> 0 do
     begin
-      Result[I] := '/';
-      I := Pos(PathDelim, Result);
+      FilenamePart[I] := '/';
+      I := Pos(PathDelim, FilenamePart);
     end;
   end;
+  if Encode then
+    FilenamePart := Escape(FilenamePart, ValidPathChars);
+
+  Result := Result + FilenamePart;
 end;