delphi - No mapping for the Unicode character exists in the target multi-byte code page -
i have soap builded delphi 2007 , work!
after conversion in delphi 10.1 berlin have lot of exceptions:
no mapping unicode character exists in target multi-byte code page
the cause seem, when webbroker httpapp parsing request parameters, raise exception if request has url encoded chars in query string, eg.:
http://localhost/soap/soap.dll/action?param=%e0
in fact if call url.decode()
%e0
(à
url encoded):
tnetencoding.url.decode('%e0');
it raise same exception:
no mapping unicode character exists in target multi-byte code page
the problem seem unit system.netencoding
on method turlencoding.dodecode(const input: string): string
. method try convert url encoded chars in utf-8 without fallback on windows-1252. string %e0
windows-1252 encoding à
, delphi conver utf-8 version: %c3%a0
.
a small (not perfect, not elegant) fix add fallback:
try result := tencoding.utf8.getstring(bytes); // original delphi 10.1 line except on e: eencodingerror result := string(pchar(bytes)); // fallback end;
full code:
function turlencoding.dodecode(const input: string): string; function decodehexchar(const c: char): byte; begin case c of '0'..'9': result := ord(c) - ord('0'); 'a'..'f': result := ord(c) - ord('a') + 10; 'a'..'f': result := ord(c) - ord('a') + 10; else raise econverterror.create(''); end; end; function decodehexpair(const c1, c2: char): byte; inline; begin result := decodehexchar(c1) shl 4 + decodehexchar(c2) end; var sp, cp: pchar; i: integer; bytes: tbytes; begin setlength(bytes, length(input) * 4); := 0; sp := pchar(input); cp := sp; try while sp^ <> #0 begin case sp^ of '+': bytes[i] := byte(' '); '%': begin inc(sp); // escaped % (%%) if (sp)^ = '%' bytes[i] := byte('%') else begin // encoded byte, may single byte (%<hex>) // or part of multi byte (%<hex>%<hex>...) character cp := sp; inc(sp); if ((cp^ = #0) or (sp^ = #0)) raise ehttpexception.createfmt(serrordecodingurltext, [cp - pchar(input)]); bytes[i] := decodehexpair(cp^, sp^) end; end; else // accept single , multi byte characters if ord(sp^) < 128 bytes[i] := byte(sp^) else := + tencoding.utf8.getbytes([sp^], 0, 1, bytes, i) - 1 end; inc(i); inc(sp); end; except on e: econverterror raise econverterror.createfmt(sinvalidurlencodedchar, [char('%') + cp^ + sp^, cp - pchar(input)]) end; setlength(bytes, i); // ------> fix <------ try result := tencoding.utf8.getstring(bytes); // original line except on e: eencodingerror result := string(pchar(bytes)); end; // end fix end;
after lot of search founds bug fix list rad studio 10.1 berlin , says bug fixed:
webbroker httpapp parsing request parameters , getting error "no mapping unicode character exists in target multi-byte code page"
but not work me...
try use web.reqmulti;
i have same exception when user webbroker handle post method web page when there multi-byte character in form.
and after added use web.reqmulti in webbroker's project, exception gone.
Comments
Post a Comment