TWebRequest issue when parsing filename with a percent symbol (%) in the name (Delphi 10.3)

104 views Asked by At

Using TWebRequest (isapi application) to receive a POST/multipart/form-data request with one or more files.

When the filename contains a % symbol (for example, "Sales % by date.doc") an exception is generated when the request is parsed (e.g. using request.contentfields.count, request.extractcontentfields()). The exception is

Invalid URL encoded character (% b) at position xx

or similar (depending on the filename).

I've tried intercepting the file name and correcting (for example "Sales %25 by date.doc"), but I'm not able to set Request.Content and parse.

I've tried intercepting and renaming the file before the client side (browser) form is submitted, but the browser is restricting that for an <input type=file> tag.

Is there a way of changing how the filename would be decoded? Is this a bug that is fixed in 11? Any other tips/tricks would be appreciated.

1

There are 1 answers

0
AngryViking On

I was able to resolve the issue by going to the source code for TWebRequest in Web.HTTPApp.pas and modify the code in the ExtractHeaderFields procedure. In that procedure, there is an untrapped instance of TNetEncoding.URL.Decode(). The behavior I was looking for: if there is an exception in the Decode operation, I want it to exit gracefully without modifying the string. So, the original code was:

    if Decode then
       Strings.Add(TNetEncoding.URL.Decode(DoStripQuotes(ExtractedField)));
       else Strings.Add(DoStripQuotes(ExtractedField));

I modified to:

 if Decode then begin
    Try
       Strings.Add(TNetEncoding.URL.Decode(DoStripQuotes(ExtractedField)));
       Except On E:Exception Do Begin
          Strings.Add(DoStripQuotes(ExtractedField));
          End;
       End;
    End Else Begin
    Strings.Add(DoStripQuotes(ExtractedField));
    End;

That resolved my issue.