Comments (4)
This is not really a bug, IMHO.
In non-Unicode builds, IOHandler.Write()
takes in an AnsiString
, where the optional ASrcEncoding
parameter specifies the encoding of that AnsiString
. By default, Indy expects that AnsiString
to use the OS's system encoding, which is how Delphi and FreePascal normally handle AnsiString
.
The AnsiString
data will be converted to Unicode using the ASrcEncoding
, and then that Unicode will be converted to the AByteEncoding
for transmission.
The TIdHTTPResponseInfo.Charset
property is meant to specify the output encoding on the wire, not the input encoding of the AnsiString
. This allows users to send, for example, a Latin-1 input string as UTF-8 output without having to encode it themselves, keeping the AnsiString
is the compiler's native format.
If you want to send a ContentText
string using an AnsiString
with a different encoding than the OS, you should set the IOHandler.DefAnsiEncoding
property, which the IOHandler
will use when no ASrcEncoding
is specified. For instance, if you have FreePascal/Lazarus setup to use UTF-8 encoded AnsiString
s, you can set the IOHandler.DefAnsiEncoding
to IndyTextEncoding_UTF8
, such as in the server's OnConnect
event.
from indy.
Thank you very much! That helped. Some notes:
- FConnection is protected in TIdHTTPResponseInfo and I had to write a helper:
procedure TIdHTTPResponseInfoHelper.WriteContentUni;
begin
{$IFDEF STRING_IS_ANSI}
FConnection.IOHandler.DefAnsiEncoding := IndyTextEncoding_UTF8;
{$ENDIF}
WriteContent;
end;
- In Lazarus under Linux I have both STRING_IS_UNICODE and STRING_IS_ANSI undefined. How is that possible?
from indy.
- FConnection is protected in TIdHTTPResponseInfo and I had to write a helper:
Why not simply set the DefAnsiEncoding
in the OnConnect
event, like I suggested?
procedure TMyForm.IdHTTPServer1Connect(AContext: TIdContext);
begin
{$IFDEF STRING_IS_ANSI}
AContext.Connection.IOHandler.DefAnsiEncoding := IndyTextEncoding_UTF8;
{$ENDIF}
end;
If you are going to write a custom wrapper, I would suggest allowing the caller configure the encoding as needed, eg:
procedure TIdHTTPResponseInfoHelper.WriteContentUni(
{$IFDEF STRING_IS_ANSI}ASrcEncoding: IIdTextEncoding = nil{$ENDIF}
);
begin
{$IFDEF STRING_IS_ANSI}
EnsureEncoding(ASrcEncoding, encUTF8);
FConnection.IOHandler.DefAnsiEncoding := ASrcEncoding;
{$ENDIF}
WriteContent;
end;
- In Lazarus under Linux I have both STRING_IS_UNICODE and STRING_IS_ANSI undefined. How is that possible?
Indy defines the STRING_IS_...
conditionals in its IdCompilerDefines.inc
file, did you {$I}
that file into your code?
Otherwise, Indy uses FreePascal's own FPC_UNICODESTRINGS
conditional to determine when to define STRING_IS_UNICODE
, so you could use that same conditional if you don't need compatibility with Delphi, eg:
procedure TMyForm.IdHTTPServer1Connect(AContext: TIdContext);
begin
{$IFNDEF FPC_UNICODESTRINGS}
AContext.Connection.IOHandler.DefAnsiEncoding := IndyTextEncoding_UTF8;
{$ENDIF}
end;
Or:
procedure TIdHTTPResponseInfoHelper.WriteContentUni(
{$IFNDEF FPC_UNICODESTRING}ASrcEncoding: IIdTextEncoding = nil{$ENDIF}
);
begin
{$IFNDEF FPC_UNICODESTRING}
EnsureEncoding(ASrcEncoding, encUTF8);
FConnection.IOHandler.DefAnsiEncoding := ASrcEncoding;
{$ENDIF}
WriteContent;
end;
from indy.
Everything goes as usual - all bugs are yours, if you think you found a bug in an outer library go back to the start of the phrase. Thanks!
from indy.
Related Issues (20)
- Assume the highest supported compiler version when a new compiler is released
- CTCP PING does not return correct value HOT 6
- Update IdCustomHTTPServer responses HOT 1
- SCRAM-SHA-1(-PLUS) + SCRAM-SHA-256(-PLUS) + SCRAM-SHA-512(-PLUS) + SCRAM-SHA3-512(-PLUS) supports
- Delphi 2007 w/ Indy 10.6.2.0 Won't Compile (File not found: IdSys.dcu) HOT 2
- EConvertError in TIdX509.notBefore and TIdX509.notAfter properties for 4-digit years
- Tidtelnet.Disconnect cannot be disconnected HOT 2
- No FHeadIndex - A Bug? HOT 4
- Wont Reset Bytes - A Bug? HOT 4
- Rad Studio 12 - Settings compatible HOT 3
- idHttp - IPv6 - IPVersion-ProtocolSwitch on Redirect (HandleRedirects=true) HOT 2
- IdSSLOpenSSL - UTC_Time_Decode - convert to char should be PAnsiChar instead of PChar on Unicode - Solved HOT 1
- Delphi Athena + Indy10 + Ubuntu 22.04.4 HOT 4
- Unable to build for C++ Builder 5 HOT 16
- how to install indy10 on the latest version of Delphi? HOT 2
- no more TCP-TLS in v10.6.3.3 HOT 2
- Cannot make a linux console application with Lazarus HOT 2
- TIdIMAP4: RFC 6154: Add a version of ListMailboxes that return also the mailbox' flags HOT 1
- Update TIdSMTP and TIdSMTPRelay to support sending a TStream instead of a TIdMessage
- Is there a way to access the documentation for indyproject? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from indy.