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