| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
| 5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/cppalliance/http
|
7 |
|
// Official repository: https://github.com/cppalliance/http
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#include <boost/http/server/mime_types.hpp>
|
10 |
|
#include <boost/http/server/mime_types.hpp>
|
| 11 |
|
#include <boost/http/server/mime_db.hpp>
|
11 |
|
#include <boost/http/server/mime_db.hpp>
|
| 12 |
|
|
12 |
|
|
| 13 |
|
|
13 |
|
|
| 14 |
|
|
14 |
|
|
| 15 |
|
|
15 |
|
|
| 16 |
|
|
16 |
|
|
| 17 |
|
|
17 |
|
|
| 18 |
|
|
18 |
|
|
| 19 |
|
|
19 |
|
|
| 20 |
|
|
20 |
|
|
| 21 |
|
|
21 |
|
|
| 22 |
|
|
22 |
|
|
| 23 |
|
|
23 |
|
|
| 24 |
|
|
24 |
|
|
| 25 |
|
|
25 |
|
|
| 26 |
|
|
26 |
|
|
| 27 |
|
// Sorted by extension for binary search
|
27 |
|
// Sorted by extension for binary search
|
| 28 |
|
constexpr ext_entry ext_db[] = {
|
28 |
|
constexpr ext_entry ext_db[] = {
|
| 29 |
|
|
29 |
|
|
| 30 |
|
{ "avif", "image/avif" },
|
30 |
|
{ "avif", "image/avif" },
|
| 31 |
|
|
31 |
|
|
| 32 |
|
{ "bz", "application/x-bzip" },
|
32 |
|
{ "bz", "application/x-bzip" },
|
| 33 |
|
{ "bz2", "application/x-bzip2" },
|
33 |
|
{ "bz2", "application/x-bzip2" },
|
| 34 |
|
{ "cjs", "application/javascript" },
|
34 |
|
{ "cjs", "application/javascript" },
|
| 35 |
|
|
35 |
|
|
| 36 |
|
|
36 |
|
|
| 37 |
|
{ "flac", "audio/flac" },
|
37 |
|
{ "flac", "audio/flac" },
|
| 38 |
|
|
38 |
|
|
| 39 |
|
{ "gz", "application/gzip" },
|
39 |
|
{ "gz", "application/gzip" },
|
| 40 |
|
|
40 |
|
|
| 41 |
|
|
41 |
|
|
| 42 |
|
{ "ico", "image/x-icon" },
|
42 |
|
{ "ico", "image/x-icon" },
|
| 43 |
|
{ "ics", "text/calendar" },
|
43 |
|
{ "ics", "text/calendar" },
|
| 44 |
|
{ "jpeg", "image/jpeg" },
|
44 |
|
{ "jpeg", "image/jpeg" },
|
| 45 |
|
|
45 |
|
|
| 46 |
|
{ "js", "text/javascript" },
|
46 |
|
{ "js", "text/javascript" },
|
| 47 |
|
{ "json", "application/json" },
|
47 |
|
{ "json", "application/json" },
|
| 48 |
|
|
48 |
|
|
| 49 |
|
|
49 |
|
|
| 50 |
|
{ "manifest", "text/cache-manifest" },
|
50 |
|
{ "manifest", "text/cache-manifest" },
|
| 51 |
|
{ "md", "text/markdown" },
|
51 |
|
{ "md", "text/markdown" },
|
| 52 |
|
{ "mjs", "text/javascript" },
|
52 |
|
{ "mjs", "text/javascript" },
|
| 53 |
|
|
53 |
|
|
| 54 |
|
|
54 |
|
|
| 55 |
|
{ "mpeg", "video/mpeg" },
|
55 |
|
{ "mpeg", "video/mpeg" },
|
| 56 |
|
|
56 |
|
|
| 57 |
|
|
57 |
|
|
| 58 |
|
|
58 |
|
|
| 59 |
|
|
59 |
|
|
| 60 |
|
|
60 |
|
|
| 61 |
|
{ "pdf", "application/pdf" },
|
61 |
|
{ "pdf", "application/pdf" },
|
| 62 |
|
|
62 |
|
|
| 63 |
|
{ "rtf", "application/rtf" },
|
63 |
|
{ "rtf", "application/rtf" },
|
| 64 |
|
{ "svg", "image/svg+xml" },
|
64 |
|
{ "svg", "image/svg+xml" },
|
| 65 |
|
{ "tar", "application/x-tar" },
|
65 |
|
{ "tar", "application/x-tar" },
|
| 66 |
|
|
66 |
|
|
| 67 |
|
{ "tiff", "image/tiff" },
|
67 |
|
{ "tiff", "image/tiff" },
|
| 68 |
|
|
68 |
|
|
| 69 |
|
|
69 |
|
|
| 70 |
|
{ "wasm", "application/wasm" },
|
70 |
|
{ "wasm", "application/wasm" },
|
| 71 |
|
|
71 |
|
|
| 72 |
|
{ "weba", "audio/webm" },
|
72 |
|
{ "weba", "audio/webm" },
|
| 73 |
|
{ "webm", "video/webm" },
|
73 |
|
{ "webm", "video/webm" },
|
| 74 |
|
{ "webp", "image/webp" },
|
74 |
|
{ "webp", "image/webp" },
|
| 75 |
|
|
75 |
|
|
| 76 |
|
{ "woff2", "font/woff2" },
|
76 |
|
{ "woff2", "font/woff2" },
|
| 77 |
|
{ "xhtml", "application/xhtml+xml" },
|
77 |
|
{ "xhtml", "application/xhtml+xml" },
|
| 78 |
|
{ "xml", "application/xml" },
|
78 |
|
{ "xml", "application/xml" },
|
| 79 |
|
{ "zip", "application/zip" },
|
79 |
|
{ "zip", "application/zip" },
|
| 80 |
|
{ "7z", "application/x-7z-compressed" },
|
80 |
|
{ "7z", "application/x-7z-compressed" },
|
| 81 |
|
|
81 |
|
|
| 82 |
|
|
82 |
|
|
| 83 |
|
constexpr std::size_t ext_db_size = sizeof( ext_db ) / sizeof( ext_db[0] );
|
83 |
|
constexpr std::size_t ext_db_size = sizeof( ext_db ) / sizeof( ext_db[0] );
|
| 84 |
|
|
84 |
|
|
| 85 |
|
// Case-insensitive comparison
|
85 |
|
// Case-insensitive comparison
|
| 86 |
|
|
86 |
|
|
| 87 |
|
compare_icase( core::string_view a, core::string_view b ) noexcept
|
87 |
|
compare_icase( core::string_view a, core::string_view b ) noexcept
|
| 88 |
|
|
88 |
|
|
| 89 |
|
auto const n = ( std::min )( a.size(), b.size() );
|
89 |
|
auto const n = ( std::min )( a.size(), b.size() );
|
| 90 |
|
for( std::size_t i = 0; i < n; ++i )
|
90 |
|
for( std::size_t i = 0; i < n; ++i )
|
| 91 |
|
|
91 |
|
|
| 92 |
|
auto const ca = static_cast<unsigned char>(
|
92 |
|
auto const ca = static_cast<unsigned char>(
|
| 93 |
|
std::tolower( static_cast<unsigned char>( a[i] ) ) );
|
93 |
|
std::tolower( static_cast<unsigned char>( a[i] ) ) );
|
| 94 |
|
auto const cb = static_cast<unsigned char>(
|
94 |
|
auto const cb = static_cast<unsigned char>(
|
| 95 |
|
std::tolower( static_cast<unsigned char>( b[i] ) ) );
|
95 |
|
std::tolower( static_cast<unsigned char>( b[i] ) ) );
|
| 96 |
|
|
96 |
|
|
| 97 |
|
|
97 |
|
|
| 98 |
|
|
98 |
|
|
| 99 |
|
|
99 |
|
|
| 100 |
|
|
100 |
|
|
| 101 |
|
if( a.size() < b.size() )
|
101 |
|
if( a.size() < b.size() )
|
| 102 |
|
|
102 |
|
|
| 103 |
|
if( a.size() > b.size() )
|
103 |
|
if( a.size() > b.size() )
|
| 104 |
|
|
104 |
|
|
| 105 |
|
|
105 |
|
|
| 106 |
|
|
106 |
|
|
| 107 |
|
|
107 |
|
|
| 108 |
|
// Extract extension from path
|
108 |
|
// Extract extension from path
|
| 109 |
|
|
109 |
|
|
| 110 |
|
get_extension( core::string_view path ) noexcept
|
110 |
|
get_extension( core::string_view path ) noexcept
|
| 111 |
|
|
111 |
|
|
| 112 |
|
|
112 |
|
|
| 113 |
|
auto const pos = path.rfind( '.' );
|
113 |
|
auto const pos = path.rfind( '.' );
|
| 114 |
|
if( pos == core::string_view::npos )
|
114 |
|
if( pos == core::string_view::npos )
|
| 115 |
|
return path; // Assume it's just an extension
|
115 |
|
return path; // Assume it's just an extension
|
| 116 |
|
return path.substr( pos + 1 );
|
116 |
|
return path.substr( pos + 1 );
|
| 117 |
|
|
117 |
|
|
| 118 |
|
|
118 |
|
|
| 119 |
|
// Binary search for extension
|
119 |
|
// Binary search for extension
|
| 120 |
|
|
120 |
|
|
| 121 |
|
lookup_ext( core::string_view ext ) noexcept
|
121 |
|
lookup_ext( core::string_view ext ) noexcept
|
| 122 |
|
|
122 |
|
|
| 123 |
|
|
123 |
|
|
| 124 |
|
std::size_t hi = ext_db_size;
|
124 |
|
std::size_t hi = ext_db_size;
|
| 125 |
|
|
125 |
|
|
| 126 |
|
|
126 |
|
|
| 127 |
|
auto const mid = lo + ( hi - lo ) / 2;
|
127 |
|
auto const mid = lo + ( hi - lo ) / 2;
|
| 128 |
|
auto const cmp = compare_icase( ext_db[mid].ext, ext );
|
128 |
|
auto const cmp = compare_icase( ext_db[mid].ext, ext );
|
| 129 |
|
|
129 |
|
|
| 130 |
|
|
130 |
|
|
| 131 |
|
|
131 |
|
|
| 132 |
|
|
132 |
|
|
| 133 |
|
|
133 |
|
|
| 134 |
|
|
134 |
|
|
| 135 |
|
|
135 |
|
|
| 136 |
|
|
136 |
|
|
| 137 |
|
|
137 |
|
|
| 138 |
|
|
138 |
|
|
| 139 |
|
|
139 |
|
|
| 140 |
|
|
140 |
|
|
| 141 |
|
|
141 |
|
|
| 142 |
|
lookup( core::string_view path_or_ext ) noexcept
|
142 |
|
lookup( core::string_view path_or_ext ) noexcept
|
| 143 |
|
|
143 |
|
|
| 144 |
|
if( path_or_ext.empty() )
|
144 |
|
if( path_or_ext.empty() )
|
| 145 |
|
|
145 |
|
|
| 146 |
|
|
146 |
|
|
| 147 |
|
// Skip leading dot if present
|
147 |
|
// Skip leading dot if present
|
| 148 |
|
if( path_or_ext[0] == '.' )
|
148 |
|
if( path_or_ext[0] == '.' )
|
| 149 |
|
path_or_ext.remove_prefix( 1 );
|
149 |
|
path_or_ext.remove_prefix( 1 );
|
| 150 |
|
|
150 |
|
|
| 151 |
|
auto const ext = get_extension( path_or_ext );
|
151 |
|
auto const ext = get_extension( path_or_ext );
|
| 152 |
|
return lookup_ext( ext );
|
152 |
|
return lookup_ext( ext );
|
| 153 |
|
|
153 |
|
|
| 154 |
|
|
154 |
|
|
| 155 |
|
|
155 |
|
|
| 156 |
|
extension( core::string_view type ) noexcept
|
156 |
|
extension( core::string_view type ) noexcept
|
| 157 |
|
|
157 |
|
|
| 158 |
|
// Linear search for type -> extension
|
158 |
|
// Linear search for type -> extension
|
| 159 |
|
// Could optimize with reverse map if needed
|
159 |
|
// Could optimize with reverse map if needed
|
| 160 |
|
for( std::size_t i = 0; i < ext_db_size; ++i )
|
160 |
|
for( std::size_t i = 0; i < ext_db_size; ++i )
|
| 161 |
|
|
161 |
|
|
| 162 |
|
if( compare_icase( ext_db[i].type, type ) == 0 )
|
162 |
|
if( compare_icase( ext_db[i].type, type ) == 0 )
|
| 163 |
|
|
163 |
|
|
| 164 |
|
|
164 |
|
|
| 165 |
|
|
165 |
|
|
| 166 |
|
|
166 |
|
|
| 167 |
|
|
167 |
|
|
| 168 |
|
|
168 |
|
|
| 169 |
|
charset( core::string_view type ) noexcept
|
169 |
|
charset( core::string_view type ) noexcept
|
| 170 |
|
|
170 |
|
|
| 171 |
|
auto const* entry = mime_db::lookup( type );
|
171 |
|
auto const* entry = mime_db::lookup( type );
|
| 172 |
|
|
172 |
|
|
| 173 |
|
|
173 |
|
|
| 174 |
|
|
174 |
|
|
| 175 |
|
|
175 |
|
|
| 176 |
|
|
176 |
|
|
| 177 |
|
|
177 |
|
|
| 178 |
|
content_type( core::string_view type_or_ext )
|
178 |
|
content_type( core::string_view type_or_ext )
|
| 179 |
|
|
179 |
|
|
| 180 |
|
|
180 |
|
|
| 181 |
|
|
181 |
|
|
| 182 |
|
// Check if it looks like an extension
|
182 |
|
// Check if it looks like an extension
|
| 183 |
|
if( ! type_or_ext.empty() &&
|
183 |
|
if( ! type_or_ext.empty() &&
|
| 184 |
|
( type_or_ext[0] == '.' ||
|
184 |
|
( type_or_ext[0] == '.' ||
|
| 185 |
|
type_or_ext.find( '/' ) == core::string_view::npos ) )
|
185 |
|
type_or_ext.find( '/' ) == core::string_view::npos ) )
|
| 186 |
|
|
186 |
|
|
| 187 |
|
type = lookup( type_or_ext );
|
187 |
|
type = lookup( type_or_ext );
|
| 188 |
|
|
188 |
|
|
| 189 |
|
|
189 |
|
|
| 190 |
|
|
190 |
|
|
| 191 |
|
|
191 |
|
|
| 192 |
|
|
192 |
|
|
| 193 |
|
|
193 |
|
|
| 194 |
|
|
194 |
|
|
| 195 |
|
|
195 |
|
|
| 196 |
|
auto const cs = charset( type );
|
196 |
|
auto const cs = charset( type );
|
| 197 |
|
|
197 |
|
|
| 198 |
|
return std::string( type );
|
198 |
|
return std::string( type );
|
| 199 |
|
|
199 |
|
|
| 200 |
|
|
200 |
|
|
| 201 |
|
result.reserve( type.size() + 10 + cs.size() );
|
201 |
|
result.reserve( type.size() + 10 + cs.size() );
|
| 202 |
|
result.append( type.data(), type.size() );
|
202 |
|
result.append( type.data(), type.size() );
|
| 203 |
|
result.append( "; charset=" );
|
203 |
|
result.append( "; charset=" );
|
| 204 |
|
result.append( cs.data(), cs.size() );
|
204 |
|
result.append( cs.data(), cs.size() );
|
| 205 |
|
|
205 |
|
|
| 206 |
|
|
206 |
|
|
| 207 |
|
|
207 |
|
|
| 208 |
|
|
208 |
|
|
| 209 |
|
|
209 |
|
|
| 210 |
|
|
210 |
|
|