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

Feat/request #5

Merged
7 commits merged into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
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
30 changes: 21 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/12 19:17:46 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

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

# define WHITESPACE " \t\r\n"

typedef struct s_read_request {
std::string request;
std::vector<std ::string> headers;
std::string body;
bool iscomplete;

} t_read_request;

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);
bool _parseRequestLine(const std::string requestLine);
bool _parseHeaders(const std::vector<std ::string> headerLines);
bool _parseBody(const std::string bodylines);
t_read_request _separateRequestData(const std::string& requestData);
std::vector<std ::string> _dataToHeaders(std::istringstream& iss);
std::string _dataToBody(std::istringstream& iss);

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

#endif
25 changes: 5 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/12 21:22:52 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,24 +16,6 @@
# 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
};

class HttpResponse
{
public:
Expand All @@ -44,6 +26,7 @@ class HttpResponse
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 +38,18 @@ 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;
};

#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
9 changes: 7 additions & 2 deletions include/StaticFileHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
/* ::: :::::::: */
/* StaticFileHandler.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 19:24:36 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef STATICFILEHANDLER_HPP
# define STATICFILEHANDLER_HPP

# include <string>
# include <sys/stat.h>
# include "HttpRequest.hpp"
# include "HttpResponse.hpp"

# define FOLDER_PATH "./www/static"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just would like to let you know that the default directory should and will be set by the Config file in future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Thanks for letting me know. Please remind me to handle this linking part later when you give the relevant PR.

class StaticFileHandler
{
public:
Expand All @@ -26,8 +28,11 @@ class StaticFileHandler
HttpResponse handleRequest(const HttpRequest request);

private:
static std::map<std::string, std::string> _mimeTypes;

std::string getMimeType(const std::string path) const;
bool fileExists(const std::string path) const;
static void initializeMimeTypes();
};

#endif
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 21:05:14 by sanghupa #+# #+# */
/* Updated: 2024/07/09 23:16:14 by sanghupa ### ########.fr */
/* Updated: 2024/07/12 15:06:39 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
4 changes: 2 additions & 2 deletions src/main_back.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main_back.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 21:05:14 by sanghupa #+# #+# */
/* Updated: 2024/07/04 15:59:03 by sanghupa ### ########.fr */
/* Updated: 2024/07/11 20:18:19 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
Loading