Skip to content

Commit

Permalink
Add the HTTP/2 example.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed Jun 27, 2024
1 parent 9d0c64c commit 29a1b34
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/http2/dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name "http2-example"

dependency "vibe-http" path="../../../vibe-http"

/*versions "VibeForceALPN"*/
targetType "executable"
/*buildOptions "profileGC"*/
20 changes: 20 additions & 0 deletions examples/http2/server.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-----BEGIN CERTIFICATE-----
MIIDUjCCAjoCCQDPHygOhe1ZVjANBgkqhkiG9w0BAQUFADBrMQswCQYDVQQGEwJE
RTEbMBkGA1UECBMSU2NobGVzd2lnLUhvbHN0ZWluMRAwDgYDVQQHFAdMw7xiZWNr
MRkwFwYDVQQKExBvdXRlcnByb2R1Y3Qub3JnMRIwEAYDVQQDEwlsb2NhbGhvc3Qw
HhcNMTIwMjE0MjAxNDM0WhcNMTMwMjEzMjAxNDM0WjBrMQswCQYDVQQGEwJERTEb
MBkGA1UECBMSU2NobGVzd2lnLUhvbHN0ZWluMRAwDgYDVQQHFAdMw7xiZWNrMRkw
FwYDVQQKExBvdXRlcnByb2R1Y3Qub3JnMRIwEAYDVQQDEwlsb2NhbGhvc3QwggEi
MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC4fLPWRShlq+o6vM5nhUCtR+IN
mlAlQCzM3asWpQ76T/MOX1Ci1brypRgOXCmYI8c5lFGOIMpH1ZAh987t4UdkwVCn
76pXv5yY4U1jJ5i9v7R7IUeKZ7utwlq1jPo3WWhiZwm1o6AxVRHGiUQ5XrcX0Rgt
0bh9/5BWHLks3OJ44myjpgGv2J7n1LcyecRAd+suN4qsBiTITQBYq27WYsDCqrVB
BoyCPhb51xTlaMlcAH2ekrm886FlC95VrwV7o4jb4sRGYdfKqT6HwXK1yTB3n742
2WavDgRj2vNQaL+XoPiBOrQZ8fj9PeWCyefSEtqz8/dpnJO+pxidKAow8As5AgMB
AAEwDQYJKoZIhvcNAQEFBQADggEBAA8N27Wb9aq9JkAEGZ0Z/CLHbLeV3UQ5qb9V
6KG8vvSoew3oxMlcHa49kq89AKx/gewt4fqCAHk64qpx8aEdbYorTbo9VIbwoLek
9Lyp+AynmqDA6zk5+uOtPkwfN84f30khH04ouSmOvbV+uqD9bVZtR8ULTzbuE2h1
jbT64JQd+GW/uLQ770EKDVFml52BMJWrRFFgaRQhkm9k8krKqsCvYfMoULk3EQqS
b5a/5q9pirXB7AHmiYAnqDu2xQL8N5e548RTZSWNl7mZD1NvVYc4l8tSKfCC3zG3
4wKngPl6t68pdnli67lX3YDTdHgOZWL+CsJT9TIRAOojfp5kWGY=
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions examples/http2/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAuHyz1kUoZavqOrzOZ4VArUfiDZpQJUAszN2rFqUO+k/zDl9Q
otW68qUYDlwpmCPHOZRRjiDKR9WQIffO7eFHZMFQp++qV7+cmOFNYyeYvb+0eyFH
ime7rcJatYz6N1loYmcJtaOgMVURxolEOV63F9EYLdG4ff+QVhy5LNzieOJso6YB
r9ie59S3MnnEQHfrLjeKrAYkyE0AWKtu1mLAwqq1QQaMgj4W+dcU5WjJXAB9npK5
vPOhZQveVa8Fe6OI2+LERmHXyqk+h8Fytckwd5++Ntlmrw4EY9rzUGi/l6D4gTq0
GfH4/T3lgsnn0hLas/P3aZyTvqcYnSgKMPALOQIDAQABAoIBABdkiJEk18h8kgi8
pBdwSBEwyjMbXAo9JvEbMnR+nXWT6afq4hijrT7TPEel3AhUkRB2BBlXgw60v7/u
4ig7pofaE1YYB6t0unCQMPXfsXht9H6ga6fbG2se982JgLi/94JyukJz6v4WYVih
UytLHUBB3SUCMLiZTT3+CmTr5T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PRIVATE KEY-----
92 changes: 92 additions & 0 deletions examples/http2/source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* ==== Vibe.d HTTP/2 Webserver Example ==== */
/* Supports both HTTP and HTTPS transport */
/* Transparent (WIP: exposing settings) */
/* ========================================= */

