1 /*
2  * lws-minimal-http-server-custom-headers
3  *
4  * Written in 2010-2019 by Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * This demonstrates a minimal http server that can produce dynamic http
10  * content as well as static content.
11  *
12  * To keep it simple, it serves the static stuff from the subdirectory
13  * "./mount-origin" of the directory it was started in.
14  *
15  * You can change that by changing mount.origin below.
16  */
17 
18 #include <libwebsockets.h>
19 #include <string.h>
20 #include <signal.h>
21 #include <time.h>
22 
23 static int interrupted;
24 
25 struct pss {
26 	char result[128 + LWS_PRE];
27 	int len;
28 };
29 
30 /*
31  * This just lets us override LWS_CALLBACK_HTTP handling before passing it
32  * and everything else to the default handler.
33  */
34 
35 static int
callback_http(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)36 callback_http(struct lws *wsi, enum lws_callback_reasons reason,
37 	      void *user, void *in, size_t len)
38 {
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 	int n, e = sizeof(pss->result) - LWS_PRE;
44 
45 	switch (reason) {
46 	case LWS_CALLBACK_HTTP:
47 		/*
48 		 * LWS doesn't have the "DNT" header built-in.  But we can
49 		 * query it using the "custom" versions of the header apis.
50 		 *
51 		 * You can set your modern browser to issue DNT, look in the
52 		 * privacy settings of your browser.
53 		 */
54 
55 		pss->len = 0;
56 		n = lws_hdr_custom_length(wsi, "dnt:", 4);
57 		if (n < 0)
58 			pss->len = lws_snprintf(pr, e,
59 					"%s: DNT header not found\n", __func__);
60 		else {
61 
62 			pss->len = lws_snprintf(pr, e,
63 					"%s: DNT length %d<br>", __func__, n);
64 			n = lws_hdr_custom_copy(wsi, value, sizeof(value), "dnt:", 4);
65 			if (n < 0)
66 				pss->len += lws_snprintf(pr + pss->len, e - pss->len,
67 					"%s: unable to get DNT value\n", __func__);
68 			else
69 
70 				pss->len += lws_snprintf(pr + pss->len , e - pss->len,
71 					"%s: DNT value '%s'\n", __func__, value);
72 		}
73 
74 		lwsl_user("%s\n", pr);
75 
76 		if (lws_add_http_common_headers(wsi, HTTP_STATUS_OK,
77 				"text/html", pss->len, &p, end))
78 			return 1;
79 
80 		if (lws_finalize_write_http_header(wsi, start, &p, end))
81 			return 1;
82 
83 
84 		/* write the body separately */
85 		lws_callback_on_writable(wsi);
86 		return 0;
87 
88 	case LWS_CALLBACK_HTTP_WRITEABLE:
89 
90 		strcpy((char *)start, "hello");
91 
92 		if (lws_write(wsi, (uint8_t *)pr, pss->len, LWS_WRITE_HTTP_FINAL) != pss->len)
93 			return 1;
94 
95 		if (lws_http_transaction_completed(wsi))
96 			return -1;
97 		return 0;
98 
99 	default:
100 		break;
101 	}
102 
103 	return lws_callback_http_dummy(wsi, reason, user, in, len);
104 }
105 
106 static struct lws_protocols protocols[] = {
107 	{ "http", callback_http, sizeof(struct pss), 0 },
108 	{ NULL, NULL, 0, 0 } /* terminator */
109 };
110 
111 static const struct lws_http_mount mount_dyn = {
112 	/* .mount_next */		NULL,		/* linked-list "next" */
113 	/* .mountpoint */		"/dyn",		/* mountpoint URL */
114 	/* .origin */			NULL,	/* protocol */
115 	/* .def */			NULL,
116 	/* .protocol */			"http",
117 	/* .cgienv */			NULL,
118 	/* .extra_mimetypes */		NULL,
119 	/* .interpret */		NULL,
120 	/* .cgi_timeout */		0,
121 	/* .cache_max_age */		0,
122 	/* .auth_mask */		0,
123 	/* .cache_reusable */		0,
124 	/* .cache_revalidate */		0,
125 	/* .cache_intermediaries */	0,
126 	/* .origin_protocol */		LWSMPRO_CALLBACK, /* dynamic */
127 	/* .mountpoint_len */		4,		/* char count */
128 	/* .basic_auth_login_file */	NULL,
129 };
130 
131 /* default mount serves the URL space from ./mount-origin */
132 
133 static const struct lws_http_mount mount = {
134 	/* .mount_next */		&mount_dyn,		/* linked-list "next" */
135 	/* .mountpoint */		"/",		/* mountpoint URL */
136 	/* .origin */		"./mount-origin",	/* serve from dir */
137 	/* .def */			"index.html",	/* default filename */
138 	/* .protocol */			NULL,
139 	/* .cgienv */			NULL,
140 	/* .extra_mimetypes */		NULL,
141 	/* .interpret */		NULL,
142 	/* .cgi_timeout */		0,
143 	/* .cache_max_age */		0,
144 	/* .auth_mask */		0,
145 	/* .cache_reusable */		0,
146 	/* .cache_revalidate */		0,
147 	/* .cache_intermediaries */	0,
148 	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
149 	/* .mountpoint_len */		1,		/* char count */
150 	/* .basic_auth_login_file */	NULL,
151 };
152 
sigint_handler(int sig)153 void sigint_handler(int sig)
154 {
155 	interrupted = 1;
156 }
157 
main(int argc,const char ** argv)158 int main(int argc, const char **argv)
159 {
160 	struct lws_context_creation_info info;
161 	struct lws_context *context;
162 	const char *p;
163 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
164 
165 	signal(SIGINT, sigint_handler);
166 
167 	if ((p = lws_cmdline_option(argc, argv, "-d")))
168 		logs = atoi(p);
169 
170 	lws_set_log_level(logs, NULL);
171 	lwsl_user("LWS minimal http server custom headers | visit http://localhost:7681\n");
172 
173 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
174 	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
175 		       LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
176 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
177 
178 	/* for testing ah queue, not useful in real world */
179 	if (lws_cmdline_option(argc, argv, "--ah1"))
180 		info.max_http_header_pool = 1;
181 
182 	context = lws_create_context(&info);
183 	if (!context) {
184 		lwsl_err("lws init failed\n");
185 		return 1;
186 	}
187 
188 	/* http on 7681 */
189 
190 	info.port = 7681;
191 	info.protocols = protocols;
192 	info.mounts = &mount;
193 	info.vhost_name = "http";
194 
195 	if (!lws_create_vhost(context, &info)) {
196 		lwsl_err("Failed to create tls vhost\n");
197 		goto bail;
198 	}
199 
200 	/* https on 7682 */
201 
202 	info.port = 7682;
203 	info.error_document_404 = "/404.html";
204 	info.ssl_cert_filepath = "localhost-100y.cert";
205 	info.ssl_private_key_filepath = "localhost-100y.key";
206 	info.vhost_name = "https";
207 
208 	if (!lws_create_vhost(context, &info)) {
209 		lwsl_err("Failed to create tls vhost\n");
210 		goto bail;
211 	}
212 
213 	while (n >= 0 && !interrupted)
214 		n = lws_service(context, 0);
215 
216 bail:
217 	lws_context_destroy(context);
218 
219 	return 0;
220 }
221