From bcd246076540a8353fa55fc0a5e19343c1a2dbc9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 14 Aug 2015 13:03:08 +0200 Subject: Implement package search service mockup --- brep/buildfile | 28 +++++-- brep/module | 2 + brep/module.cxx | 25 +++--- brep/options.cli | 34 ++++++-- brep/package-search | 32 ++++++++ brep/package-search.cxx | 154 ++++++++++++++++++++++++++++++++++++ brep/package-version-search | 32 ++++++++ brep/package-version-search.cxx | 73 +++++++++++++++++ brep/search | 32 -------- brep/search.cxx | 169 ---------------------------------------- brep/services.cxx | 26 ++++--- brep/shared-database | 4 +- brep/shared-database.cxx | 8 +- brep/view | 32 -------- brep/view.cxx | 131 ------------------------------- build.sh | 11 ++- etc/httpd.conf | 32 ++++---- etc/package-search.conf | 11 +++ etc/package-version-search.conf | 11 +++ etc/search.conf | 8 -- etc/view.conf | 5 -- tests/web/xhtml/driver.cxx | 28 +++++++ web/apache/request | 12 +-- web/apache/request.cxx | 125 +++++------------------------ web/mime-url-encoding | 26 +++++++ web/mime-url-encoding.cxx | 135 ++++++++++++++++++++++++++++++++ web/module | 12 +++ web/xhtml | 53 +++++++++---- www/htdocs/index.html | 31 -------- 29 files changed, 687 insertions(+), 595 deletions(-) create mode 100644 brep/package-search create mode 100644 brep/package-search.cxx create mode 100644 brep/package-version-search create mode 100644 brep/package-version-search.cxx delete mode 100644 brep/search delete mode 100644 brep/search.cxx delete mode 100644 brep/view delete mode 100644 brep/view.cxx create mode 100644 etc/package-search.conf create mode 100644 etc/package-version-search.conf delete mode 100644 etc/search.conf delete mode 100644 etc/view.conf create mode 100644 web/mime-url-encoding create mode 100644 web/mime-url-encoding.cxx delete mode 100644 www/htdocs/index.html diff --git a/brep/buildfile b/brep/buildfile index 5340d22..9b64ed4 100644 --- a/brep/buildfile +++ b/brep/buildfile @@ -6,20 +6,36 @@ using cli .: libso{brep brep-apache} -import libs += libbpkg%lib{bpkg} -import libs += libodb-pgsql%lib{odb-pgsql} +# brep library build rules. +# import libs += libodb%lib{odb} +import libs += libodb-pgsql%lib{odb-pgsql} +import libs += libbpkg%lib{bpkg} brep = cxx{package package-odb} + libso{brep}: $brep $libs libso{brep}: cxx.export.poptions = -I$out_root -I$src_root -brep = cxx{diagnostics module services search view shared-database} \ - cli.cxx{options} -web = ../web/apache/cxx{request service} +# brep-apache library build rules. +# +import libs += libstudxml%lib{studxml} + +brep = cxx{diagnostics module services package-search package-version-search \ + shared-database} cli.cxx{options} +web = ../web/apache/cxx{request service} ../web/cxx{mime-url-encoding} + libso{brep-apache}: $brep $web libso{brep} $libs +# Set option prefix to the empty value to handle all unknown request parameters +# uniformly with a single catch block. +# +# @@ Adding --option-prefix "" leads to appearing the following warnings +# related to options _parse methed: +# brep/options.cxx:663:33: warning: unused parameter ‘opt_mode’ [-Wunused-parameter] +# ::cli::unknown_mode opt_mode, +# cli.options += -I $src_root --include-with-brackets --include-prefix brep \ ---guard-prefix BREP --generate-file-scanner --suppress-usage +--guard-prefix BREP --generate-file-scanner --suppress-usage --option-prefix "" cli.cxx{options}: cli{options} diff --git a/brep/module b/brep/module index 08fbfea..0743598 100644 --- a/brep/module +++ b/brep/module @@ -92,6 +92,8 @@ namespace brep module (); module (const module& ); + // Can be used by module implementation to parse HTTP request parameters. + // class param_scanner: public cli::scanner { public: diff --git a/brep/module.cxx b/brep/module.cxx index c52d13a..62c70c9 100644 --- a/brep/module.cxx +++ b/brep/module.cxx @@ -22,6 +22,7 @@ using namespace std; using namespace placeholders; // For std::bind's _1, etc. +using namespace cli; namespace brep { @@ -98,22 +99,22 @@ namespace brep { // Read module implementation configuration. // - cli::argv_file_scanner s (0, - argc, - const_cast (argv.data ()), - "conf"); + argv_file_scanner s (0, + argc, + const_cast (argv.data ()), + "conf"); init (s); } // Read brep::module configuration. // - cli::argv_file_scanner s (0, - argc, - const_cast (argv.data ()), - "conf"); + argv_file_scanner s (0, + argc, + const_cast (argv.data ()), + "conf"); - options::module o (s, cli::unknown_mode::skip, cli::unknown_mode::skip); + options::module o (s, unknown_mode::skip, unknown_mode::skip); verb_ = o.verb (); } catch (const server_error& e) @@ -253,7 +254,7 @@ namespace brep if (i_ != name_values_.end ()) return name_ ? i_->name.c_str () : i_->value.c_str (); else - throw cli::eos_reached (); + throw eos_reached (); } const char* module::param_scanner:: @@ -270,7 +271,7 @@ namespace brep return r; } else - throw cli::eos_reached (); + throw eos_reached (); } void module::param_scanner:: @@ -284,6 +285,6 @@ namespace brep name_ = !name_; } else - throw cli::eos_reached (); + throw eos_reached (); } } diff --git a/brep/options.cli b/brep/options.cli index 2894979..8062f59 100644 --- a/brep/options.cli +++ b/brep/options.cli @@ -3,7 +3,7 @@ // license : MIT; see accompanying LICENSE file include ; -include ; +include ; // uint16_t namespace brep { @@ -22,23 +22,43 @@ namespace brep std::uint16_t db-port = 5432; }; - class search: module, db + class package_search: module, db { - size_t results-on-page = 10; + std::uint16_t results-on-page = 10; }; - class view: module, db + class package_version_search: module, db { + std::uint16_t results-on-page = 10; }; } - // Web module request parameters. + // Web module HTTP request parameters. // namespace params { - class search + // Use parameters long names in the C++ code, short aliases in HTTP URL. + // + class package_search { - size_t page = 0; + // Display package search result list starting from this page. + // + std::uint16_t page | p = 0; + + // Package search criteria. + // + std::string query | q = ""; + }; + + class package_version_search + { + // Display package version search result list starting from this page. + // + std::uint16_t page | p = 0; + + // Package version search criteria. + // + std::string query | q = ""; }; } } diff --git a/brep/package-search b/brep/package-search new file mode 100644 index 0000000..156ed68 --- /dev/null +++ b/brep/package-search @@ -0,0 +1,32 @@ +// file : brep/package-search -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BREP_PACKAGE_SEARCH +#define BREP_PACKAGE_SEARCH + +#include // shared_ptr + +#include // database + +#include +#include + +namespace brep +{ + class package_search: public module + { + private: + virtual void + handle (request&, response&); + + virtual void + init (cli::scanner&); + + private: + std::shared_ptr options_; + std::shared_ptr db_; + }; +} + +#endif // BREP_PACKAGE_SEARCH diff --git a/brep/package-search.cxx b/brep/package-search.cxx new file mode 100644 index 0000000..3317d43 --- /dev/null +++ b/brep/package-search.cxx @@ -0,0 +1,154 @@ +// file : brep/package-search.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include +#include // make_shared() + +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include + +using namespace std; +using namespace cli; +using namespace odb::core; + +namespace brep +{ + void package_search:: + init (scanner& s) + { + MODULE_DIAG; + + options_ = make_shared ( + s, unknown_mode::fail, unknown_mode::fail); + + db_ = shared_database (options_->db_host (), options_->db_port ()); + } + + void package_search:: + handle (request& rq, response& rs) + { + using namespace xml; + using namespace web; + using namespace web::xhtml; + + MODULE_DIAG; + + params::package_search pr; + + try + { + param_scanner s (rq.parameters ()); + pr = params::package_search (s, unknown_mode::fail, unknown_mode::fail); + } + catch (const unknown_argument& e) + { + throw invalid_request (400, e.what ()); + } + + // @@ Would be nice to have a manipulator identing string properly + // according to the most nested element identation. + // + const char* ident ("\n "); + const char* title ("Package Search"); + serializer s (rs.content (), title); + + s << HTML + << HEAD + << TITLE << title << ~TITLE + << STYLE(TYPE="text/css") << ident + << ".package {margin: 0 0 0.5em;}" << ident + << ".name a {text-decoration: none;}" << ident + << ".summary {font-size: small;}" + << ~STYLE + << ~HEAD + << BODY; + + string q ( + pr.query ().empty () ? "" : "q=" + mime_url_encode (pr.query ())); + + transaction t (db_->begin ()); + + // @@ Use appropriate view when clarify which package info to be displayed + // and search index structure get implemented. + // + using query = query; + auto r ( + db_->query ( + "ORDER BY" + query::name + + "OFFSET" + to_string (pr.page () * options_->results_on_page ()) + + "LIMIT" + to_string (options_->results_on_page ()))); + + for (const auto& p: r) + { + s << DIV(CLASS="package") + << DIV(CLASS="name") + << A + << HREF + << "/go/" << mime_url_encode (p.name); + + // Propagate search criteria to the package version search url. + // + if (!q.empty ()) + s << "?" << q; + + s << ~HREF + << p.name + << ~A + << ~DIV + << DIV(CLASS="summary") + << p.summary + << ~DIV + << ~DIV; + } + + t.commit (); + + if (pr.page () || r.size () == options_->results_on_page ()) + { + s << DIV; + + if (pr.page ()) + { + s << A + << HREF << "/?p=" << pr.page () - 1 + << (q.empty () ? "" : "&" + q) + << ~HREF + << "Previous" + << ~A + << " "; + } + + // @@ Not ideal as can produce link to an empty page, but easy to fix + // and most likelly will be replaced with something more meaningful + // based on knowing the total number of matched packages. + // + if (r.size () == options_->results_on_page ()) + { + s << A + << HREF << "/?p=" << pr.page () + 1 + << (q.empty () ? "" : "&" + q) + << ~HREF + << "Next" + << ~A; + } + + s << ~DIV; + } + + s << ~BODY + << ~HTML; + } +} diff --git a/brep/package-version-search b/brep/package-version-search new file mode 100644 index 0000000..0292ae0 --- /dev/null +++ b/brep/package-version-search @@ -0,0 +1,32 @@ +// file : brep/package-version-search -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BREP_PACKAGE_VERSION_SEARCH +#define BREP_PACKAGE_VERSION_SEARCH + +#include // shared_ptr + +#include // database + +#include +#include + +namespace brep +{ + class package_version_search: public module + { + private: + virtual void + handle (request&, response&); + + virtual void + init (cli::scanner&); + + private: + std::shared_ptr options_; + std::shared_ptr db_; + }; +} + +#endif // BREP_PACKAGE_VERSION_SEARCH diff --git a/brep/package-version-search.cxx b/brep/package-version-search.cxx new file mode 100644 index 0000000..a989434 --- /dev/null +++ b/brep/package-version-search.cxx @@ -0,0 +1,73 @@ +// file : brep/package-version-search.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include +#include // make_shared() + +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include + +using namespace std; +using namespace cli; +using namespace odb::core; + +namespace brep +{ + void package_version_search:: + init (scanner& s) + { + MODULE_DIAG; + + options_ = make_shared ( + s, unknown_mode::fail, unknown_mode::fail); + + db_ = shared_database (options_->db_host (), options_->db_port ()); + } + + void package_version_search:: + handle (request& rq, response& rs) + { + using namespace xml; + using namespace web; + using namespace web::xhtml; + + MODULE_DIAG; + + const string& name (*rq.path ().rbegin ()); + params::package_version_search pr; + + try + { + param_scanner s (rq.parameters ()); + pr = params::package_version_search ( + s, unknown_mode::fail, unknown_mode::fail); + } + catch (const unknown_argument& e) + { + throw invalid_request (400, e.what ()); + } + + const char* title ("Package"); + serializer s (rs.content (), title); + + s << HTML + << HEAD + << TITLE << title << ~TITLE + << ~HEAD + << BODY << name << ~BODY + << ~HTML; + } +} diff --git a/brep/search b/brep/search deleted file mode 100644 index 0283859..0000000 --- a/brep/search +++ /dev/null @@ -1,32 +0,0 @@ -// file : brep/search -*- C++ -*- -// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd -// license : MIT; see accompanying LICENSE file - -#ifndef BREP_SEARCH -#define BREP_SEARCH - -#include // shared_ptr - -#include - -#include -#include - -namespace brep -{ - class search: public module - { - private: - virtual void - handle (request&, response&); - - virtual void - init (cli::scanner&); - - private: - std::shared_ptr options_; - std::shared_ptr db_; - }; -} - -#endif // BREP_SEARCH diff --git a/brep/search.cxx b/brep/search.cxx deleted file mode 100644 index 9185aa5..0000000 --- a/brep/search.cxx +++ /dev/null @@ -1,169 +0,0 @@ -// file : brep/search.cxx -*- C++ -*- -// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd -// license : MIT; see accompanying LICENSE file - -#include - -#include // shared_ptr, make_shared() -#include -#include - -#include -#include -#include - -#include - -#include - -#include -#include -#include - -using namespace std; -using namespace odb::core; - -namespace brep -{ - void search:: - init (cli::scanner& s) - { - MODULE_DIAG; - - options_ = make_shared (s, - cli::unknown_mode::fail, - cli::unknown_mode::fail); - - db_ = shared_database (options_->db_host (), options_->db_port ()); - - if (options_->results_on_page () > 30) - fail << "too many search results on page: " - << options_->results_on_page (); - else if (options_->results_on_page () > 10) - warn << options_->results_on_page () - << " search results on page is quite a lot but will try to cope"; - } - - void search:: - handle (request& rq, response& rs) - { - MODULE_DIAG; - - shared_ptr cli ( - make_shared ("cli", - "CLI is ...", - strings ({"compiler", "c++"}), - string ("This is CLI"), - url (), - url (), - email (), - email ())); - - shared_ptr stable ( - make_shared ( - repository_location ("http://pkg.cpp.org/1/stable"), - "Stable", - dir_path ("/var/pkg/1/stable"))); - - licenses l; - l.comment = "License\"A'"; - l.push_back ("XXX"); - l.push_back ("AAA"); - l.push_back ("BBB"); - l.push_back ("CCC"); - - dependency_alternatives da; - da.push_back ( - {"icl", version_comparison{version ("1.3.3"), comparison::gt}}); - - da.push_back ( - {"ocl", version_comparison{version ("1.5.5"), comparison::lt}}); - - requirement_alternatives ra1; - ra1.push_back ("TAO"); - ra1.push_back ("ORBacus"); - - requirement_alternatives ra2; - ra2.push_back ("Xerces"); - - shared_ptr v ( - make_shared (stable, - cli, - version ("1.1"), - priority (), - license_alternatives ({l}), - "some changes 1\nsome changes 2", - dependencies ({da}), - requirements ({ra1, ra2}))); - - transaction t (db_->begin ()); -// t.tracer (odb::stderr_full_tracer); - - { - db_->persist (cli); - db_->persist (stable); - db_->persist (v); - } - - t.commit (); - - chrono::seconds ma (60); - rs.cookie ("Oh", " Ah\n\n", &ma, "/"); - rs.cookie ("Hm", ";Yes", &ma); - - info << "handling search request from "; // << rq.client_ip (); - - ostream& o (rs.content ()); - - o << ""; - - o << "Options:" - << "
\ntracing verbosity: " << options_->verb () - << "
\ndb endpoint: " << options_->db_host () << ":" - << options_->db_port () - << "
\nsearch results on page: " << options_->results_on_page (); - - o << "

