1 /* 2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 * OF SUCH DAMAGE. 26 * 27 * This file is part of the lwIP TCP/IP stack. 28 * 29 * Author: Adam Dunkels <[email protected]> 30 * 31 */ 32 33 #include "lwip/apps/httpd_opts.h" 34 #include "lwip/def.h" 35 #include "lwip/apps/fs.h" 36 #include "fsdata.h" 37 #include <string.h> 38 39 40 #if HTTPD_USE_CUSTOM_FSDATA 41 #include "fsdata_custom.c" 42 #else /* HTTPD_USE_CUSTOM_FSDATA */ 43 #include "fsdata.c" 44 #endif /* HTTPD_USE_CUSTOM_FSDATA */ 45 46 /*-----------------------------------------------------------------------------------*/ 47 48 #if LWIP_HTTPD_CUSTOM_FILES 49 int fs_open_custom(struct fs_file *file, const char *name); 50 void fs_close_custom(struct fs_file *file); 51 #if LWIP_HTTPD_FS_ASYNC_READ 52 u8_t fs_canread_custom(struct fs_file *file); 53 u8_t fs_wait_read_custom(struct fs_file *file, fs_wait_cb callback_fn, void *callback_arg); 54 int fs_read_async_custom(struct fs_file *file, char *buffer, int count, fs_wait_cb callback_fn, void *callback_arg); 55 #else /* LWIP_HTTPD_FS_ASYNC_READ */ 56 int fs_read_custom(struct fs_file *file, char *buffer, int count); 57 #endif /* LWIP_HTTPD_FS_ASYNC_READ */ 58 #endif /* LWIP_HTTPD_CUSTOM_FILES */ 59 60 /*-----------------------------------------------------------------------------------*/ 61 err_t 62 fs_open(struct fs_file *file, const char *name) 63 { 64 const struct fsdata_file *f; 65 66 if ((file == NULL) || (name == NULL)) { 67 return ERR_ARG; 68 } 69 70 #if LWIP_HTTPD_CUSTOM_FILES 71 if (fs_open_custom(file, name)) { 72 file->is_custom_file = 1; 73 return ERR_OK; 74 } 75 file->is_custom_file = 0; 76 #endif /* LWIP_HTTPD_CUSTOM_FILES */ 77 78 for (f = FS_ROOT; f != NULL; f = f->next) { 79 if (!strcmp(name, (const char *)f->name)) { 80 file->data = (const char *)f->data; 81 file->len = f->len; 82 file->index = f->len; 83 file->pextension = NULL; 84 file->flags = f->flags; 85 #if HTTPD_PRECALCULATED_CHECKSUM 86 file->chksum_count = f->chksum_count; 87 file->chksum = f->chksum; 88 #endif /* HTTPD_PRECALCULATED_CHECKSUM */ 89 #if LWIP_HTTPD_FILE_STATE 90 file->state = fs_state_init(file, name); 91 #endif /* #if LWIP_HTTPD_FILE_STATE */ 92 return ERR_OK; 93 } 94 } 95 /* file not found */ 96 return ERR_VAL; 97 } 98 99 /*-----------------------------------------------------------------------------------*/ 100 void 101 fs_close(struct fs_file *file) 102 { 103 #if LWIP_HTTPD_CUSTOM_FILES 104 if (file->is_custom_file) { 105 fs_close_custom(file); 106 } 107 #endif /* LWIP_HTTPD_CUSTOM_FILES */ 108 #if LWIP_HTTPD_FILE_STATE 109 fs_state_free(file, file->state); 110 #endif /* #if LWIP_HTTPD_FILE_STATE */ 111 LWIP_UNUSED_ARG(file); 112 } 113 /*-----------------------------------------------------------------------------------*/ 114 #if LWIP_HTTPD_DYNAMIC_FILE_READ 115 #if LWIP_HTTPD_FS_ASYNC_READ 116 int 117 fs_read_async(struct fs_file *file, char *buffer, int count, fs_wait_cb callback_fn, void *callback_arg) 118 #else /* LWIP_HTTPD_FS_ASYNC_READ */ 119 int 120 fs_read(struct fs_file *file, char *buffer, int count) 121 #endif /* LWIP_HTTPD_FS_ASYNC_READ */ 122 { 123 int read; 124 if(file->index == file->len) { 125 return FS_READ_EOF; 126 } 127 #if LWIP_HTTPD_FS_ASYNC_READ 128 LWIP_UNUSED_ARG(callback_fn); 129 LWIP_UNUSED_ARG(callback_arg); 130 #endif /* LWIP_HTTPD_FS_ASYNC_READ */ 131 #if LWIP_HTTPD_CUSTOM_FILES 132 if (file->is_custom_file) { 133 #if LWIP_HTTPD_FS_ASYNC_READ 134 return fs_read_async_custom(file, buffer, count, callback_fn, callback_arg); 135 #else /* LWIP_HTTPD_FS_ASYNC_READ */ 136 return fs_read_custom(file, buffer, count); 137 #endif /* LWIP_HTTPD_FS_ASYNC_READ */ 138 } 139 #endif /* LWIP_HTTPD_CUSTOM_FILES */ 140 141 read = file->len - file->index; 142 if(read > count) { 143 read = count; 144 } 145 146 MEMCPY(buffer, (file->data + file->index), read); 147 file->index += read; 148 149 return(read); 150 } 151 #endif /* LWIP_HTTPD_DYNAMIC_FILE_READ */ 152 /*-----------------------------------------------------------------------------------*/ 153 #if LWIP_HTTPD_FS_ASYNC_READ 154 int 155 fs_is_file_ready(struct fs_file *file, fs_wait_cb callback_fn, void *callback_arg) 156 { 157 if (file != NULL) { 158 #if LWIP_HTTPD_FS_ASYNC_READ 159 #if LWIP_HTTPD_CUSTOM_FILES 160 if (!fs_canread_custom(file)) { 161 if (fs_wait_read_custom(file, callback_fn, callback_arg)) { 162 return 0; 163 } 164 } 165 #else /* LWIP_HTTPD_CUSTOM_FILES */ 166 LWIP_UNUSED_ARG(callback_fn); 167 LWIP_UNUSED_ARG(callback_arg); 168 #endif /* LWIP_HTTPD_CUSTOM_FILES */ 169 #endif /* LWIP_HTTPD_FS_ASYNC_READ */ 170 } 171 return 1; 172 } 173 #endif /* LWIP_HTTPD_FS_ASYNC_READ */ 174 /*-----------------------------------------------------------------------------------*/ 175 int 176 fs_bytes_left(struct fs_file *file) 177 { 178 return file->len - file->index; 179 } 180