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 <adam@sics.se>
30 *
31 */
32
33#include "lwip/apps/httpd_opts.h"
34#include "lwip/def.h"
35#include "lwip/apps/fs.h"
36#include <string.h>
37
38
39#include HTTPD_FSDATA_FILE
40
41/*-----------------------------------------------------------------------------------*/
42
43#if LWIP_HTTPD_CUSTOM_FILES
44int fs_open_custom(struct fs_file *file, const char *name);
45void fs_close_custom(struct fs_file *file);
46#if LWIP_HTTPD_FS_ASYNC_READ
47u8_t fs_canread_custom(struct fs_file *file);
48u8_t fs_wait_read_custom(struct fs_file *file, fs_wait_cb callback_fn, void *callback_arg);
49int fs_read_async_custom(struct fs_file *file, char *buffer, int count, fs_wait_cb callback_fn, void *callback_arg);
50#else /* LWIP_HTTPD_FS_ASYNC_READ */
51int fs_read_custom(struct fs_file *file, char *buffer, int count);
52#endif /* LWIP_HTTPD_FS_ASYNC_READ */
53#endif /* LWIP_HTTPD_CUSTOM_FILES */
54
55/*-----------------------------------------------------------------------------------*/
56err_t
57fs_open(struct fs_file *file, const char *name)
58{
59  const struct fsdata_file *f;
60
61  if ((file == NULL) || (name == NULL)) {
62    return ERR_ARG;
63  }
64
65#if LWIP_HTTPD_CUSTOM_FILES
66  if (fs_open_custom(file, name)) {
67    file->is_custom_file = 1;
68    return ERR_OK;
69  }
70  file->is_custom_file = 0;
71#endif /* LWIP_HTTPD_CUSTOM_FILES */
72
73  for (f = FS_ROOT; f != NULL; f = f->next) {
74    if (!strcmp(name, (const char *)f->name)) {
75      file->data = (const char *)f->data;
76      file->len = f->len;
77      file->index = f->len;
78      file->pextension = NULL;
79      file->flags = f->flags;
80#if HTTPD_PRECALCULATED_CHECKSUM
81      file->chksum_count = f->chksum_count;
82      file->chksum = f->chksum;
83#endif /* HTTPD_PRECALCULATED_CHECKSUM */
84#if LWIP_HTTPD_FILE_STATE
85      file->state = fs_state_init(file, name);
86#endif /* #if LWIP_HTTPD_FILE_STATE */
87      return ERR_OK;
88    }
89  }
90  /* file not found */
91  return ERR_VAL;
92}
93
94/*-----------------------------------------------------------------------------------*/
95void
96fs_close(struct fs_file *file)
97{
98#if LWIP_HTTPD_CUSTOM_FILES
99  if (file->is_custom_file) {
100    fs_close_custom(file);
101  }
102#endif /* LWIP_HTTPD_CUSTOM_FILES */
103#if LWIP_HTTPD_FILE_STATE
104  fs_state_free(file, file->state);
105#endif /* #if LWIP_HTTPD_FILE_STATE */
106  LWIP_UNUSED_ARG(file);
107}
108/*-----------------------------------------------------------------------------------*/
109#if LWIP_HTTPD_DYNAMIC_FILE_READ
110#if LWIP_HTTPD_FS_ASYNC_READ
111int
112fs_read_async(struct fs_file *file, char *buffer, int count, fs_wait_cb callback_fn, void *callback_arg)
113#else /* LWIP_HTTPD_FS_ASYNC_READ */
114int
115fs_read(struct fs_file *file, char *buffer, int count)
116#endif /* LWIP_HTTPD_FS_ASYNC_READ */
117{
118  int read;
119  if (file->index == file->len) {
120    return FS_READ_EOF;
121  }
122#if LWIP_HTTPD_FS_ASYNC_READ
123  LWIP_UNUSED_ARG(callback_fn);
124  LWIP_UNUSED_ARG(callback_arg);
125#endif /* LWIP_HTTPD_FS_ASYNC_READ */
126#if LWIP_HTTPD_CUSTOM_FILES
127  if (file->is_custom_file) {
128#if LWIP_HTTPD_FS_ASYNC_READ
129    return fs_read_async_custom(file, buffer, count, callback_fn, callback_arg);
130#else /* LWIP_HTTPD_FS_ASYNC_READ */
131    return fs_read_custom(file, buffer, count);
132#endif /* LWIP_HTTPD_FS_ASYNC_READ */
133  }
134#endif /* LWIP_HTTPD_CUSTOM_FILES */
135
136  read = file->len - file->index;
137  if (read > count) {
138    read = count;
139  }
140
141  MEMCPY(buffer, (file->data + file->index), read);
142  file->index += read;
143
144  return (read);
145}
146#endif /* LWIP_HTTPD_DYNAMIC_FILE_READ */
147/*-----------------------------------------------------------------------------------*/
148#if LWIP_HTTPD_FS_ASYNC_READ
149int
150fs_is_file_ready(struct fs_file *file, fs_wait_cb callback_fn, void *callback_arg)
151{
152  if (file != NULL) {
153#if LWIP_HTTPD_FS_ASYNC_READ
154#if LWIP_HTTPD_CUSTOM_FILES
155    if (!fs_canread_custom(file)) {
156      if (fs_wait_read_custom(file, callback_fn, callback_arg)) {
157        return 0;
158      }
159    }
160#else /* LWIP_HTTPD_CUSTOM_FILES */
161    LWIP_UNUSED_ARG(callback_fn);
162    LWIP_UNUSED_ARG(callback_arg);
163#endif /* LWIP_HTTPD_CUSTOM_FILES */
164#endif /* LWIP_HTTPD_FS_ASYNC_READ */
165  }
166  return 1;
167}
168#endif /* LWIP_HTTPD_FS_ASYNC_READ */
169/*-----------------------------------------------------------------------------------*/
170int
171fs_bytes_left(struct fs_file *file)
172{
173  return file->len - file->index;
174}
175