TsgcHTTP3Server는 QUIC를 통해 HTTP 요청을 제공하는 HTTP/3 서버(RFC 9114)를 구현합니다.
TsgcHTTP3Server는 RFC 9114에 정의된 HTTP/3 서버를 구현합니다. HTTP/3는 QUIC(TLS 1.3가 내장된 UDP 기반 전송)를 통해 HTTP 요청과 응답을 전달하므로, 모든 연결은 head-of-line 차단 없이 암호화되고 다중화됩니다. 서버는 기본적으로 OpenSSL 3.5 이상에서 제공하는 네이티브 QUIC 리스너에서 실행되며, 해당 리스너를 사용할 수 없으면 sgc QUIC 엔진으로 대체됩니다(QUICMode 참조). 따라서 외부 HTTP/3 라이브러리가 필요하지 않습니다.
이 구성 요소를 구성하려면 아래 단계를 따르십시오:
1. TsgcHTTP3Server의 새 인스턴스를 만들고 서버가 수신 대기할 Port를 설정합니다(HTTP/3는 UDP를 사용함).
2. QUICOptions.SSLOptions에 인증서와 개인 키를 설정합니다. QUIC는 TLS를 요구하므로 인증서가 필요합니다.
3. OnH3Request 이벤트를 처리하고, 응답을 채운 다음 Start를 호출하십시오. aResponse 매개변수는 서버가 생성합니다(상태 코드 기본값은 200). StatusCode, ContentType 및 본문을 설정하면 핸들러가 반환될 때 서버가 자동으로 전송합니다. 예제:
oServer := TsgcHTTP3Server.Create(nil);
oServer.Port := 443;
oServer.QUICOptions.SSLOptions.CertFile := 'cert.pem';
oServer.QUICOptions.SSLOptions.KeyFile := 'key.pem';
oServer.OnH3Request := OnH3RequestEvent;
oServer.Start;
procedure OnH3RequestEvent(Sender: TObject; const aConnection:
TsgcQUICServerConnection; const aRequest: TsgcHTTP3Request;
const aResponse: TsgcHTTP3Response);
begin
aResponse.StatusCode := 200;
aResponse.ContentType := 'text/plain';
aResponse.Data := TEncoding.UTF8.GetBytes('Hello HTTP/3');
end;
| Name | 설명 |
|---|---|
| Port | HTTP/3 서버가 수신 대기하는 UDP 포트. |
| QUICOptions | 인증서, 개인 키 및 연결 제한을 포함한 QUIC 전송 옵션. |
| H3Settings | 피어에게 알리는 HTTP/3 프로토콜 설정(QPACK 및 필드 섹션 제한). |
| Name | 설명 |
|---|---|
| Start | QUIC 리스너를 시작하고 HTTP/3 연결 수락을 시작합니다. |
| Stop | 리스너를 중지하고 모든 활성 연결을 닫습니다. |
| SendResponse | 요청 스트림으로 HTTP/3 응답을 보냅니다. |
| Name | 설명 |
|---|---|
| OnH3Request | 완전한 HTTP/3 요청이 수신될 때 발생하여 애플리케이션이 응답을 채울 수 있습니다. |
| OnH3Connect | 클라이언트가 HTTP/3 연결을 설정할 때 발생합니다. |
| OnH3Disconnect | HTTP/3 연결이 닫힐 때 발생합니다. |
다음 예제는 몇 가지 경로를 라우팅하고 JSON 또는 일반 텍스트 본문을 반환하는 HTTP/3 서버를 시작합니다. 응답은 OnH3Request 내에서 채워지고 서버에 의해 자동으로 전송됩니다.
oServer := TsgcHTTP3Server.Create(nil);
oServer.Port := 443;
oServer.QUICOptions.SSLOptions.CertFile := 'cert.pem';
oServer.QUICOptions.SSLOptions.KeyFile := 'key.pem';
oServer.OnH3Request := OnH3RequestEvent;
oServer.OnH3Connect := OnH3ConnectEvent;
oServer.OnH3Disconnect := OnH3DisconnectEvent;
oServer.Start;
procedure OnH3ConnectEvent(Sender: TObject; const aConnection:
TsgcQUICServerConnection);
begin
Log('HTTP/3 client connected: ' + aConnection.PeerIP);
end;
procedure OnH3DisconnectEvent(Sender: TObject; const aConnection:
TsgcQUICServerConnection; aCode: Int64; const aReason: string);
begin
Log('HTTP/3 client disconnected');
end;
procedure OnH3RequestEvent(Sender: TObject; const aConnection:
TsgcQUICServerConnection; const aRequest: TsgcHTTP3Request;
const aResponse: TsgcHTTP3Response);
begin
if aRequest.Path = '/api/time' then
begin
aResponse.StatusCode := 200;
aResponse.ContentType := 'application/json';
aResponse.Data := TEncoding.UTF8.GetBytes(
'{"time":"' + DateTimeToStr(Now) + '"}');
end
else
begin
aResponse.StatusCode := 404;
aResponse.ContentType := 'text/plain';
aResponse.Data := TEncoding.UTF8.GetBytes('Not Found');
end;
end;
SGC_HTTP3 조건부 정의를 활성화하여 컴파일하십시오.sgcHTTP3_Server를 추가합니다.QUICOptions.SSLOptions.IOHandler = iohOpenSSL인 경우 OpenSSL 3.5 이상이 필요합니다. 이전 버전의 OpenSSL로는 대체되지 않습니다. iohNativeTLS를 사용하면 OpenSSL이 필요하지 않습니다.