Skip to content

Commit

Permalink
Merge pull request #6 from san-ghun/dev/config
Browse files Browse the repository at this point in the history
Consolidate mina/feat/request from mina and apply additional commits
  • Loading branch information
miooo0o authored Jul 14, 2024
2 parents dfae0b7 + 7bd6adc commit 4b71224
Show file tree
Hide file tree
Showing 21 changed files with 639 additions and 175 deletions.
25 changes: 25 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/webserv_test",
"args": ["abc"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
16 changes: 15 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@
"shared_mutex": "cpp",
"source_location": "cpp",
"thread": "cpp",
"typeindex": "cpp"
"typeindex": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"compare": "cpp",
"concepts": "cpp",
"deque": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"numbers": "cpp"
}
}
60 changes: 60 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++98",
"-Wall",
"-Wextra",
"-Werror",
"-lstdc++",
"-I${workspaceFolder}/include",
"${workspaceFolder}/src/main.cpp",
"${workspaceFolder}/src/network/Poller.cpp",
"${workspaceFolder}/src/network/Socket.cpp",
"${workspaceFolder}/src/server/Server.cpp",
"${workspaceFolder}/src/server/HttpRequest.cpp",
"${workspaceFolder}/src/server/HttpResponse.cpp",
"${workspaceFolder}/src/server/RequestHandler.cpp",
"${workspaceFolder}/src/server/StaticFileHandler.cpp",
"-o",
"${workspaceFolder}/webserv_test"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
],
"detail": "Generated task by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ CFLAGS = -Wall -Wextra -Werror -std=c++98

RM = rm -f

# SRC_NAME = ./src/main.cpp \
# ./src/server/Server.cpp \
# ./src/network/Poller.cpp \
# ./src/network/Socket.cpp
SRC_NAME = $(shell find ./src -iname "*.cpp")
SRC_NAME = ./src/main.cpp \
./src/server/Server.cpp \
./src/network/Poller.cpp \
./src/network/Socket.cpp \
./src/server/RequestHandler.cpp \
./src/server/HttpRequest.cpp \
./src/server/HttpResponse.cpp \
./src/server/StaticFileHandler.cpp

# SRC_NAME = $(shell find ./src -iname "*.cpp")
OBJ_NAME = $(SRC_NAME:.cpp=.o)

