Skip to content

Commit

Permalink
Merge pull request #13 from miooo0o/fix/post_21_Oct
Browse files Browse the repository at this point in the history
Fix/post 21 oct
  • Loading branch information
miooo0o authored Oct 30, 2024
2 parents 64754ff + 0be60cb commit aeed324
Show file tree
Hide file tree
Showing 29 changed files with 1,307 additions and 305 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
"random": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"numbers": "cpp"
"numbers": "cpp",
"ranges": "cpp",
"span": "cpp",
"cinttypes": "cpp"
}
}
3 changes: 3 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
"${workspaceFolder}/src/server/Server.cpp",
"${workspaceFolder}/src/server/HttpRequest.cpp",
"${workspaceFolder}/src/server/HttpResponse.cpp",
"${workspaceFolder}/src/server/ErrorResponse.cpp",
"${workspaceFolder}/src/server/Context.cpp",
"${workspaceFolder}/src/server/RequestHandler.cpp",
"${workspaceFolder}/src/server/StaticFileHandler.cpp",
"${workspaceFolder}/src/util/Location.cpp",
"${workspaceFolder}/src/util/Config.cpp",
"${workspaceFolder}/src/util/Util.cpp",
"-o",
"${workspaceFolder}/webserv_test"
],
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ SRC_NAME = ./src/main.cpp \
./src/server/Context.cpp \
./src/server/StaticFileHandler.cpp \
./src/util/Config.cpp \
./src/util/Location.cpp
./src/util/Location.cpp \
./src/util/Util.cpp

# SRC_NAME = $(shell find ./src -iname "*.cpp")
OBJ_NAME = $(SRC_NAME:.cpp=.o)
Expand Down
31 changes: 15 additions & 16 deletions include/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,31 @@
#include "Location.hpp"
#include "HttpRequest.hpp"

std::string getMatchedLocation(std::string path, const std::map<std::string, Location*>& locations);
std::string getMatchedLocation(const std::string& path, const std::map<std::string, Location*>& locations);
std::string getParentPath(const std::string& path);
std::string normalisePath(const std::string& path);

class Context {
public:
Context(ServerConfig& config, HttpRequest& request);
Context(const Context& other);
~Context();
Context& operator=(const Context& other);

const ServerConfig& getServer() const;
const Location& getLocation() const;
Context(ServerConfig& config, HttpRequest& request);
Context(const Context& other);
~Context();
Context& operator=(const Context& other);
;
const ServerConfig& getServer() const;
const Location& getLocation() const;
const HttpRequest& getRequest() const;

void setRequest(HttpRequest& request);
void setServer(ServerConfig& config);
void setLocation(Location& location);

bool isInitialized() const;
void setRequest(HttpRequest& request);
void setServer(ServerConfig& config);
void setLocation(Location& location);

private:
ServerConfig& _serverConfig;
ServerConfig& _serverConfig;
HttpRequest& _request;
Location& _location;
Location& _location;

Location& _findLocation(Context& context) const;
Location& _findLocation(Context& context) const;
};

