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