From 7ffdfcff4f0c5b7ab5699a448353a21532563b80 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Fri, 15 Oct 2021 11:47:07 +0200 Subject: httpasio: Implemented support for specifying accept type via url suffix --- zenhttp/httpasio.cpp | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/zenhttp/httpasio.cpp b/zenhttp/httpasio.cpp index 1f049b0c1..12fbdb61e 100644 --- a/zenhttp/httpasio.cpp +++ b/zenhttp/httpasio.cpp @@ -930,12 +930,46 @@ HttpAsioServerRequest::HttpAsioServerRequest(asio_http::HttpRequest& Request, Ht m_ContentLength = Request.Body().Size(); m_ContentType = Request.ContentType(); + HttpContentType AcceptContentType = HttpContentType::kUnknownContentType; + + // Parse any extension, to allow requesting a particular response encoding via the URL + + { + std::string_view UriSuffix8{m_Uri}; + + const size_t LastComponentIndex = UriSuffix8.find_last_of('/'); + + if (LastComponentIndex != std::string_view::npos) + { + UriSuffix8.remove_prefix(LastComponentIndex); + } + + const size_t LastDotIndex = UriSuffix8.find_last_of('.'); + + if (LastDotIndex != std::string_view::npos) + { + UriSuffix8.remove_prefix(LastDotIndex + 1); + + AcceptContentType = ParseContentType(UriSuffix8); + + if (AcceptContentType != HttpContentType::kUnknownContentType) + { + m_Uri.remove_suffix(uint32_t(m_Uri.size() - LastComponentIndex - LastDotIndex - 1)); + } + } + } + // It an explicit content type extension was specified then we'll use that over any // Accept: header value that may be present - // - // TODO! - m_AcceptType = Request.AcceptType(); + if (AcceptContentType != HttpContentType::kUnknownContentType) + { + m_AcceptType = AcceptContentType; + } + else + { + m_AcceptType = Request.AcceptType(); + } } HttpAsioServerRequest::~HttpAsioServerRequest() -- cgit v1.2.3