此代码启动一个http服务器,该服务器侦听端口8080上的请求。使用Delphi2009编译时,中文文本呈现正确。但是,如果使用Free Pascal 2.6.0,浏览器将显示中文,而不是中文
使用indy和free pascal编写unicode/utf-8http响应的正确方法是什么?

program IdHTTPUnicode;

{$APPTYPE CONSOLE}

uses
  IdHTTPServer, IdCustomHTTPServer, IdContext, IdSocketHandle, IdGlobal,
  SysUtils;

type
  TMyServer = class (TIdHTTPServer)
  public
    procedure InitComponent; override;
    procedure DoCommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo;
      AResponseInfo: TIdHTTPResponseInfo); override;
  end;

procedure Demo;
var
  Server: TMyServer;
begin
  Server := TMyServer.Create(nil);
  try
    try
      Server.Active := True;
    except
      on E: Exception do
      begin
        WriteLn(E.ClassName + ' ' + E.Message);
      end;
    end;
    WriteLn('Hit any key to terminate.');
    ReadLn;
  finally
    Server.Free;
  end;
end;

procedure TMyServer.InitComponent;
var
  Binding: TIdSocketHandle;
begin
  inherited;

  Bindings.Clear;
  Binding := Bindings.Add;
  Binding.IP := '127.0.0.1';
  Binding.Port := 8080;
  Binding.IPVersion := Id_IPv4;
end;

procedure TMyServer.DoCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
const
  UNI = '中文';
begin
  AResponseInfo.ContentText := '<html>' + UNI + '</html>';
  AResponseInfo.ContentType := 'text/html';
  AResponseInfo.CharSet := 'UTF-8';
end;

begin
  Demo;
end.

在调试器中,我可以看到tidiohandler.write方法中的不同代码被执行,对于free pascal,string_is_ansi被定义为:
procedure TIdIOHandler.Write(const AOut: string; AByteEncoding: TIdTextEncoding = nil
  {$IFDEF STRING_IS_ANSI}; ASrcEncoding: TIdTextEncoding = nil{$ENDIF}
  );
begin
  if AOut <> '' then begin
    AByteEncoding := iif(AByteEncoding, FDefStringEncoding);
    {$IFDEF STRING_IS_ANSI}
    ASrcEncoding := iif(ASrcEncoding, FDefAnsiEncoding, encOSDefault);
    {$ENDIF}
    Write(
      ToBytes(AOut, -1, 1, AByteEncoding
        {$IFDEF STRING_IS_ANSI}, ASrcEncoding{$ENDIF}
        )
      );
  end;
end;

最佳答案

freepascal字符串不像delphi 2009+中那样是utf-16编码的。在freepascal和delphi 2007及更早版本中,您的代码需要考虑实际的字符串编码。这就是为什么indy为这些平台公开了额外的基于ansi的参数/属性。
TIdHTTPServer使用ContentText写出TIdIOHandler.Write()时,ASrcEncoding参数不在非Unicode平台上使用,因此您必须使用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;

09-04 21:09
查看更多