2 * lws-minimal-http-server-custom-headers
4 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * This demonstrates a minimal http server that can produce dynamic http
10 * content as well as static content.
12 * To keep it simple, it serves the static stuff from the subdirectory
13 * "./mount-origin" of the directory it was started in.
15 * You can change that by changing mount.origin below.
18 #include <libwebsockets.h>
23 static int interrupted;
26 char result[128 + LWS_PRE];
31 * This just lets us override LWS_CALLBACK_HTTP handling before passing it
32 * and everything else to the default handler.
36 callback_http(struct lws *wsi, enum lws_callback_reasons reason,
37 void *user, void *in, size_t len)
39 uint8_t buf[LWS_PRE + 2048], *start = &buf[LWS_PRE], *p = start,
40 *end = &buf[sizeof(buf) - LWS_PRE - 1];
41 struct pss *pss = (struct pss *)user;
42 char value[32], *pr = &pss->result[LWS_PRE];
43 size_t e = sizeof(pss->result) - LWS_PRE;
47 case LWS_CALLBACK_HTTP:
49 * LWS doesn't have the "DNT" header built-in. But we can
50 * query it using the "custom" versions of the header apis.
52 * You can set your modern browser to issue DNT, look in the
53 * privacy settings of your browser.
57 n = lws_hdr_custom_length(wsi, "dnt:", 4);
59 pss->len = lws_snprintf(pr, e,
60 "%s: DNT header not found\n", __func__);
63 pss->len = lws_snprintf(pr, e,
64 "%s: DNT length %d<br>", __func__, n);
65 n = lws_hdr_custom_copy(wsi, value, sizeof(value), "dnt:", 4);
67 pss->len += lws_snprintf(pr + pss->len, e - (unsigned int)pss->len,
68 "%s: unable to get DNT value\n", __func__);
71 pss->len += lws_snprintf(pr + pss->len , e - (unsigned int)pss->len,
72 "%s: DNT value '%s'\n", __func__, value);
75 lwsl_user("%s\n", pr);
77 if (lws_add_http_common_headers(wsi, HTTP_STATUS_OK,
78 "text/html", (lws_filepos_t)pss->len, &p, end))
81 if (lws_finalize_write_http_header(wsi, start, &p, end))
85 /* write the body separately */
86 lws_callback_on_writable(wsi);
89 case LWS_CALLBACK_HTTP_WRITEABLE:
91 strcpy((char *)start, "hello");
93 if (lws_write(wsi, (uint8_t *)pr, (unsigned int)pss->len, LWS_WRITE_HTTP_FINAL) != pss->len)
96 if (lws_http_transaction_completed(wsi))
104 return lws_callback_http_dummy(wsi, reason, user, in, len);
107 static struct lws_protocols protocols[] = {
108 { "http", callback_http, sizeof(struct pss), 0, 0, NULL, 0 },
109 LWS_PROTOCOL_LIST_TERM
112 static const struct lws_http_mount mount_dyn = {
113 /* .mount_next */ NULL, /* linked-list "next" */
114 /* .mountpoint */ "/dyn", /* mountpoint URL */
115 /* .origin */ NULL, /* protocol */
117 /* .protocol */ "http",
119 /* .extra_mimetypes */ NULL,
120 /* .interpret */ NULL,
121 /* .cgi_timeout */ 0,
122 /* .cache_max_age */ 0,
124 /* .cache_reusable */ 0,
125 /* .cache_revalidate */ 0,
126 /* .cache_intermediaries */ 0,
127 /* .origin_protocol */ LWSMPRO_CALLBACK, /* dynamic */
128 /* .mountpoint_len */ 4, /* char count */
129 /* .basic_auth_login_file */ NULL,
132 /* default mount serves the URL space from ./mount-origin */
134 static const struct lws_http_mount mount = {
135 /* .mount_next */ &mount_dyn, /* linked-list "next" */
136 /* .mountpoint */ "/", /* mountpoint URL */
137 /* .origin */ "./mount-origin", /* serve from dir */
138 /* .def */ "index.html", /* default filename */
139 /* .protocol */ NULL,
141 /* .extra_mimetypes */ NULL,
142 /* .interpret */ NULL,
143 /* .cgi_timeout */ 0,
144 /* .cache_max_age */ 0,
146 /* .cache_reusable */ 0,
147 /* .cache_revalidate */ 0,
148 /* .cache_intermediaries */ 0,
149 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
150 /* .mountpoint_len */ 1, /* char count */
151 /* .basic_auth_login_file */ NULL,
154 void sigint_handler(int sig)
159 int main(int argc, const char **argv)
161 struct lws_context_creation_info info;
162 struct lws_context *context;
164 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
166 signal(SIGINT, sigint_handler);
168 if ((p = lws_cmdline_option(argc, argv, "-d")))
171 lws_set_log_level(logs, NULL);
172 lwsl_user("LWS minimal http server custom headers | visit http://localhost:7681\n");
174 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
175 info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
176 LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
177 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
179 /* for testing ah queue, not useful in real world */
180 if (lws_cmdline_option(argc, argv, "--ah1"))
181 info.max_http_header_pool = 1;
183 context = lws_create_context(&info);
185 lwsl_err("lws init failed\n");
192 info.protocols = protocols;
193 info.mounts = &mount;
194 info.vhost_name = "http";
196 if (!lws_create_vhost(context, &info)) {
197 lwsl_err("Failed to create tls vhost\n");
204 info.error_document_404 = "/404.html";
205 #if defined(LWS_WITH_TLS)
206 info.ssl_cert_filepath = "localhost-100y.cert";
207 info.ssl_private_key_filepath = "localhost-100y.key";
209 info.vhost_name = "https";
211 if (!lws_create_vhost(context, &info)) {
212 lwsl_err("Failed to create tls vhost\n");
216 while (n >= 0 && !interrupted)
217 n = lws_service(context, 0);
220 lws_context_destroy(context);