首页 > 其他分享 >mormot2 https演示

mormot2 https演示

时间:2023-02-01 14:13:59浏览次数:44  
标签:tls begin 演示 https FHttpServer pmMode mormot2 serverOptions end

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;

 

标签:tls,begin,演示,https,FHttpServer,pmMode,mormot2,serverOptions,end
From: https://www.cnblogs.com/hnxxcxg/p/17082358.html

相关文章