\nParams:"; - - const name_values& ps (rq.parameters ()); - - if (ps.empty ()) - throw invalid_request (422, "search parameters expected"); - - if (ps.size () > 100) - fail << "too many parameters: " << ps.size () << - info << "are you crazy to specify so many?"; - - level2 ([&]{trace << "search request with " << ps.size () << " params";}); - - for (const auto& p: ps) - { - o << "
\n" << p.name << "=" << p.value; - } - - param_scanner s (ps); - unique_ptr prm; - - try - { - prm.reset (new params::search (s, - cli::unknown_mode::fail, - cli::unknown_mode::fail)); - } - catch (const cli::unknown_argument& e) - { - throw invalid_request (400, e.what ()); - } - - o << "
\nPage num: " << prm->page (); - o << "

\nCookies:"; - - for (const auto& c: rq.cookies ()) - { - o << "
\n" << c.name << "=" << c.value; - } - - o << "

View" - << ""; - } -} diff --git a/brep/services.cxx b/brep/services.cxx index 86b27b7..510a231 100644 --- a/brep/services.cxx +++ b/brep/services.cxx @@ -1,21 +1,25 @@ -// file : services.cxx -*- C++ -*- +// file : brep/services.cxx -*- C++ -*- // copyright : Copyright (c) 2014-2015 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file -#include -#include +#include // AP_MODULE_DECLARE_DATA #include +#include +#include + using namespace brep; using web::apache::service; -static search search_mod; -service AP_MODULE_DECLARE_DATA search_srv ("search", - search_mod, - {"db-host", "db-port", "conf"}); +static package_search package_search_mod; +service AP_MODULE_DECLARE_DATA package_search_srv ( + "package-search", + package_search_mod, + {"db-host", "db-port", "conf"}); -static view view_mod; -service AP_MODULE_DECLARE_DATA view_srv ("view", - view_mod, - {"db-host", "db-port", "conf"}); +static package_version_search package_version_search_mod; +service AP_MODULE_DECLARE_DATA package_version_search_srv ( + "package-version-search", + package_version_search_mod, + {"db-host", "db-port", "conf"}); diff --git a/brep/shared-database b/brep/shared-database index cad526f..36fa0a2 100644 --- a/brep/shared-database +++ b/brep/shared-database @@ -14,8 +14,8 @@ namespace brep { // Returns pointer to the shared database instance, creating one on the // first call. On subsequent calls ensures passed host and port equals - // to ones of the shared database instance throwing runtime_error otherwise. - // Is not thread-safe. + // to ones of the existing database instance throwing runtime_error + // otherwise. Is not thread-safe. // std::shared_ptr shared_database (const std::string& host, unsigned int port); diff --git a/brep/shared-database.cxx b/brep/shared-database.cxx index fb952d8..fc17659 100644 --- a/brep/shared-database.cxx +++ b/brep/shared-database.cxx @@ -10,14 +10,14 @@ #include using namespace std; -using namespace odb; namespace brep { - shared_ptr - shared_database (const string& host, unsigned int port) + shared_ptr + shared_database (const string& h, unsigned int p) { - static weak_ptr db; + using odb::pgsql::database; + static weak_ptr db; // In C++11, function-static variable initialization is // guaranteed to be thread-safe, thought this doesn't diff --git a/brep/view b/brep/view deleted file mode 100644 index 280b9ab..0000000 --- a/brep/view +++ /dev/null @@ -1,32 +0,0 @@ -// file : brep/view -*- C++ -*- -// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd -// license : MIT; see accompanying LICENSE file - -#ifndef BREP_VIEW -#define BREP_VIEW - -#include // shared_ptr - -#include - -#include -#include - -namespace brep -{ - class view: public module - { - private: - virtual void - handle (request&, response&); - - virtual void - init (cli::scanner&); - - private: - std::shared_ptr options_; - std::shared_ptr db_; - }; -} - -#endif // BREP_VIEW diff --git a/brep/view.cxx b/brep/view.cxx deleted file mode 100644 index 3098e93..0000000 --- a/brep/view.cxx +++ /dev/null @@ -1,131 +0,0 @@ -// file : brep/view.cxx -*- C++ -*- -// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd -// license : MIT; see accompanying LICENSE file - -#include - -#include // shared_ptr, make_shared() -#include - -#include -#include -#include - -#include - -#include -#include -#include - -using namespace std; -using namespace odb::core; - -#pragma db namespace session -namespace brep -{ - void view:: - init (cli::scanner& s) - { - options_ = make_shared (s, - cli::unknown_mode::fail, - cli::unknown_mode::fail); - - db_ = shared_database (options_->db_host (), options_->db_port ()); - } - - void view:: - handle (request& rq, response& rs) - { - session s; - transaction t (db_->begin ()); - - shared_ptr p (db_->load ("cli")); - - for (auto& vp: p->versions) - { - shared_ptr v (vp.load ()); - v->repository.load (); - v->package.load (); - } - - t.commit (); - - ostream& o (rs.content (200, "text/html;charset=utf-8", false)); - - o << ""; - - o << "Options:" - << "
\ntracing verbosity: " << options_->verb () - << "
\ndb endpoint: " << options_->db_host () << ":" - << options_->db_port (); - - o << "

