Skip to content

Commit

Permalink
Merge pull request #10 from miooo0o/dev/location_test_pr
Browse files Browse the repository at this point in the history
Dev/location test pr
  • Loading branch information
san-ghun authored Jul 30, 2024
2 parents 2fdcf24 + 301f682 commit 08931a4
Show file tree
Hide file tree
Showing 11 changed files with 955 additions and 327 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/webserv_test",
"args": ["abc"],
"args": ["webserv.conf"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
Expand Down
2 changes: 2 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"${workspaceFolder}/src/server/HttpResponse.cpp",
"${workspaceFolder}/src/server/RequestHandler.cpp",
"${workspaceFolder}/src/server/StaticFileHandler.cpp",
"${workspaceFolder}/src/util/Location.cpp",
"${workspaceFolder}/src/util/Config.cpp",
"-o",
"${workspaceFolder}/webserv_test"
],
Expand Down
20 changes: 12 additions & 8 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/14 14:49:37 by minakim ### ########.fr */
/* Updated: 2024/07/26 12:30:28 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,16 +15,18 @@

# include <string>
# include <map>
# include <vector>

# define WHITESPACE " \t\r\n"
class HttpResponse;

# define WHITESPACE " \t\r\n"

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

class HttpRequest
Expand All @@ -42,6 +44,8 @@ class HttpRequest
std::map<std::string, std::string> getHeaders() const;
std::string getBody() const;

void setUri(std::string uri);

bool isConnectionClose() const;
static std::string trim(const std::string& str);
private:
Expand All @@ -51,12 +55,12 @@ class HttpRequest
std::map<std::string, std::string> _headers;
std::string _body;

t_read_request _splitRequestData(const std::string& requestData);
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> _dataToHeaders(std::istringstream& iss);
std::string _dataToBody(std::istringstream& iss);
std::vector<std ::string> _convertPartToHeaders(std::istringstream& iss);
std::string _convertPartToBody(std::istringstream& iss);

};

Expand Down
84 changes: 57 additions & 27 deletions include/HttpResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* HttpResponse.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/14 22:26:03 by sanghupa ### ########.fr */
/* Updated: 2024/07/25 16:46:01 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,50 +16,80 @@
# include <string>
# include <map>

std::string getErrorPagePath(int pageCode);
# include "Config.hpp"

# define LOCATION_ROOT_PATH "/www"

class HttpRequest;
class Config;

struct t_page_detail
{
std::string path;
bool isValid;
};

std::string getFullErrorPath(const std::string& path);
bool isFile(const std::string path);
bool isDir(const std::string path);
t_page_detail constructPageDetail(const std::string& path);


class HttpResponse
{
public:
HttpResponse();
HttpResponse(const std::string filePath);
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;

void fromFile(const std::string filePath);
// static HttpResponse fromFile(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();
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);
void setDefaultHeaders();
static void setDefaultHeaders(HttpResponse& resp);

std::string getBody();
size_t getBodyLength();
std::string getResponseLine() const;
std::string toString() 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();

private:
int _statusCode;
std::string _statusMessage;
std::string _body;
std::map<std::string, std::string> _headers;
size_t _bodyLength;

std::string _getStatusLine() const;
std::string _getHeadersString() const;
std::string _getResponseLine() const;
void _fileToBody(const std::string& filePath);

static HttpResponse _createErrorResponse(int code);
static HttpResponse _createSimpleHttpResponse(int code);

std::string _generateHtmlBody();
void _setDefaultHeadersImpl();

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

#endif
23 changes: 19 additions & 4 deletions include/RequestHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,42 @@
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/12 11:12:44 by minakim ### ########.fr */
/* Updated: 2024/07/23 22:24:17 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef REQUESTHANDLER_HPP
# define REQUESTHANDLER_HPP

# include "HttpRequest.hpp"
# include "HttpResponse.hpp"
class HttpResponse;
class HttpRequest;
class Location;

# include <algorithm>
# include "StaticFileHandler.hpp"
# include "webserv.hpp"

std::string _extractPathFromUri(const std::string& uri);
std::string _getMatchedLocation(std::string path,
const std::map<std::string,
Location*>& locations);
class RequestHandler
{
public:
RequestHandler();
~RequestHandler();

HttpResponse handleRequest(const HttpRequest request);
HttpResponse handleRequest(const HttpRequest& request);

private:
StaticFileHandler _staticFileHandler;

Location* _findLocation(const std::string& uri);
HttpResponse _processRequest(const HttpRequest& request, const Location& location);

bool _isAllowedMethod(const std::string &target, std::vector<std::string> list) const;
HttpResponse _handleGet(const HttpRequest& request, const Location& location);
HttpResponse _handlePost(const HttpRequest& request, const Location& location);
HttpResponse _handleDelete(const HttpRequest& request, const Location& location);
};
#endif
49 changes: 36 additions & 13 deletions include/StaticFileHandler.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/14 16:44:21 by minakim ### ########.fr */
/* Updated: 2024/07/24 00:35:43 by minakim ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,30 +15,53 @@

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

// FIXME: Delete when correct logic is applied in the future
# define LOCATION_FIXME "./www/static"
# define LOOT_DIR "./www/static"
class HttpResponse;
class HttpRequest;
class Location;

# define LOCATION_PATH "./www/static"
# define INDEX_HTML "index.html"

std::string genDirListingHtml(const std::string& path);

class StaticFileHandler
{
public:
StaticFileHandler();
~StaticFileHandler();
StaticFileHandler(const StaticFileHandler& other);
StaticFileHandler& operator=(const StaticFileHandler& other);

HttpResponse handleRequest(const HttpRequest& request, const Location& location);

HttpResponse handleRequest(const HttpRequest request);
std::string getFullPath() const;
std::string resolveMimeType(const std::string path) const;

private:
static std::map<std::string, std::string> _staticMimeTypes;
std::string _handledPath;

static void _staticInitMimeTypes();

HttpResponse _handleDirListing(const HttpRequest& request, const Location& location);
HttpResponse _handleDirRequest(const HttpRequest& request, const Location& location);
HttpResponse _handleFileRequest(const HttpRequest& request, const Location& location);


HttpResponse _createResponseForFile() const;
HttpResponse _createDirListingResponse() const;

HttpResponse _handleRoot(const Location& location);
HttpResponse _handleNotFound();

std::string _buildPathWithUri(const HttpRequest& request, const Location& location) const;
std::string _buildAbsolutePathWithRoot(const Location& location) const;
std::string _buildAbsolutePathWithIndex(const Location& location) const;


std::string _getMimeType(const std::string path) const;
bool _fileExists(const std::string path) const;
static void _staticInitializeMimeTypes();
HttpResponse _handleRoot();
std::string _getFilePath(const std::string& uri) const;
HttpResponse _handleFileNotFound();
void _setHandledPath(const std::string& fullPath);
};

#endif
Loading

0 comments on commit 08931a4

Please sign in to comment.