Skip to content

Commit 054f621

Browse files
committedMar 19, 2024
Change mime type to enum class
1 parent 2431331 commit 054f621

15 files changed

+42
-43
lines changed
 

‎include/cgimap/handler.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class handler {
6161
http::method::HEAD |
6262
http::method::OPTIONS;
6363

64-
handler(mime::type default_type = mime::unspecified_type,
64+
handler(mime::type default_type = mime::type::unspecified_type,
6565
http::method methods = default_methods);
6666
virtual ~handler() = default;
6767

@@ -90,7 +90,7 @@ using handler_ptr_t = std::unique_ptr<handler>;
9090

9191
class payload_enabled_handler : public handler {
9292
public:
93-
payload_enabled_handler(mime::type default_type = mime::unspecified_type,
93+
payload_enabled_handler(mime::type default_type = mime::type::unspecified_type,
9494
http::method methods = http::method::POST | http::method::OPTIONS);
9595

9696
// Responder used to update the database

‎include/cgimap/mime_types.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* simple set of supported mime types.
1717
*/
1818
namespace mime {
19-
enum type {
19+
enum class type {
2020
unspecified_type, // a "null" type, used to indicate no choice.
2121
text_plain,
2222
application_xml,

‎src/api06/changeset_close_handler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ changeset_close_responder::changeset_close_responder(mime::type mt,
4040

4141
changeset_close_handler::changeset_close_handler(const request &,
4242
osm_changeset_id_t id)
43-
: payload_enabled_handler(mime::text_plain,
43+
: payload_enabled_handler(mime::type::text_plain,
4444
http::method::PUT | http::method::OPTIONS),
4545
id(id) {}
4646

‎src/api06/changeset_create_handler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ changeset_create_responder::changeset_create_responder(mime::type mt,
4242
}
4343

4444
changeset_create_handler::changeset_create_handler(const request &)
45-
: payload_enabled_handler(mime::text_plain,
45+
: payload_enabled_handler(mime::type::text_plain,
4646
http::method::PUT | http::method::OPTIONS) {}
4747

4848
std::string changeset_create_handler::log_name() const {

‎src/api06/changeset_update_handler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ changeset_update_sel_responder::changeset_update_sel_responder(
5454

5555

5656
changeset_update_handler::changeset_update_handler(const request &req, osm_changeset_id_t id)
57-
: payload_enabled_handler(mime::application_xml,
57+
: payload_enabled_handler(mime::type::application_xml,
5858
http::method::PUT | http::method::OPTIONS),
5959
id(id) {}
6060

‎src/api06/changeset_upload_handler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ changeset_upload_responder::changeset_upload_responder(mime::type mt,
7575

7676
changeset_upload_handler::changeset_upload_handler(const request &,
7777
osm_changeset_id_t id)
78-
: payload_enabled_handler(mime::unspecified_type,
78+
: payload_enabled_handler(mime::type::unspecified_type,
7979
http::method::POST | http::method::OPTIONS),
8080
id(id) {}
8181

‎src/choose_formatter.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class acceptable_types {
5454
public:
5555
explicit acceptable_types(const std::string &accept_header);
5656
bool is_acceptable(mime::type) const;
57-
// note: returns mime::unspecified_type if none were acceptable
57+
// note: returns mime::type::unspecified_type if none were acceptable
5858
mime::type most_acceptable_of(const std::vector<mime::type> &available) const;
5959

6060
private:
@@ -121,10 +121,10 @@ struct http_accept_grammar
121121
};
122122
/*
123123
= lit("* / *") [_val = mime::any_type]
124-
| lit("text/xml") [_val = mime::application_xml]
125-
| lit("application/xml") [_val = mime::application_xml]
124+
| lit("text/xml") [_val = mime::type::application_xml]
125+
| lit("application/xml") [_val = mime::type::application_xml]
126126
#if HAVE_YAJL
127-
| lit("application/json")[_val = mime::application_json]
127+
| lit("application/json")[_val = mime::type::application_json]
128128
#endif
129129
;
130130
*/
@@ -144,7 +144,7 @@ acceptable_types::acceptable_types(const std::string &accept_header) {
144144
for (media_range range : ranges) {
145145
// figure out the mime::type from the string.
146146
mime::type mime_type = mime::parse_from(range.mime_type);
147-
if (mime_type == mime::unspecified_type) {
147+
if (mime_type == mime::type::unspecified_type) {
148148
// if it's unknown then skip this type...
149149
continue;
150150
}
@@ -174,7 +174,7 @@ bool acceptable_types::is_acceptable(mime::type mt) const {
174174
}
175175

176176
mime::type acceptable_types::most_acceptable_of(const std::vector<mime::type> &available) const {
177-
mime::type best = mime::unspecified_type;
177+
mime::type best = mime::type::unspecified_type;
178178
float score = std::numeric_limits<float>::min();
179179
for (mime::type type : available) {
180180
auto itr = mapping.find(type);
@@ -188,7 +188,7 @@ mime::type acceptable_types::most_acceptable_of(const std::vector<mime::type> &a
188188

189189
// also check the full wildcard.
190190
if (!available.empty()) {
191-
auto itr = mapping.find(mime::any_type);
191+
auto itr = mapping.find(mime::type::any_type);
192192
if ((itr != mapping.end()) && (itr->second > score)) {
193193
best = available.front();
194194
}
@@ -230,7 +230,7 @@ mime::type choose_best_mime_type(const request &req, const responder& hptr) {
230230
mime::type best_type = hptr.resource_type();
231231
// check if the handler is capable of supporting an acceptable set of mime
232232
// types.
233-
if (best_type != mime::unspecified_type) {
233+
if (best_type != mime::type::unspecified_type) {
234234
// check that this doesn't conflict with anything in the Accept header.
235235
if (!hptr.is_available(best_type))
236236
throw http::not_acceptable(fmt::format("Acceptable formats for {} are: {}",
@@ -243,11 +243,11 @@ mime::type choose_best_mime_type(const request &req, const responder& hptr) {
243243
} else {
244244
best_type = types.most_acceptable_of(types_available);
245245
// if none were acceptable then...
246-
if (best_type == mime::unspecified_type) {
246+
if (best_type == mime::type::unspecified_type) {
247247
throw http::not_acceptable(fmt::format("Acceptable formats for {} are: {}",
248248
get_request_path(req),
249249
mime_types_to_string(types_available)));
250-
} else if (best_type == mime::any_type) {
250+
} else if (best_type == mime::type::any_type) {
251251
// choose the first of the available types if nothing is preferred.
252252
best_type = *(hptr.types_available().begin());
253253
}
@@ -260,14 +260,14 @@ mime::type choose_best_mime_type(const request &req, const responder& hptr) {
260260
std::unique_ptr<output_formatter> create_formatter(mime::type best_type, output_buffer& out) {
261261

262262
switch (best_type) {
263-
case mime::application_xml:
263+
case mime::type::application_xml:
264264
return std::make_unique<xml_formatter>(std::make_unique<xml_writer>(out, true));
265265

266266
#if HAVE_YAJL
267-
case mime::application_json:
267+
case mime::type::application_json:
268268
return std::make_unique<json_formatter>(std::make_unique<json_writer>(out, false));
269269
#endif
270-
case mime::text_plain:
270+
case mime::type::text_plain:
271271
return std::make_unique<text_formatter>(std::make_unique<text_writer>(out, true));
272272

273273
default:

‎src/json_formatter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ json_formatter::json_formatter(std::unique_ptr<json_writer> w) : writer(std::mov
1515

1616
json_formatter::~json_formatter() = default;
1717

18-
mime::type json_formatter::mime_type() const { return mime::application_json; }
18+
mime::type json_formatter::mime_type() const { return mime::type::application_json; }
1919

2020
void json_formatter::write_tags(const tags_t &tags) {
2121

‎src/mime_types.cpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ using std::runtime_error;
1515

1616
namespace mime {
1717
string to_string(type t) {
18-
if (any_type == t) {
18+
if (mime::type::any_type == t) {
1919
return "*/*";
20-
} else if (text_plain == t) {
20+
} else if (mime::type::text_plain == t) {
2121
return "text/plain";
22-
} else if (application_xml == t) {
22+
} else if (mime::type::application_xml == t) {
2323
return "application/xml";
2424
#if HAVE_YAJL
25-
} else if (application_json == t) {
25+
} else if (mime::type::application_json == t) {
2626
return "application/json";
2727
#endif
2828
} else {
@@ -31,26 +31,25 @@ string to_string(type t) {
3131
}
3232

3333
type parse_from(const std::string &name) {
34-
type t = unspecified_type;
3534

3635
if (name == "*") {
37-
t = any_type;
36+
return mime::type::any_type;
3837
} else if (name == "*/*") {
39-
t = any_type;
38+
return mime::type::any_type;
4039
} else if (name == "text/*") {
41-
t = any_type;
40+
return mime::type::any_type;
4241
} else if (name == "text/plain") {
43-
t = text_plain;
42+
return mime::type::text_plain;
4443
} else if (name == "text/xml") { // alias according to RFC 7303, section 9.2
45-
t = application_xml;
44+
return mime::type:: application_xml;
4645
} else if (name == "application/xml") {
47-
t = application_xml;
46+
return mime::type::application_xml;
4847
#if HAVE_YAJL
4948
} else if (name == "application/json") {
50-
t = application_json;
49+
return mime::type::application_json;
5150
#endif
5251
}
5352

54-
return t;
53+
return mime::type::unspecified_type;
5554
}
5655
}

‎src/osm_responder.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ osm_responder::osm_responder(mime::type mt, std::optional<bbox> b)
1414

1515
std::vector<mime::type> osm_responder::types_available() const {
1616
std::vector<mime::type> types;
17-
types.push_back(mime::application_xml);
17+
types.push_back(mime::type::application_xml);
1818
#if HAVE_YAJL
19-
types.push_back(mime::application_json);
19+
types.push_back(mime::type::application_json);
2020
#endif
2121
return types;
2222
}

‎src/osmchange_responder.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ osmchange_responder::osmchange_responder(mime::type mt, data_selection &s)
227227

228228
std::vector<mime::type> osmchange_responder::types_available() const {
229229
std::vector<mime::type> types; // TODO: don't reconstruct on every call
230-
types.push_back(mime::application_xml);
230+
types.push_back(mime::type::application_xml);
231231
return types;
232232
}
233233

‎src/routes.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ namespace {
297297
std::size_t json_found = path.rfind(".json");
298298

299299
if (json_found != string::npos && json_found == path.length() - 5) {
300-
return make_pair(path.substr(0, json_found), mime::application_json);
300+
return make_pair(path.substr(0, json_found), mime::type::application_json);
301301
}
302302
}
303303
#endif
@@ -306,11 +306,11 @@ namespace {
306306
std::size_t xml_found = path.rfind(".xml");
307307

308308
if (xml_found != string::npos && xml_found == path.length() - 4) {
309-
return make_pair(path.substr(0, xml_found), mime::application_xml);
309+
return make_pair(path.substr(0, xml_found), mime::type::application_xml);
310310
}
311311
}
312312

313-
return make_pair(path, mime::unspecified_type);
313+
return make_pair(path, mime::type::unspecified_type);
314314
}
315315

316316
handler_ptr_t route_resource(request &req, const string &path,

‎src/text_formatter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using std::transform;
1818

1919
text_formatter::text_formatter(std::unique_ptr<text_writer> w) : writer(std::move(w)) {}
2020

21-
mime::type text_formatter::mime_type() const { return mime::text_plain; }
21+
mime::type text_formatter::mime_type() const { return mime::type::text_plain; }
2222

2323
void text_formatter::start_document(
2424
const std::string &generator, const std::string &root_name) {

‎src/text_responder.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ text_responder::text_responder(mime::type mt)
1414

1515
std::vector<mime::type> text_responder::types_available() const {
1616
std::vector<mime::type> types;
17-
types.push_back(mime::text_plain);
17+
types.push_back(mime::type::text_plain);
1818
return types;
1919
}
2020

‎src/xml_formatter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using std::transform;
1717

1818
xml_formatter::xml_formatter(std::unique_ptr<xml_writer> w) : writer(std::move(w)) {}
1919

20-
mime::type xml_formatter::mime_type() const { return mime::application_xml; }
20+
mime::type xml_formatter::mime_type() const { return mime::type::application_xml; }
2121

2222
void xml_formatter::start_document(
2323
const std::string &generator, const std::string &root_name) {

0 commit comments

Comments
 (0)