\nCookies:"; - - for (const auto& c: rq.cookies ()) - { - o << "
\n" << c.name << "=" << c.value; - } - - o << "

\n" << p->name << ": " << p->versions.size (); - - for (const auto& vp: p->versions) - { - // Just finds package_version object in session cache. - // - shared_ptr v (vp.load ()); - - assert (v != nullptr); - assert (v->repository.get_eager () != nullptr); - assert (v->package.get_eager () != nullptr); - - o << "
version:" << v->version.string () - << "
package:" << v->package->name - << "
repo:" << v->repository->display_name - << "
changes:" << v->changes - << "
licenses:" << v->license_alternatives.size (); - - for (const auto& la: v->license_alternatives) - { - o << "
"; - - for (const auto& l: la) - { - o << " |" << l << "|"; - } - } - - o << "
deps:" << v->dependencies.size (); - - for (const auto& da: v->dependencies) - { - o << "
"; - - for (const auto& d: da) - { - o << " |" << d.package; - - if (d.version) - { - o << "," << d.version->value.string () << "," - << static_cast (d.version->operation) << "|"; - } - } - } - - o << "
requirements:" << v->requirements.size (); - - for (const auto& ra: v->requirements) - { - o << "
"; - - for (const auto& r: ra) - { - o << " |" << r << "|"; - } - } - } - - o << "

