-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwipHttpServerFsIntegration.cpp
112 lines (90 loc) · 2.92 KB
/
lwipHttpServerFsIntegration.cpp
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
/**
* \file
* \brief Definitions of lwIP functions related to file system integration for http server
*
* \author Copyright (C) 2019 Aleksander Szczygiel
* \author Copyright (C) 2020-2022 Kamil Szczygiel http://www.distortec.com http://www.freddiechopin.info
*
* \par License
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
* distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "lwipHttpServerFsConfigure.hpp"
#include "lwip/apps/fs.h"
#include "estd/ScopeGuard.hpp"
#include <cassert>
#include <cstring>
namespace
{
/*---------------------------------------------------------------------------------------------------------------------+
| local objects
+---------------------------------------------------------------------------------------------------------------------*/
/// path to root directory of http server
const char* httpServerRootDirectory {};
} // namespace
/*---------------------------------------------------------------------------------------------------------------------+
| global functions
+---------------------------------------------------------------------------------------------------------------------*/
void lwipHttpServerFsConfigure(const char* rootDirectory)
{
httpServerRootDirectory = rootDirectory != nullptr ? rootDirectory : "";
}
extern "C" int fs_open_custom(fs_file* const fileStruct, const char* const name)
{
assert(fileStruct != nullptr);
assert(httpServerRootDirectory != nullptr);
FILE* file;
{
char fullPath[256];
const auto ret = sniprintf(fullPath, sizeof(fullPath), "%s%s", httpServerRootDirectory, name);
if (ret < 0)
return {};
if (static_cast<unsigned int>(ret) >= sizeof(fullPath))
return {};
file = fopen(fullPath, "r");
if (file == nullptr)
return {};
}
auto closeScopeGuard = estd::makeScopeGuard(
[file]()
{
fclose(file);
});
memset(fileStruct, 0, sizeof(*fileStruct));
{
const auto ret = fseek(file, 0, SEEK_END);
if (ret != 0)
return {};
}
{
const auto ret = ftell(file);
if (ret == -1)
return {};
fileStruct->len = ret;
}
{
const auto ret = fseek(file, 0, SEEK_SET);
if (ret != 0)
return {};
}
fileStruct->flags = FS_FILE_FLAGS_HEADER_PERSISTENT | FS_FILE_FLAGS_HEADER_HTTPVER_1_1;
fileStruct->pextension = file;
closeScopeGuard.release();
return 1;
}
extern "C" void fs_close_custom(fs_file* const fileStruct)
{
if (fileStruct == nullptr || fileStruct->pextension == nullptr)
return;
fclose(static_cast<FILE*>(fileStruct->pextension));
fileStruct->pextension = {};
}
extern "C" int fs_read_custom(fs_file* const fileStruct, char* const buffer, const int count)
{
assert(fileStruct != nullptr);
assert(fileStruct->pextension != nullptr);
assert(buffer != nullptr);
const auto bytesRead = fread(buffer, 1, count, static_cast<FILE*>(fileStruct->pextension));
fileStruct->index += bytesRead;
return bytesRead;
}