mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-03-15 04:26:51 +01:00
「✨」 feat(requests): GET done, time for the two others D:
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
|
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
|
||||||
/* Updated: 2025/02/12 01:21:31 by mmoussou ### ########.fr */
|
/* Updated: 2025/02/12 08:56:21 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -26,10 +26,10 @@ namespace http {
|
|||||||
|
|
||||||
class IRequest: public http::IMessage {
|
class IRequest: public http::IMessage {
|
||||||
public:
|
public:
|
||||||
virtual void parse(http::IRequest const &request) = 0;
|
virtual void parse(std::string const &data) = 0;
|
||||||
virtual http::Response execute(void) = 0;
|
virtual http::Response execute(void) = 0;
|
||||||
|
|
||||||
//std::string str(void) const;
|
std::string str(void) const;
|
||||||
|
|
||||||
std::string getMethod(void) const;
|
std::string getMethod(void) const;
|
||||||
std::string getTarget(void) const;
|
std::string getTarget(void) const;
|
||||||
@ -39,7 +39,7 @@ public:
|
|||||||
void setTarget(std::string const target);
|
void setTarget(std::string const target);
|
||||||
void setProtocol(std::string const protocol);
|
void setProtocol(std::string const protocol);
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
std::string _method;
|
std::string _method;
|
||||||
std::string _target;
|
std::string _target;
|
||||||
std::string _protocol;
|
std::string _protocol;
|
||||||
@ -51,7 +51,7 @@ public:
|
|||||||
Get(void);
|
Get(void);
|
||||||
Get(std::string &data);
|
Get(std::string &data);
|
||||||
|
|
||||||
void parse(const http::IRequest &request);
|
void parse(std::string const &data);
|
||||||
|
|
||||||
http::Response execute(void);
|
http::Response execute(void);
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ public:
|
|||||||
Post(void);
|
Post(void);
|
||||||
Post(std::string &data);
|
Post(std::string &data);
|
||||||
|
|
||||||
void parse(const http::IRequest &request);
|
void parse(std::string const &data);
|
||||||
|
|
||||||
http::Response execute(void);
|
http::Response execute(void);
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ public:
|
|||||||
Delete(void);
|
Delete(void);
|
||||||
Delete(std::string &data);
|
Delete(std::string &data);
|
||||||
|
|
||||||
void parse(const http::IRequest &request);
|
void parse(std::string const &data);
|
||||||
|
|
||||||
http::Response execute(void);
|
http::Response execute(void);
|
||||||
|
|
||||||
|
22
src/main.cpp
22
src/main.cpp
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
|
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
|
||||||
/* Updated: 2025/02/11 14:54:22 by mmoussou ### ########.fr */
|
/* Updated: 2025/02/12 10:02:11 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ int main() {
|
|||||||
std::cerr << "Error registering signal handler!" << std::endl;
|
std::cerr << "Error registering signal handler!" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
// create a socket
|
// create a socket
|
||||||
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (server_socket == -1) {
|
if (server_socket == -1) {
|
||||||
@ -81,26 +81,26 @@ int main() {
|
|||||||
|
|
||||||
// parse the request
|
// parse the request
|
||||||
std::string received_data(buffer, bytes_received);
|
std::string received_data(buffer, bytes_received);
|
||||||
HttpRequest request = parseRequest(received_data);
|
//HttpRequest request = parseRequest(received_data);
|
||||||
|
http::Get request(received_data);
|
||||||
std::cout << "Received " << request.method << " request for " << request.target << std::endl;
|
|
||||||
|
|
||||||
|
std::cout << "Received " << request.getMethod() << " request for " << request.getTarget() << std::endl;
|
||||||
|
|
||||||
// handle the request
|
// handle the request
|
||||||
if (request.method == "GET")
|
std::string response;
|
||||||
|
if (request.getMethod() == "GET")
|
||||||
{
|
{
|
||||||
HttpResponse response(request);
|
response = request.execute().str();
|
||||||
send(client_socket, response.str().c_str(), response.str().length(), 0);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::string 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>";
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
send(client_socket, response.c_str(), response.length(), 0);
|
||||||
close(client_socket);
|
close(client_socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(server_socket);*/
|
close(server_socket);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -6,104 +6,31 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
||||||
/* Updated: 2025/02/12 01:19:52 by mmoussou ### ########.fr */
|
/* Updated: 2025/02/12 10:04:33 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <requests/HttpRequest.hpp>
|
#include <requests/HttpRequest.hpp>
|
||||||
/*
|
|
||||||
HttpRequest parseRequest(const std::string &rawRequest)
|
|
||||||
{
|
|
||||||
std::istringstream stream(rawRequest);
|
|
||||||
HttpRequest request;
|
|
||||||
std::string line;
|
|
||||||
|
|
||||||
if (std::getline(stream, line))
|
|
||||||
{
|
|
||||||
std::istringstream line_stream(line);
|
|
||||||
line_stream >> request.method >> request.target >> request.protocol;
|
|
||||||
request.target.insert(request.target.begin(), '.');
|
|
||||||
}
|
|
||||||
|
|
||||||
while (std::getline(stream, line) && line != "\r")
|
|
||||||
{
|
|
||||||
size_t delimiter_index = line.find(':');
|
|
||||||
if (delimiter_index != std::string::npos)
|
|
||||||
{
|
|
||||||
std::string key = line.substr(0, delimiter_index);
|
|
||||||
std::string value = line.substr(delimiter_index + 2);
|
|
||||||
request.headers[key] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ostringstream body_stream;
|
|
||||||
while (std::getline(stream, line))
|
|
||||||
body_stream << line << "\n";
|
|
||||||
request.body = body_stream.str();
|
|
||||||
|
|
||||||
std::cout << "method: " << request.method << std::endl;
|
|
||||||
std::cout << "target: " << request.target << std::endl;
|
|
||||||
std::cout << "protocol: " << request.protocol << std::endl;
|
|
||||||
std::cout << std::endl;
|
|
||||||
std::cout << "-- headers --" << std::endl;
|
|
||||||
for (std::map<std::string, std::string>::const_iterator it = request.headers.begin(); it != request.headers.end(); ++it)
|
|
||||||
std::cout << it->first << ": " << it->second << std::endl;
|
|
||||||
std::cout << std::endl;
|
|
||||||
std::cout << "-- body --" << std::endl << request.body << std::endl;
|
|
||||||
|
|
||||||
return request;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* GET response creation
|
|
||||||
HttpResponse::HttpResponse(const HttpRequest &request): _protocol(request.protocol)
|
|
||||||
{
|
|
||||||
std::ifstream file(request.target.c_str(), std::ios::binary);
|
|
||||||
if (file)
|
|
||||||
{
|
|
||||||
this->_status_code = 200;
|
|
||||||
this->_status_text = "OK";
|
|
||||||
this->_headers["Content-Type"] = "text/html";
|
|
||||||
this->_body = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// TODO: replace with the new predefined array of response
|
|
||||||
this->_status_code = 404;
|
|
||||||
this->_status_text = "Not Found";
|
|
||||||
this->_headers["Content-Type"] = "text/html";
|
|
||||||
this->_body = "<html><body>nuh uh, get 404'd >:D</body></html>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
class IRequest: public http::IMessage {
|
|
||||||
public:
|
|
||||||
virtual void parse(const http::IRequest &request) = 0;
|
|
||||||
virtual http::Response execute(void) = 0;
|
|
||||||
|
|
||||||
//std::string str(void) const;
|
|
||||||
|
|
||||||
std::string getProtocol(void) const;
|
|
||||||
size_t getStatusCode(void) const;
|
|
||||||
std::string getStatusText(void) const;
|
|
||||||
|
|
||||||
void setProtocol(std::string const protocol);
|
|
||||||
void setStatusCode(size_t const status_code);
|
|
||||||
void setStatusText(std::string const status_text);
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::string _method;
|
|
||||||
std::string _target;
|
|
||||||
std::string _protocol;
|
|
||||||
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
using namespace webserv;
|
using namespace webserv;
|
||||||
|
|
||||||
void http::IRequest::parse(http::IRequest const &request) { (void) request; }
|
std::string http::IRequest::str(void) const
|
||||||
|
{
|
||||||
|
std::ostringstream response;
|
||||||
|
|
||||||
|
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)
|
||||||
|
response << it->first << ": " << it->second << "\r\n";
|
||||||
|
|
||||||
|
response << "\r\n";
|
||||||
|
response << this->_body;
|
||||||
|
|
||||||
|
return (response.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void parse(std::string const &data) { (void) data; }
|
||||||
http::Response execute(void) { return (http::Response()); }
|
http::Response execute(void) { return (http::Response()); }
|
||||||
|
|
||||||
std::string http::IRequest::getMethod(void) const
|
std::string http::IRequest::getMethod(void) const
|
||||||
@ -135,3 +62,83 @@ void http::IRequest::setProtocol(std::string const protocol)
|
|||||||
{
|
{
|
||||||
this->_protocol = protocol;
|
this->_protocol = protocol;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
|
||||||
|
http::Get::Get(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
http::Get::Get(std::string &data)
|
||||||
|
{
|
||||||
|
this->parse(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void http::Get::parse(std::string const &data)
|
||||||
|
{
|
||||||
|
std::istringstream stream(data);
|
||||||
|
std::string line;
|
||||||
|
|
||||||
|
if (std::getline(stream, line))
|
||||||
|
{
|
||||||
|
std::istringstream line_stream(line);
|
||||||
|
line_stream >> this->_method >> this->_target >> this->_protocol;
|
||||||
|
this->_target.insert(this->_target.begin(), '.');
|
||||||
|
}
|
||||||
|
|
||||||
|
while (std::getline(stream, line) && line != "\r")
|
||||||
|
{
|
||||||
|
size_t delimiter_index = line.find(':');
|
||||||
|
if (delimiter_index != std::string::npos)
|
||||||
|
{
|
||||||
|
std::string key = line.substr(0, delimiter_index);
|
||||||
|
std::string value = line.substr(delimiter_index + 2);
|
||||||
|
this->_headers.insert(std::make_pair(key, value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostringstream body_stream;
|
||||||
|
while (std::getline(stream, line))
|
||||||
|
body_stream << line << "\n";
|
||||||
|
this->_body = body_stream.str();
|
||||||
|
|
||||||
|
/*
|
||||||
|
std::cout << "method: " << this->_method << std::endl;
|
||||||
|
std::cout << "target: " << this->_target << std::endl;
|
||||||
|
std::cout << "protocol: " << this->_protocol << std::endl;
|
||||||
|
std::cout << std::endl;
|
||||||
|
std::cout << "-- headers --" << std::endl;
|
||||||
|
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||||
|
std::cout << it->first << ": " << it->second << std::endl;
|
||||||
|
std::cout << std::endl;
|
||||||
|
std::cout << "-- body --" << std::endl << this->_body << std::endl;
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
http::Response http::Get::execute(void)
|
||||||
|
{
|
||||||
|
http::Response response;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
std::ifstream file(this->_target.c_str(), std::ios::binary);
|
||||||
|
response.setProtocol(this->_protocol);
|
||||||
|
response.setStatusCode(200);
|
||||||
|
response.setStatusText("OK");
|
||||||
|
response.addHeader("Content-Type", "text/html");
|
||||||
|
response.setBody(std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()));
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
// TODO: replace with a predefined array of error pages
|
||||||
|
response.setProtocol(this->_protocol);
|
||||||
|
response.setStatusCode(404);
|
||||||
|
response.setStatusText("Not Found");
|
||||||
|
response.addHeader("Content-Type", "text/html");
|
||||||
|
response.setBody("<html><body>nuh uh, get 404'd >:D</body></html>");
|
||||||
|
}
|
||||||
|
|
||||||
|
return (response);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
Reference in New Issue
Block a user