Search" - << ""; - } -} diff --git a/build.sh b/build.sh index 3d06a80..ae71e72 100755 --- a/build.sh +++ b/build.sh @@ -25,16 +25,19 @@ g++ -shared $DEBUG -std=c++11 -I.. -I../../libbpkg \ echo "cli brep-apache options" cli --include-with-brackets --include-prefix brep --hxx-suffix "" \ - --guard-prefix BREP --generate-file-scanner --suppress-usage ./options.cli + --guard-prefix BREP --generate-file-scanner --suppress-usage \ + --option-prefix "" ./options.cli echo "g++ libbrep-apache.so" -s="search.cxx view.cxx module.cxx diagnostics.cxx services.cxx options.cxx \ -shared-database.cxx ../web/apache/request.cxx ../web/apache/service.cxx" +s="package-search.cxx package-version-search.cxx module.cxx diagnostics.cxx \ +services.cxx options.cxx shared-database.cxx \ +../web/apache/request.cxx ../web/apache/service.cxx \ +../web/mime-url-encoding.cxx" g++ -shared $DEBUG -std=c++11 -I. -I/usr/include/apr-1 -I/usr/include/httpd \ -I.. -I../../libbpkg -I../../libbutl -L. -L../../libbpkg/bpkg \ - -fPIC -o libbrep-apache.so $s -lbrep -lbpkg -lodb-pgsql -lodb + -fPIC -o libbrep-apache.so $s -lbrep -lbpkg -lodb-pgsql -lodb -lstudxml cd ../loader diff --git a/etc/httpd.conf b/etc/httpd.conf index 71ac903..ef8cb50 100644 --- a/etc/httpd.conf +++ b/etc/httpd.conf @@ -40,30 +40,33 @@ LoadModule authz_host_module /usr/lib64/httpd/modules/mod_authz_host.so LoadModule expires_module /usr/lib64/httpd/modules/mod_expires.so LoadModule dir_module /usr/lib64/httpd/modules/mod_dir.so -LoadModule search_srv ${AP_MODULE_DIR}/libbrep-apache.so +LoadModule package_search_srv ${AP_MODULE_DIR}/libbrep-apache.so - - search-db-host ${AP_DB_HOST} - search-db-port ${AP_DB_PORT} - search-conf "${AP_CONFIG_DIR}/search.conf" + + package-search-db-host ${AP_DB_HOST} + package-search-db-port ${AP_DB_PORT} + package-search-conf "${AP_CONFIG_DIR}/package-search.conf" -LoadModule view_srv ${AP_MODULE_DIR}/libbrep-apache.so +LoadModule package_version_search_srv ${AP_MODULE_DIR}/libbrep-apache.so - - view-db-host ${AP_DB_HOST} - view-db-port ${AP_DB_PORT} - view-conf "${AP_CONFIG_DIR}/view.conf" + + package-version-search-db-host ${AP_DB_HOST} + package-version-search-db-port ${AP_DB_PORT} + package-version-search-conf "${AP_CONFIG_DIR}/package-version-search.conf" - - SetHandler search + + SetHandler package-search - - SetHandler view + + SetHandler package-version-search +# Location for package version details url +# + ExtendedStatus On @@ -81,3 +84,4 @@ ExtendedStatus On DirectoryIndex index.html TypesConfig /etc/mime.types +AddOutputFilterByType DEFLATE application/xhtml+xml diff --git a/etc/package-search.conf b/etc/package-search.conf new file mode 100644 index 0000000..f7eb07c --- /dev/null +++ b/etc/package-search.conf @@ -0,0 +1,11 @@ +# file : etc/package-search.conf +# copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file +# +# brep::module options +# +verb 1 + +# brep::package_search options +# +results-on-page 2 diff --git a/etc/package-version-search.conf b/etc/package-version-search.conf new file mode 100644 index 0000000..aa69170 --- /dev/null +++ b/etc/package-version-search.conf @@ -0,0 +1,11 @@ +# file : etc/package-version-search.conf +# copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file +# +# brep::module options +# +verb 1 + +# brep::package_version_search options +# +results-on-page 10 diff --git a/etc/search.conf b/etc/search.conf deleted file mode 100644 index be86098..0000000 --- a/etc/search.conf +++ /dev/null @@ -1,8 +0,0 @@ -# brep::module options - -verb 1 - -# brep::search options - -#ah 7 -results-on-page 20 diff --git a/etc/view.conf b/etc/view.conf deleted file mode 100644 index 7c9b163..0000000 --- a/etc/view.conf +++ /dev/null @@ -1,5 +0,0 @@ -# brep::module options - -verb 2 - -#oh 8 diff --git a/tests/web/xhtml/driver.cxx b/tests/web/xhtml/driver.cxx index 23531f7..9f393fd 100644 --- a/tests/web/xhtml/driver.cxx +++ b/tests/web/xhtml/driver.cxx @@ -3,6 +3,7 @@ // license : MIT; see accompanying LICENSE file #include +#include #include @@ -11,11 +12,38 @@ using namespace std; using namespace xml; +static bool +bad_sequence (const function& f) +{ + ostringstream o; + serializer s (o, "osstream"); + + try + { + f (s); + return false; + } + catch (const serialization&) + { + return true; + } +} + int main () { using namespace web::xhtml; + assert (bad_sequence ([](serializer& s) {s << HTML << ~HEAD;})); + assert (bad_sequence ([](serializer& s) {s << HTML << DIV << ~P << ~HTML;})); + assert (bad_sequence ([](serializer& s) {s << HTML << DIV << ~A << ~HTML;})); + assert (bad_sequence ([](serializer& s) {s << P << A << "a" << ~P << ~P;})); + assert (bad_sequence ([](serializer& s) {s << P << A << "a" << ~I << ~P;})); + + assert ( + bad_sequence ( + [](serializer& s) {s << P << A << ID << "A" << ~HREF << ~A << ~P;})); + serializer s (cout, "output"); s << HTML diff --git a/web/apache/request b/web/apache/request index 59d4600..88b38a9 100644 --- a/web/apache/request +++ b/web/apache/request @@ -43,6 +43,11 @@ namespace web int flush (); + // Get request path. + // + virtual const path_type& + path (); + // Get request body data stream. // virtual std::istream& @@ -93,12 +98,6 @@ namespace web void parse_parameters (const char* args); - static void - mime_url_encode (const char* v, std::ostream& o); - - static std::string - mime_url_decode (const char* b, const char* e, bool trim = false); - bool get_write_state () const noexcept {return write_state_;} @@ -129,6 +128,7 @@ namespace web std::unique_ptr out_; std::unique_ptr in_buf_; std::unique_ptr in_; + path_type path_; std::unique_ptr parameters_; std::unique_ptr cookies_; std::unique_ptr form_data_; diff --git a/web/apache/request.cxx b/web/apache/request.cxx index 7727b35..497d2d6 100644 --- a/web/apache/request.cxx +++ b/web/apache/request.cxx @@ -14,18 +14,20 @@ #include // unique_ptr #include #include +#include #include #include // move() #include #include +#include + using namespace std; namespace web { namespace apache { - istream& request:: content () { @@ -46,10 +48,25 @@ namespace web return *in_; } + const path& request:: + path () + { + if (path_.empty ()) + { + path_ = path_type (rec_->uri); + + // Module request handler can not be called if URI is empty. + // + assert (!path_.empty ()); + } + + return path_; + } + const name_values& request:: parameters () { - if (!parameters_) + if (parameters_ == nullptr) { parameters_.reset (new name_values ()); @@ -70,7 +87,7 @@ namespace web const name_values& request:: cookies () { - if (!cookies_) + if (cookies_ == nullptr) { cookies_.reset (new name_values ()); @@ -264,107 +281,5 @@ namespace web n = e ? e + 1 : 0; } } - - void request:: - mime_url_encode (const char* v, ostream& o) - { - char f (o.fill ()); - ios_base::fmtflags g (o.flags ()); - o << hex << uppercase << right << setfill ('0'); - - char c; - - while ((c = *v++) != '\0') - { - if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || - (c >= '0' && c <= '9')) - { - o << c; - } - else - switch (c) - { - case ' ': o << '+'; break; - case '.': - case '_': - case '-': - case '~': o << c; break; - default: - { - o << "%" << setw (2) << static_cast (c); - break; - } - } - } - - o.flags (g); - o.fill (f); - } - - string request:: - mime_url_decode (const char* b, const char* e, bool trim) - { - if (trim) - { - b += strspn (b, " "); - - if (b >= e) - return string (); - - while (*--e == ' '); - ++e; - } - - string value; - value.reserve (e - b); - - char bf[3]; - bf[2] = '\0'; - - while (b != e) - { - char c (*b++); - - switch (c) - { - case '+': - { - value.append (" "); - break; - } - case '%': - { - if (*b == '\0' || b[1] == '\0') - { - throw invalid_argument ( - "::web::apache::request::mime_url_decode short"); - } - - *bf = *b; - bf[1] = b[1]; - - char* ebf (nullptr); - size_t vl (strtoul (bf, &ebf, 16)); - - if (*ebf != '\0') - { - throw invalid_argument ( - "::web::apache::request::mime_url_decode wrong"); - } - - value.append (1, static_cast (vl)); - b += 2; - break; - } - default: - { - value.append (1, c); - break; - } - } - } - - return value; - } } } diff --git a/web/mime-url-encoding b/web/mime-url-encoding new file mode 100644 index 0000000..9ae5d6d --- /dev/null +++ b/web/mime-url-encoding @@ -0,0 +1,26 @@ +// file : web/mime-url-encoding -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef WEB_MIME_URL_ENCODING +#define WEB_MIME_URL_ENCODING + +#include +#include + +namespace web +{ + void + mime_url_encode (const char* v, std::ostream& o); + + std::string + mime_url_encode (const char* v); + + std::string + mime_url_encode (const std::string& v); + + std::string + mime_url_decode (const char* b, const char* e, bool trim = false); +} + +#endif // WEB_MIME_URL_ENCODING diff --git a/web/mime-url-encoding.cxx b/web/mime-url-encoding.cxx new file mode 100644 index 0000000..1d68bf3 --- /dev/null +++ b/web/mime-url-encoding.cxx @@ -0,0 +1,135 @@ +// file : web/mime-url-encoding.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include // hex, uppercase, right +#include +#include // setw(), setfill () +#include +#include +#include // size_t, strspn() +#include // invalid_argument + +using namespace std; + +namespace web +{ + // Encode characters different from unreserved ones specified in + // "2.3. Unreserved Characters" of http://tools.ietf.org/html/rfc3986. + // + void + mime_url_encode (const char* v, ostream& o) + { + char f (o.fill ()); + ostream::fmtflags g (o.flags ()); + o << hex << uppercase << right << setfill ('0'); + + char c; + while ((c = *v++) != '\0') + { + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || + (c >= '0' && c <= '9')) + { + o << c; + } + else + { + switch (c) + { + case ' ': o << '+'; break; + case '.': + case '_': + case '-': + case '~': o << c; break; + default: + { + o << "%" << setw (2) << static_cast (c); + break; + } + } + } + } + + o.flags (g); + o.fill (f); + } + + string + mime_url_encode (const char* v) + { + stringstream o; + mime_url_encode (v, o); + return o.str (); + } + + string + mime_url_encode (const string& v) + { + return mime_url_encode (v.c_str ()); + } + + string + mime_url_decode (const char* b, const char* e, bool trim) + { + if (trim) + { + b += strspn (b, " "); + + if (b >= e) + return string (); + + while (*--e == ' '); + ++e; + } + + string value; + value.reserve (e - b); + + char bf[3]; + bf[2] = '\0'; + + while (b != e) + { + char c (*b++); + switch (c) + { + case '+': + { + value.append (" "); + break; + } + case '%': + { + if (*b == '\0' || b[1] == '\0') + { + throw invalid_argument ("::web::mime_url_decode short"); + } + + *bf = *b; + bf[1] = b[1]; + + char* ebf (nullptr); + size_t vl (strtoul (bf, &ebf, 16)); + + if (*ebf != '\0') + { + throw invalid_argument ("::web::mime_url_decode wrong"); + } + + value.append (1, static_cast (vl)); + b += 2; + break; + } + default: + { + value.append (1, c); + break; + } + } + } + + return value; + } +} diff --git a/web/module b/web/module index e1cccf0..7398d45 100644 --- a/web/module +++ b/web/module @@ -13,6 +13,8 @@ #include // move() #include // runtime_error +#include + namespace web { // HTTP status code. @@ -68,10 +70,20 @@ namespace web }; using name_values = std::vector; + using path = butl::path; class request { public: + using path_type = web::path; + + // Corresponds to abs_path portion of HTTP URL as described in + // "3.2.2 HTTP URL" of http://tools.ietf.org/html/rfc2616. + // Returns '/' if no abs_path is present in URL. + // + virtual const path_type& + path () = 0; + //@@ Why not pass parameters directly? Lazy parsing? //@@ Why not have something like operator[] for lookup? Probably // in name_values. diff --git a/web/xhtml b/web/xhtml index b8d6ce0..4fc6119 100644 --- a/web/xhtml +++ b/web/xhtml @@ -5,6 +5,8 @@ #ifndef WEB_XHTML #define WEB_XHTML +#include // function + #include namespace web @@ -36,9 +38,9 @@ namespace web // namespace xhtml { - const char* xmlns = "http://www.w3.org/1999/xhtml"; + const char* const xmlns = "http://www.w3.org/1999/xhtml"; - using serializer_func = void (*) (xml::serializer&); + using serializer_func = std::function; struct attr_value_base { @@ -124,7 +126,10 @@ namespace web operator() (xml::serializer& s) const {s.start_element (xmlns, name);} virtual serializer_func - operator~ () const {return [](xml::serializer& s) {s.end_element ();};} + operator~ () const + { + return [this](xml::serializer& s) {s.end_element (xmlns, name);}; + } // s << elem(attr1 = 123, attr2 = "abc"); // @@ -143,8 +148,11 @@ namespace web return attr_element (*this, a1); } - protected: - element () = default; +// @@ Now always need to provide element name, so operator~ () could create +// the lambda capable to pass a valid name to end_element call. +// +// protected: +// element () = default; }; struct inline_element: element @@ -162,9 +170,9 @@ namespace web virtual serializer_func operator~ () const { - return [](xml::serializer& s) + return [this](xml::serializer& s) { - s.end_element (); s.resume_indentation (); + s.end_element (xmlns, name); s.resume_indentation (); }; } }; @@ -194,7 +202,10 @@ namespace web operator() (xml::serializer& s) const {s.start_attribute (name);} virtual serializer_func - operator~ () const {return [](xml::serializer& s) {s.end_attribute ();};} + operator~ () const + { + return [this](xml::serializer& s) {s.end_attribute (name);}; + } }; // Elements. @@ -206,13 +217,13 @@ namespace web // struct html_element: element { - html_element () {} // Uninitialized const static. + html_element (): element ("html") {} virtual void operator() (xml::serializer& s) const { s.doctype_decl ("html"); - s.start_element (xmlns, "html"); + s.start_element (xmlns, name); s.namespace_decl (xmlns, ""); } }; @@ -220,12 +231,12 @@ namespace web struct head_element: element { - head_element () {} // Uninitialized const static. + head_element (): element ("head") {} virtual void operator() (xml::serializer& s) const { - s.start_element (xmlns, "head"); + s.start_element (xmlns, name); s.start_element (xmlns, "meta"); s.attribute ("charset", "UTF-8"); s.end_element (); @@ -233,10 +244,13 @@ namespace web }; static const head_element HEAD; + static const element BODY ("body"); + static const element DIV ("div"); + static const element P ("p"); + static const element STYLE ("style"); static const element TITLE ("title"); - static const element BODY ("body"); - static const element P ("p"); + static const inline_element A ("a"); static const inline_element B ("b"); static const inline_element I ("i"); static const inline_element U ("u"); @@ -246,9 +260,16 @@ namespace web // Attributes. // - static const attribute ID ("id"); static const attribute CLASS ("class"); - static const attribute STYLE ("style"); + static const attribute HREF ("href"); + static const attribute ID ("id"); + static const attribute TYPE ("type"); + +// @@ Attribute variable names clash with element variable names. +// Should we prefix/suffix attribute names like _STYLE, STYLE_ or there +// are some better ideas ? +// +// static const attribute STYLE ("style"); } } diff --git a/www/htdocs/index.html b/www/htdocs/index.html deleted file mode 100644 index d8c1185..0000000 --- a/www/htdocs/index.html +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - -

