1 /*
2 * ws protocol handler plugin for "POST demo"
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 * The person who associated a work with this deed has dedicated
10 * the work to the public domain by waiving all of his or her rights
11 * to the work worldwide under copyright law, including all related
12 * and neighboring rights, to the extent allowed by law. You can copy,
13 * modify, distribute and perform the work, even for commercial purposes,
14 * all without asking permission.
15 *
16 * These test plugins are intended to be adapted for use in your code, which
17 * may be proprietary. So unlike the library itself, they are licensed
18 * Public Domain.
19 */
20
21 #if !defined (LWS_PLUGIN_STATIC)
22 #if !defined(LWS_DLL)
23 #define LWS_DLL
24 #endif
25 #if !defined(LWS_INTERNAL)
26 #define LWS_INTERNAL
27 #endif
28 #include <libwebsockets.h>
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #ifdef WIN32
37 #include <io.h>
38 #endif
39 #include <stdio.h>
40
41 struct per_session_data__post_demo {
42 struct lws_spa *spa;
43 char result[LWS_PRE + LWS_RECOMMENDED_MIN_HEADER_SPACE];
44 char filename[64];
45 long file_length;
46 #if !defined(LWS_WITH_ESP32)
47 lws_filefd_type fd;
48 #endif
49 uint8_t completed:1;
50 uint8_t sent_headers:1;
51 uint8_t sent_body:1;
52 };
53
54 static const char * const param_names[] = {
55 "text",
56 "send",
57 "file",
58 "upload",
59 };
60
61 enum enum_param_names {
62 EPN_TEXT,
63 EPN_SEND,
64 EPN_FILE,
65 EPN_UPLOAD,
66 };
67
68 static int
file_upload_cb(void * data,const char * name,const char * filename,char * buf,int len,enum lws_spa_fileupload_states state)69 file_upload_cb(void *data, const char *name, const char *filename,
70 char *buf, int len, enum lws_spa_fileupload_states state)
71 {
72 struct per_session_data__post_demo *pss =
73 (struct per_session_data__post_demo *)data;
74 #if !defined(LWS_WITH_ESP32)
75 int n;
76
77 (void)n;
78 #endif
79
80 switch (state) {
81 case LWS_UFS_OPEN:
82 lws_strncpy(pss->filename, filename, sizeof(pss->filename));
83 /* we get the original filename in @filename arg, but for
84 * simple demo use a fixed name so we don't have to deal with
85 * attacks */
86 #if !defined(LWS_WITH_ESP32)
87 pss->fd = (lws_filefd_type)(lws_intptr_t)lws_open("/tmp/post-file",
88 O_CREAT | O_TRUNC | O_RDWR, 0600);
89 #endif
90 break;
91 case LWS_UFS_FINAL_CONTENT:
92 case LWS_UFS_CONTENT:
93 if (len) {
94 pss->file_length += len;
95
96 /* if the file length is too big, drop it */
97 if (pss->file_length > 100000)
98 return 1;
99
100 #if !defined(LWS_WITH_ESP32)
101 n = (int)write((int)(lws_intptr_t)pss->fd, buf, (unsigned int)len);
102 lwsl_info("%s: write %d says %d\n", __func__, len, n);
103 #else
104 lwsl_notice("%s: Received chunk size %d\n", __func__, len);
105 #endif
106 }
107 if (state == LWS_UFS_CONTENT)
108 break;
109 #if !defined(LWS_WITH_ESP32)
110 close((int)(lws_intptr_t)pss->fd);
111 pss->fd = LWS_INVALID_FILE;
112 #endif
113 break;
114 case LWS_UFS_CLOSE:
115 break;
116 }
117
118 return 0;
119 }
120
121 /*
122 * returns length in bytes
123 */
124
125 static int
format_result(struct per_session_data__post_demo * pss)126 format_result(struct per_session_data__post_demo *pss)
127 {
128 unsigned char *p, *start, *end;
129 int n;
130
131 p = (unsigned char *)pss->result + LWS_PRE;
132 start = p;
133 end = p + sizeof(pss->result) - LWS_PRE - 1;
134
135 if (!pss->spa) {
136 p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
137 "pss->spa already NULL");
138 goto bail;
139 }
140
141 p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
142 "<!DOCTYPE html><html lang=\"en\"><head>"
143 "<meta charset=utf-8 http-equiv=\"Content-Language\" "
144 "content=\"en\"/>"
145 "<title>LWS Server Status</title>"
146 "</head><body><h1>Form results (after urldecoding)</h1>"
147 "<table><tr><td>Name</td><td>Length</td><td>Value</td></tr>");
148
149 for (n = 0; n < (int)LWS_ARRAY_SIZE(param_names); n++) {
150 if (!lws_spa_get_string(pss->spa, n))
151 p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
152 "<tr><td><b>%s</b></td><td>0"
153 "</td><td>NULL</td></tr>",
154 param_names[n]);
155 else
156 p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
157 "<tr><td><b>%s</b></td><td>%d"
158 "</td><td>%s</td></tr>",
159 param_names[n],
160 lws_spa_get_length(pss->spa, n),
161 lws_spa_get_string(pss->spa, n));
162 }
163
164 p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
165 "</table><br><b>filename:</b> %s, "
166 "<b>length</b> %ld",
167 pss->filename, pss->file_length);
168
169 p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p), "</body></html>");
170
171 bail:
172 return (int)lws_ptr_diff(p, start);
173 }
174
175 static int
callback_post_demo(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)176 callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
177 void *user, void *in, size_t len)
178 {
179 struct per_session_data__post_demo *pss =
180 (struct per_session_data__post_demo *)user;
181 unsigned char *p, *start, *end;
182 int n;
183
184 switch (reason) {
185 case LWS_CALLBACK_HTTP_BODY:
186 /* create the POST argument parser if not already existing */
187 if (!pss->spa) {
188 pss->spa = lws_spa_create(wsi, param_names,
189 LWS_ARRAY_SIZE(param_names), 1024,
190 file_upload_cb, pss);
191 if (!pss->spa)
192 return -1;
193
194 pss->filename[0] = '\0';
195 pss->file_length = 0;
196 }
197
198 /* let it parse the POST data */
199 if (lws_spa_process(pss->spa, in, (int)len))
200 return -1;
201 break;
202
203 case LWS_CALLBACK_HTTP_BODY_COMPLETION:
204 lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION: %s\n", lws_wsi_tag(wsi));
205 /* call to inform no more payload data coming */
206 lws_spa_finalize(pss->spa);
207
208 pss->completed = 1;
209 lws_callback_on_writable(wsi);
210 break;
211
212 case LWS_CALLBACK_HTTP_WRITEABLE:
213 if (!pss->completed)
214 break;
215
216 p = (unsigned char *)pss->result + LWS_PRE;
217 start = p;
218 end = p + sizeof(pss->result) - LWS_PRE - 1;
219
220 if (!pss->sent_headers) {
221 n = format_result(pss);
222
223 if (lws_add_http_header_status(wsi, HTTP_STATUS_OK,
224 &p, end))
225 goto bail;
226
227 if (lws_add_http_header_by_token(wsi,
228 WSI_TOKEN_HTTP_CONTENT_TYPE,
229 (unsigned char *)"text/html", 9,
230 &p, end))
231 goto bail;
232 if (lws_add_http_header_content_length(wsi, (unsigned int)n, &p, end))
233 goto bail;
234 if (lws_finalize_http_header(wsi, &p, end))
235 goto bail;
236
237 /* first send the headers ... */
238 n = lws_write(wsi, start, lws_ptr_diff_size_t(p, start),
239 LWS_WRITE_HTTP_HEADERS);
240 if (n < 0)
241 goto bail;
242
243 pss->sent_headers = 1;
244 lws_callback_on_writable(wsi);
245 break;
246 }
247
248 if (!pss->sent_body) {
249 n = format_result(pss);
250
251 n = lws_write(wsi, (unsigned char *)start, (unsigned int)n,
252 LWS_WRITE_HTTP_FINAL);
253
254 pss->sent_body = 1;
255 if (n < 0)
256 return 1;
257 goto try_to_reuse;
258 }
259 break;
260
261 case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
262 /* called when our wsi user_space is going to be destroyed */
263 if (pss->spa) {
264 lws_spa_destroy(pss->spa);
265 pss->spa = NULL;
266 }
267 break;
268
269 default:
270 break;
271 }
272
273 return 0;
274
275 bail:
276
277 return 1;
278
279 try_to_reuse:
280 if (lws_http_transaction_completed(wsi))
281 return -1;
282
283 return 0;
284 }
285
286 #define LWS_PLUGIN_PROTOCOL_POST_DEMO \
287 { \
288 "protocol-post-demo", \
289 callback_post_demo, \
290 sizeof(struct per_session_data__post_demo), \
291 1024, \
292 0, NULL, 0 \
293 }
294
295 #if !defined (LWS_PLUGIN_STATIC)
296
297 LWS_VISIBLE const struct lws_protocols post_demo_protocols[] = {
298 LWS_PLUGIN_PROTOCOL_POST_DEMO
299 };
300
301 LWS_VISIBLE const lws_plugin_protocol_t post_demo = {
302 .hdr = {
303 "post demo",
304 "lws_protocol_plugin",
305 LWS_BUILD_HASH,
306 LWS_PLUGIN_API_MAGIC
307 },
308
309 .protocols = post_demo_protocols,
310 .count_protocols = LWS_ARRAY_SIZE(post_demo_protocols),
311 .extensions = NULL,
312 .count_extensions = 0,
313 };
314
315 #endif
316