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