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 */
23 struct pss {
24 struct lws_spa *spa;
25 };
26
27 static int interrupted, use303;
28
29 static const char * const param_names[] = {
30 "text1",
31 "send",
32 };
33
34 enum enum_param_names {
35 EPN_TEXT1,
36 EPN_SEND,
37 };
38
39 static int
callback_http(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)40 callback_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 if (pss->spa && lws_spa_destroy(pss->spa))
110 return -1;
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, use303 ? HTTP_STATUS_SEE_OTHER :
118 HTTP_STATUS_MOVED_PERMANENTLY,
119 (unsigned char *)"after-form1.html",
120 16, &p, end) < 0)
121 return -1;
122 break;
123
124 case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
125 /* called when our wsi user_space is going to be destroyed */
126 if (pss->spa) {
127 lws_spa_destroy(pss->spa);
128 pss->spa = NULL;
129 }
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 },
141 { NULL, NULL, 0, 0 } /* terminator */
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 info.ssl_cert_filepath = "localhost-100y.cert";
202 info.ssl_private_key_filepath = "localhost-100y.key";
203 }
204
205 if (lws_cmdline_option(argc, argv, "--303")) {
206 lwsl_user("%s: using 303 redirect\n", __func__);
207 use303 = 1;
208 }
209
210 context = lws_create_context(&info);
211 if (!context) {
212 lwsl_err("lws init failed\n");
213 return 1;
214 }
215
216 while (n >= 0 && !interrupted)
217 n = lws_service(context, 0);
218
219 lws_context_destroy(context);
220
221 return 0;
222 }
223