• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * HTTPD custom file system example for runtime generated files
4  *
5  * This file demonstrates how to add support for generated files to httpd.
6  */
7 
8  /*
9  * Copyright (c) 2017 Simon Goldschmidt
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without modification,
13  * are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  *    this list of conditions and the following disclaimer in the documentation
19  *    and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
26  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32  * OF SUCH DAMAGE.
33  *
34  * This file is part of the lwIP TCP/IP stack.
35  *
36  * Author: Simon Goldschmidt <goldsimon@gmx.de>
37  *
38  */
39 
40 #include "lwip/opt.h"
41 #include "genfiles_example.h"
42 
43 #include "lwip/apps/fs.h"
44 #include "lwip/def.h"
45 #include "lwip/mem.h"
46 
47 #include <stdio.h>
48 #include <string.h>
49 
50 /** define LWIP_HTTPD_EXAMPLE_GENERATEDFILES to 1 to enable this file system */
51 #ifndef LWIP_HTTPD_EXAMPLE_GENERATEDFILES
52 #define LWIP_HTTPD_EXAMPLE_GENERATEDFILES 0
53 #endif
54 
55 #if LWIP_HTTPD_EXAMPLE_GENERATEDFILES
56 
57 #if !LWIP_HTTPD_CUSTOM_FILES
58 #error This needs LWIP_HTTPD_CUSTOM_FILES
59 #endif
60 #if !LWIP_HTTPD_FILE_EXTENSION
61 #error This needs LWIP_HTTPD_FILE_EXTENSION
62 #endif
63 #if !LWIP_HTTPD_DYNAMIC_HEADERS
64 #error This needs LWIP_HTTPD_DYNAMIC_HEADERS
65 #endif
66 
67 /* This is the page we send. It's not generated, as you see.
68  * Generating custom things instead of memcpy is left to your imagination :-)
69  */
70 const char generated_html[] =
71 "<html>\n"
72 "<head><title>lwIP - A Lightweight TCP/IP Stack</title></head>\n"
73 " <body bgcolor=\"white\" text=\"black\">\n"
74 "  <table width=\"100%\">\n"
75 "   <tr valign=\"top\">\n"
76 "    <td width=\"80\">\n"
77 "     <a href=\"http://www.sics.se/\"><img src=\"/img/sics.gif\"\n"
78 "      border=\"0\" alt=\"SICS logo\" title=\"SICS logo\"></a>\n"
79 "    </td>\n"
80 "    <td width=\"500\">\n"
81 "     <h1>lwIP - A Lightweight TCP/IP Stack</h1>\n"
82 "     <h2>Generated page</h2>\n"
83 "     <p>This page might be generated in-memory at runtime</p>\n"
84 "    </td>\n"
85 "    <td>\n"
86 "    &nbsp;\n"
87 "    </td>\n"
88 "   </tr>\n"
89 "  </table>\n"
90 " </body>\n"
91 "</html>";
92 
93 
94 void
genfiles_ex_init(void)95 genfiles_ex_init(void)
96 {
97   /* nothing to do here yet */
98 }
99 
100 int
fs_open_custom(struct fs_file * file,const char * name)101 fs_open_custom(struct fs_file *file, const char *name)
102 {
103   /* this example only provides one file */
104   if (!strcmp(name, "/generated.html")) {
105     /* initialize fs_file correctly */
106     memset(file, 0, sizeof(struct fs_file));
107     file->pextension = mem_malloc(sizeof(generated_html));
108     if (file->pextension != NULL) {
109       /* instead of doing memcpy, you would generate e.g. a JSON here */
110       memcpy(file->pextension, generated_html, sizeof(generated_html));
111       file->data = (const char *)file->pextension;
112       file->len = sizeof(generated_html) - 1; /* don't send the trailing 0 */
113       file->index = file->len;
114       /* allow persisteng connections */
115       file->flags = FS_FILE_FLAGS_HEADER_PERSISTENT;
116       return 1;
117     }
118   }
119   return 0;
120 }
121 
122 void
fs_close_custom(struct fs_file * file)123 fs_close_custom(struct fs_file *file)
124 {
125   if (file && file->pextension) {
126     mem_free(file->pextension);
127     file->pextension = NULL;
128   }
129 }
130 
131 #if LWIP_HTTPD_FS_ASYNC_READ
132 u8_t
fs_canread_custom(struct fs_file * file)133 fs_canread_custom(struct fs_file *file)
134 {
135   LWIP_UNUSED_ARG(file);
136   /* This example does not use delayed/async reading */
137   return 1;
138 }
139 
140 u8_t
fs_wait_read_custom(struct fs_file * file,fs_wait_cb callback_fn,void * callback_arg)141 fs_wait_read_custom(struct fs_file *file, fs_wait_cb callback_fn, void *callback_arg)
142 {
143   LWIP_ASSERT("not implemented in this example configuration", 0);
144   LWIP_UNUSED_ARG(file);
145   LWIP_UNUSED_ARG(callback_fn);
146   LWIP_UNUSED_ARG(callback_arg);
147   /* Return
148      - 1 if ready to read (at least one byte)
149      - 0 if reading should be delayed (call 'tcpip_callback(callback_fn, callback_arg)' when ready) */
150   return 1;
151 }
152 
153 int
fs_read_async_custom(struct fs_file * file,char * buffer,int count,fs_wait_cb callback_fn,void * callback_arg)154 fs_read_async_custom(struct fs_file *file, char *buffer, int count, fs_wait_cb callback_fn, void *callback_arg)
155 {
156   LWIP_ASSERT("not implemented in this example configuration", 0);
157   LWIP_UNUSED_ARG(file);
158   LWIP_UNUSED_ARG(buffer);
159   LWIP_UNUSED_ARG(count);
160   LWIP_UNUSED_ARG(callback_fn);
161   LWIP_UNUSED_ARG(callback_arg);
162   /* Return
163      - FS_READ_EOF if all bytes have been read
164      - FS_READ_DELAYED if reading is delayed (call 'tcpip_callback(callback_fn, callback_arg)' when done) */
165   /* all bytes read already */
166   return FS_READ_EOF;
167 }
168 
169 #else /* LWIP_HTTPD_FS_ASYNC_READ */
170 int
fs_read_custom(struct fs_file * file,char * buffer,int count)171 fs_read_custom(struct fs_file *file, char *buffer, int count)
172 {
173   LWIP_ASSERT("not implemented in this example configuration", 0);
174   LWIP_UNUSED_ARG(file);
175   LWIP_UNUSED_ARG(buffer);
176   LWIP_UNUSED_ARG(count);
177   /* Return
178      - FS_READ_EOF if all bytes have been read
179      - FS_READ_DELAYED if reading is delayed (call 'tcpip_callback(callback_fn, callback_arg)' when done) */
180   /* all bytes read already */
181   return FS_READ_EOF;
182 }
183 
184 #endif /* LWIP_HTTPD_FS_ASYNC_READ */
185 
186 #endif /* LWIP_HTTPD_EXAMPLE_GENERATEDFILES */
187