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 @ 2023-02-01 13:58  delphi中间件  阅读(421)  评论(0编辑  收藏  举报