summaryrefslogtreecommitdiff
path: root/http/http_connection.hpp
diff options
context:
space:
mode:
authorEd Tanous <edtanous@google.com>2022-08-08 02:42:51 +0300
committerEd Tanous <ed@tanous.net>2022-09-13 01:15:45 +0300
commit99351cd856038475cac146029e5db03767a1459c (patch)
treee7a493c416851de342a71f06fe0d71f3afae5c4b /http/http_connection.hpp
parent3d36e3a514b0cfafcd00edc880b83d0eae87fed7 (diff)
downloadbmcweb-99351cd856038475cac146029e5db03767a1459c.tar.xz
Improve content type
We have a number of specialized content-type functions for varying levels of degree, and most of them rely on quite a few strings. This commit changes them to consolidate on two APIs. isContentTypeSupported, which as the name implies, takes a single content type, and returns a bool about whether or not that content type is allowed. getPreferedContentType, which takes an array of multiple options, and fine the first one in the list that matches the clients expected string. These two functions makes these functions more able to be reused in the future, and don't require specialized entries for each possible type or combination of types that we need to check for. Tested: Unit tests passing. Pretty good coverage. Signed-off-by: Ed Tanous <edtanous@google.com> Change-Id: I8b976d0cefec5f24e62fbbfae33d12cc803cb373
Diffstat (limited to 'http/http_connection.hpp')
-rw-r--r--http/http_connection.hpp11
1 files changed, 10 insertions, 1 deletions
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index 6494586231..376eeec73d 100644
--- a/http/http_connection.hpp
+++ b/http/http_connection.hpp
@@ -454,12 +454,21 @@ class Connection :
}
if (res.body().empty() && !res.jsonValue.empty())
{
- if (http_helpers::requestPrefersHtml(req->getHeaderValue("Accept")))
+ using http_helpers::ContentType;
+ std::array<ContentType, 2> allowed{ContentType::HTML,
+ ContentType::JSON};
+ ContentType prefered =
+ getPreferedContentType(req->getHeaderValue("Accept"), allowed);
+
+ if (prefered == ContentType::HTML)
{
prettyPrintJson(res);
}
else
{
+ // Technically prefered could also be NoMatch here, but we'd
+ // like to default to something rather than return 400 for
+ // backward compatibility.
res.addHeader(boost::beast::http::field::content_type,
"application/json");
res.body() = res.jsonValue.dump(