diff --git a/sources/core/src/Http.h b/sources/core/src/Http.h index 1d2f0bb..fd781c7 100644 --- a/sources/core/src/Http.h +++ b/sources/core/src/Http.h @@ -79,15 +79,18 @@ namespace http { data += std::to_string(response.status_code) + " "; data += response.status_message + "\n"; - for (auto &it: response.headers) - data += (it.first + ": " + it.second + "\n"); + bool first = true; + for (auto &it: response.headers) { + if (!first) data += "\n"; + data += (it.first + ": " + it.second); + if (first) first = false; + } data += headers_end_delimiter; + buffer.insert(buffer.end(), data.begin(), data.end()); if (!response.body.empty()) - data.insert(data.end(), response.body.begin(), response.body.end()); - - buffer.insert(buffer.end(), data.begin(), data.end()); + buffer.insert(buffer.end(), response.body.begin(), response.body.end()); } } diff --git a/sources/modules/php/dist/sample/content/404.phtml b/sources/modules/php/dist/sample/content/404.phtml new file mode 100644 index 0000000..70b9ea7 --- /dev/null +++ b/sources/modules/php/dist/sample/content/404.phtml @@ -0,0 +1 @@ +

404 - This page does not exist.

\ No newline at end of file diff --git a/sources/modules/php/dist/sample/content/about-us.phtml b/sources/modules/php/dist/sample/content/about-us.phtml new file mode 100644 index 0000000..a86d130 --- /dev/null +++ b/sources/modules/php/dist/sample/content/about-us.phtml @@ -0,0 +1,2 @@ +

This is about page. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

+

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

diff --git a/sources/modules/php/dist/sample/content/contact.phtml b/sources/modules/php/dist/sample/content/contact.phtml new file mode 100644 index 0000000..fa7c350 --- /dev/null +++ b/sources/modules/php/dist/sample/content/contact.phtml @@ -0,0 +1,2 @@ +

This is contact page. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

+

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

diff --git a/sources/modules/php/dist/sample/content/home.phtml b/sources/modules/php/dist/sample/content/home.phtml new file mode 100644 index 0000000..6868b92 --- /dev/null +++ b/sources/modules/php/dist/sample/content/home.phtml @@ -0,0 +1,2 @@ +

This is home page.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

+

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

diff --git a/sources/modules/php/dist/sample/content/products.phtml b/sources/modules/php/dist/sample/content/products.phtml new file mode 100644 index 0000000..0af9d3d --- /dev/null +++ b/sources/modules/php/dist/sample/content/products.phtml @@ -0,0 +1,2 @@ +

This is product page. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

+

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

diff --git a/sources/modules/php/dist/sample/includes/config.php b/sources/modules/php/dist/sample/includes/config.php new file mode 100644 index 0000000..ad25dc8 --- /dev/null +++ b/sources/modules/php/dist/sample/includes/config.php @@ -0,0 +1,26 @@ + 'Simple PHP Website', + 'site_url' => '', + 'pretty_uri' => false, + 'nav_menu' => [ + '' => 'Home', + 'about-us' => 'About Us', + 'products' => 'Products', + 'contact' => 'Contact', + ], + 'template_path' => 'template', + 'content_path' => 'content', + 'version' => 'v3.0', + ]; + + return isset($config[$key]) ? $config[$key] : null; +} diff --git a/sources/modules/php/dist/sample/includes/functions.php b/sources/modules/php/dist/sample/includes/functions.php new file mode 100644 index 0000000..73ee106 --- /dev/null +++ b/sources/modules/php/dist/sample/includes/functions.php @@ -0,0 +1,80 @@ + $name) { + $class = str_replace('page=', '', $_SERVER['QUERY_STRING']) == $uri ? ' active' : ''; + $url = config('site_url') . '/' . (config('pretty_uri') || $uri == '' ? '' : '?page=') . $uri; + + $nav_menu .= '' . $name . '' . $sep; + } + + echo trim($nav_menu, $sep); +} + +/** + * Displays page title. It takes the data from + * URL, it replaces the hyphens with spaces and + * it capitalizes the words. + */ +function page_title() +{ + $page = isset($_GET['page']) ? htmlspecialchars($_GET['page']) : 'Home'; + + echo ucwords(str_replace('-', ' ', $page)); +} + +/** + * Displays page content. It takes the data from + * the static pages inside the pages/ directory. + * When not found, display the 404 error page. + */ +function page_content() +{ + $page = isset($_GET['page']) ? $_GET['page'] : 'home'; + + $path = getcwd() . '/' . config('content_path') . '/' . $page . '.phtml'; + + if (! file_exists($path)) { + $path = getcwd() . '/' . config('content_path') . '/404.phtml'; + } + + echo file_get_contents($path); +} + +/** + * Starts everything and displays the template. + */ +function init() +{ + require config('template_path') . '/template.php'; +} diff --git a/sources/modules/php/dist/sample/index.php b/sources/modules/php/dist/sample/index.php index 248f226..8db9bd7 100644 --- a/sources/modules/php/dist/sample/index.php +++ b/sources/modules/php/dist/sample/index.php @@ -1,4 +1,10 @@ -

