Skip to content

Commit

Permalink
fix: conflict해결
Browse files Browse the repository at this point in the history
  • Loading branch information
SOONG-E committed May 1, 2023
2 parents 1b7aa7e + dc01304 commit 3512716
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 59 deletions.
35 changes: 0 additions & 35 deletions cgi-bin/login.py

This file was deleted.

8 changes: 8 additions & 0 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <sys/event.h>

#include <algorithm>
#include <ctime>
#include <exception>
#include <string>
Expand Down Expand Up @@ -51,6 +52,12 @@ class Client {
void setSession(Session* session);
void setProcess(Process& cgi_process);

void setClientTimeout(std::time_t time = std::time(NULL));
void setSessionTimeout(void);
void setAllTimeout(std::time_t time = std::time(NULL));
void setTimer(std::time_t time = std::time(NULL));
void handleTimeout(void);

void processEvent(const int event_type);

/* request */
Expand Down Expand Up @@ -89,6 +96,7 @@ class Client {
std::string fullUri_;
std::string response_;
int status_;
std::time_t timeout_;

bool is_response_ready_;
};
Expand Down
1 change: 1 addition & 0 deletions include/HttpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class HttpServer {

bool isExistSessionId(std::string &id);
void addSession(std::string &id, Session *session);
void destroySession(const std::string &id);

private:
const int server_id_;
Expand Down
3 changes: 2 additions & 1 deletion include/TcpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TcpServer {
public:
typedef std::map<std::string, const HttpServer *> VirtualServerType;
typedef std::map<std::string, HttpServer *> VirtualServerType;

TcpServer(const std::string &key);
TcpServer(const std::string &ip, const std::string &port);
Expand All @@ -16,6 +16,7 @@ class TcpServer {
std::string getIp(void) const;
std::string getPort(void) const;
HttpServer *getDefaultServer(void) const;
HttpServer *getVirtualServer(const std::string &host) const;
VirtualServerType getVirtualServers(void) const;

void appendServer(const ServerBlock &servers, HttpServer *virtual_server);
Expand Down
14 changes: 2 additions & 12 deletions include/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <ctime>
#include <map>
#include <set>
#include <string>

#include "setting.hpp"

enum MethodIndex {
GET,
Expand All @@ -18,25 +19,14 @@ enum MethodIndex {
};

const std::string EMPTY_STRING = "";
const std::string DEFAULT_ERROR_DIRECTORY = "html/default_error/";
const std::string DEFAULT_ERROR_PAGE = "html/default_error/error.html";
const std::string DIRECTORY_LISTING_PAGE = "static/autoindex_template.html";
const std::string SESSION_ID_FIELD = "Session-ID";
const int CAPABLE_EVENT_SIZE = 8;
const std::size_t BUFFER_SIZE = 65536;
const int DEFAULT_FD = -1;
const std::time_t KEEPALIVE_TIMEOUT = 30;
const std::time_t SESSION_TIMEOUT = 1800;
const std::time_t CGI_TIMEOUT = 30;
const int METHODS_COUNT = 8;
const std::size_t NPOS = -1;

const std::string BASE10 = "0123456789";
const std::string COOKIE_MAX_AGE = "3600";
const std::string CRLF = "\r\n";
const std::string DOUBLE_CRLF = "\r\n\r\n";
const std::string DEFAULT_PATH = "conf/default.conf";
const std::string DEFAULT_PORT = "80";
const std::string LF = "\n";
const std::string DOUBLE_LF = "\n\n";
const std::string WHITESPACE = " \t\n\v\f\r";
Expand Down
2 changes: 2 additions & 0 deletions include/handler/CgiHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class CgiHandler {
static std::map<std::string, std::string> generateHeader(
const std::string &headers);

static void setTimer(Client *client);

static char **generateEnvp(const Client *client);

static void deleteEnvp(char **envp);
Expand Down
23 changes: 23 additions & 0 deletions include/setting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef SETTING_HPP_
#define SETTING_HPP_

#include <string>

/* setting for default value */
const std::string DEFAULT_ERROR_DIRECTORY = "html/default_error/";
const std::string DEFAULT_ERROR_PAGE = "html/default_error/error.html";
const std::string DEFAULT_PATH = "conf/default.conf";
const std::string DEFAULT_PORT = "80";
const std::string DIRECTORY_LISTING_PAGE = "static/autoindex_template.html";

/* setting for data size */
const int CAPABLE_EVENT_SIZE = 8;
const std::size_t BUFFER_SIZE = 65536;

/* setting for max time */
const std::time_t KEEPALIVE_TIMEOUT = 500;
const std::time_t SESSION_TIMEOUT = 3600;
const std::time_t CGI_TIMEOUT = 3;
const std::string COOKIE_MAX_AGE = "3600";

#endif
23 changes: 23 additions & 0 deletions src/handler/CgiHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ void CgiHandler::execute(Client* client) {
client->setProcess(process);

setPhase(client, P_WRITE);
client->setAllTimeout();
setTimer(client);
}

/*======================================//
Expand All @@ -83,6 +85,8 @@ void CgiHandler::handle(Client* client, int event_type) {
case EVFILT_PROC:
setPhase(client, P_READ);
break;
case EVFILT_TIMER:
throw ResponseException(C500);
}
}
/*=========================//
Expand Down Expand Up @@ -167,6 +171,19 @@ std::map<std::string, std::string> CgiHandler::generateHeader(
return splited_header;
}

/*=========================//
set Timer
===========================*/

void CgiHandler::setTimer(Client* client) {
if (KEEPALIVE_TIMEOUT < CGI_TIMEOUT || SESSION_TIMEOUT < CGI_TIMEOUT) {
return;
}
client->getServerManager()->createEvent(client->getFd(), EVFILT_TIMER,
EV_ADD | EV_ONESHOT, NOTE_SECONDS,
CGI_TIMEOUT, client);
}

/*=========================//
utils
===========================*/
Expand Down Expand Up @@ -269,6 +286,10 @@ void CgiHandler::setPhase(Client* client, int phase) {

case P_DONE:
close(process.input_fd);
if (CGI_TIMEOUT < KEEPALIVE_TIMEOUT && CGI_TIMEOUT < SESSION_TIMEOUT) {
manager->createEvent(client->getFd(), EVFILT_TIMER, EV_DELETE, 0, 0,
client);
}
manager->createEvent(client->getFd(), EVFILT_READ, EV_ENABLE, 0, 0,
client);
process.phase = P_DONE;
Expand All @@ -282,6 +303,8 @@ void CgiHandler::setPhase(Client* client, int phase) {
manager->createEvent(process.pid, EVFILT_PROC, EV_DELETE, 0, 0, client);
manager->createEvent(process.input_fd, EVFILT_READ, EV_DELETE, 0, 0,
client);
manager->createEvent(client->getFd(), EVFILT_TIMER, EV_DELETE, 0, 0,
client);
process.phase = P_UNSTARTED;
cleanUp(client);
}
Expand Down
1 change: 1 addition & 0 deletions src/handler/SessionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void SessionHandler::createSession(Client *client, std::string &id) {
client->getHttpServer()->addSession(id, session);

client->setSession(session);
client->setSessionTimeout();
}

Session::ValueType SessionHandler::parseData(const std::string &data) {
Expand Down
56 changes: 47 additions & 9 deletions src/server/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,46 @@ void Client::setStatus(int status) { status_ = status; }
void Client::setSession(Session* session) { session_ = session; }
void Client::setProcess(Process& cgi_process) { cgi_process_ = cgi_process; }

void Client::setClientTimeout(std::time_t time) {
timeout_ = time + KEEPALIVE_TIMEOUT;
setTimer();
}

void Client::setSessionTimeout() {
if (session_) {
session_->setTimeout();
}
setTimer();
}

void Client::setAllTimeout(std::time_t time) {
timeout_ = time + KEEPALIVE_TIMEOUT;
if (session_) {
session_->setTimeout();
}
setTimer();
}

void Client::setTimer(std::time_t time) {
time_t timeout = timeout_ - time;
if (session_) {
timeout = std::min(timeout_, session_->getTimeout()) - time;
}
manager_->createEvent(fd_, EVFILT_TIMER, EV_DELETE, 0, 0, this);
manager_->createEvent(fd_, EVFILT_TIMER, EV_ADD | EV_ONESHOT, NOTE_SECONDS,
timeout, this);
}

void Client::handleTimeout() {
if (session_ && session_->getTimeout() < std::time(NULL)) {
http_server_->destroySession(session_->getID());
delete session_;
session_ = NULL;
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!";
}
throw ConnectionClosedException(fd_);
}

/*======================//
process
========================*/
Expand All @@ -84,6 +124,9 @@ void Client::processEvent(const int event_type) {
writeData();
break;

case EVFILT_TIMER:
handleTimeout();

default:
throw std::runtime_error(strerror(errno)); // 수정!
}
Expand All @@ -92,6 +135,7 @@ void Client::processEvent(const int event_type) {
/* parse request and pass it to handler */
void Client::processRequest(void) {
try {
setClientTimeout();
std::string data = readData();
request_.tailRequest(data);
request_.parse();
Expand Down Expand Up @@ -132,15 +176,7 @@ std::string Client::readData(void) {
/* lookup associated virtual server
if there isn't a matched server then default server is setted */
void Client::lookUpHttpServer(void) {
const TcpServer::VirtualServerType virtual_servers =
tcp_server_->getVirtualServers();
std::map<std::string, const HttpServer*>::const_iterator server =
virtual_servers.find(request_.getHost());
if (server == virtual_servers.end()) {
http_server_ = tcp_server_->getDefaultServer();
return;
}
http_server_ = const_cast<HttpServer*>(server->second);
http_server_ = tcp_server_->getVirtualServer(request_.getHost());
}

/* lookup associated location block */
Expand Down Expand Up @@ -224,6 +260,8 @@ void Client::passToCgi(const int event_type) {

/* send the response to client */
void Client::writeData(void) {
setAllTimeout();

std::size_t write_bytes;
if (is_response_ready_ == false) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/server/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@ bool HttpServer::isExistSessionId(std::string& id) {

void HttpServer::addSession(std::string& id, Session* session) {
sessions_[id] = session;
}
}

void HttpServer::destroySession(const std::string& id) { sessions_.erase(id); }
4 changes: 3 additions & 1 deletion src/server/ServerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void ServerManager::runServer(void) {

while (true) {
events = kevent(kq_, &change_event_list[0], change_event_list.size(),
event_list, CAPABLE_EVENT_SIZE, 0); // timeout 추가할 지
event_list, CAPABLE_EVENT_SIZE, 0);
if (events == -1) {
throw std::runtime_error(strerror(errno));
}
Expand Down Expand Up @@ -189,6 +189,8 @@ void ServerManager::createClient(const int client_fd,
const SocketAddress socket_address) {
Client *new_client = new Client(client_fd, tcp_server, socket_address, this);
createEvent(client_fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, new_client);
createEvent(client_fd, EVFILT_TIMER, EV_ADD | EV_ONESHOT, NOTE_SECONDS,
KEEPALIVE_TIMEOUT, new_client);
clients_[client_fd] = new_client;
}

Expand Down
8 changes: 8 additions & 0 deletions src/server/TcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ void TcpServer::setDefaultServer(HttpServer *default_server) {
std::string TcpServer::getIp(void) const { return ip_; }
std::string TcpServer::getPort(void) const { return port_; }
HttpServer *TcpServer::getDefaultServer(void) const { return default_server_; }
HttpServer *TcpServer::getVirtualServer(const std::string &host) const {
VirtualServerType::const_iterator server = virtual_servers_.find(host);
if (server == virtual_servers_.end()) {
return default_server_;
}
return (server->second);
}

TcpServer::VirtualServerType TcpServer::getVirtualServers(void) const {
return virtual_servers_;
}
Expand Down

0 comments on commit 3512716

Please sign in to comment.