1/*
2 * lws-minimal-http-server-form-post
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 performs POST with a couple
10 * of parameters.  It dumps the parameters to the console log and redirects
11 * to another page.
12 */
13
14#include <libwebsockets.h>
15#include <string.h>
16#include <signal.h>
17
18/*
19 * Unlike ws, http is a stateless protocol.  This pss only exists for the
20 * duration of a single http transaction.  With http/1.1 keep-alive and http/2,
21 * that is unrelated to (shorter than) the lifetime of the network connection.
22 */
23struct pss {
24	struct lws_spa *spa;
25};
26
27static int interrupted, use303;
28
29static const char * const param_names[] = {
30	"text1",
31	"send",
32};
33
34enum enum_param_names {
35	EPN_TEXT1,
36	EPN_SEND,
37};
38
39static int
40callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
41	      void *in, size_t len)
42{
43	struct pss *pss = (struct pss *)user;
44	uint8_t buf[LWS_PRE + LWS_RECOMMENDED_MIN_HEADER_SPACE], *start = &buf[LWS_PRE],
45		*p = start, *end = &buf[sizeof(buf) - 1];
46	int n;
47
48	switch (reason) {
49	case LWS_CALLBACK_HTTP:
50
51		/*
52		 * Manually report that our form target URL exists
53		 *
54		 * you can also do this by adding a mount for the form URL
55		 * to the protocol with type LWSMPRO_CALLBACK, then no need
56		 * to trap LWS_CALLBACK_HTTP.
57		 */
58
59		if (!strcmp((const char *)in, "/form1"))
60			/* assertively allow it to exist in the URL space */
61			return 0;
62
63		/* default to 404-ing the URL if not mounted */
64		break;
65
66	case LWS_CALLBACK_HTTP_BODY:
67
68		/* create the POST argument parser if not already existing */
69
70		if (!pss->spa) {
71			pss->spa = lws_spa_create(wsi, param_names,
72					LWS_ARRAY_SIZE(param_names), 1024,
73					NULL, NULL); /* no file upload */
74			if (!pss->spa)
75				return -1;
76		}
77
78		/* let it parse the POST data */
79
80		if (lws_spa_process(pss->spa, in, (int)len))
81			return -1;
82		break;
83
84	case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
85		if (pss->spa && lws_spa_destroy(pss->spa))
86			return -1;
87		break;
88
89	case LWS_CALLBACK_HTTP_BODY_COMPLETION:
90
91		/* inform the spa no more payload data coming */
92
93		lwsl_user("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
94		lws_spa_finalize(pss->spa);
95
96		/* we just dump the decoded things to the log */
97
98		if (pss->spa)
99			for (n = 0; n < (int)LWS_ARRAY_SIZE(param_names); n++) {
100				if (!lws_spa_get_string(pss->spa, n))
101					lwsl_user("%s: undefined\n", param_names[n]);
102				else
103					lwsl_user("%s: (len %d) '%s'\n",
104					    param_names[n],
105					    lws_spa_get_length(pss->spa, n),
106					    lws_spa_get_string(pss->spa, n));
107			}
108
109		/*
110		 * Our response is to redirect to a static page.  We could
111		 * have generated a dynamic html page here instead.
112		 */
113
114		if (lws_http_redirect(wsi, use303 ? HTTP_STATUS_SEE_OTHER :
115					   HTTP_STATUS_MOVED_PERMANENTLY,
116				      (unsigned char *)"after-form1.html",
117				      16, &p, end) < 0)
118			return -1;
119		break;
120
121	case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
122		/* called when our wsi user_space is going to be destroyed */
123		if (pss->spa) {
124			lws_spa_destroy(pss->spa);
125			pss->spa = NULL;
126		}
127		break;
128
129	default:
130		break;
131	}
132
133	return lws_callback_http_dummy(wsi, reason, user, in, len);
134}
135
136static struct lws_protocols protocols[] = {
137	{ "http", callback_http, sizeof(struct pss), 0, 0, NULL, 0 },
138	LWS_PROTOCOL_LIST_TERM
139};
140
141/* default mount serves the URL space from ./mount-origin */
142
143static const struct lws_http_mount mount = {
144	/* .mount_next */	       NULL,		/* linked-list "next" */
145	/* .mountpoint */		"/",		/* mountpoint URL */
146	/* .origin */		"./mount-origin",	/* serve from dir */
147	/* .def */			"index.html",	/* default filename */
148	/* .protocol */			NULL,
149	/* .cgienv */			NULL,
150	/* .extra_mimetypes */		NULL,
151	/* .interpret */		NULL,
152	/* .cgi_timeout */		0,
153	/* .cache_max_age */		0,
154	/* .auth_mask */		0,
155	/* .cache_reusable */		0,
156	/* .cache_revalidate */		0,
157	/* .cache_intermediaries */	0,
158	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
159	/* .mountpoint_len */		1,		/* char count */
160	/* .basic_auth_login_file */	NULL,
161};
162
163void sigint_handler(int sig)
164{
165	interrupted = 1;
166}
167
168int main(int argc, const char **argv)
169{
170	struct lws_context_creation_info info;
171	struct lws_context *context;
172	const char *p;
173	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
174			/* for LLL_ verbosity above NOTICE to be built into lws,
175			 * lws must have been configured and built with
176			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
177			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
178			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
179			/* | LLL_DEBUG */;
180
181	signal(SIGINT, sigint_handler);
182
183	if ((p = lws_cmdline_option(argc, argv, "-d")))
184		logs = atoi(p);
185
186	lws_set_log_level(logs, NULL);
187	lwsl_user("LWS minimal http server POST | visit http://localhost:7681\n");
188
189	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
190	info.port = 7681;
191	info.protocols = protocols;
192	info.mounts = &mount;
193	info.options =
194		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
195#if defined(LWS_WITH_TLS)
196	if (lws_cmdline_option(argc, argv, "-s")) {
197		info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
198		info.ssl_cert_filepath = "localhost-100y.cert";
199		info.ssl_private_key_filepath = "localhost-100y.key";
200	}
201#endif
202
203	if ((p = lws_cmdline_option(argc, argv, "--port")))
204		info.port = atoi(p);
205
206	if (lws_cmdline_option(argc, argv, "--303")) {
207		lwsl_user("%s: using 303 redirect\n", __func__);
208		use303 = 1;
209	}
210
211	context = lws_create_context(&info);
212	if (!context) {
213		lwsl_err("lws init failed\n");
214		return 1;
215	}
216
217	while (n >= 0 && !interrupted)
218		n = lws_service(context, 0);
219
220	lws_context_destroy(context);
221
222	return 0;
223}
224