From 68fcce69fa067cbc7c866a5f72438bc54588a098 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Tue, 21 Aug 2018 03:35:03 +0200 Subject: [PATCH] Port http_server to both SSL/non-SSL --- examples/http_server.c | 28 ++++++++++++++++++++++++++++ src/ssl.c | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/examples/http_server.c b/examples/http_server.c index d515196..dbacabd 100644 --- a/examples/http_server.c +++ b/examples/http_server.c @@ -6,6 +6,25 @@ #include #include +#ifndef LIBUS_NO_SSL +#define us_socket us_ssl_socket +#define us_socket_context us_ssl_socket_context +#define us_socket_write us_ssl_socket_write +#define us_socket_close us_ssl_socket_close +#define us_socket_shutdown us_ssl_socket_shutdown +#define us_socket_context_on_end us_ssl_socket_context_on_end +#define us_socket_context_on_open us_ssl_socket_context_on_open +#define us_socket_context_on_close us_ssl_socket_context_on_close +#define us_socket_context_on_writable us_ssl_socket_context_on_writable +#define us_socket_context_on_data us_ssl_socket_context_on_data +#define us_socket_context_on_timeout us_ssl_socket_context_on_timeout +#define us_socket_ext us_ssl_socket_ext +#define us_socket_context_ext us_ssl_socket_context_ext +#define us_socket_get_context us_ssl_socket_get_context +#define us_socket_context_listen us_ssl_socket_context_listen +#define us_socket_timeout us_ssl_socket_timeout +#endif + struct http_socket { /* How far we have streamed our response */ int offset; @@ -83,7 +102,16 @@ int main() { struct us_loop *loop = us_create_loop(1, on_wakeup, on_pre, on_post, 0); /* Create a socket context for HTTP */ +#ifndef LIBUS_NO_SSL + struct us_ssl_socket_context_options ssl_options = {}; + ssl_options.key_file_name = "/home/alexhultman/uWebSockets/misc/ssl/key.pem"; + ssl_options.cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem"; + ssl_options.passphrase = "1234"; + + struct us_ssl_socket_context *http_context = us_create_ssl_socket_context(loop, sizeof(struct http_context), ssl_options); +#else struct us_socket_context *http_context = us_create_socket_context(loop, sizeof(struct http_context)); +#endif /* Generate the shared response */ const char body[] = "

Why hello there!

"; diff --git a/src/ssl.c b/src/ssl.c index a3a215c..5a0b72a 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -215,7 +215,7 @@ void ssl_on_data(struct us_ssl_socket *s, void *data, int length) { if (s->ssl_write_wants_read) { s->ssl_write_wants_read = 0; - context->sc.on_writable(s); // cast here! + context->sc.on_writable(&s->s); // cast here! // if we are closed here, then exit if (us_internal_socket_is_closed(&s->s)) { return;