-
Notifications
You must be signed in to change notification settings - Fork 6
/
http_upload.c
297 lines (238 loc) · 7.89 KB
/
http_upload.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
http_upload.c - An embedded CNC Controller with rs274/ngc (g-code) support
Webserver backend - file upload
Part of grblHAL
Copyright (c) 2019-2022 Terje Io
Some parts of the code is based on test code by francoiscolas
https://github.com/francoiscolas/multipart-parser/blob/master/tests.cpp
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef ARDUINO
#include "../driver.h"
#else
#include "driver.h"
#endif
#if WEBUI_ENABLE && SDCARD_ENABLE
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#ifdef STDIO_FS
#include <sys/unistd.h>
#endif
#include "strutils.h"
#include "http_upload.h"
#include "multipartparser.h"
#include "sdcard/sdcard.h"
static struct multipartparser parser;
static struct multipartparser_callbacks *sd_callbacks = NULL;
static void do_cleanup (file_upload_t *upload)
{
// close and unlink open file
if(upload->file.handle) {
#ifdef GRBL_VFS
vfs_close(upload->file.vfs_handle);
vfs_unlink(upload->filename);
#else
if(upload->to_fatfs) {
f_close(upload->file.fatfs_handle);
f_unlink(upload->filename);
}
#ifdef STDIO_FS
else {
fclose(upload->file.handle);
unlink(upload->filename);
}
#endif
#endif
upload->file.handle = NULL;
}
}
static void cleanup (void *upload)
{
if(upload) {
do_cleanup((file_upload_t *)upload);
free(upload);
}
}
static int on_body_begin (struct multipartparser *parser)
{
return 0;
}
static int on_part_begin (struct multipartparser *parser)
{
return 0;
}
static void on_header_done (struct multipartparser *parser)
{
file_upload_t *upload = (file_upload_t *)parser->data;
if(*upload->header_value) {
if(!lwip_stricmp(upload->header_name, "Content-Disposition")) {
char *name;
if(strstr(upload->header_value, "name=\"path\"")) {
upload->state = Upload_GetPath;
*upload->path = '\0';
} else if((name = strstr(upload->header_value, "filename=\""))) {
strcpy(upload->filename, name + 10);
upload->filename[strlen(upload->filename) - 1] = '\0';
if(*upload->size_str)
upload->size = atoi(upload->size_str);
} else if(strstr(upload->header_value, "name=\"")) {
upload->state = Upload_GetSize;
*upload->size_str = '\0';
}
}
if(*upload->filename && stristr(upload->header_name, "Content-Type")) {
if(upload->on_filename_parsed)
upload->on_filename_parsed(upload->filename, upload->on_filename_parsed_arg);
if(upload->to_fatfs) {
#ifdef GRBL_VFS
if((upload->file.vfs_handle = vfs_open(upload->filename, "w")) != NULL)
upload->state = Upload_Write;
#else
upload->file.fatfs_handle = &upload->fatfs_fd;
if(f_open(upload->file.fatfs_handle, upload->filename, FA_WRITE|FA_CREATE_ALWAYS) == FR_OK)
upload->state = Upload_Write;
else
upload->file.fatfs_handle = NULL;
#endif
}
#ifdef STDIO_FS
else if((upload->file.handle = fopen(upload->filename, "w")))
upload->state = Upload_Write;
#endif
upload->uploaded = 0;
}
}
*upload->header_name = '\0';
*upload->header_value = '\0';
}
static int on_header_field (struct multipartparser *parser, const char* data, size_t size)
{
if (*((file_upload_t *)parser->data)->header_value)
on_header_done(parser);
strncat(((file_upload_t *)parser->data)->header_name, data, size);
return 0;
}
static int on_header_value (struct multipartparser *parser, const char* data, size_t size)
{
strncat(((file_upload_t *)parser->data)->header_value, data, size);
return 0;
}
static int on_headers_complete (struct multipartparser *parser)
{
if (*((file_upload_t *)parser->data)->header_value)
on_header_done(parser);
return 0;
}
static int on_data (struct multipartparser *parser, const char* data, size_t size)
{
file_upload_t *upload = (file_upload_t *)parser->data;
switch(upload->state) {
case Upload_Write:
{
size_t count;
if(upload->to_fatfs) {
#ifdef GRBL_VFS
count = vfs_write(data, 1, size, upload->file.vfs_handle);
#else
f_write(upload->file.fatfs_handle, data, size, &count);
#endif
} else
count = fwrite(data, sizeof(char), size, upload->file.handle);
if(count != size)
upload->state = Upload_Failed;
upload->uploaded += count;
}
break;
case Upload_GetPath:
strncat(upload->path, data, size);
break;
case Upload_GetSize:
strncat(upload->size_str, data, size);
break;
default:
break;
}
return 0;
}
static int on_part_end (struct multipartparser *parser)
{
file_upload_t *upload = (file_upload_t *)parser->data;
switch(upload->state) {
case Upload_Write:
if(upload->to_fatfs) {
#ifdef GRBL_VFS
vfs_close(upload->file.vfs_handle);
upload->file.vfs_handle = NULL;
#else
f_close(upload->file.fatfs_handle);
upload->file.fatfs_handle = NULL;
#endif
} else {
#ifdef STDIO_FS
fclose(upload->file.handle);
#endif
upload->file.handle = NULL;
}
break;
case Upload_Failed:
do_cleanup(upload);
break;
default:
break;
}
upload->state = Upload_Parsing;
return 0;
}
static int on_body_end (struct multipartparser *parser)
{
((file_upload_t *)parser->data)->state = Upload_Complete;
return 0;
}
void http_upload_on_filename_parsed (file_upload_t *upload, http_upload_filename_parsed_ptr fn, void *data)
{
upload->on_filename_parsed = fn;
upload->on_filename_parsed_arg = data;
}
file_upload_t *http_upload_start (http_request_t *request, const char* boundary, bool to_fatfs)
{
#ifndef STDIO_FS
if(!to_fatfs)
return false;
#endif
if(sd_callbacks == NULL && (sd_callbacks = malloc(sizeof(struct multipartparser_callbacks)))) {
multipartparser_callbacks_init(sd_callbacks);
sd_callbacks->on_body_begin = on_body_begin;
sd_callbacks->on_part_begin = on_part_begin;
sd_callbacks->on_header_field = on_header_field;
sd_callbacks->on_header_value = on_header_value;
sd_callbacks->on_headers_complete = on_headers_complete;
sd_callbacks->on_data = on_data;
sd_callbacks->on_part_end = on_part_end;
sd_callbacks->on_body_end = on_body_end;
}
if(sd_callbacks) {
multipartparser_init(&parser, boundary);
if((parser.data = malloc(sizeof(file_upload_t)))) {
request->private_data = parser.data;
request->on_request_completed = cleanup;
memset(parser.data, 0, sizeof(file_upload_t));
((file_upload_t *)parser.data)->to_fatfs = to_fatfs;
}
}
return parser.data;
}
size_t http_upload_chunk (http_request_t *req, const char* data, size_t size)
{
return multipartparser_execute(&parser, sd_callbacks, data, size);
}
#endif