import vibe.http.server;
import vibe.stream.tls;
import vibe.http.internal.http2.server : http2Callback; // ALPN negotiation
import vibe.core.core : runApplication;

/* ==== declare two handlers (could use the same one) ==== */
void handleReq(HTTPServerRequest req, HTTPServerResponse res)
@safe {
if (res.httpVersion == HTTPVersion.HTTP_2)
res.writeBody("Hello, you connected to "~req.path~"! This response is sent through HTTP/2\n");
else
res.writeBody("Hello, World! You connected through HTTP/1, try using HTTP/2!\n");
}

void tlsHandleReq(HTTPServerRequest req, HTTPServerResponse res)
@safe {
if (req.httpVersion == HTTPVersion.HTTP_2)
res.writeBody("Hello, you connected to "~req.path~"! This response is sent through HTTP/2 with TLS\n");
else
res.writeBody("Hello, World! You connected through HTTP/1 with TLS, try using HTTP/2!\n");
}

// sends a very big data frame
void bigHandleReq(size_t DIM)(HTTPServerRequest req, HTTPServerResponse res)
@trusted {
import vibe.container.internal.appender : FixedAppender;
import std.range : iota;

FixedAppender!(immutable(char)[], DIM) appender;

if (req.path == "/") {
foreach(i; iota(1,DIM-4)) appender.put('1');
appender.put(['O','k','!', '\n']);
res.writeBody(appender.data);
}
}

void main()
{
//import vibe.core.log;
//setLogLevel(LogLevel.trace);

/* ==== cleartext HTTP/2 support (h2c) ==== */
auto settings = new HTTPServerSettings;
settings.port = 8090;
settings.bindAddresses = ["127.0.0.1"];
listenHTTP(settings, &handleReq);

/* ==== cleartext HTTP/2 support (h2c) with a heavy DATA frame ==== */
auto bigSettings = new HTTPServerSettings;
settings.port = 8092;
settings.bindAddresses = ["127.0.0.1"];
listenHTTP(settings, &bigHandleReq!100000);

/* ========== HTTPS (h2) support ========== */
auto tlsSettings = new HTTPServerSettings;
tlsSettings.port = 8091;
tlsSettings.bindAddresses = ["127.0.0.1"];

/// setup TLS context by using cert and key in example rootdir
tlsSettings.tlsContext = createTLSContext(TLSContextKind.server);
tlsSettings.tlsContext.useCertificateChainFile("server.crt");
tlsSettings.tlsContext.usePrivateKeyFile("server.key");

// set alpn callback to support HTTP/2 protocol negotiation
tlsSettings.tlsContext.alpnCallback(http2Callback);
listenHTTP(tlsSettings, &tlsHandleReq);

/* ========== HTTPS (h2) support with a heavy DATA frame ========== */
auto bigTLSSettings = new HTTPServerSettings;
bigTLSSettings.port = 8093;
bigTLSSettings.bindAddresses = ["127.0.0.1"];

/// setup TLS context by using cert and key in example rootdir
bigTLSSettings.tlsContext = createTLSContext(TLSContextKind.server);
bigTLSSettings.tlsContext.useCertificateChainFile("server.crt");
bigTLSSettings.tlsContext.usePrivateKeyFile("server.key");

// set alpn callback to support HTTP/2 protocol negotiation
bigTLSSettings.tlsContext.alpnCallback(http2Callback);
auto l = listenHTTP(bigTLSSettings, &bigHandleReq!100000);
scope(exit) l.stopListening();

/* ========== Run both `listenHTTP` handlers ========== */
// UNCOMMENT to run
runApplication();
}

0 comments on commit 29a1b34

Please sign in to comment.