Skip to content

Commit

Permalink
Port http_server to both SSL/non-SSL
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Aug 21, 2018
1 parent 78f227b commit 68fcce6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions examples/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@
#include <stdlib.h>
#include <string.h>

#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;
Expand Down Expand Up @@ -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[] = "<html><body><h1>Why hello there!</h1></body></html>";
Expand Down
2 changes: 1 addition & 1 deletion src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 68fcce6

Please sign in to comment.