-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 47-loop-per-ticks-for-datatransfer
- Loading branch information
Showing
6 changed files
with
105 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,85 @@ | ||
#include "Response.hpp" | ||
#include <dirent.h> | ||
|
||
void Response::handleDelete(Request& req, Config &config) | ||
{ | ||
(void)config; | ||
(void)req; | ||
std::cout << "DELETE request received" << std::endl; | ||
} | ||
std::string content = req.getBody(); | ||
std::string path = req.getPath(); | ||
|
||
setVersion("HTTP/1.1"); | ||
|
||
t_location location = get_location(config, Path(path, Path::Type::URL, config).asFilePath()); | ||
|
||
if (!get_location(config, Path(path, Path::Type::URL, config).asUrl()).allowed_methods[static_cast<int>(Method::DELETE)]) | ||
{ | ||
std::cerr << "DELETE method not allowed" << std::endl; | ||
setStatus(Status::Forbidden); | ||
setBody("DELETE method not allowed"); | ||
return; | ||
} | ||
|
||
// FIXME: sometimes the path is corrent and sometimes not | ||
path = "." + location.root_dir + path; | ||
std::cout << "Path: " << path << std::endl; | ||
|
||
// delete file | ||
if(path[path.length() - 1] != '/') | ||
{ | ||
std::cout << "Trying to delete file: " << path << std::endl; | ||
if (remove(path.c_str()) != 0) | ||
{ | ||
std::cerr << "Error deleting file" << std::endl; | ||
setStatus(Status::Forbidden); | ||
setBody("Error deleting file"); | ||
} | ||
else | ||
{ | ||
setStatus(Status::OK); | ||
setBody("File deleted"); | ||
} | ||
return; | ||
} | ||
|
||
// delete directory | ||
DIR *dir; | ||
struct dirent *ent; | ||
|
||
std::cout << "Trying to delete directory: " << path << std::endl; | ||
|
||
if ((dir = opendir(path.c_str())) != NULL) | ||
{ | ||
while ((ent = readdir(dir)) != NULL) | ||
{ | ||
std::string file = ent->d_name; | ||
if (file != "." && file != "..") | ||
{ | ||
std::string filepath = path + file; | ||
if (remove(filepath.c_str()) != 0) | ||
{ | ||
std::cerr << "Error deleting file" << std::endl; | ||
setStatus(Status::Forbidden); | ||
setBody("Error deleting file"); | ||
return; | ||
} | ||
} | ||
} | ||
closedir(dir); | ||
if (rmdir(path.c_str()) != 0) | ||
{ | ||
std::cerr << "Error deleting directory" << std::endl; | ||
setStatus(Status::Forbidden); | ||
setBody("Error deleting directory"); | ||
} | ||
else | ||
{ | ||
setStatus(Status::OK); | ||
setBody("Directory deleted"); | ||
} | ||
} | ||
else | ||
{ | ||
std::cerr << "Error opening directory" << std::endl; | ||
setStatus(Status::Forbidden); | ||
setBody("Error opening directory"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters