Skip to content

Commit 68fcce6

Browse files
committed
Port http_server to both SSL/non-SSL
1 parent 78f227b commit 68fcce6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

examples/http_server.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66
#include <stdlib.h>
77
#include <string.h>
88

9+
#ifndef LIBUS_NO_SSL
10+
#define us_socket us_ssl_socket
11+
#define us_socket_context us_ssl_socket_context
12+
#define us_socket_write us_ssl_socket_write
13+
#define us_socket_close us_ssl_socket_close
14+
#define us_socket_shutdown us_ssl_socket_shutdown
15+
#define us_socket_context_on_end us_ssl_socket_context_on_end
16+
#define us_socket_context_on_open us_ssl_socket_context_on_open
17+
#define us_socket_context_on_close us_ssl_socket_context_on_close
18+
#define us_socket_context_on_writable us_ssl_socket_context_on_writable
19+
#define us_socket_context_on_data us_ssl_socket_context_on_data
20+
#define us_socket_context_on_timeout us_ssl_socket_context_on_timeout
21+
#define us_socket_ext us_ssl_socket_ext
22+
#define us_socket_context_ext us_ssl_socket_context_ext
23+
#define us_socket_get_context us_ssl_socket_get_context
24+
#define us_socket_context_listen us_ssl_socket_context_listen
25+
#define us_socket_timeout us_ssl_socket_timeout
26+
#endif
27+
928
struct http_socket {
1029
/* How far we have streamed our response */
1130
int offset;
@@ -83,7 +102,16 @@ int main() {
83102
struct us_loop *loop = us_create_loop(1, on_wakeup, on_pre, on_post, 0);
84103

85104
/* Create a socket context for HTTP */
105+
#ifndef LIBUS_NO_SSL
106+
struct us_ssl_socket_context_options ssl_options = {};
107+
ssl_options.key_file_name = "/home/alexhultman/uWebSockets/misc/ssl/key.pem";
108+
ssl_options.cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem";
109+
ssl_options.passphrase = "1234";
110+
111+
struct us_ssl_socket_context *http_context = us_create_ssl_socket_context(loop, sizeof(struct http_context), ssl_options);
112+
#else
86113
struct us_socket_context *http_context = us_create_socket_context(loop, sizeof(struct http_context));
114+
#endif
87115

88116
/* Generate the shared response */
89117
const char body[] = "<html><body><h1>Why hello there!</h1></body></html>";

src/ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void ssl_on_data(struct us_ssl_socket *s, void *data, int length) {
215215
if (s->ssl_write_wants_read) {
216216
s->ssl_write_wants_read = 0;
217217

218-
context->sc.on_writable(s); // cast here!
218+
context->sc.on_writable(&s->s); // cast here!
219219
// if we are closed here, then exit
220220
if (us_internal_socket_is_closed(&s->s)) {
221221
return;

0 commit comments

Comments
 (0)