lol

\ No newline at end of file + +// Comment these lines to hide errors +error_reporting(E_ALL); +ini_set('display_errors', 1); + +require 'includes/config.php'; +require 'includes/functions.php'; + +init(); \ No newline at end of file diff --git a/sources/modules/php/dist/sample/simple/.gitignore b/sources/modules/php/dist/sample/simple/.gitignore new file mode 100644 index 0000000..f84c98b --- /dev/null +++ b/sources/modules/php/dist/sample/simple/.gitignore @@ -0,0 +1,2 @@ +/.idea +.php_cs.cache \ No newline at end of file diff --git a/sources/modules/php/dist/sample/simple/.htaccess b/sources/modules/php/dist/sample/simple/.htaccess new file mode 100644 index 0000000..b2e67c0 --- /dev/null +++ b/sources/modules/php/dist/sample/simple/.htaccess @@ -0,0 +1,6 @@ +Options +FollowSymLinks +RewriteEngine On + +RewriteCond %{REQUEST_FILENAME} !-d +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^(.*)$ index.php?page=$1 [L] diff --git a/sources/modules/php/dist/sample/template/style.css b/sources/modules/php/dist/sample/template/style.css new file mode 100644 index 0000000..03487f9 --- /dev/null +++ b/sources/modules/php/dist/sample/template/style.css @@ -0,0 +1,20 @@ + +.wrap { + max-width: 720px; + margin: 50px auto; + padding: 30px 40px; + text-align: center; + box-shadow: 0 4px 25px -4px #9da5ab; +} +article { + padding: 40px; + line-height: 150%; + text-align: left; +} + +nav .item { + text-decoration: none; +} +nav .active { + border-bottom: 1px solid; +} \ No newline at end of file diff --git a/sources/modules/php/dist/sample/template/template.php b/sources/modules/php/dist/sample/template/template.php new file mode 100644 index 0000000..d55477e --- /dev/null +++ b/sources/modules/php/dist/sample/template/template.php @@ -0,0 +1,29 @@ + + + + + <?php page_title(); ?> | <?php site_name(); ?> + + + +
+ +
+

+ +
+ +
+