#endif // CONTEXT_H
95 changes: 63 additions & 32 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/26 12:30:28 by minakim ### ########.fr */
/* Updated: 2024/10/23 11:19:30 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,51 +17,82 @@
# include <map>
# include <vector>

# include "Util.hpp"

class HttpResponse;


# define WHITESPACE " \t\r\n"
# define NOT_SET 0

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


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

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

std::string getMethod() const;
std::string getUri() const;
std::string getVersion() const;
public:
enum e_body_type
{
RAW,
CHUNKED,
FORM_DATA,
NONE
};

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

void setUri(std::string uri);
HttpRequest();
HttpRequest(std::string& data);
~HttpRequest();
bool parse(const std::string& requestData);

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

size_t getContentLength() const;

void setUri(const std::string& uri);
void setMethod(const std::string& method);
void setVersion(const std::string& version);
void setHeaders(const std::map<std::string, std::string>& headers);
void setBody(const std::vector<std::string>& bodyLines, e_body_type type);
void setBody(const std::string& bodyLines, e_body_type type);
void setContentLength(const ssize_t& contentLength);

bool hasBody() const;

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

private:
std::string _method; // GET, POST, DELETE
std::string _uri; //
std::string _version; // HTTP/1.1
std::map<std::string, std::string> _headers; // key: value
std::string _body; // raw, chunked, formdata
e_body_type _type; // type of body @see e_body_type
std::pair<bool, size_t> _content; // from Headers["Content-Length"], if not found NOT_SET -1

t_readed_parts _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> _convertPartToHeaders(std::istringstream& iss);
std::string _convertPartToBody(std::istringstream& iss);

ReadedLines _splitRequestData(const std::string& requestData);

bool _processRequestBody(const std::string& bodyLines);
bool _parseRequestLine(const std::string& requestLine);
bool _parseHeaders(const std::vector<std ::string>& headerLines);

std::vector<std ::string> _convertPartToHeaders(std::istringstream& iss);
std ::string _convertPartToBodyLines(std::istringstream& iss);

};

// TODO: implement "<< operator" for HttpRequest
// std::ostream& operator<<(std::ostream& os, const HttpRequest& request);
#endif
50 changes: 22 additions & 28 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/08/08 21:47:33 by minakim ### ########.fr */
/* Updated: 2024/10/22 23:10:03 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,6 +17,7 @@
# include <map>

# include "Config.hpp"
# include "Util.hpp"

class HttpRequest;
class Config;
Expand All @@ -30,25 +31,26 @@ struct t_page_detail
bool isValid;
};

enum e_status {
STATUS_INVALID = -1,
STATUS_INFORMATIONAL,
STATUS_SUCCESS,
STATUS_REDIRECTION,
STATUS_ERROR
};

bool isFile(const std::string path);
bool isDir(const std::string path);
// std::string generateHtmlBody(int code, const std::string& message);

class HttpResponse
{
public:
// HttpResponse();
// HttpResponse(const std::string& filePath);
// HttpResponse(const ServerConfig& config);
// HttpResponse(const ServerConfig& config, const Location& location);
HttpResponse(const Context& context);
HttpResponse(const Context& context, 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);
Expand All @@ -60,25 +62,12 @@ class HttpResponse
std::string getBody();
size_t getBodyLength();
std::string getResponseLine() const;
std::string toString() const;
std::string generateResponseToString() const;
int getStatusCode() const;
std::string getStatusMessage() const;
std::string toString(int value) const;
std::string toString(size_t value) const;
void initializefromFile(const std::string& filePath);

// static HttpResponse badRequest_400();;
// static HttpResponse forbidden_403();
// static HttpResponse notFound_404();
// static HttpResponse methodNotAllowed_405();
// static HttpResponse requestTimeout_408();
// static HttpResponse requestEntityTooLarge_413();
// static HttpResponse imaTeapot_418();
// static HttpResponse internalServerError_500();
// static HttpResponse success_200();
// static HttpResponse notImplemented_501();

static HttpResponse _createErrorResponse(int code, const Context& context);
void initializefromFile(const Context& context, const std::string& filePath);

static HttpResponse createErrorResponse(int code, const Context& context);
static HttpResponse badRequest_400(const Context& context);
static HttpResponse forbidden_403(const Context& context);
static HttpResponse notFound_404(const Context& context);
Expand All @@ -91,6 +80,8 @@ class HttpResponse
static HttpResponse success_200(const Context& context);


static e_status checkStatusRange(int code);

private:
int _statusCode;
std::string _statusMessage;
Expand All @@ -100,18 +91,21 @@ class HttpResponse

std::string _getStatusLine() const;
std::string _getHeadersString() const;
void _fileToBody(const std::string& filePath);
void _fileToBody( const Context& context, const std::string& filePath);
std::string _generateHtmlBody();
void _setDefaultHeadersImpl();

static const std::map<int, std::string>& _StaticInitStatusMap();
static const std::map<int, std::string>& _staticInitStatusMap();

protected:
Context& _context;

HttpResponse createErrorResponse(int code);
HttpResponse _createSimpleHttpResponse(int code);
HttpResponse _createErrorResponse(int code);
t_page_detail _constructPageDetail(const std::string& path);
};


// TODO: implement "<< operator" for HttpResponse
// std::ostream& operator<<(std::ostream& os, const HttpResponse& response);
#endif
8 changes: 5 additions & 3 deletions include/RequestHandler.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/08/08 20:47:32 by minakim ### ########.fr */
/* Updated: 2024/10/22 19:51:30 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

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

class HttpResponse;
class HttpRequest;
class ServerConfig;
class Location;
class Context;

Expand All @@ -35,11 +34,14 @@ class RequestHandler
private:
StaticFileHandler _staticFileHandler;

HttpResponse _processRequest(const Context& context);
HttpResponse _processStandardMethods(const Context& context);

bool _isAllowedMethod(const Context& context) const;
HttpResponse _handleGet(const Context& context);
HttpResponse _handlePost(const Context& context);
HttpResponse _handleDelete(const Context& context);

bool _isCGIReqeust(const Context& context) const;
HttpResponse _handleCGIRequest(const Context& context);
};
#endif
Loading

0 comments on commit aeed324

Please sign in to comment.