From c1c2c38083d2d4a5dd59f6527f7dbd7d3e8adab7 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Mon, 30 Nov 2020 18:47:16 +0100 Subject: [PATCH] Debugging 6 --- src/URI.cpp | 2 ++ src/URI.h | 4 +++- src/client.cpp | 34 +++++++++++++++++----------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/URI.cpp b/src/URI.cpp index 83d15fa..db4d7e7 100644 --- a/src/URI.cpp +++ b/src/URI.cpp @@ -37,6 +37,8 @@ bool fileExists(string path) { return stat(path.c_str(), &statbuf) == 0; } +URI::URI() = default; + URI::URI(string webroot, string reqpath) { unsigned long pos = reqpath.find('?'); if (pos != string::npos) { diff --git a/src/URI.h b/src/URI.h index 9a97be4..703e05a 100644 --- a/src/URI.h +++ b/src/URI.h @@ -15,9 +15,11 @@ private: string info; string filepath; string newpath; - bool queryinit; + bool queryinit{}; public: + URI(); + URI(string webroot, string reqpath); string getWebRoot(); diff --git a/src/client.cpp b/src/client.cpp index b39daa1..f47f198 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -252,7 +252,7 @@ bool connection_handler(const char *preprefix, const char *col1, const char *col try { bool noRedirect, redir, invalidMethod, etag; - URI *path; + URI path; pid_t childpid; FILE *file; int statuscode; @@ -319,27 +319,27 @@ bool connection_handler(const char *preprefix, const char *col1, const char *col redir = false; } - *path = URI(getWebRoot(host), req.getPath()); + path = URI(getWebRoot(host), req.getPath()); childpid = 0; printf("STAGE 5\n"); flush(cout); if (redir) { goto respond; - } else if (!path->getNewPath().empty() && req.getMethod() != "POST") { - req.redirect(303, path->getNewPath()); + } else if (!path.getNewPath().empty() && req.getMethod() != "POST") { + req.redirect(303, path.getNewPath()); goto respond; } - file = path->openFile(); + file = path.openFile(); if (file == nullptr) { req.setField("Cache-Control", "public, max-age=60"); req.respond(404); goto respond; } - type = path->getFileType(); + type = path.getFileType(); - if (type.find("inode/") == 0 || (path->getRelativeFilePath().find("/.") != string::npos && !noRedirect)) { + if (type.find("inode/") == 0 || (path.getRelativeFilePath().find("/.") != string::npos && !noRedirect)) { req.respond(403); goto respond; } @@ -347,13 +347,13 @@ bool connection_handler(const char *preprefix, const char *col1, const char *col flush(cout); req.setField("Content-Type", type); - req.setField("Last-Modified", getHttpDate(path->getFilePath())); + req.setField("Last-Modified", getHttpDate(path.getFilePath())); invalidMethod = false; etag = false; - if (path->isStatic()) { - hash = getETag(path->getFilePath()); + if (path.isStatic()) { + hash = getETag(path.getFilePath()); req.setField("ETag", hash); req.setField("Accept-Ranges", "bytes"); if (type.find("text/") == 0) { @@ -388,7 +388,7 @@ bool connection_handler(const char *preprefix, const char *col1, const char *col } statuscode = 0; - if (!path->isStatic()) { + if (!path.isStatic()) { string cmd = (string) "env -i" + " REDIRECT_STATUS=" + cli_encode("CGI") + " DOCUMENT_ROOT=" + cli_encode(getWebRoot(host)) + @@ -400,16 +400,16 @@ bool connection_handler(const char *preprefix, const char *col1, const char *col (req.isExistingField("Content-Type") ? " CONTENT_TYPE=" + cli_encode( req.getField("Content-Type")) : "") + ((socket->isSecured()) ? " HTTPS=on" : "") + - " PATH_INFO=" + cli_encode(path->getFilePathInfo()) + - " PATH_TRANSLATED=" + cli_encode(path->getAbsolutePath()) + - " QUERY_STRING=" + cli_encode(path->getQuery()) + + " PATH_INFO=" + cli_encode(path.getFilePathInfo()) + + " PATH_TRANSLATED=" + cli_encode(path.getAbsolutePath()) + + " QUERY_STRING=" + cli_encode(path.getQuery()) + " REMOTE_ADDR=" + cli_encode(socket->getPeerAddress()->toString()) + " REMOTE_HOST=" + cli_encode(info->host) + " REMOTE_PORT=" + cli_encode(to_string(socket->getPeerPort())) + " REQUEST_METHOD=" + cli_encode(req.getMethod()) + " REQUEST_URI=" + cli_encode(req.getPath()) + - " SCRIPT_FILENAME=" + cli_encode(path->getFilePath()) + - " SCRIPT_NAME=" + cli_encode(path->getRelativePath()) + + " SCRIPT_FILENAME=" + cli_encode(path.getFilePath()) + + " SCRIPT_NAME=" + cli_encode(path.getRelativePath()) + " SERVER_ADMIN=" + cli_encode("lorenz.stechauner@gmail.com") + " SERVER_NAME=" + cli_encode(host) + " SERVER_PORT=" + cli_encode(to_string(socket->getSocketPort())) + @@ -492,7 +492,7 @@ bool connection_handler(const char *preprefix, const char *col1, const char *col req.respond(416); } else if (req.isExistingField("Range")) { string range = req.getField("Range"); - if (range.find("bytes=") != 0 || !path->isStatic()) { + if (range.find("bytes=") != 0 || !path.isStatic()) { req.respond(416); } else { fseek(file, 0L, SEEK_END);