+ +
+ + + +
+ + \ No newline at end of file diff --git a/sources/modules/php/src/RequestHandler.hpp b/sources/modules/php/src/RequestHandler.hpp index ddb1e48..f419ee9 100644 --- a/sources/modules/php/src/RequestHandler.hpp +++ b/sources/modules/php/src/RequestHandler.hpp @@ -3,9 +3,10 @@ #include #include #include -#include #include +#include #include +#include #include using namespace Zia::API; @@ -14,22 +15,25 @@ using namespace boost::process; class PhpRequestHandler : public RequestHandler { public: PhpRequestHandler(std::string const &phpBinaryPath, std::string const &phpWwwPath) - : RequestHandler(), _phpBinaryPath(phpBinaryPath), _phpWwwPath(phpWwwPath) {} + : RequestHandler(), _phpBinaryPath(phpBinaryPath), _phpWwwPath(phpWwwPath) {} HookResultType onRequest(const Connection &connection, const Request &request, Response &response) override { - ipstream pipe_stream; - child proc(_phpBinaryPath, std_out > pipe_stream, buildCgiEnvironment(request)); + ipstream output_stream; + opstream input_stream; + child proc(_phpBinaryPath, std_out > output_stream, std_in < input_stream, buildCgiEnvironment(request)); - std::string output((std::istreambuf_iterator(pipe_stream)), (std::istreambuf_iterator())); - parseCgi(output, response); + if (!request.body.empty()) + input_stream << &request.body[0]; + std::string output((std::istreambuf_iterator(output_stream)), (std::istreambuf_iterator())); + parseCgi(output, request, response); proc.wait(); return HookResult::Ok; } HookResultType onResponse(const Connection &connection, Response &response) override { static const std::string not_found = "The requested URL does not exist"; - switch(response.status_code) { + switch (response.status_code) { case 404: response.body.clear(); response.body.insert(response.body.end(), not_found.begin(), not_found.end()); @@ -49,36 +53,62 @@ class PhpRequestHandler : public RequestHandler { } private: - environment buildCgiEnvironment(Request const &request) { - boost::process::environment cgi_env = boost::this_process::environment(); - - static char const *beginPath = + static inline const auto ext_html_result = std::vector{".html", ".htm", ".php", ".phtml", ".phtm"}; + static inline char const *beginPath = #ifdef _WIN32 ".\\"; #else "./"; #endif - cgi_env["SCRIPT_FILENAME"] = beginPath + _phpWwwPath + request.uri; + environment buildCgiEnvironment(Request const &request) { + boost::process::environment cgi_env = boost::this_process::environment(); + cgi_env["SERVER_PROTOCOL"] = request.protocol; cgi_env["REQUEST_METHOD"] = request.method; cgi_env["GATEWAY_INTERFACE"] = "CGI/1.1"; cgi_env["REDIRECT_STATUS"] = "200"; + cgi_env["DOCUMENT_ROOT"] = beginPath + _phpWwwPath; + + auto it = request.headers.find("Content-Length"); + if (it != request.headers.end()) + cgi_env["CONTENT_LENGTH"] = it->second; + + it = request.headers.find("Content-Type"); + if (it != request.headers.end()) + cgi_env["CONTENT_TYPE"] = it->second; + + setupUri(request.uri); + auto queryIndex = _requestedUri.find('?'); + + if (queryIndex != std::string::npos) { + cgi_env["QUERY_STRING"] = _requestedUri.substr(queryIndex + 1, _requestedUri.size()); + _requestedUri = _requestedUri.substr(0, queryIndex); + } else + cgi_env["QUERY_STRING"] = _requestedUri; + + cgi_env["SCRIPT_FILENAME"] = beginPath + _phpWwwPath + _requestedUri; return cgi_env; } - void parseCgi(std::string &data, Response &response) { + void parseCgi(std::string &data, Request const &request, Response &response) { static const std::regex regex_status("([0-9]+)\\s+(.+)"); - static const std::regex regex_header("([^:\n]+):\\s?(.+)"); + static const std::regex regex_header("(?:\n)?([^:\n]+):\\s?(.+)"); static const std::string headers_end = "\r\n\r\n"; + auto body_index = data.find(headers_end); + std::string headers = data.substr(0, body_index); + std::string body = data.substr(body_index + headers_end.size(), data.size()); + std::smatch matcher; - while (std::regex_search(data, matcher, regex_header)) { + while (std::regex_search(headers, matcher, regex_header)) { response.headers[matcher[1]] = matcher[2]; - data = matcher.suffix(); + headers = matcher.suffix(); } + setResponseHeaders(response); + auto it = response.headers.find("Status"); if (it != response.headers.end()) { auto status = it->second; @@ -91,11 +121,46 @@ class PhpRequestHandler : public RequestHandler { response.status_message = "OK"; } - std::string body = data.substr(headers_end.size()); response.body.insert(response.body.end(), body.begin(), body.end()); } + void setResponseHeaders(Response &response) { + auto ext = boost::filesystem::extension(_requestedUri); + if (ext.empty()) return; + + auto it = response.headers.find("Content-type"); + if (std::find(ext_html_result.begin(), ext_html_result.end(), ext) == ext_html_result.end()) + response.headers.erase(it); + else { + response.headers["Content-Type"] = it->second; + response.headers.erase(it); + } + + it = response.headers.find("Content-length"); + if (it != response.headers.end()) { + response.headers["Content-Length"] = it->second; + response.headers.erase(it); + } + } + + void setupUri(std::string const &uri) { + _requestedUri = uri; + + if (uri != "/" && uri.rfind("/?", 0) != 0) return; + + for (auto &ext: ext_html_result) { + if (boost::filesystem::exists(beginPath + _phpWwwPath + "/index" + ext)) { + _requestedUri = "/index" + ext; + + if (uri.size() > 1) + _requestedUri += uri.substr(1); + return; + } + } + } + private: std::string _phpBinaryPath; std::string _phpWwwPath; + std::string _requestedUri; }; \ No newline at end of file diff --git a/sources/modules/secure_network/CMakeLists.txt b/sources/modules/secure_network/CMakeLists.txt index e9e4f8e..53c8bd4 100644 --- a/sources/modules/secure_network/CMakeLists.txt +++ b/sources/modules/secure_network/CMakeLists.txt @@ -6,8 +6,6 @@ set_target_properties(secure_network PROPERTIES PREFIX "zia-" ) -find_package(OpenSSL REQUIRED) - target_link_libraries(secure_network CONAN_PKG::OpenSSL CONAN_PKG::boost diff --git a/sources/modules/secure_network/src/RequestHandler.hpp b/sources/modules/secure_network/src/RequestHandler.hpp index 824f859..70c613f 100644 --- a/sources/modules/secure_network/src/RequestHandler.hpp +++ b/sources/modules/secure_network/src/RequestHandler.hpp @@ -39,7 +39,7 @@ class SecureNetworkRequestHandler : public RequestHandler { onConnectionWrite(const Connection &connection, tcp::socket &socket, const std::vector &buffer, size_t &size) override { boost::system::error_code error; - _socket->write_some(boost::asio::buffer(buffer), error); + size = boost::asio::write(*_socket, boost::asio::buffer(buffer), boost::asio::transfer_all(), error); return HookResult::Ok; } diff --git a/sources/modules/unsecure_network/src/RequestHandler.hpp b/sources/modules/unsecure_network/src/RequestHandler.hpp index 1b76a75..9720fe5 100644 --- a/sources/modules/unsecure_network/src/RequestHandler.hpp +++ b/sources/modules/unsecure_network/src/RequestHandler.hpp @@ -22,8 +22,7 @@ class UnsecureNetworkRequestHandler : public RequestHandler { HookResultType onConnectionWrite(const Connection &connection, tcp::socket &socket, const std::vector &buffer, size_t &size) override { boost::system::error_code error; - socket.write_some(boost::asio::buffer(buffer), error); - + size = boost::asio::write(socket, boost::asio::buffer(buffer), boost::asio::transfer_all(), error); return HookResult::Ok; } }; \ No newline at end of file