mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-11 05:18:46 +02:00
「🏗️」 wip(requests/POST): updating only things that does make sense
This commit is contained in:
29
src/main.cpp
29
src/main.cpp
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/03/19 03:11:14 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 05:24:59 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -27,6 +27,11 @@ void close_socket(int signal)
|
||||
exit(signal);
|
||||
}
|
||||
|
||||
std::string getMethod(std::string &data)
|
||||
{
|
||||
return (data.substr(0, data.substr(0, 4).find_last_not_of(" ") + 1));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// handle ctrl-C to close server socket
|
||||
@ -89,21 +94,33 @@ int main()
|
||||
|
||||
// parse the request
|
||||
std::string received_data(buffer, bytes_received);
|
||||
http::Get request(received_data);
|
||||
|
||||
std::cout << "Received " << request.getMethod() << " request for " << request.getTarget() << std::endl;
|
||||
|
||||
// handle the request
|
||||
std::string response;
|
||||
if (request.getMethod() == "GET" || request.getMethod() == "POST")
|
||||
|
||||
std::cout << getMethod(received_data) << std::endl;
|
||||
|
||||
if (getMethod(received_data) == "GET")
|
||||
{
|
||||
std::cout << "------------ GET REQUEST ------------" << std::endl;
|
||||
http::Get request(received_data);
|
||||
|
||||
response = request.execute().str();
|
||||
}
|
||||
else if (getMethod(received_data) == "POST")
|
||||
{
|
||||
std::cout << "------------ POST REQUEST ------------" << std::endl;
|
||||
http::Post request(received_data);
|
||||
|
||||
response = request.execute().str();
|
||||
}
|
||||
else
|
||||
{
|
||||
response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n<html><body><h1>501 Not Implemented</h1></body></html>";
|
||||
}
|
||||
|
||||
send(client_socket, response.c_str(), response.length(), 0);
|
||||
close(client_socket);
|
||||
//close(client_socket);
|
||||
}
|
||||
|
||||
close(server_socket);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/17 14:08:12 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/03/24 15:12:50 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 01:48:20 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/11 23:34:45 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/02/12 00:59:22 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 01:46:52 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
|
||||
using namespace webserv;
|
||||
|
||||
std::multimap<std::string, std::string> http::IMessage::getHeaders(void) const
|
||||
std::map<std::string, std::string> http::IMessage::getHeaders(void) const
|
||||
{
|
||||
return (this->_headers);
|
||||
}
|
||||
@ -24,7 +24,7 @@ std::string http::IMessage::getBody(void) const
|
||||
return (this->_body);
|
||||
}
|
||||
|
||||
void http::IMessage::setHeaders(std::multimap<std::string, std::string> const headers)
|
||||
void http::IMessage::setHeaders(std::map<std::string, std::string> const headers)
|
||||
{
|
||||
this->_headers = headers;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/03/24 16:00:41 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 05:24:17 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -27,7 +27,7 @@ std::string http::IRequest::str(void) const
|
||||
response << this->_method << " " << this->_target << " " << this->_protocol;
|
||||
response << "\r\n";
|
||||
|
||||
for (std::multimap<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
response << it->first << ": " << it->second << "\r\n";
|
||||
|
||||
response << "\r\n";
|
||||
@ -187,7 +187,6 @@ http::Response http::Get::execute(void)
|
||||
response.setBody(std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()));
|
||||
std::stringstream length;
|
||||
length << (file.tellg() - file_start);
|
||||
std::cout << length.str() << std::endl;
|
||||
response.addHeader("Content-Length", length.str());
|
||||
|
||||
response.setProtocol(this->_protocol);
|
||||
@ -345,12 +344,64 @@ void http::Post::parse(std::string const &data)
|
||||
//*/
|
||||
}
|
||||
|
||||
std::string extractFilename(const std::string &header)
|
||||
{
|
||||
size_t start = header.find("filename=\"") + 10;
|
||||
size_t end = header.find("\"", start);
|
||||
return header.substr(start, end - start);
|
||||
}
|
||||
|
||||
void handleMultipartData(const std::string &body, const std::string &boundary)
|
||||
{
|
||||
size_t i = 0;
|
||||
std::string delim = "--" + boundary;
|
||||
delim.erase(delim.size() - 1);
|
||||
|
||||
while ((i = body.find(delim, i)) != std::string::npos)
|
||||
{
|
||||
size_t start = i + delim.length();
|
||||
size_t end = body.find("\r\n\r\n", start);
|
||||
|
||||
if (end != std::string::npos)
|
||||
{
|
||||
std::string part_header = body.substr(start, end - start);
|
||||
std::cout << std::endl<< std::endl<< std::endl<< std::endl<< std::endl;
|
||||
std::string part_content = body.substr(end + 4, body.find(delim, end) - end - 4);
|
||||
|
||||
std::ofstream outfile(extractFilename(part_header).c_str(), std::ios::binary);
|
||||
if (outfile.is_open())
|
||||
{
|
||||
outfile.write(part_content.c_str(), part_content.length());
|
||||
outfile.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "open failed" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
i += delim.length();
|
||||
}
|
||||
}
|
||||
|
||||
http::Response http::Post::execute(void)
|
||||
{
|
||||
http::Response response;
|
||||
|
||||
// uh idk anymore
|
||||
|
||||
try
|
||||
{
|
||||
handleMultipartData(this->_body, this->getHeaders()["Content-Type"].substr(this->getHeaders()["Content-Type"].find("=", this->getHeaders()["Content-Type"].find(";")) + 1));
|
||||
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(204);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(500);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(http::Errors::getResponseBody(response.getStatusCode()));
|
||||
}
|
||||
return (response);
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 17:28:31 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/03/24 15:17:52 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 01:48:39 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -34,7 +34,7 @@ std::string http::Response::str(void) const
|
||||
response << this->_protocol << " " << this->_status_code << " " << this->_status_text;
|
||||
response << "\r\n";
|
||||
|
||||
for (std::multimap<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
response << it->first << ": " << it->second << "\r\n";
|
||||
|
||||
response << "\r\n";
|
||||
|
Reference in New Issue
Block a user