HEADERS = -I ./include
Expand Down
7 changes: 6 additions & 1 deletion include/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/08 23:35:53 by sanghupa ### ########.fr */
/* Updated: 2024/07/12 16:28:59 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,6 +15,7 @@

# include <string>
# include <map>
# include "Location.hpp"

class Config
{
Expand All @@ -26,8 +27,12 @@ class Config
static std::string get(const std::string key);
static int getInt(const std::string key);
static int getPort();
static Location getLocation(const std::string key);

static std::map<std::string, std::string> getConfigMap();

private:
static std::map<std::string, Location> _locationsMap;
static std::map<std::string, std::string> _configMap;

static void _parseConfigFile(const std::string filename);
Expand Down
31 changes: 22 additions & 9 deletions include/HttpRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/08 22:47:33 by minakim ### ########.fr */
/* Updated: 2024/07/14 14:49:37 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,35 +16,48 @@
# include <string>
# include <map>

# define WHITESPACE " \t\r\n"


struct t_read_request {
std::string request;
std::vector<std ::string> headers;
std::string body;
bool iscomplete;
t_read_request() : iscomplete(false) {}
};

class HttpRequest
{
public:
HttpRequest();
~HttpRequest();

bool parse(const std::string requestData);
bool parse(const std::string& requestData);

std::string getMethod() const;
std::string getPath() const;
std::string getUri() const;
std::string getVersion() const;

std::map<std::string, std::string> getHeaders() const;
std::string getBody() const;

bool isConnectionClose() const;

static std::string trim(const std::string& str);
private:
std::string _method;
std::string _path;
std::string _uri;
std::string _version;
std::map<std::string, std::string> _headers;
std::string _body;

void _parseRequestLine(const std::string requestLine);
void _parseHeaders(const std::string headerLines);
void _parseBody(const std::string bodylines);
t_read_request _splitRequestData(const std::string& requestData);
bool _parseRequestLine(const std::string requestLine);
bool _parseHeaders(const std::vector<std ::string> headerLines);
bool _parseBody(const std::string bodylines);
std::vector<std ::string> _dataToHeaders(std::istringstream& iss);
std::string _dataToBody(std::istringstream& iss);

static std::string _trim(const std::string& str);
};

#endif
34 changes: 14 additions & 20 deletions include/HttpResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/08 22:27:34 by minakim ### ########.fr */
/* Updated: 2024/07/14 17:27:02 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,34 +16,23 @@
# include <string>
# include <map>

enum e_status_code
{
STATUS_SUCCESS = 200,
STATUS_CREATED = 201,
STATUS_NO_CONTENT = 204,
STATUS_MOVED = 301,
STATUS_BAD_REQUEST = 400,
STATUS_FORBIDDEN = 403,
STATUS_NOT_FOUND = 404,
STATUS_NOT_ALLOWED = 405,
STATUS_CONFLICT = 409,
STATUS_LENGTH_REQUIRED = 411,
STATUS_TOO_LARGE = 413,
STATUS_URI_TOO_LONG = 414,
STATUS_INTERNAL_ERR = 500,
STATUS_NOT_IMPLEMENTED = 501
};
std::string getErrorPagePath(int pageCode);

class HttpResponse
{
public:
HttpResponse();
HttpResponse(const std::string filePath);
HttpResponse(const HttpResponse& other);
HttpResponse& operator=(const HttpResponse& other);
~HttpResponse();


void setStatusCode(int code);
void setStatusCode(int code, const std::string statusMessage);
void setHeader(const std::string key, const std::string value);
void setBody(const std::string bodyContent);

std::string getBody();
std::string toString() const;

static HttpResponse fromFile(const std::string filePath);
Expand All @@ -55,16 +44,21 @@ class HttpResponse
static HttpResponse requestEntityTooLarge_413();
static HttpResponse imaTeapot_418();
static HttpResponse internalServerError_500();
static HttpResponse success_200();
static HttpResponse notImplemented_501();

private:
int _statusCode;
std::string _statusMessage;
std::string _body;

std::map<std::string, std::string> _headers;

std::string _getStatusLine() const;
std::string _getHeadersString() const;
std::string _getResponseLine() const;

static const std::map<int, std::string>& _getStatusMessages();
static HttpResponse _errorResponse(int code);
};

#endif
4 changes: 2 additions & 2 deletions include/RequestHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* RequestHandler.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/04 15:59:03 by sanghupa ### ########.fr */
/* Updated: 2024/07/12 11:12:44 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
6 changes: 3 additions & 3 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:46 by sanghupa #+# #+# */
/* Updated: 2024/07/10 21:23:36 by sanghupa ### ########.fr */
/* Updated: 2024/07/12 15:16:05 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -73,7 +73,7 @@ class Server
Socket _listenSocket;
Poller _poller;
std::vector<pollfd> _pollfds;
// RequestHandler _requestHandler;
RequestHandler _requestHandler;

void _handleNewConnection();
void _handleClientData(Poller::t_event event);
Expand Down
3 changes: 2 additions & 1 deletion include/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/03 16:23:55 by sanghupa #+# #+# */
/* Updated: 2024/07/05 20:30:35 by sanghupa ### ########.fr */
/* Updated: 2024/07/12 15:57:02 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef SOCKET_HPP
# define SOCKET_HPP

# include <string>
# include <cstring>
# include <netinet/in.h>

class Socket
Expand Down
Loading

0 comments on commit 4b71224

Please sign in to comment.