GCC Code Coverage Report


Directory: libs/http_proto/include/boost/http_proto/
File: boost/http_proto/service/impl/mime_types_service.ipp
Date: 2023-02-02 18:17:22
Exec Total Coverage
Lines: 0 26 0.0%
Functions: 0 5 0.0%
Branches: 0 18 0.0%

Line Branch Exec Source
1 //
2 // Copyright (c) 2021 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
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)
6 //
7 // Official repository: https://github.com/CPPAlliance/http_proto
8 //
9
10 #ifndef BOOST_HTTP_PROTO_SERVICE_IMPL_MIME_TYPES_SERVICE_IPP
11 #define BOOST_HTTP_PROTO_SERVICE_IMPL_MIME_TYPES_SERVICE_IPP
12
13 #include <boost/http_proto/service/mime_types_service.hpp>
14 #include <boost/http_proto/context.hpp>
15 #include <boost/url/grammar/ci_string.hpp>
16 #include <string>
17 #include <unordered_map>
18
19 namespace boost {
20 namespace http_proto {
21
22 namespace detail {
23
24 class mime_types_service_impl
25 : public mime_types_service
26 , public service
27 {
28 struct element
29 {
30 std::string value;
31 std::size_t prefix;
32 };
33
34 std::unordered_map<
35 std::string,
36 element,
37 grammar::ci_hash,
38 grammar::ci_equal> t_;
39
40 public:
41 using key_type = mime_types_service;
42
43 BOOST_STATIC_ASSERT(
44 std::is_same<
45 get_key_type<mime_types_service_impl>,
46 mime_types_service>::value);
47
48 struct param
49 {
50 string_view ext;
51 string_view type;
52 string_view subtype;
53 };
54
55 explicit
56 mime_types_service_impl(
57 context&) noexcept
58 {
59 init({
60 { "htm", "text", "html" },
61 { "html", "text", "html" },
62 { "php", "text", "html" },
63 { "css", "text", "css" },
64 { "txt", "text", "plain" },
65 { "js", "application", "javascript" },
66 { "json", "application", "json" },
67 { "xml", "application", "xml" },
68 { "swf", "application", "x-shockwave-flash" },
69 { "flv", "video", "x-flv" },
70 { "png", "image", "png" },
71 { "jpe", "image", "jpeg" },
72 { "jpeg", "image", "jpeg" },
73 { "jpg", "image", "jpeg" },
74 { "gif", "image", "gif" },
75 { "bmp", "image", "bmp" },
76 { "ico", "image", "vnd.microsoft.icon" },
77 { "tiff", "image", "tiff" },
78 { "tif", "image", "tiff" },
79 { "svg", "image", "svg+xml" },
80 { "svgz", "image", "svg+xml" },
81 //return "application/text" } // default
82 });
83 }
84
85 void
86 init(std::initializer_list<
87 param> init)
88 {
89 for(auto v : init)
90 insert(
91 v.ext,
92 v.type,
93 v.subtype);
94 }
95
96 void
97 insert(
98 string_view ext,
99 string_view type,
100 string_view subtype)
101 {
102 std::string s;
103 s.reserve(
104 type.size() + 1 +
105 subtype.size());
106 s.append(
107 type.data(),
108 type.size());
109 s.push_back('/');
110 s.append(
111 subtype.data(),
112 subtype.size());
113 t_.insert({
114 std::string(ext),
115 element {
116 std::move(s),
117 type.size() }});
118 }
119
120 mime_type
121 find(
122 string_view ext) const noexcept override
123 {
124 auto it = t_.find(ext);
125 if(it == t_.end())
126 return {};
127 string_view v = it->second.value;
128 return {
129 v,
130 v.substr(0, it->second.prefix),
131 v.substr(it->second.prefix + 1) };
132 }
133 };
134
135 } // detail
136
137 mime_types_service&
138 install_mime_types_service(
139 context& ctx)
140 {
141 return ctx.make_service<
142 detail::mime_types_service_impl>();
143 }
144
145 } // http_proto
146 } // boost
147
148 #endif
149