MySQL 9.1.0
Source Code Documentation
content_type.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQLROUTER_HTTP_CONTENT_TYPE_INCLUDED
27#define MYSQLROUTER_HTTP_CONTENT_TYPE_INCLUDED
28
29#include <algorithm>
30#include <array>
31#include <string>
32
33class MimeType {
34 public:
35 // RFC4329 deprecated text/javascript for application/javascript
36 static constexpr const char ApplicationJavascript[] =
37 "application/javascript";
38 static constexpr const char ApplicationJson[] = "application/json";
39 static constexpr const char ApplicationOctetStream[] =
40 "application/octet-stream";
41 static constexpr const char TextCss[] = "text/css";
42 static constexpr const char TextHtml[] = "text/html";
43 static constexpr const char ImageJpeg[] = "image/jpeg";
44 static constexpr const char ImagePng[] = "image/png";
45 static constexpr const char ImageSvgXML[] = "image/svg+xml";
46};
47
49 public:
50 /**
51 * get a mimetype for a file-extension.
52 *
53 * file-extension is matched case-insensitive
54 *
55 * returns 'application/octet-stream' in case no mapping is found
56 *
57 * @returns a mimetype for the extension
58 * @retval 'application/octet-stream' if no mapping is found
59 */
60 static const char *from_extension(std::string extension) {
61 // sorted list of extensions and their mapping to their mimetype
62 constexpr auto mimetypes =
63 std::to_array<std::pair<std::string_view, const char *>>({
64 {"css", MimeType::TextCss},
65 {"htm", MimeType::TextHtml},
66 {"html", MimeType::TextHtml},
67 {"jpeg", MimeType::ImageJpeg},
68 {"jpg", MimeType::ImageJpeg},
71 {"png", MimeType::ImagePng},
72 {"svg", MimeType::ImageSvgXML},
73 });
74
75 // lower-case file-extensions.
76 //
77 // Use ASCII only conversion as our map is ASCII too
78 //
79 // not using std::lolower() and friends as they are locale-aware which
80 // isn't wanted in this case.
82 extension.begin(), extension.end(), extension.begin(),
83 [](char c) { return (c >= 'A' && c <= 'Z') ? c + ('z' - 'Z') : c; });
84
85 const auto low_bound_it = std::lower_bound(
86 mimetypes.begin(), mimetypes.end(), extension,
87 [](const auto &a, const auto &_ext) { return a.first < _ext; });
88
89 // std::lower_bound() returns it to first element that's >= value
90 return (low_bound_it != mimetypes.end() && low_bound_it->first == extension)
91 ? low_bound_it->second
93 }
94};
95
96#endif
Definition: content_type.h:48
static const char * from_extension(std::string extension)
get a mimetype for a file-extension.
Definition: content_type.h:60
Definition: content_type.h:33
static constexpr const char ImageSvgXML[]
Definition: content_type.h:45
static constexpr const char ImageJpeg[]
Definition: content_type.h:43
static constexpr const char TextCss[]
Definition: content_type.h:41
static constexpr const char ImagePng[]
Definition: content_type.h:44
static constexpr const char ApplicationOctetStream[]
Definition: content_type.h:39
static constexpr const char TextHtml[]
Definition: content_type.h:42
static constexpr const char ApplicationJson[]
Definition: content_type.h:38
static constexpr const char ApplicationJavascript[]
Definition: content_type.h:36
bool transform(const dd::Spatial_reference_system *source_srs, const Geometry &in, const dd::Spatial_reference_system *target_srs, const char *func_name, std::unique_ptr< Geometry > *out) noexcept
Transforms a geometry from one SRS to another.
Definition: transform.cc:216