TsgcWebSocketServer | 同一端口上的 TLS 与明文 HTTP

TLS 服务器(使用默认 Indy、IOCP 或 EPOLL IOHandler 的 TsgcWebSocketServer、TsgcWebSocketHTTPServer 和 TsgcHTTPServer)还可以在其用于 TLS 的同一端口上接受明文 HTTP 和 WebSocket 连接。启用此选项后,服务器会预读每个新连接的第一个字节。如果是 $16 字节(TLS 握手记录),表示客户端开始 TLS 握手;任何其他字节都表示该连接按明文方式处理。如果在 PlainTextTimeout 内没有收到任何字节,则该连接按 TLS 处理,这与之前的行为一致。使用 IOCP 和 EPOLL IOHandler 时,服务器会在收到第一批数据时做出判断,不会阻塞,也不使用 PlainTextTimeout。

典型用途是将已发布的明文 HTTP 端口迁移到 HTTPS,而无需更改客户端中配置的端口。在更新客户端期间启用 AllowPlainText,待所有客户端都使用 TLS 后再将其关闭。

属性

枚举 TsgcSSLPlainTextPolicy 在单元 sgcWebSocket_Types 中声明。

示例

在 443 端口启动一个同时接受 TLS 和明文连接的服务器,并对两种连接都提供服务。


oServer := TsgcWebSocketHTTPServer.Create(nil);
oServer.SSL := True;
oServer.Port := 443;
oServer.SSLOptions.Port := 443;
oServer.SSLOptions.CertFile := 'c:\certificates\mycert.pem';
oServer.SSLOptions.KeyFile := 'c:\certificates\mycert.pem';
oServer.SSLOptions.RootCertFile := 'c:\certificates\mycert.pem';
oServer.SSLOptions.AllowPlainText := True;
oServer.SSLOptions.PlainTextPolicy := ptpServe;
oServer.Active := True;

要了解请求是否使用 TLS,请调用服务器方法 IsTLS(AContext),例如在 OnCommandGet 或 OnCommandOther 事件中。连接的 TLS 属性(TsgcWSConnectionServer)同样可用。


procedure OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  if TsgcWebSocketHTTPServer1.IsTLS(AContext) then
    AResponseInfo.ContentText := 'Secure connection'
  else
    AResponseInfo.ContentText := 'Plain connection, please use https://';
end;

限制