TsgcHTTP3Server implementa un server HTTP/3 (RFC 9114) che serve richieste HTTP su QUIC.
TsgcHTTP3Server implementa un server HTTP/3 come definito in RFC 9114. HTTP/3 trasporta richieste e risposte HTTP su QUIC (un trasporto basato su UDP con TLS 1.3 integrato), quindi ogni connessione è crittografata e multiplexata senza head-of-line blocking. Per impostazione predefinita il server funziona sul listener QUIC nativo fornito da OpenSSL 3.5 o successivo, e ripiega sul motore QUIC sgc quando quel listener non è disponibile (vedere QUICMode), quindi non è richiesta alcuna libreria HTTP/3 esterna.
Segua i passi seguenti per configurare questo componente:
1. Creare una nuova istanza di TsgcHTTP3Server e impostare la Port su cui il server sarà in ascolto (HTTP/3 usa UDP).
2. Impostare il certificato e la chiave privata in QUICOptions.SSLOptions. QUIC richiede TLS, quindi è necessario un certificato.
3. Gestire l'evento OnH3Request, compilare la risposta e chiamare Start. Il parametro aResponse viene creato dal server (il codice di stato predefinito è 200); impostarne StatusCode, ContentType e corpo, e il server la invia automaticamente quando il gestore ritorna. esempio:
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;
| Nome | Descrizione |
|---|---|
| Port | Porta UDP su cui il server HTTP/3 è in ascolto. |
| QUICOptions | Opzioni di trasporto QUIC, inclusi il certificato, la chiave privata e i limiti di connessione. |
| H3Settings | Impostazioni del protocollo HTTP/3 (limiti QPACK e field-section) comunicate ai peer. |
| Nome | Descrizione |
|---|---|
| Start | Avvia il listener QUIC e inizia ad accettare connessioni HTTP/3. |
| Stop | Arresta il listener e chiude tutte le connessioni attive. |
| SendResponse | Invia una risposta HTTP/3 su uno stream di richiesta. |
| Nome | Descrizione |
|---|---|
| OnH3Request | Si attiva quando viene ricevuta una richiesta HTTP/3 completa, in modo che l'applicazione possa compilare la risposta. |
| OnH3Connect | Si attiva quando un client stabilisce una connessione HTTP/3. |
| OnH3Disconnect | Si attiva quando una connessione HTTP/3 viene chiusa. |
L'esempio seguente avvia un server HTTP/3 che instrada un paio di percorsi e restituisce un corpo JSON o di testo semplice. La risposta viene riempita all'interno di OnH3Request e inviata automaticamente dal server.
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 abilitata.sgcHTTP3_Server alla clausola uses.QUICOptions.SSLOptions.IOHandler = iohOpenSSL. Non esiste fallback su una versione precedente di OpenSSL. Con iohNativeTLS, OpenSSL non è necessario.