2 に答える
FreePascal 文字列は、Delphi 2009 以降のように UTF-16 でエンコードされていません。FreePascal および Delphi 2007 以前では、コードで実際の文字列エンコーディングを考慮する必要があります。そのため、Indy はこれらのプラットフォーム用に追加の Ansi ベースのパラメーター/プロパティを公開しています。
がusing をTIdHTTPServer
書き出すとき、パラメーターは非 Unicode プラットフォームでは使用されないため、代わりにプロパティを使用して、のエンコーディングが何であるかを知る必要があります。次に例を示します。ContentText
TIdIOHandler.Write()
ASrcEncoding
TIdIOHandler.DefAnsiEncoding
Write()
ContentText
procedure TMyServer.DoCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
const
UNI: WideString = '中文';
begin
AResponseInfo.ContentText := UTF8Encode('<html>' + UNI + '</html>');
AResponseInfo.ContentType := 'text/html';
// this tells TIdHTTPServer what to encode bytes to during socket transmission
AResponseInfo.CharSet := 'utf-8';
// this tells TIdHTTPServer what encoding the ContentText is using
// so it can be decoded to Unicode prior to then being charset-encoded
// for output. If the input and output encodings are the same, the
// Ansi string data gets transmitted as-is without decoding/reencoding...
AContext.Connection.IOHandler.DefAnsiEncoding := IndyUTF8Encoding;
end;
または、より一般的に:
{$I IdCompilerDefines.inc}
procedure TMyServer.DoCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
const
UNI{$IFNDEF STRING_IS_UNICODE}: WideString{$ENDIF} = '中文';
begin
{$IFDEF STRING_IS_UNICODE}
AResponseInfo.ContentText := '<html>' + UNI + '</html>';
{$ELSE}
AResponseInfo.ContentText := UTF8Encode('<html>' + UNI + '</html>');
{$ENDIF}
AResponseInfo.ContentType := 'text/html';
AResponseInfo.CharSet := 'utf-8';
{$IFNDEF STRING_IS_UNICODE}
AContext.Connection.IOHandler.DefAnsiEncoding := IndyUTF8Encoding;
{$ENDIF}
end;
最新の FreePascal 文字列では、コンパイラ オプションを微調整しない限り、デフォルトで UTF-8 です。
したがってiif(ASrcEncoding, FDefAnsiEncoding, encOSDefault);
、 の値encOSDefault
が間違っているようです。必要に応じてINDYソースでその検出を修正するか、設定する方がよいと思いますDefAnsiEncoding := 'utf-8';
(RFC AFAIRによる低ケース)
安全のために、プログラムの開始時に UTF-8 モードをチェックできます。ラテン語以外の定数(中国語、ギリシャ語、キリル文字など)を設定し、UTF8かどうかを確認します:http://compaspascal.blogspot.ru/2009/03/utf-8-automatic-detection. html
ただし、全体としては、Indy よりも FPC と Linux を重視するライブラリを見つけてみてください。Indy は、Delphi でも停滞しており、見捨てられているように思えます。おそらくSynopse mORMot
(DataSnap パフォーマンス テストの記事を参照してください)、CodeTyphon
ディストリビューションに付属のライブラリが役立つ可能性があります。