mormot2 https演示

mormot2 https演示

复制代码
function TFileServer.StartServer(const pmcPort: RawUtf8; pmMode: THttpServerMode;
  const pmcCertFileName, pmcPrivKeyFileName: TFileName; const pmcPrivKeyPassword: RawUtf8): Boolean;
const
  WAIT_SECONDS = 10;
var
  tls: TNetTlsContext;
  keepAliveMs: Integer;
  serverOptions: THttpServerOptions;
begin
  Result := False;
  if FHttpServer <> Nil then Exit; //=>
  if (pmMode = hsmHttpsCert)
    and not (FileExists(pmcCertFileName) and FileExists(pmcPrivKeyFileName) and (pmcPrivKeyPassword <> '')) then Exit; //=>

  keepAliveMs := 30000;
  if pmMode = hsmHttp10 then
    keepAliveMs := 0;  // HTTP/1.0: KeepAliveTimeOut = 0

  serverOptions := [];
  if (pmMode = hsmHttpsSelf) or (pmMode = hsmHttpsCert) then
    serverOptions := [hsoEnableTls];

  FHttpServer := THttpServer.Create(pmcPort, Nil, Nil, '', {ServerThreadPoolCount=} 2, keepAliveMs, serverOptions);
  if (pmMode = hsmHttp10) or (pmMode = hsmHttp11) then
    FHttpServer.WaitStarted(WAIT_SECONDS)
  else
  begin
    if pmMode = hsmHttpsSelf then
    begin
      InitNetTlsContextSelfSignedServer(tls);//自签名证书
      try
        FHttpServer.WaitStarted(WAIT_SECONDS, @tls);
      finally
        DeleteFile(Utf8ToString(tls.CertificateFile));
        DeleteFile(Utf8ToString(tls.PrivateKeyFile));
      end;
    end
    else
    begin  
      InitNetTlsContext(tls, True, pmcCertFileName, pmcPrivKeyFileName, pmcPrivKeyPassword);  //非自签名证书
      FHttpServer.WaitStarted(WAIT_SECONDS, @tls);
    end;
  end;

  Result := FHttpServer.Started;
  if Result then
  begin
    FServerPort := pmcPort;
    if pmMode in [hsmHttpsSelf, hsmHttpsCert] then
      FUrlScheme := 'https'
    else
      FUrlScheme := 'http';

    FHttpServer.OnRequest := DoRequest;
  end;
end;
复制代码

 

posted @   delphi中间件  阅读(451)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示