Home Page

- build2 is a build system aimed at improving cross-platform C++ software building and library use. It will be accompanied by a C++ package manager and a central repository of open source C++ libraries. - build2 is a build system aimed build2 is a build system aimed at improving cross-platform C++ software building and library use. It will be accompanied by a C++ package manager and a central repository of open source C++ libraries. - build2 is a build system aimed at improving cross-platform C++ at improving cross-platform C++ at improving cross-platform C++ software building and library use. It will be accompanied by a C++ package manager and a central repository of open source C++ libraries. - build2 is a build system aimed at ddw improving cross-platform C++ software building and library use. It will be accompanied by a C++ package manager and a central repository of open source C++ libraries. - build2 is a build system aimed at improving cross-platform C++ software building and library use. It will be accompanied by a C++ package manager and a central repository of open source C++ libraries. - build2 is a build system aimed at improving cross-platform C++ software building and library use. It will be accompanied by a C++ package manager and a central repository of open source C++ libraries. - build2 is a build sdhjsd system aimed at improving cross-platform C++ software building and library use. It will be accompanied by a C++ package manager and a central repository of open source C++ libraries. - build2 is a build system aimed at rkdf improving cross-platform C++ software building and library use. It will be accompanied by a C++ package manager and a central repository of open source C++ libraries. - - -- cgit v1.1