maph 1.0.0 High-Performance Memory-Mapped Key-Value Database with O(1) Lookups

Loading...
Searching...
No Matches
http_server.c
Go to the documentation of this file.
1/* This is a barebone keep-alive HTTP server */
2#include <libusockets.h>
3/* Compiling with LIBUS_NO_SSL will ignore SSL */
4 const int SSL = 1;
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10 struct http_socket {
11 /* How far we have streamed our response */
12 int offset;
13};
14
15 struct http_context {
16 /* The shared response */
17 char *response;
18 int length;
19};
20
21/* We don't need any of these */
22 void on_wakeup(struct us_loop_t *loop) {
23
24}
25
26 void on_pre(struct us_loop_t *loop) {
27
28}
29
30/* This is not HTTP POST, it is merely an event emitted post loop iteration */
31 void on_post(struct us_loop_t *loop) {
32
33}
34
35 struct us_socket_t *on_http_socket_writable(struct us_socket_t *s) {
36 struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
37 struct http_context *http_context = (struct http_context *) us_socket_context_ext(SSL, us_socket_context(SSL, s));
38
39 /* Stream whatever is remaining of the response */
40 http_socket->offset += us_socket_write(SSL, s, http_context->response + http_socket->offset, http_context->length - http_socket->offset, 0);
41
42 return s;
43}
44
45 struct us_socket_t *on_http_socket_close(struct us_socket_t *s, int code, void *reason) {
46 printf("Client disconnected\n");
47
48 return s;
49}
50
51 struct us_socket_t *on_http_socket_end(struct us_socket_t *s) {
52 /* HTTP does not support half-closed sockets */
53 us_socket_shutdown(SSL, s);
54 return us_socket_close(SSL, s, 0, NULL);
55}
56
57 struct us_socket_t *on_http_socket_data(struct us_socket_t *s, char *data, int length) {
58 /* Get socket extension and the socket's context's extension */
59 struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
60 struct http_context *http_context = (struct http_context *) us_socket_context_ext(SSL, us_socket_context(SSL, s));
61
62 /* We treat all data events as a request */
63 http_socket->offset = us_socket_write(SSL, s, http_context->response, http_context->length, 0);
64
65 /* Reset idle timer */
66 us_socket_timeout(SSL, s, 30);
67
68 return s;
69}
70
71 struct us_socket_t *on_http_socket_open(struct us_socket_t *s, int is_client, char *ip, int ip_length) {
72 struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
73
74 /* Reset offset */
75 http_socket->offset = 0;
76
77 /* Timeout idle HTTP connections */
78 us_socket_timeout(SSL, s, 30);
79
80 printf("Client connected\n");
81
82 return s;
83}
84
85 struct us_socket_t *on_http_socket_timeout(struct us_socket_t *s) {
86 /* Close idle HTTP sockets */
87 return us_socket_close(SSL, s, 0, NULL);
88}
89
90 int main() {
91 /* Create the event loop */
92 struct us_loop_t *loop = us_create_loop(0, on_wakeup, on_pre, on_post, 0);
93
94 /* Create a socket context for HTTP */
95 struct us_socket_context_options_t options = {};
96 options.key_file_name = "../../misc/key.pem";
97 options.cert_file_name = "../../misc/cert.pem";
98 options.passphrase = "1234";
99
100 struct us_socket_context_t *http_context = us_create_socket_context(SSL, loop, sizeof(struct http_context), options);
101
102 if (!http_context) {
103 printf("Could not load SSL cert/key\n");
104 exit(0);
105 }
106
107 /* Generate the shared response */
108 const char body[] = "<html><body><h1>Why hello there!</h1></body></html>";
109
110 struct http_context *http_context_ext = (struct http_context *) us_socket_context_ext(SSL, http_context);
111 http_context_ext->response = (char *) malloc(128 + sizeof(body) - 1);
112 http_context_ext->length = snprintf(http_context_ext->response, 128 + sizeof(body) - 1, "HTTP/1.1 200 OK\r\nContent-Length: %ld\r\n\r\n%s", sizeof(body) - 1, body);
113
114 /* Set up event handlers */
115 us_socket_context_on_open(SSL, http_context, on_http_socket_open);
116 us_socket_context_on_data(SSL, http_context, on_http_socket_data);
117 us_socket_context_on_writable(SSL, http_context, on_http_socket_writable);
118 us_socket_context_on_close(SSL, http_context, on_http_socket_close);
119 us_socket_context_on_timeout(SSL, http_context, on_http_socket_timeout);
120 us_socket_context_on_end(SSL, http_context, on_http_socket_end);
121
122 /* Start serving HTTP connections */
123 struct us_listen_socket_t *listen_socket = us_socket_context_listen(SSL, http_context, 0, 3000, 0, sizeof(struct http_socket));
124
125 if (listen_socket) {
126 printf("Listening on port 3000...\n");
127 us_loop_run(loop);
128 } else {
129 printf("Failed to listen!\n");
130 }
131}
us_socket_context_listen
struct us_listen_socket_t * us_socket_context_listen(int ssl, struct us_socket_context_t *context, const char *host, int port, int options, int socket_ext_size)
Definition context.c:248
us_socket_context_on_data
void us_socket_context_on_data(int ssl, struct us_socket_context_t *context, struct us_socket_t *(*on_data)(struct us_socket_t *s, char *data, int length))
Definition context.c:437
us_socket_context_on_writable
void us_socket_context_on_writable(int ssl, struct us_socket_context_t *context, struct us_socket_t *(*on_writable)(struct us_socket_t *s))
Definition context.c:448
us_socket_context_on_timeout
void us_socket_context_on_timeout(int ssl, struct us_socket_context_t *context, struct us_socket_t *(*on_timeout)(struct us_socket_t *))
Definition context.c:470
us_socket_context_on_close
void us_socket_context_on_close(int ssl, struct us_socket_context_t *context, struct us_socket_t *(*on_close)(struct us_socket_t *s, int code, void *reason))
Definition context.c:426
us_socket_context_ext
void * us_socket_context_ext(int ssl, struct us_socket_context_t *context)
Definition context.c:503
us_create_socket_context
struct us_socket_context_t * us_create_socket_context(int ssl, struct us_loop_t *loop, int context_ext_size, struct us_socket_context_options_t options)
Definition context.c:202
us_socket_context_on_open
void us_socket_context_on_open(int ssl, struct us_socket_context_t *context, struct us_socket_t *(*on_open)(struct us_socket_t *s, int is_client, char *ip, int ip_length))
Definition context.c:415
us_socket_context_on_end
void us_socket_context_on_end(int ssl, struct us_socket_context_t *context, struct us_socket_t *(*on_end)(struct us_socket_t *))
Definition context.c:481
on_http_socket_data
struct us_socket_t * on_http_socket_data(struct us_socket_t *s, char *data, int length)
Definition http_server.c:57
on_pre
void on_pre(struct us_loop_t *loop)
Definition http_server.c:26
on_http_socket_timeout
struct us_socket_t * on_http_socket_timeout(struct us_socket_t *s)
Definition http_server.c:85
on_wakeup
void on_wakeup(struct us_loop_t *loop)
Definition http_server.c:22
http_context::length
int length
Definition http_server.c:18
on_http_socket_open
struct us_socket_t * on_http_socket_open(struct us_socket_t *s, int is_client, char *ip, int ip_length)
Definition http_server.c:71
on_post
void on_post(struct us_loop_t *loop)
Definition http_server.c:31
SSL
const int SSL
Definition http_server.c:4
http_socket::offset
int offset
Definition http_server.c:12
on_http_socket_end
struct us_socket_t * on_http_socket_end(struct us_socket_t *s)
Definition http_server.c:51
on_http_socket_writable
struct us_socket_t * on_http_socket_writable(struct us_socket_t *s)
Definition http_server.c:35
http_context::response
char * response
Definition http_server.c:17
main
int main()
Definition http_server.c:90
on_http_socket_close
struct us_socket_t * on_http_socket_close(struct us_socket_t *s, int code, void *reason)
Definition http_server.c:45
http_context
Definition http_server.c:15
http_socket
Definition http_server.c:10
us_listen_socket_t
Definition internal.h:108
us_socket_t
Definition internal.h:89
libusockets.h
us_socket_write
int us_socket_write(int ssl, struct us_socket_t *s, const char *data, int length, int msg_more)
Definition socket.c:147
us_loop_run
void us_loop_run(struct us_loop_t *loop)
us_socket_context_options_t::cert_file_name
const char * cert_file_name
Definition libusockets.h:126
us_socket_shutdown
void us_socket_shutdown(int ssl, struct us_socket_t *s)
Definition socket.c:187
us_socket_context_options_t::passphrase
const char * passphrase
Definition libusockets.h:127
us_socket_context
struct us_socket_context_t * us_socket_context(int ssl, struct us_socket_t *s)
Definition socket.c:50
us_socket_close
struct us_socket_t * us_socket_close(int ssl, struct us_socket_t *s, int code, void *reason)
Definition socket.c:105
us_create_loop
struct us_loop_t * us_create_loop(void *hint, void(*wakeup_cb)(struct us_loop_t *loop), void(*pre_cb)(struct us_loop_t *loop), void(*post_cb)(struct us_loop_t *loop), unsigned int ext_size)
us_socket_context_options_t::key_file_name
const char * key_file_name
Definition libusockets.h:125
us_socket_timeout
void us_socket_timeout(int ssl, struct us_socket_t *s, unsigned int seconds)
Definition socket.c:54
us_socket_ext
void * us_socket_ext(int ssl, struct us_socket_t *s)
Definition socket.c:167
us_socket_context_options_t
Definition libusockets.h:124
us_loop_t
Definition asio.h:26
us_socket_context_t
Definition internal.h:117
us_socket_context_t::loop
struct us_loop_t * loop
Definition internal.h:118