Skip to content

Commit

Permalink
fix: circular dependency 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
SOONG-E committed Apr 13, 2023
1 parent 04b244f commit 5d33e2f
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 78 deletions.
1 change: 0 additions & 1 deletion include/Cgi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <map>
#include <string>

#include "HttpRequest.hpp"
#include "HttpResponse.hpp"
#include "Selector.hpp"
#include "SocketAddress.hpp"
Expand Down
6 changes: 0 additions & 6 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
#include <string>

#include "Cgi.hpp"
#include "HttpParser.hpp"
#include "HttpResponse.hpp"
#include "Selector.hpp"
#include "ServerBlock.hpp"
#include "Session.hpp"
#include "SocketAddress.hpp"

class Client {
public:
Expand Down
3 changes: 2 additions & 1 deletion include/HttpRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include <vector>

#include "HttpParser.hpp"
#include "HttpResponse.hpp"

class HttpParser;

class HttpRequest {
static const std::size_t DEFAULT_CONTENT_LENGTH;
Expand Down
2 changes: 1 addition & 1 deletion include/HttpResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <string>

#include "HttpRequest.hpp"
#include "LocationBlock.hpp"
#include "ResponseGenerator.hpp"
#include "ServerBlock.hpp"
#include "Session.hpp"

Expand Down
26 changes: 12 additions & 14 deletions include/ResponseGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
#include "HttpResponse.hpp"
#include "utility.hpp"

class HttpResponse;

class ResponseGenerator {
public:
static std::string generateResponse(HttpResponse &response,
HttpRequest &request);

private:
static std::string generateBody(HttpRequest &request, HttpResponse &response,
std::string &body);
static std::string generateHeader(HttpRequest &request,
HttpResponse &response, std::string &header,
const std::string &body);
static std::string generatePage(HttpRequest &request, HttpResponse &response,
std::string &body);
static void generateBody(HttpRequest &request, HttpResponse &response,
std::string &body);
static void generateHeader(HttpRequest &request, HttpResponse &response,
std::string &header, const std::string &body);
static void generatePage(HttpResponse &response, std::string &body);
static std::string generateErrorPage(HttpResponse &response);

static void processCgiResponse(HttpResponse &response, std::string &header,
Expand All @@ -27,13 +27,11 @@ class ResponseGenerator {
static void generateHeaderFromCgi(HttpResponse &response,
std::string &header);

static std::string generateGeneralHeader(HttpRequest &request,
HttpResponse &response,
std::string &header);
static std::string generateEntityHeader(HttpRequest &request,
HttpResponse &response,
std::string &header,
const std::string &body);
static void generateGeneralHeader(HttpRequest &request,
HttpResponse &response,
std::string &header);
static void generateEntityHeader(HttpResponse &response, std::string &header,
const std::string &body);
static std::string generateCookie(HttpRequest &request);
static std::string getConnectionHeader(HttpRequest &request,
HttpResponse &response);
Expand Down
8 changes: 2 additions & 6 deletions include/ServerManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@

#include "Client.hpp"
#include "Config.hpp"
#include "HttpRequest.hpp"
#include "LocationBlock.hpp"
#include "ResponseGenerator.hpp"
#include "Selector.hpp"
#include "ServerBlock.hpp"
#include "ServerSocket.hpp"
#include "Session.hpp"
#include "SessionHandler.hpp"

class ServerManager {
Expand Down Expand Up @@ -48,7 +43,8 @@ class ServerManager {
void sendResponse(Client& client);
const ServerBlock& findServerBlock(const std::string& server_socket_key,
const std::string& server_name);
void validateRequest(const HttpRequest& request, const LocationBlock &location_block);
void validateRequest(const HttpRequest& request,
const LocationBlock& location_block);
void deleteClients(std::queue<int>& delete_clients);
void deleteTimeoutClients();
std::string completeUri(std::string& uri,
Expand Down
17 changes: 17 additions & 0 deletions include/bootServer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef BOOT_SERVER_HPP_
#define BOOT_SERVER_HPP_

#include <iostream>

#include "Config.hpp"
#include "ConfigParser.hpp"
#include "Error.hpp"
#include "ServerManager.hpp"
#include "utility.hpp"

void checkArgs(int argc);
void printLogo();
Config createConfig(int argc, char** argv);
ServerManager setServer(const Config& config);

#endif
10 changes: 0 additions & 10 deletions include/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include <string>
#include <vector>

#include "Config.hpp"
#include "ResponseStatus.hpp"
#include "ServerManager.hpp"
#include "constant.hpp"

/*====================*/
Expand All @@ -26,14 +24,6 @@ std::vector<std::string> split(const std::string& content,
std::size_t stoi(const std::string& value);
std::string trim(const std::string& str);

/*====================*/
// bootServer.cpp //
/*====================*/
void checkArgs(int argc);
void printLogo();
Config createConfig(int argc, char** argv);
ServerManager setServer(const Config& config);

/*========================*/
// Template functions //
/*========================*/
Expand Down
8 changes: 3 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "Config.hpp"
#include "ServerManager.hpp"
#include "bootServer.hpp"
#include "constant.hpp"
#include "utility.hpp"

void runServer(ServerManager& manager) {
while (1) {
Expand All @@ -17,8 +15,8 @@ int main(int argc, char* argv[]) {
checkArgs(argc);
printLogo();

const Config& config = createConfig(argc, argv);
ServerManager& manager = setServer(config);
const Config config = createConfig(argc, argv);
ServerManager manager = setServer(config);

runServer(manager);

Expand Down
50 changes: 26 additions & 24 deletions src/response/ResponseGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,35 @@ std::string ResponseGenerator::generateResponse(HttpResponse &response,
return header + body;
}

std::string ResponseGenerator::generateBody(HttpRequest &request,
HttpResponse &response,
std::string &body) {
void ResponseGenerator::generateBody(HttpRequest &request,
HttpResponse &response,
std::string &body) {
if (response.isSuccessCode() == false) {
body = generateErrorPage(response);
return;
}
if (isCgi(request, response) == true) {
return;
}
body = generatePage(request, response, body);
generatePage(response, body);
}

std::string ResponseGenerator::generateHeader(HttpRequest &request,
HttpResponse &response,
std::string &header,
const std::string &body) {
void ResponseGenerator::generateHeader(HttpRequest &request,
HttpResponse &response,
std::string &header,
const std::string &body) {
if (header.empty()) {
header = "HTTP/1.1 " + ResponseStatus::CODES[response.getStatus()] + " " +
ResponseStatus::REASONS[response.getStatus()] + CRLF;
}
generateGeneralHeader(request, response, header);
generateEntityHeader(request, response, header, body);
header += generateCookie(request) + CRLF;
generateEntityHeader(response, header, body);
// header += generateCookie(request) + CRLF;
header += CRLF;
}

std::string ResponseGenerator::generatePage(HttpRequest &request,
HttpResponse &response,
std::string &body) {
void ResponseGenerator::generatePage(HttpResponse &response,
std::string &body) {
std::string uri = response.getFullUri();
try {
body = readFile(response, uri);
Expand Down Expand Up @@ -141,30 +140,31 @@ void ResponseGenerator::generateHeaderFromCgi(HttpResponse &response,
// generate header field //
/*==========================*/

std::string ResponseGenerator::generateGeneralHeader(HttpRequest &request,
HttpResponse &response,
std::string &header) {
void ResponseGenerator::generateGeneralHeader(HttpRequest &request,
HttpResponse &response,
std::string &header) {
header += getConnectionHeader(request, response) + CRLF;
header += getDateHeader() + CRLF;
header += getTransferEncodingHeader(request) + CRLF;
header += getTransferEncodingHeader(request);
header += "Cache-Control: no-cache, no-store, must-revalidate" + CRLF;
}

std::string ResponseGenerator::generateEntityHeader(HttpRequest &request,
HttpResponse &response,
std::string &header,
const std::string &body) {
void ResponseGenerator::generateEntityHeader(HttpResponse &response,
std::string &header,
const std::string &body) {
header += "Server: Webserv" + CRLF;
header +=
"Allow: " + join(response.getLocationBlock().getAllowedMethods(), ", ") +
CRLF;
if (body.empty() == false) {
header += "Content_Length: " + body.length() + CRLF;
header += "Content_Length: " + toString(body.length()) + CRLF;
header += "Content-Type: text/html" + CRLF;
}
}

std::string ResponseGenerator::generateCookie(HttpRequest &request) {
(void)request;
return ""; // temp
// if (session_ && request.getCookie("Session-ID") != session_->getID()) {
// header += "Set-Cookie: Session-ID=" + session_->getID() +
// "; Max-Age=" + COOKIE_MAX_AGE + "; HttpOnly;" + CRLF;
Expand All @@ -190,8 +190,10 @@ std::string ResponseGenerator::getDateHeader(void) {

std::string ResponseGenerator::getTransferEncodingHeader(HttpRequest &request) {
if (request.getHeader("Transfer-Encoding").empty() == false) {
return ("Transfer-Encoding: " + request.getHeader("Transfer-Encoding"));
return ("Transfer-Encoding: " + request.getHeader("Transfer-Encoding") +
CRLF);
}
return "";
}

/*==========================*/
Expand Down Expand Up @@ -242,7 +244,7 @@ std::string ResponseGenerator::readFile(HttpResponse &response,
url = (*uri.rbegin() == '/') ? uri : uri + '/';
return readIndexFile(response, url);
}
return readFile(response, uri);
return ::readFile(uri);
}

/*==========================*/
Expand Down
1 change: 1 addition & 0 deletions src/server/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Client::Client(int fd, const ServerBlock& default_server,
const SocketAddress& cli_addr, const SocketAddress& serv_addr)
: fd_(fd), cli_address_(cli_addr), serv_address_(serv_addr) {
this->setTimeout();
(void)default_server;
// logConnectionInfo();
}

Expand Down
2 changes: 0 additions & 2 deletions src/server/ServerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include "Error.hpp"
#include "Log.hpp"
#include "ResponseStatus.hpp"
#include "constant.hpp"
#include "exception.hpp"

ServerManager::ServerManager() {}
Expand Down
6 changes: 3 additions & 3 deletions src/session/SessionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ Session* SessionHandler::findSession(const Client& client) {
sessions_mapped_type& sessions = sessions_[server_block_key];
try {
Session& session = sessions.at(session_key);
std::string session_id = client.getRequestObj().getCookie("Session-ID");
std::string session_id = client.getRequest().getCookie("Session-ID");
if (session.getID() == session_id) {
session.setTimeout();
return &session;
}
Client* client = session.getClient();
if (client) {
client->getResponseObj().setSession(NULL);
client->getResponse().setSession(NULL);
}
sessions.erase(session_key);
} catch (const std::out_of_range& e) {
Expand All @@ -67,7 +67,7 @@ void SessionHandler::deleteTimeoutSessions() {
delete_sessions.push(mapped_it->first);
Client* client = session->getClient();
if (client) {
client->getResponseObj().setSession(NULL);
client->getResponse().setSession(NULL);
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/util/bootServer.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#include <iostream>

#include "ConfigParser.hpp"
#include "Error.hpp"
#include "utility.hpp"
#include "bootServer.hpp"

void checkArgs(int argc) {
if (argc > 2) {
Expand Down

0 comments on commit 5d33e2f

Please sign in to comment.