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