source: azure_iot_hub_f767zi/trunk/asp_baseplatform/lwip/contrib-2.1.0/examples/httpd/fs_example/fs_example.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 9.5 KB
Line 
1/**
2 * @file
3 * HTTPD custom file system example
4 *
5 * This file demonstrates how to add support for an external file system to httpd.
6 * It provides access to the specified root directory and uses stdio.h file functions
7 * to read files.
8 *
9 * ATTENTION: This implementation is *not* secure: no checks are added to ensure
10 * files are only read below the specified root directory!
11 */
12
13 /*
14 * Copyright (c) 2017 Simon Goldschmidt
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without modification,
18 * are permitted provided that the following conditions are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright notice,
21 * this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright notice,
23 * this list of conditions and the following disclaimer in the documentation
24 * and/or other materials provided with the distribution.
25 * 3. The name of the author may not be used to endorse or promote products
26 * derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
31 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
33 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
37 * OF SUCH DAMAGE.
38 *
39 * This file is part of the lwIP TCP/IP stack.
40 *
41 * Author: Simon Goldschmidt <goldsimon@gmx.de>
42 *
43 */
44
45#include "lwip/opt.h"
46#include "fs_example.h"
47
48#include "lwip/apps/fs.h"
49#include "lwip/def.h"
50#include "lwip/mem.h"
51
52#include <stdio.h>
53#include <string.h>
54
55/** define LWIP_HTTPD_EXAMPLE_CUSTOMFILES to 1 to enable this file system */
56#ifndef LWIP_HTTPD_EXAMPLE_CUSTOMFILES
57#define LWIP_HTTPD_EXAMPLE_CUSTOMFILES 0
58#endif
59
60/** define LWIP_HTTPD_EXAMPLE_CUSTOMFILES_DELAYED to 1 to delay open and read
61 * as if e.g. reading from external SPI flash */
62#ifndef LWIP_HTTPD_EXAMPLE_CUSTOMFILES_DELAYED
63#define LWIP_HTTPD_EXAMPLE_CUSTOMFILES_DELAYED 1
64#endif
65
66/** define LWIP_HTTPD_EXAMPLE_CUSTOMFILES_LIMIT_READ to the number of bytes
67 * to read to emulate limited transfer buffers and don't read whole files in
68 * one chunk.
69 * WARNING: lowering this slows down the connection!
70 */
71#ifndef LWIP_HTTPD_EXAMPLE_CUSTOMFILES_LIMIT_READ
72#define LWIP_HTTPD_EXAMPLE_CUSTOMFILES_LIMIT_READ 0
73#endif
74
75#if LWIP_HTTPD_EXAMPLE_CUSTOMFILES
76
77#if !LWIP_HTTPD_CUSTOM_FILES
78#error This needs LWIP_HTTPD_CUSTOM_FILES
79#endif
80#if !LWIP_HTTPD_DYNAMIC_HEADERS
81#error This needs LWIP_HTTPD_DYNAMIC_HEADERS
82#endif
83#if !LWIP_HTTPD_DYNAMIC_FILE_READ
84#error This wants to demonstrate read-after-open, so LWIP_HTTPD_DYNAMIC_FILE_READ is required!
85#endif
86#if !LWIP_HTTPD_FS_ASYNC_READ
87#error This needs LWIP_HTTPD_FS_ASYNC_READ
88#endif
89
90#if LWIP_HTTPD_EXAMPLE_CUSTOMFILES_DELAYED
91#include "lwip/tcpip.h"
92#endif
93
94struct fs_custom_data {
95 FILE *f;
96#if LWIP_HTTPD_EXAMPLE_CUSTOMFILES_DELAYED
97 int delay_read;
98 fs_wait_cb callback_fn;
99 void *callback_arg;
100#endif
101};
102
103const char* fs_ex_root_dir;
104
105void
106fs_ex_init(const char *httpd_root_dir)
107{
108 fs_ex_root_dir = strdup(httpd_root_dir);
109}
110
111#if LWIP_HTTPD_CUSTOM_FILES
112int
113fs_open_custom(struct fs_file *file, const char *name)
114{
115 char full_filename[256];
116 FILE *f;
117
118 snprintf(full_filename, 255, "%s%s", fs_ex_root_dir, name);
119 full_filename[255] = 0;
120
121 f = fopen(full_filename, "rb");
122 if (f != NULL) {
123 if (!fseek(f, 0, SEEK_END)) {
124 int len = (int)ftell(f);
125 if(!fseek(f, 0, SEEK_SET)) {
126 struct fs_custom_data *data = (struct fs_custom_data *)mem_malloc(sizeof(struct fs_custom_data));
127 LWIP_ASSERT("out of memory?", data != NULL);
128 memset(file, 0, sizeof(struct fs_file));
129#if LWIP_HTTPD_EXAMPLE_CUSTOMFILES_DELAYED
130 file->len = 0; /* read size delayed */
131 data->delay_read = 3;
132 LWIP_UNUSED_ARG(len);
133#else
134 file->len = len;
135#endif
136 file->flags = FS_FILE_FLAGS_HEADER_PERSISTENT;
137 data->f = f;
138 file->pextension = data;
139 return 1;
140 }
141 }
142 fclose(f);
143 }
144 return 0;
145}
146
147void
148fs_close_custom(struct fs_file *file)
149{
150 if (file && file->pextension) {
151 struct fs_custom_data *data = (struct fs_custom_data *)file->pextension;
152 if (data->f != NULL) {
153 fclose(data->f);
154 data->f = NULL;
155 }
156 mem_free(data);
157 }
158}
159
160#if LWIP_HTTPD_FS_ASYNC_READ
161u8_t
162fs_canread_custom(struct fs_file *file)
163{
164 /* This function is only necessary for asynchronous I/O:
165 If reading would block, return 0 and implement fs_wait_read_custom() to call the
166 supplied callback if reading works. */
167#if LWIP_HTTPD_EXAMPLE_CUSTOMFILES_DELAYED
168 struct fs_custom_data *data;
169 LWIP_ASSERT("file != NULL", file != NULL);
170 data = (struct fs_custom_data *)file->pextension;
171 if (data == NULL) {
172 /* file transfer has been completed already */
173 LWIP_ASSERT("transfer complete", file->index == file->len);
174 return 1;
175 }
176 LWIP_ASSERT("data != NULL", data != NULL);
177 /* This just simulates a simple delay. This delay would normally come e.g. from SPI transfer */
178 if (data->delay_read == 3) {
179 /* delayed file size mode */
180 data->delay_read = 1;
181 LWIP_ASSERT("", file->len == 0);
182 if (!fseek(data->f, 0, SEEK_END)) {
183 int len = (int)ftell(data->f);
184 if(!fseek(data->f, 0, SEEK_SET)) {
185 file->len = len; /* read size delayed */
186 data->delay_read = 1;
187 return 0;
188 }
189 }
190 /* if we come here, something is wrong with the file */
191 LWIP_ASSERT("file error", 0);
192 }
193 if (data->delay_read == 1) {
194 /* tell read function to delay further */
195 }
196#endif
197 LWIP_UNUSED_ARG(file);
198 return 1;
199}
200
201#if LWIP_HTTPD_EXAMPLE_CUSTOMFILES_DELAYED
202static void
203fs_example_read_cb(void *arg)
204{
205 struct fs_custom_data *data = (struct fs_custom_data *)arg;
206 fs_wait_cb callback_fn = data->callback_fn;
207 void *callback_arg = data->callback_arg;
208 data->callback_fn = NULL;
209 data->callback_arg = NULL;
210
211 LWIP_ASSERT("no callback_fn", callback_fn != NULL);
212
213 callback_fn(callback_arg);
214}
215#endif
216
217u8_t
218fs_wait_read_custom(struct fs_file *file, fs_wait_cb callback_fn, void *callback_arg)
219{
220#if LWIP_HTTPD_EXAMPLE_CUSTOMFILES_DELAYED
221 err_t err;
222 struct fs_custom_data *data = (struct fs_custom_data *)file->pextension;
223 LWIP_ASSERT("data not set", data != NULL);
224 data->callback_fn = callback_fn;
225 data->callback_arg = callback_arg;
226 err = tcpip_try_callback(fs_example_read_cb, data);
227 LWIP_ASSERT("out of queue elements?", err == ERR_OK);
228 LWIP_UNUSED_ARG(err);
229#else
230 LWIP_ASSERT("not implemented in this example configuration", 0);
231#endif
232 LWIP_UNUSED_ARG(file);
233 LWIP_UNUSED_ARG(callback_fn);
234 LWIP_UNUSED_ARG(callback_arg);
235 /* Return
236 - 1 if ready to read (at least one byte)
237 - 0 if reading should be delayed (call 'tcpip_callback(callback_fn, callback_arg)' when ready) */
238 return 1;
239}
240
241int
242fs_read_async_custom(struct fs_file *file, char *buffer, int count, fs_wait_cb callback_fn, void *callback_arg)
243{
244 struct fs_custom_data *data = (struct fs_custom_data *)file->pextension;
245 FILE *f;
246 int len;
247 int read_count = count;
248 LWIP_ASSERT("data not set", data != NULL);
249
250#if LWIP_HTTPD_EXAMPLE_CUSTOMFILES_DELAYED
251 /* This just simulates a delay. This delay would normally come e.g. from SPI transfer */
252 LWIP_ASSERT("invalid state", data->delay_read >= 0 && data->delay_read <= 2);
253 if (data->delay_read == 2) {
254 /* no delay next time */
255 data->delay_read = 0;
256 return FS_READ_DELAYED;
257 } else if (data->delay_read == 1) {
258 err_t err;
259 /* execute requested delay */
260 data->delay_read = 2;
261 LWIP_ASSERT("duplicate callback request", data->callback_fn == NULL);
262 data->callback_fn = callback_fn;
263 data->callback_arg = callback_arg;
264 err = tcpip_try_callback(fs_example_read_cb, data);
265 LWIP_ASSERT("out of queue elements?", err == ERR_OK);
266 LWIP_UNUSED_ARG(err);
267 return FS_READ_DELAYED;
268 }
269 /* execute this read but delay the next one */
270 data->delay_read = 1;
271#endif
272
273#if LWIP_HTTPD_EXAMPLE_CUSTOMFILES_LIMIT_READ
274 read_count = LWIP_MIN(read_count, LWIP_HTTPD_EXAMPLE_CUSTOMFILES_LIMIT_READ);
275#endif
276
277 f = data->f;
278 len = (int)fread(buffer, 1, read_count, f);
279
280 LWIP_UNUSED_ARG(callback_fn);
281 LWIP_UNUSED_ARG(callback_arg);
282
283 file->index += len;
284
285 /* Return
286 - FS_READ_EOF if all bytes have been read
287 - FS_READ_DELAYED if reading is delayed (call 'tcpip_callback(callback_fn, callback_arg)' when done) */
288 if (len == 0) {
289 /* all bytes read already */
290 return FS_READ_EOF;
291 }
292 return len;
293}
294
295#else /* LWIP_HTTPD_FS_ASYNC_READ */
296int
297fs_read_custom(struct fs_file *file, char *buffer, int count)
298{
299 struct fs_custom_data *data = (struct fs_custom_data *)file->pextension;
300 FILE *f;
301 int len;
302 int read_count = count;
303 LWIP_ASSERT("data not set", data != NULL);
304
305#if LWIP_HTTPD_EXAMPLE_CUSTOMFILES_LIMIT_READ
306 read_count = LWIP_MIN(read_count, LWIP_HTTPD_EXAMPLE_CUSTOMFILES_LIMIT_READ);
307#endif
308
309 f = data->f;
310 len = (int)fread(buffer, 1, read_count, f);
311
312 file->index += len;
313
314 /* Return FS_READ_EOF if all bytes have been read */
315 return len;
316}
317
318#endif /* LWIP_HTTPD_FS_ASYNC_READ */
319#endif /* LWIP_HTTPD_CUSTOM_FILES */
320
321#endif /* LWIP_HTTPD_EXAMPLE_CUSTOMFILES */
Note: See TracBrowser for help on using the repository browser.