fs_example.c 9.6 KB

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