Skip to content

Commit 4831d17

Browse files
committed
Add basic http2 CPP server
1 parent e9423ee commit 4831d17

8 files changed

Lines changed: 193 additions & 0 deletions

File tree

cpp/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required (VERSION 3.5.1)
2+
3+
project(cpphttpserver LANGUAGES CXX)
4+
5+
message( STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}" )
6+
7+
set(CMAKE_CXX_STANDARD 17)
8+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9+
set(CMAKE_CXX_EXTENSIONS OFF)
10+
11+
add_compile_options(
12+
"-Wall"
13+
"-Werror"
14+
$<$<CONFIG:Debug>:--coverage>
15+
)
16+
17+
link_libraries(
18+
$<$<CONFIG:Debug>:--coverage>
19+
)
20+
21+
add_subdirectory(src/main/cpp)

cpp/docker/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM cppserverbase:latest as builder
2+
3+
COPY . /code
4+
WORKDIR /code/build
5+
RUN cmake -DCMAKE_BUILD_TYPE=Release .. \
6+
&& make -j4 && mkdir -p /cppserver \
7+
&& cp /code/build/src/main/cpp/cpphttpserver /cppserver/cppserver \
8+
&& rm -rf /code
9+
10+
WORKDIR /cppserver
11+
CMD ["./cppserver"]
12+
EXPOSE 3000
13+

cpp/docker/base/Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
FROM alpine:latest
2+
3+
RUN apk add build-base cmake wget tar linux-headers openssl-dev libev-dev openssl-libs-static curl-dev zlib-dev git openssl ca-certificates
4+
5+
RUN cd / && git clone --recurse-submodules https://github.com/aws/aws-sdk-cpp.git
6+
7+
# Build
8+
RUN cd / && mkdir -p sdkbuild && cd sdkbuild && \
9+
cmake -DBUILD_ONLY="lambda;sts" -DBUILD_SHARED_LIBS=OFF \
10+
-DENABLE_TESTING=OFF -DFORCE_SHARED_CRT=OFF ../aws-sdk-cpp
11+
12+
# Install the sdk
13+
RUN cd /sdkbuild && make install && cd ..
14+
15+
# Other dependencies
16+
17+
WORKDIR /code/deps
18+
19+
RUN set -x && wget https://boostorg.jfrog.io/artifactory/main/release/1.76.0/source/boost_1_76_0.tar.gz && \
20+
tar xvf boost* && cd boost* && ./bootstrap.sh && ./b2 -j4 install && cd .. && rm -rf * && set +x
21+
22+
RUN set -x && wget https://github.com/google/googletest/archive/2fe3bd9.tar.gz -O gtest.tar.gz && \
23+
tar xvf gtest.tar.gz && cd googletest* && cmake . && make -j4 install && cd .. && rm -rf * && set +x
24+
25+
RUN set -x && wget https://github.com/nghttp2/nghttp2/releases/download/v1.39.2/nghttp2-1.39.2.tar.bz2 && \
26+
tar xf nghttp2* && cd nghttp2* && ./configure --enable-asio-lib --disable-shared && make -j4 install && \
27+
cd .. && rm -rf * && set +x
28+
29+
WORKDIR /code/build

cpp/scripts/build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
docker build --no-cache -t cppserverhttp2talk -f docker/Dockerfile .

cpp/scripts/buildBase.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
docker build --no-cache -t cppserverbase -f docker/Dockerfile .

cpp/src/main/cpp/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
add_executable(cpphttpserver main.cpp)
2+
find_package(AWSSDK REQUIRED COMPONENTS lambda sts)
3+
4+
# TODO: try static linking
5+
target_link_libraries(cpphttpserver
6+
# Keep the list of files sorted from down to top.
7+
${AWSSDK_LINK_LIBRARIES}
8+
nghttp2_asio
9+
nghttp2
10+
boost_thread
11+
boost_filesystem
12+
boost_system
13+
ssl
14+
crypto
15+
dl
16+
pthread
17+
)
18+
19+

cpp/src/main/cpp/main.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Server libs
2+
#include <nghttp2/asio_http2_server.h>
3+
4+
// Standard libs
5+
#include <fstream>
6+
#include <iostream>
7+
8+
#include <chrono>
9+
#include <sys/time.h>
10+
#include <ctime>
11+
12+
#include <thread>
13+
14+
//namespaces
15+
using namespace nghttp2::asio_http2;
16+
using namespace nghttp2::asio_http2::server;
17+
18+
namespace ba = boost::asio;
19+
namespace
20+
{
21+
// server push demonstration constants
22+
static const auto html = R"(
23+
<!DOCTYPE html>
24+
<html lang="en">
25+
<title>HTTP/2 FTW</title>
26+
<body>
27+
<link href="/style.css" rel="stylesheet" type="text/css">
28+
<h1>This should be green</h1>
29+
</body>
30+
</html>\n)";
31+
32+
static const auto css = "h1 { color: green; }";
33+
34+
}
35+
36+
int main(int argc, char *argv[]) {
37+
boost::system::error_code ec;
38+
http2 server;
39+
40+
// Concurrency
41+
//unsigned int cores = std::thread::hardware_concurrency();
42+
//server.num_threads((cores?cores:1));
43+
44+
// Server push resource
45+
server.handle("/serverpush", [](const request &req, const response &res) {
46+
std::cout << "Got a /serverpush request!" << std::endl;
47+
boost::system::error_code ec;
48+
auto push = res.push(ec, "GET", "/style.css");
49+
push->write_head(200);
50+
push->end(css);
51+
res.write_head(200);
52+
res.end(html);
53+
});
54+
55+
// Normal application/json resource
56+
server.handle("/", [](const request &req, const response &res) {
57+
std::cout << "Got a / request!" << std::endl;
58+
nghttp2::asio_http2::header_map headers;
59+
headers.insert(
60+
std::make_pair<std::string, nghttp2::asio_http2::header_value>(
61+
"content-type",
62+
{"application/json; charset=utf-8",false}));
63+
res.write_head(200,headers);
64+
res.end("{\"SERVER\":\"UP\"}\n");
65+
});
66+
67+
// TLS context for HTTP/2
68+
boost::asio::ssl::context tlsCtx(boost::asio::ssl::context::sslv23);
69+
tlsCtx.use_private_key_file("/usr/local/share/ca-certificates/cpphttp2server.key",
70+
boost::asio::ssl::context::pem);
71+
tlsCtx.use_certificate_chain_file("/usr/local/share/ca-certificates/cpphttp2server.crt");
72+
configure_tls_context_easy(ec, tlsCtx);
73+
74+
std::cout << "STARTING HTTP/2 CPP server!" << std::endl;
75+
//std::cout << "STARTING HTTP/2 CPP server WITH " << cores << " concurrent threads!" << std::endl;
76+
if (server.listen_and_serve(ec,tlsCtx, "0.0.0.0", "3000")) {
77+
std::cerr << "error: " << ec.message() << std::endl;
78+
}
79+
80+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"dns": "cpphttp2server",
3+
"port": "3000",
4+
"secure": true,
5+
"timeout": 5000,
6+
"ranges": {
7+
"range_1": {
8+
"min": 0,
9+
"max": 999999
10+
}
11+
},
12+
"flow": [
13+
"post1"
14+
],
15+
"messages": {
16+
"post1": {
17+
"method": "GET",
18+
"url": "",
19+
"body": {},
20+
"response": {
21+
"code": 200
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)