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_DYNAMIC_HEADERS
|
---|
61 | #error This needs LWIP_HTTPD_DYNAMIC_HEADERS
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | /* This is the page we send. It's not generated, as you see.
|
---|
65 | * Generating custom things instead of memcpy is left to your imagination :-)
|
---|
66 | */
|
---|
67 | const char generated_html[] =
|
---|
68 | "<html>\n"
|
---|
69 | "<head><title>lwIP - A Lightweight TCP/IP Stack</title></head>\n"
|
---|
70 | " <body bgcolor=\"white\" text=\"black\">\n"
|
---|
71 | " <table width=\"100%\">\n"
|
---|
72 | " <tr valign=\"top\">\n"
|
---|
73 | " <td width=\"80\">\n"
|
---|
74 | " <a href=\"http://www.sics.se/\"><img src=\"/img/sics.gif\"\n"
|
---|
75 | " border=\"0\" alt=\"SICS logo\" title=\"SICS logo\"></a>\n"
|
---|
76 | " </td>\n"
|
---|
77 | " <td width=\"500\">\n"
|
---|
78 | " <h1>lwIP - A Lightweight TCP/IP Stack</h1>\n"
|
---|
79 | " <h2>Generated page</h2>\n"
|
---|
80 | " <p>This page might be generated in-memory at runtime</p>\n"
|
---|
81 | " </td>\n"
|
---|
82 | " <td>\n"
|
---|
83 | " \n"
|
---|
84 | " </td>\n"
|
---|
85 | " </tr>\n"
|
---|
86 | " </table>\n"
|
---|
87 | " </body>\n"
|
---|
88 | "</html>";
|
---|
89 |
|
---|
90 |
|
---|
91 | void
|
---|
92 | genfiles_ex_init(void)
|
---|
93 | {
|
---|
94 | /* nothing to do here yet */
|
---|
95 | }
|
---|
96 |
|
---|
97 | int
|
---|
98 | fs_open_custom(struct fs_file *file, const char *name)
|
---|
99 | {
|
---|
100 | /* this example only provides one file */
|
---|
101 | if (!strcmp(name, "/generated.html")) {
|
---|
102 | /* initialize fs_file correctly */
|
---|
103 | memset(file, 0, sizeof(struct fs_file));
|
---|
104 | file->pextension = mem_malloc(sizeof(generated_html));
|
---|
105 | if (file->pextension != NULL) {
|
---|
106 | /* instead of doing memcpy, you would generate e.g. a JSON here */
|
---|
107 | memcpy(file->pextension, generated_html, sizeof(generated_html));
|
---|
108 | file->data = (const char *)file->pextension;
|
---|
109 | file->len = sizeof(generated_html) - 1; /* don't send the trailing 0 */
|
---|
110 | file->index = file->len;
|
---|
111 | /* allow persisteng connections */
|
---|
112 | file->flags = FS_FILE_FLAGS_HEADER_PERSISTENT;
|
---|
113 | return 1;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | void
|
---|
120 | fs_close_custom(struct fs_file *file)
|
---|
121 | {
|
---|
122 | if (file && file->pextension) {
|
---|
123 | mem_free(file->pextension);
|
---|
124 | file->pextension = NULL;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | #if LWIP_HTTPD_FS_ASYNC_READ
|
---|
129 | u8_t
|
---|
130 | fs_canread_custom(struct fs_file *file)
|
---|
131 | {
|
---|
132 | LWIP_UNUSED_ARG(file);
|
---|
133 | /* This example does not use delayed/async reading */
|
---|
134 | return 1;
|
---|
135 | }
|
---|
136 |
|
---|
137 | u8_t
|
---|
138 | fs_wait_read_custom(struct fs_file *file, fs_wait_cb callback_fn, void *callback_arg)
|
---|
139 | {
|
---|
140 | LWIP_ASSERT("not implemented in this example configuration", 0);
|
---|
141 | LWIP_UNUSED_ARG(file);
|
---|
142 | LWIP_UNUSED_ARG(callback_fn);
|
---|
143 | LWIP_UNUSED_ARG(callback_arg);
|
---|
144 | /* Return
|
---|
145 | - 1 if ready to read (at least one byte)
|
---|
146 | - 0 if reading should be delayed (call 'tcpip_callback(callback_fn, callback_arg)' when ready) */
|
---|
147 | return 1;
|
---|
148 | }
|
---|
149 |
|
---|
150 | int
|
---|
151 | fs_read_async_custom(struct fs_file *file, char *buffer, int count, fs_wait_cb callback_fn, void *callback_arg)
|
---|
152 | {
|
---|
153 | LWIP_ASSERT("not implemented in this example configuration", 0);
|
---|
154 | LWIP_UNUSED_ARG(file);
|
---|
155 | LWIP_UNUSED_ARG(buffer);
|
---|
156 | LWIP_UNUSED_ARG(count);
|
---|
157 | LWIP_UNUSED_ARG(callback_fn);
|
---|
158 | LWIP_UNUSED_ARG(callback_arg);
|
---|
159 | /* Return
|
---|
160 | - FS_READ_EOF if all bytes have been read
|
---|
161 | - FS_READ_DELAYED if reading is delayed (call 'tcpip_callback(callback_fn, callback_arg)' when done) */
|
---|
162 | /* all bytes read already */
|
---|
163 | return FS_READ_EOF;
|
---|
164 | }
|
---|
165 |
|
---|
166 | #else /* LWIP_HTTPD_FS_ASYNC_READ */
|
---|
167 | int
|
---|
168 | fs_read_custom(struct fs_file *file, char *buffer, int count)
|
---|
169 | {
|
---|
170 | LWIP_ASSERT("not implemented in this example configuration", 0);
|
---|
171 | LWIP_UNUSED_ARG(file);
|
---|
172 | LWIP_UNUSED_ARG(buffer);
|
---|
173 | LWIP_UNUSED_ARG(count);
|
---|
174 | /* Return
|
---|
175 | - FS_READ_EOF if all bytes have been read
|
---|
176 | - FS_READ_DELAYED if reading is delayed (call 'tcpip_callback(callback_fn, callback_arg)' when done) */
|
---|
177 | /* all bytes read already */
|
---|
178 | return FS_READ_EOF;
|
---|
179 | }
|
---|
180 |
|
---|
181 | #endif /* LWIP_HTTPD_FS_ASYNC_READ */
|
---|
182 |
|
---|
183 | #endif /* LWIP_HTTPD_EXAMPLE_GENERATEDFILES */
|
---|