add: PUT method

This commit is contained in:
Talyx
2022-02-09 18:48:12 +03:00
parent cb50c16afc
commit 0103dc97dd
3 changed files with 27 additions and 7 deletions

View File

@@ -28,7 +28,7 @@
[[server.location]]
location = "*.jpeg"
root = "www/images/jpegImg"
methods = ["GET"]
methods = ["GET", "PUT"]
[[server.location]]
location = "*.png"
root = "www/images/pngImg"
@@ -45,7 +45,7 @@
[[server.location]]
location = "*.php"
root = "www/script"
methods = ["GET", "POST"]
methods = ["GET"]
autoindex = false
cgi_pass = "/usr/bin/php-cgi"
[[server]]

View File

@@ -203,9 +203,6 @@ std::string Response::getFullURI(void)
tmp = _request.getURI().substr(len);
tmp = _location->root + tmp;
}
DBOUT << RED << _location->location << ENDL;
DBOUT << len << ENDL;
DBOUT << RED << tmp << ENDL;
if (_request.isDir(tmp) == 0)
{
if (_location->directoryFile.empty() || _Autoindex)
@@ -323,9 +320,10 @@ void Response::generate2(serverListen &l)
methodGet();
else if (_method == "POST")
methodPost();
else
else if (_method == "DELETE")
methodDelete();
else if (_method == "PUT")
methodPut();
}
bool Response::isRedirect()
@@ -365,11 +363,31 @@ void Response::methodPost(void)
std::ofstream outfile(_fullURI.c_str(), std::ios::out | std::ios::binary);
outfile.write(_request.getBody().data(), _request.getBody().size());
outfile.close();
_code = 204;
setHeaderBlocks();
generateHeader();
DBOUT << GREEN << "POST method called" << ENDL;
}
void Response::methodPut(void)
{
_code = 201;
if (_request.isFile(_fullURI) == 0)
_code = 204;
std::ofstream file(_fullURI.c_str(), std::ios::out | std::ios::binary);
if (!file.is_open())
_code = 403;
else
{
file.write(_request.getBody().data(), _request.getBody().size());
}
file.close();
setHeaderBlocks();
generateHeader();
DBOUT << GREEN << "PUT method called" << ENDL;
}
void Response::methodDelete(void)
{
if (_request.isFile(_fullURI) == 0)
@@ -388,6 +406,7 @@ void Response::methodDelete(void)
DBOUT << GREEN << "Delete method called" << ENDL;
}
//-------------------------------------------------GET/SET---------------------------------------
std::map<std::string, std::string> Response::_errorCode;

View File

@@ -49,6 +49,7 @@ private:
private:
void methodGet(void);
void methodPost(void);
void methodPut(void);
void methodDelete(void);
void invalidClient(void);
void generateHeader(void);