diff options
| author | Roland Reichwein <mail@reichwein.it> | 2020-04-18 16:31:49 +0200 | 
|---|---|---|
| committer | Roland Reichwein <mail@reichwein.it> | 2020-04-18 16:31:49 +0200 | 
| commit | a047b5cdcc574cd15d26320d528a41b4014eeba2 (patch) | |
| tree | e0a0a68bd798dfff4d4cc7b67cee7e92d8ca323c /plugins/cgi | |
| parent | 121e80515c244dea0b110cc8c96f1476341b7228 (diff) | |
Bugfixes
Diffstat (limited to 'plugins/cgi')
| -rw-r--r-- | plugins/cgi/cgi.cpp | 52 | 
1 files changed, 41 insertions, 11 deletions
diff --git a/plugins/cgi/cgi.cpp b/plugins/cgi/cgi.cpp index 5921e98..232abff 100644 --- a/plugins/cgi/cgi.cpp +++ b/plugins/cgi/cgi.cpp @@ -24,16 +24,22 @@ namespace {     std::function<std::string(const std::string& key)>& GetRequestParam; // request including body (POST...)     std::function<void(const std::string& key, const std::string& value)>& SetResponseHeader; // to be added to result string     fs::path& path; +   fs::path& file_path; +   fs::path& path_info;     CGIContext(std::function<std::string(const std::string& key)>& p_GetServerParam,                std::function<std::string(const std::string& key)>& p_GetRequestParam,                std::function<void(const std::string& key, const std::string& value)>& p_SetResponseHeader, -              fs::path& p_path +              fs::path& p_path, +              fs::path& p_file_path, +              fs::path& p_path_info                )      : GetServerParam(p_GetServerParam)      , GetRequestParam(p_GetRequestParam)      , SetResponseHeader(p_SetResponseHeader)      , path(p_path) +    , file_path(p_file_path) +    , path_info(p_path_info)    {    }   }; @@ -89,7 +95,17 @@ namespace {   }   std::unordered_map<std::string, std::function<void(std::string&, CGIContext&)>> headerMap { -  { "Content-Type", [](std::string& v, CGIContext& c){ c.SetResponseHeader("content_type", v); } } +  { "Cache-control", [](std::string& v, CGIContext& c){ c.SetResponseHeader("cache_control", v); } }, + +  { "Content-Type", [](std::string& v, CGIContext& c){ c.SetResponseHeader("content_type", v); } }, + +  { "Status", [](std::string& v, CGIContext& c) { +      std::string status{"500"}; +      if (v.size() >= 3) { +       status = v.substr(0, 3); +      } +      c.SetResponseHeader("status", status); +  } }   };   void handleHeader(const std::string& s, CGIContext& context) @@ -123,11 +139,11 @@ namespace {    size_t query_pos {target.find("?")};    std::string query;    if (query_pos != target.npos) { -   query = target.substr(0, query_pos); -   target = target.substr(query_pos + 1); +   query = target.substr(query_pos + 1); +   target = target.substr(0, query_pos);    } -  env["PATH_INFO"] = target; +  env["PATH_INFO"] = c.path_info.string();    env["PATH_TRANSLATED"] = c.path.string();    env["QUERY_STRING"] = query;    env["REMOTE_ADDR"] = ""; @@ -135,7 +151,8 @@ namespace {    env["REMOTE_IDENT"] = "";    env["REMOTE_USER"] = "";    env["REQUEST_METHOD"] = c.GetRequestParam("method"); -  env["SCRIPT_NAME"] = c.GetRequestParam("rel_target"); +  env["REQUEST_URI"] = target; +  env["SCRIPT_NAME"] = c.file_path;    env["SERVER_NAME"] = c.GetRequestParam("host");    env["SERVER_PORT"] = c.GetServerParam("port");    env["SERVER_PROTOCOL"] = c.GetRequestParam("http_version"); @@ -259,12 +276,25 @@ std::string cgi_plugin::generate_page(     return HttpStatus("301", "Correcting directory path", SetResponseHeader);    } +  fs::path path_info{}; +  fs::path file_path{target};    try { -    if (!fs::is_regular_file(path)) { -     return HttpStatus("500", "Bad Script: "s + rel_target, SetResponseHeader); -    } +   // file file name part and separate path_info +   while (!fs::is_regular_file(path) && path != "/" && path != "") { +    if (path_info.string() == "") +     path_info = fs::path{"/"} / path.filename(); +    else +     path_info = fs::path{"/"} / path.filename() / path_info.relative_path(); + +    path = path.parent_path(); +    file_path = file_path.parent_path(); +   } + +   if (!fs::is_regular_file(path)) { +    return HttpStatus("500", "Bad Script: "s + rel_target, SetResponseHeader); +   }    } catch (const std::exception& ex) { -    return HttpStatus("500", "Bad file access: "s + rel_target, SetResponseHeader); +   return HttpStatus("500", "Bad file access: "s + rel_target, SetResponseHeader);    }    try { @@ -277,7 +307,7 @@ std::string cgi_plugin::generate_page(    SetResponseHeader("content_type", mime_type(path)); -  CGIContext context(GetServerParam, GetRequestParam, SetResponseHeader, path); +  CGIContext context(GetServerParam, GetRequestParam, SetResponseHeader, path, file_path, path_info);    try {     return executeFile(path, context);  | 
