Skip to content

cHttpServer TLS/HTTPS 支持

概述

cHttpServer 支持通过链式函数配置 TLS 证书,将 HTTP 服务升级为 HTTPS。底层使用 cTlsReMaster(基于 cTlsSocket),支持 TLS 1.2/1.3。

链式函数

TlsCertFile — 证书文件模式

最常用方式,支持 PFX/P12 和 PEM 格式。

vb
Public Function TlsCertFile( _
    ByVal CertFile As String, _
    Optional ByVal Password As String, _
    Optional ByVal AlpnProtocols As String = "http/1.1") As cHttpServer

参数:

参数类型必要说明
CertFileString证书文件路径。PFX/P12 或 PEM 格式,PEM 支持用 | 分隔多文件
PasswordString证书密码,PEM 无加密私钥时留空
AlpnProtocolsStringALPN 协议协商,默认 "http/1.1",HTTP/2 可设为 "h2,http/1.1"

CertFile 格式:

  • PFX/P12:"C:\certs\server.pfx"
  • PEM 单文件:"C:\certs\server.pem"(含证书和私钥)
  • PEM 多文件:"C:\certs\fullchain.pem|C:\certs\privkey.pem"

TlsCertSubject — Windows 证书存储模式

从 Windows 系统证书库查找证书,适合企业环境。

vb
Public Function TlsCertSubject( _
    ByVal CertSubject As String, _
    Optional ByVal AlpnProtocols As String = "http/1.1") As cHttpServer

参数:

参数类型必要说明
CertSubjectString证书主题名称(如 "www.example.com"
AlpnProtocolsStringALPN 协议协商,默认 "http/1.1"

TlsCertMemory — 内存证书集合模式

高级用法,从内存中的 Collection 加载证书。

vb
Public Function TlsCertMemory( _
    ByVal Certificates As Collection, _
    ByVal PrivateKey As Collection, _
    Optional ByVal AlpnProtocols As String = "http/1.1") As cHttpServer

Start 方法

改造后只保留端口和 IP 参数:

vb
Public Function Start(Optional Port As Long = 80, Optional IP As String = "0.0.0.0") As Boolean

使用示例

PEM 证书(Let's Encrypt 最常见)

vb
Server.TlsCertFile("C:\certs\fullchain.pem|C:\certs\privkey.pem").Start 443

PFX 带密码

vb
Server.TlsCertFile("C:\certs\server.pfx", "my-password").WebRoot("C:\www").Start 443

Windows 证书存储

vb
Server.TlsCertSubject("www.example.com").Start 443

纯 HTTP(不调用任何 Tls 函数)

vb
Server.WebRoot("C:\www").Start 80

HTTP + HTTPS 双端口

vb
' HTTP
Dim httpSvr As New cHttpServer
httpSvr.WebRoot("C:\www").Start 80

' HTTPS
Dim httpsSvr As New cHttpServer
httpsSvr.TlsCertFile("C:\certs\server.pfx", "pwd").WebRoot("C:\www").Start 443

条件启用

vb
If Config.EnableHTTPS Then
    If Config.CertFromStore Then
        Server.TlsCertSubject Config.CertSubject
    Else
        Server.TlsCertFile Config.CertPath, Config.CertPwd
    End If
End If
Server.WebRoot(Config.WebRoot).Start Config.Port, Config.BindIP

内部机制

TLS 模式枚举

vb
Private Enum EnumTlsMode
    TlsNone          ' 未启用 TLS(默认)
    TlsModeCertFile  ' 证书文件
    TlsModeSubject   ' Windows 证书存储
    TlsModeMemory    ' 内存证书集合
End Enum
  • 调用任一 Tls 函数 = 设置对应枚举值
  • Start() 根据枚举值选择 TCP 或 TLS 协议
  • StopMe() 重置枚举为 TlsNone,清空所有 TLS/WebRoot 中间变量

向后兼容

不调用任何 Tls 函数时,行为与改造前完全一致:

  • Start(Port, IP) — 纯 HTTP
  • WebRoot() 链式函数替代了原来 StartWebRoot 参数

版本变更

vbman 1.0.0.419 起,Start() 不再接受 WebRoot 参数,请使用链式函数 WebRoot() 配置静态文件目录。

证书来源说明

三种证书模式的详细介绍,参见 TLS 证书配置总览

证书模式详见
TlsCertFile(证书文件)证书文件模式
TlsCertSubject(Windows 证书存储)Windows 证书存储模式
TlsCertMemory(内存证书集合)内存证书集合模式

底层 InitServerTls 按以下优先级检测:

  1. 内存集合 (Certificates + PrivateKey)
  2. 证书文件 (CertFile)
  3. Windows 证书存储 (CertSubject)

注意事项

  1. 证书链完整性:PEM 格式需包含 fullchain(服务器证书 + 中间证书)
  2. ALPN 协商:默认 "http/1.1",如需 HTTP/2 支持设为 "h2,http/1.1"
  3. 错误处理:证书文件不存在/密码错误/证书过期时,Start 返回 False 并设置 LastError
  4. StopMe 重置:停止后重新 Start 为纯 HTTP,无需手动清除 TLS 配置
  5. TLS 对 HTTP 协议解析完全透明:其他 HttpServer 类文件无需任何改动

VB6及其LOGO版权为微软公司所有