protocol-lws-messageboard
[platform/upstream/libwebsockets.git] / plugins / protocol_post_demo.c
1 /*
2  * ws protocol handler plugin for "POST demo"
3  *
4  * Copyright (C) 2010-2016 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 #define LWS_DLL
22 #define LWS_INTERNAL
23 #include "../lib/libwebsockets.h"
24
25 #include <string.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #ifdef WIN32
30 #include <io.h>
31 #endif
32 #include <stdio.h>
33
34 struct per_session_data__post_demo {
35         struct lws_spa *spa;
36         char result[LWS_PRE + 500];
37         int result_len;
38
39         char filename[256];
40         long file_length;
41         lws_filefd_type fd;
42 };
43
44 static const char * const param_names[] = {
45         "text",
46         "send",
47         "file",
48         "upload",
49 };
50
51 enum enum_param_names {
52         EPN_TEXT,
53         EPN_SEND,
54         EPN_FILE,
55         EPN_UPLOAD,
56 };
57
58 static int
59 file_upload_cb(void *data, const char *name, const char *filename,
60                char *buf, int len, enum lws_spa_fileupload_states state)
61 {
62         struct per_session_data__post_demo *pss =
63                         (struct per_session_data__post_demo *)data;
64         int n;
65
66         switch (state) {
67         case LWS_UFS_OPEN:
68                 strncpy(pss->filename, filename, sizeof(pss->filename) - 1);
69                 /* we get the original filename in @filename arg, but for
70                  * simple demo use a fixed name so we don't have to deal with
71                  * attacks  */
72                 pss->fd = open("/tmp/post-file",
73                                O_CREAT | O_TRUNC | O_RDWR, 0600);
74                 break;
75         case LWS_UFS_FINAL_CONTENT:
76         case LWS_UFS_CONTENT:
77                 if (len) {
78                         pss->file_length += len;
79
80                         /* if the file length is too big, drop it */
81                         if (pss->file_length > 100000)
82                                 return 1;
83
84                         n = write(pss->fd, buf, len);
85                         lwsl_notice("%s: write %d says %d\n", __func__, len, n);
86                 }
87                 if (state == LWS_UFS_CONTENT)
88                         break;
89                 close(pss->fd);
90                 pss->fd = LWS_INVALID_FILE;
91                 break;
92         }
93
94         return 0;
95 }
96
97 static int
98 callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
99                    void *user, void *in, size_t len)
100 {
101         struct per_session_data__post_demo *pss =
102                         (struct per_session_data__post_demo *)user;
103         unsigned char buffer[LWS_PRE + 512];
104         unsigned char *p, *start, *end;
105         int n;
106
107         switch (reason) {
108         case LWS_CALLBACK_HTTP_BODY:
109                 /* create the POST argument parser if not already existing */
110                 if (!pss->spa) {
111                         pss->spa = lws_spa_create(wsi, param_names,
112                                         ARRAY_SIZE(param_names), 1024,
113                                         file_upload_cb, pss);
114                         if (!pss->spa)
115                                 return -1;
116
117                         pss->filename[0] = '\0';
118                         pss->file_length = 0;
119                 }
120
121                 /* let it parse the POST data */
122                 if (lws_spa_process(pss->spa, in, len))
123                         return -1;
124                 break;
125
126         case LWS_CALLBACK_HTTP_BODY_COMPLETION:
127                 lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
128                 /* call to inform no more payload data coming */
129                 lws_spa_finalize(pss->spa);
130
131                 p = (unsigned char *)pss->result + LWS_PRE;
132                 end = p + sizeof(pss->result) - LWS_PRE - 1;
133                 p += sprintf((char *)p,
134                         "<html><body><h1>Form results (after urldecoding)</h1>"
135                         "<table><tr><td>Name</td><td>Length</td><td>Value</td></tr>");
136
137                 for (n = 0; n < ARRAY_SIZE(param_names); n++)
138                         p += snprintf((char *)p, end - p,
139                                     "<tr><td><b>%s</b></td><td>%d</td><td>%s</td></tr>",
140                                     param_names[n],
141                                     lws_spa_get_length(pss->spa, n),
142                                     lws_spa_get_string(pss->spa, n));
143
144                 p += snprintf((char *)p, end - p, "</table><br><b>filename:</b> %s, <b>length</b> %ld",
145                                 pss->filename, pss->file_length);
146
147                 p += snprintf((char *)p, end - p, "</body></html>");
148                 pss->result_len = p - (unsigned char *)(pss->result + LWS_PRE);
149
150                 p = buffer + LWS_PRE;
151                 start = p;
152                 end = p + sizeof(buffer) - LWS_PRE;
153
154                 if (lws_add_http_header_status(wsi, 200, &p, end))
155                         return 1;
156
157                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
158                                 (unsigned char *)"text/html", 9, &p, end))
159                         return 1;
160                 if (lws_add_http_header_content_length(wsi, pss->result_len, &p, end))
161                         return 1;
162                 if (lws_finalize_http_header(wsi, &p, end))
163                         return 1;
164
165                 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
166                 if (n < 0)
167                         return 1;
168
169                 lws_callback_on_writable(wsi);
170                 break;
171
172         case LWS_CALLBACK_HTTP_WRITEABLE:
173                 lwsl_debug("LWS_CALLBACK_HTTP_WRITEABLE: sending %d\n",
174                            pss->result_len);
175                 n = lws_write(wsi, (unsigned char *)pss->result + LWS_PRE,
176                               pss->result_len, LWS_WRITE_HTTP);
177                 if (n < 0)
178                         return 1;
179                 goto try_to_reuse;
180
181         case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
182                 /* called when our wsi user_space is going to be destroyed */
183                 if (pss->spa) {
184                         lws_spa_destroy(pss->spa);
185                         pss->spa = NULL;
186                 }
187                 break;
188
189         default:
190                 break;
191         }
192
193         return 0;
194
195 try_to_reuse:
196         if (lws_http_transaction_completed(wsi))
197                 return -1;
198
199         return 0;
200 }
201
202 static const struct lws_protocols protocols[] = {
203         {
204                 "protocol-post-demo",
205                 callback_post_demo,
206                 sizeof(struct per_session_data__post_demo),
207                 1024,
208         },
209 };
210
211 LWS_EXTERN LWS_VISIBLE int
212 init_protocol_post_demo(struct lws_context *context,
213                         struct lws_plugin_capability *c)
214 {
215         if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
216                 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
217                          c->api_magic);
218                 return 1;
219         }
220
221         c->protocols = protocols;
222         c->count_protocols = ARRAY_SIZE(protocols);
223         c->extensions = NULL;
224         c->count_extensions = 0;
225
226         return 0;
227 }
228
229 LWS_EXTERN LWS_VISIBLE int
230 destroy_protocol_post_demo(struct lws_context *context)
231 {
232         return 0;
233 }