From bed7ade3fa57ef3a7d4e8bd99c35aecb1f414bed Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 16 Mar 2017 12:51:24 +0300 Subject: Fix brep module to properly handle request parameters parsing exceptions --- mod/mod-package-details.cxx | 29 ++++++++++--------- mod/mod-package-search.cxx | 14 +++++----- mod/mod-package-version-details.cxx | 17 ++++++------ mod/mod-repository-details.cxx | 2 +- mod/mod-repository-root.cxx | 55 +++++++++++++++++++------------------ 5 files changed, 58 insertions(+), 59 deletions(-) diff --git a/mod/mod-package-details.cxx b/mod/mod-package-details.cxx index 3a7b31c..b533454 100644 --- a/mod/mod-package-details.cxx +++ b/mod/mod-package-details.cxx @@ -97,21 +97,20 @@ handle (request& rq, response& rs) size_t page (params.page ()); const string& squery (params.query ()); - auto url ( - [&ename](bool f = false, - const string& q = "", - size_t p = 0, - const string& a = "") -> string - { - string s ("?"); - string u (ename); - - if (f) { u += "?f=full"; s = "&"; } - if (!q.empty ()) { u += s + "q=" + mime_url_encode (q); s = "&"; } - if (p > 0) { u += s + "p=" + to_string (p); s = "&"; } - if (!a.empty ()) { u += '#' + a; } - return u; - }); + auto url = [&ename] (bool f = false, + const string& q = "", + size_t p = 0, + const string& a = "") -> string + { + string s ("?"); + string u (ename); + + if (f) { u += "?f=full"; s = "&"; } + if (!q.empty ()) { u += s + "q=" + mime_url_encode (q); s = "&"; } + if (p > 0) { u += s + "p=" + to_string (p); s = "&"; } + if (!a.empty ()) { u += '#' + a; } + return u; + }; xml::serializer s (rs.content (), name); diff --git a/mod/mod-package-search.cxx b/mod/mod-package-search.cxx index 0fecc0a..6f13eb2 100644 --- a/mod/mod-package-search.cxx +++ b/mod/mod-package-search.cxx @@ -96,7 +96,7 @@ handle (request& rq, response& rs) params = params::package_search ( s, unknown_mode::fail, unknown_mode::fail); } - catch (const unknown_argument& e) + catch (const cli::exception& e) { throw invalid_request (400, e.what ()); } @@ -146,12 +146,12 @@ handle (request& rq, response& rs) // Enclose the subsequent tables to be able to use nth-child CSS selector. // s << DIV; - for (const auto& pr: - db_->query ( - search_param (squery) + - "ORDER BY rank DESC, name" + - "OFFSET" + to_string (page * res_page) + - "LIMIT" + to_string (res_page))) + for (const auto& pr: + db_->query ( + search_param (squery) + + "ORDER BY rank DESC, name" + + "OFFSET" + to_string (page * res_page) + + "LIMIT" + to_string (res_page))) { shared_ptr p (db_->load (pr.id)); diff --git a/mod/mod-package-version-details.cxx b/mod/mod-package-version-details.cxx index 796f9e6..0da9678 100644 --- a/mod/mod-package-version-details.cxx +++ b/mod/mod-package-version-details.cxx @@ -89,20 +89,19 @@ handle (request& rq, response& rs) full = params.form () == page_form::full; } - catch (const unknown_argument& e) + catch (const cli::exception& e) { throw invalid_request (400, e.what ()); } - auto url ( - [&sver](bool f = false, const string& a = "") -> string - { - string u (sver); + auto url = [&sver] (bool f = false, const string& a = "") -> string + { + string u (sver); - if (f) { u += "?f=full"; } - if (!a.empty ()) { u += '#' + a; } - return u; - }); + if (f) { u += "?f=full"; } + if (!a.empty ()) { u += '#' + a; } + return u; + }; const string title (name + " " + sver); xml::serializer s (rs.content (), title); diff --git a/mod/mod-repository-details.cxx b/mod/mod-repository-details.cxx index 8f434ef..410c143 100644 --- a/mod/mod-repository-details.cxx +++ b/mod/mod-repository-details.cxx @@ -74,7 +74,7 @@ handle (request& rq, response& rs) name_value_scanner s (rq.parameters ()); params::repository_details (s, unknown_mode::fail, unknown_mode::fail); } - catch (const unknown_argument& e) + catch (const cli::exception& e) { throw invalid_request (400, e.what ()); } diff --git a/mod/mod-repository-root.cxx b/mod/mod-repository-root.cxx index 84c1ed1..5bb5b69 100644 --- a/mod/mod-repository-root.cxx +++ b/mod/mod-repository-root.cxx @@ -154,34 +154,35 @@ namespace brep // Delegate the request handling to the sub-module. Intercept exception // handling to add sub-module attribution. // - auto handle = [&rs, this](module& m, request& rq, const char* name) -> bool + auto handle = + [&rs, this] (module& m, request& rq, const char* name) -> bool + { + try { - try - { - return m.handle (rq, rs, *log_); - } - catch (const invalid_request&) - { - // Preserve invalid_request exception type, so the web server can - // properly respond to the client with a 4XX error code. - // - throw; - } - catch (const std::exception& e) - { - // All exception types inherited from std::exception (and different - // from invalid_request) are handled by the web server as - // std::exception. The only sensible way to handle them is to respond - // to the client with the internal server error (500) code. By that - // reason it is valid to reduce all these types to a single one. - // Note that the server_error exception is handled internally by the - // module::handle() function call. - // - ostringstream os; - os << name << ": " << e; - throw runtime_error (os.str ()); - } - }; + return m.handle (rq, rs, *log_); + } + catch (const invalid_request&) + { + // Preserve invalid_request exception type, so the web server can + // properly respond to the client with a 4XX error code. + // + throw; + } + catch (const std::exception& e) + { + // All exception types inherited from std::exception (and different + // from invalid_request) are handled by the web server as + // std::exception. The only sensible way to handle them is to respond + // to the client with the internal server error (500) code. By that + // reason it is valid to reduce all these types to a single one. Note + // that the server_error exception is handled internally by the + // module::handle() function call. + // + ostringstream os; + os << name << ": " << e; + throw runtime_error (os.str ()); + } + }; if (lpath.empty ()) { -- cgit v1.1