Debugging 6

This commit is contained in:
2020-11-30 18:47:16 +01:00
parent 3fb951fc29
commit c1c2c38083
3 changed files with 22 additions and 18 deletions

View File

@ -37,6 +37,8 @@ bool fileExists(string path) {
return stat(path.c_str(), &statbuf) == 0; return stat(path.c_str(), &statbuf) == 0;
} }
URI::URI() = default;
URI::URI(string webroot, string reqpath) { URI::URI(string webroot, string reqpath) {
unsigned long pos = reqpath.find('?'); unsigned long pos = reqpath.find('?');
if (pos != string::npos) { if (pos != string::npos) {

View File

@ -15,9 +15,11 @@ private:
string info; string info;
string filepath; string filepath;
string newpath; string newpath;
bool queryinit; bool queryinit{};
public: public:
URI();
URI(string webroot, string reqpath); URI(string webroot, string reqpath);
string getWebRoot(); string getWebRoot();

View File

@ -252,7 +252,7 @@ bool connection_handler(const char *preprefix, const char *col1, const char *col
try { try {
bool noRedirect, redir, invalidMethod, etag; bool noRedirect, redir, invalidMethod, etag;
URI *path; URI path;
pid_t childpid; pid_t childpid;
FILE *file; FILE *file;
int statuscode; int statuscode;
@ -319,27 +319,27 @@ bool connection_handler(const char *preprefix, const char *col1, const char *col
redir = false; redir = false;
} }
*path = URI(getWebRoot(host), req.getPath()); path = URI(getWebRoot(host), req.getPath());
childpid = 0; childpid = 0;
printf("STAGE 5\n"); printf("STAGE 5\n");
flush(cout); flush(cout);
if (redir) { if (redir) {
goto respond; goto respond;
} else if (!path->getNewPath().empty() && req.getMethod() != "POST") { } else if (!path.getNewPath().empty() && req.getMethod() != "POST") {
req.redirect(303, path->getNewPath()); req.redirect(303, path.getNewPath());
goto respond; goto respond;
} }
file = path->openFile(); file = path.openFile();
if (file == nullptr) { if (file == nullptr) {
req.setField("Cache-Control", "public, max-age=60"); req.setField("Cache-Control", "public, max-age=60");
req.respond(404); req.respond(404);
goto respond; 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); req.respond(403);
goto respond; goto respond;
} }
@ -347,13 +347,13 @@ bool connection_handler(const char *preprefix, const char *col1, const char *col
flush(cout); flush(cout);
req.setField("Content-Type", type); req.setField("Content-Type", type);
req.setField("Last-Modified", getHttpDate(path->getFilePath())); req.setField("Last-Modified", getHttpDate(path.getFilePath()));
invalidMethod = false; invalidMethod = false;
etag = false; etag = false;
if (path->isStatic()) { if (path.isStatic()) {
hash = getETag(path->getFilePath()); hash = getETag(path.getFilePath());
req.setField("ETag", hash); req.setField("ETag", hash);
req.setField("Accept-Ranges", "bytes"); req.setField("Accept-Ranges", "bytes");
if (type.find("text/") == 0) { if (type.find("text/") == 0) {
@ -388,7 +388,7 @@ bool connection_handler(const char *preprefix, const char *col1, const char *col
} }
statuscode = 0; statuscode = 0;
if (!path->isStatic()) { if (!path.isStatic()) {
string cmd = (string) "env -i" + string cmd = (string) "env -i" +
" REDIRECT_STATUS=" + cli_encode("CGI") + " REDIRECT_STATUS=" + cli_encode("CGI") +
" DOCUMENT_ROOT=" + cli_encode(getWebRoot(host)) + " 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.isExistingField("Content-Type") ? " CONTENT_TYPE=" + cli_encode(
req.getField("Content-Type")) : "") + req.getField("Content-Type")) : "") +
((socket->isSecured()) ? " HTTPS=on" : "") + ((socket->isSecured()) ? " HTTPS=on" : "") +
" PATH_INFO=" + cli_encode(path->getFilePathInfo()) + " PATH_INFO=" + cli_encode(path.getFilePathInfo()) +
" PATH_TRANSLATED=" + cli_encode(path->getAbsolutePath()) + " PATH_TRANSLATED=" + cli_encode(path.getAbsolutePath()) +
" QUERY_STRING=" + cli_encode(path->getQuery()) + " QUERY_STRING=" + cli_encode(path.getQuery()) +
" REMOTE_ADDR=" + cli_encode(socket->getPeerAddress()->toString()) + " REMOTE_ADDR=" + cli_encode(socket->getPeerAddress()->toString()) +
" REMOTE_HOST=" + cli_encode(info->host) + " REMOTE_HOST=" + cli_encode(info->host) +
" REMOTE_PORT=" + cli_encode(to_string(socket->getPeerPort())) + " REMOTE_PORT=" + cli_encode(to_string(socket->getPeerPort())) +
" REQUEST_METHOD=" + cli_encode(req.getMethod()) + " REQUEST_METHOD=" + cli_encode(req.getMethod()) +
" REQUEST_URI=" + cli_encode(req.getPath()) + " REQUEST_URI=" + cli_encode(req.getPath()) +
" SCRIPT_FILENAME=" + cli_encode(path->getFilePath()) + " SCRIPT_FILENAME=" + cli_encode(path.getFilePath()) +
" SCRIPT_NAME=" + cli_encode(path->getRelativePath()) + " SCRIPT_NAME=" + cli_encode(path.getRelativePath()) +
" SERVER_ADMIN=" + cli_encode("lorenz.stechauner@gmail.com") + " SERVER_ADMIN=" + cli_encode("lorenz.stechauner@gmail.com") +
" SERVER_NAME=" + cli_encode(host) + " SERVER_NAME=" + cli_encode(host) +
" SERVER_PORT=" + cli_encode(to_string(socket->getSocketPort())) + " 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); req.respond(416);
} else if (req.isExistingField("Range")) { } else if (req.isExistingField("Range")) {
string range = req.getField("Range"); string range = req.getField("Range");
if (range.find("bytes=") != 0 || !path->isStatic()) { if (range.find("bytes=") != 0 || !path.isStatic()) {
req.respond(416); req.respond(416);
} else { } else {
fseek(file, 0L, SEEK_END); fseek(file, 0L, SEEK_END);