Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

. #18

Merged
merged 1 commit into from
Jul 23, 2024
Merged

. #18

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion include/Request.hpp
Original file line number Diff line number Diff line change
@@ -15,9 +15,16 @@

# include "common.hpp"

enum RequestMethod {
GET,
POST,
DELETE,
OTHER
};

typedef struct s_request
{
std::string method;
RequestMethod method;
std::string uri;
std::map<std::string, std::string> headers;
} t_request;
@@ -35,6 +42,7 @@ class Request
t_request _request;

std::string _trim(const std::string & str);
void _getMethod(const std::string & str);
void _parseHTTPRequest();

};
21 changes: 20 additions & 1 deletion include/Response.hpp
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/13 13:06:55 by bmoretti #+# #+# */
/* Updated: 2024/07/13 14:02:44 by bmoretti ### ########.fr */
/* Updated: 2024/07/22 22:27:39 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

@@ -16,6 +16,25 @@
# include "common.hpp"
# include "Request.hpp"

namespace HttpStatus {
enum Code {
OK = 200,
CREATED = 201,
NO_CONTENT = 204,
MOVED_PERMANENTLY = 301,
BAD_REQUEST = 400,
FORBIDDEN = 403,
NOT_FOUND = 404,
NOT_ALLOWED = 405,
TIMEOUT = 408,
CONFLICT = 409,
PAYLOAD_TOO_LARGE = 413,
SERVER_ERR = 500,
NOT_IMPLEMENTED = 501,
SERVICE_UNAVAILABLE = 503,
};
};

typedef struct s_response
{
std::string statusLine;
19 changes: 17 additions & 2 deletions src/Request.cpp
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 18:29:18 by bmoretti #+# #+# */
/* Updated: 2024/07/13 13:29:09 by bmoretti ### ########.fr */
/* Updated: 2024/07/22 22:19:24 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

@@ -38,14 +38,29 @@ std::string Request::_trim(const std::string & str)
return str.substr(first, last - first + 1);
}

void Request::_getMethod(const std::string & str)
{
if (str == "GET") {
this->_request.method = GET;
} else if (str == "POST") {
this->_request.method = POST;
} else if (str == "DELETE") {
this->_request.method = DELETE;
} else {
this->_request.method = OTHER;
}
}

void Request::_parseHTTPRequest()
{
std::istringstream requestStream(this->_str);
std::string line;
std::string requestMethod;

std::getline(requestStream, line);
std::istringstream requestLineStream(line);
requestLineStream >> this->_request.method;
requestLineStream >> requestMethod;
this->_getMethod(requestMethod);
requestLineStream >> this->_request.uri;

while (std::getline(requestStream, line) && line != "\r") {