Added OpenSSL support. #5

This commit is contained in:
yhirose 2017-04-21 23:00:00 -04:00
parent 2c276ed31a
commit 22f124f871
8 changed files with 373 additions and 100 deletions

View file

@ -1,24 +1,25 @@
USE_CLANG = 1
ifdef USE_CLANG
CC = clang++
CFLAGS = -std=c++1y -stdlib=libc++ -g
else
CC = g++-4.9
CFLAGS = -std=c++1y -g
endif
CFLAGS = -std=c++14 -I..
#OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto
all: server client hello simplesvr
server : server.cc ../httplib.h
$(CC) -o server $(CFLAGS) -I.. server.cc
$(CC) -o server $(CFLAGS) server.cc $(OPENSSL_SUPPORT)
client : client.cc ../httplib.h
$(CC) -o client $(CFLAGS) -I.. client.cc
$(CC) -o client $(CFLAGS) client.cc $(OPENSSL_SUPPORT)
hello : hello.cc ../httplib.h
$(CC) -o hello $(CFLAGS) -I.. hello.cc
$(CC) -o hello $(CFLAGS) hello.cc $(OPENSSL_SUPPORT)
simplesvr : simplesvr.cc ../httplib.h
$(CC) -o simplesvr $(CFLAGS) -I.. simplesvr.cc
$(CC) -o simplesvr $(CFLAGS) simplesvr.cc $(OPENSSL_SUPPORT)
pem:
openssl genrsa 2048 > key.pem
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
clean:
rm server client hello simplesvr *.pem

View file

@ -12,7 +12,11 @@ using namespace std;
int main(void)
{
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
httplib::SSLClient cli("localhost", 8080);
#else
httplib::Client cli("localhost", 8080);
#endif
auto res = cli.get("/hi");
if (res) {

View file

@ -8,6 +8,9 @@
#include <httplib.h>
#include <cstdio>
#define SERVER_CERT_FILE "./cert.pem"
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
using namespace httplib;
std::string dump_headers(const MultiMap& headers)
@ -51,7 +54,7 @@ std::string log(const Request& req, const Response& res)
snprintf(buf, sizeof(buf), "%d\n", res.status);
s += buf;
s += dump_headers(res.headers);
if (!res.body.empty()) {
s += res.body;
}
@ -63,7 +66,11 @@ std::string log(const Request& req, const Response& res)
int main(void)
{
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
#else
Server svr;
#endif
svr.get("/", [=](const auto& req, auto& res) {
res.set_redirect("/hi");

View file

@ -9,6 +9,9 @@
#include <cstdio>
#include <iostream>
#define SERVER_CERT_FILE "./cert.pem"
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
using namespace httplib;
using namespace std;
@ -52,7 +55,7 @@ string log(const Request& req, const Response& res)
snprintf(buf, sizeof(buf), "%d\n", res.status);
s += buf;
s += dump_headers(res.headers);
return s;
}
@ -63,7 +66,11 @@ int main(int argc, const char** argv)
return 1;
}
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
#else
Server svr;
#endif
svr.set_error_handler([](const auto& req, auto& res) {
const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";