Skip to content

Commit

Permalink
細々とした修正 (#85)
Browse files Browse the repository at this point in the history
* fix: HTTP Requestのcontent-lengthを0で初期化するように

* fix: serverlocationのデフォルト値にHEADも追加

* fix: hostとpathをリクエストから取るように

* fix: ServerLocationのコピーassingnment operatorを実装

* fix: client_max_body_sizeの初期値を修正

* fix: requestの終了処理を実装

* fix: google testsを修正
  • Loading branch information
hayashi-ay authored Jul 21, 2022
1 parent 8aaccfa commit 2e3cd6f
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 26 deletions.
4 changes: 1 addition & 3 deletions srcs/Config/LocationContext.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include "LocationContext.hpp"

// TODO(iyamada) 初期値、どんな値を入れたら良いかわからないので適当に入れている
LocationContext::LocationContext()
: client_max_body_size_(-1), auto_index_(false) {}
: client_max_body_size_(1024), auto_index_(false) {}

LocationContext::LocationContext(const LocationContext& other) {
*this = other;
}

// TODO(iyamada) コピーの処理、いらないかも。とりあえずかいた
LocationContext& LocationContext::operator=(const LocationContext& other) {
if (this != &other) {
this->error_pages_ = other.error_pages_;
Expand Down
3 changes: 1 addition & 2 deletions srcs/Config/ServerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ static T strtonum(const std::string& s) {
return num;
}

// TODO(iyamada) 初期値、どんな値を入れたら良いかわからないので適当に入れている
ServerContext::ServerContext()
: client_max_body_size_(-1), auto_index_(false), port_(-1) {}
: client_max_body_size_(1024), auto_index_(false), port_(80) {}

ServerContext::ServerContext(const ServerContext& other) { *this = other; }

Expand Down
6 changes: 3 additions & 3 deletions srcs/Config/WebservConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

#include "ConfigProcesser.hpp"

// TODO(iyamada) 初期値、どんな値を入れたら良いかわからないので適当に入れている
WebservConfig::WebservConfig()
: client_max_body_size_(-1), auto_index_(false) {}
: client_max_body_size_(1024), auto_index_(false) {}

WebservConfig::WebservConfig(WebservConfig const &other) { *this = other; }

Expand Down Expand Up @@ -70,9 +69,10 @@ static ServerLocation CreateServerLocation() {
error_pages[505] = "/50x.html";
std::set<std::string> allow_methods;
allow_methods.insert("GET");
allow_methods.insert("HEAD");
allow_methods.insert("POST");
allow_methods.insert("DELETE");
return ServerLocation(8081, "webserv1", "/html", error_pages, 4086, false,
return ServerLocation(8081, "webserv1", "/html", error_pages, 1024, false,
"index.html", "", allow_methods, "/var/www", "");
}

Expand Down
4 changes: 3 additions & 1 deletion srcs/Http/HTTPResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
HTTPResponse::HTTPResponse()
: logging_(Logging(__FUNCTION__)),
new_line_string_("\r\n"),
connection_("close") {}
connection_("close"),
content_length_(0) {}

HTTPResponse::HTTPResponse(HTTPResponse const &other) { *this = other; }

Expand Down Expand Up @@ -114,6 +115,7 @@ std::string HTTPResponse::GetResponseString() const {

oss << this->GetStatusLineString();
oss << this->GetHeadersString();
// TODO(ahayashi): HEADリクエストの場合はbodyを付与しない
if (this->content_length() > 0) {
oss << new_line_string_;
oss << this->GetBodyString();
Expand Down
6 changes: 3 additions & 3 deletions srcs/Http/RequestFacade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ HTTPRequest *RequestFacade::SelectRequest(Socket const &socket) {
return this->list_.at(socket_fd);
}

void RequestFacade::Finish(Socket socket) {
// socketをcloseする
(void)socket;
void RequestFacade::Finish(Socket *socket) {
this->list_.erase(socket->sock_fd());
delete socket;
}
2 changes: 1 addition & 1 deletion srcs/Http/RequestFacade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RequestFacade {
public:
static RequestFacade *GetInstance();
HTTPRequest *SelectRequest(Socket const &socket);
void Finish(Socket socket);
void Finish(Socket *socket);

private:
RequestFacade();
Expand Down
11 changes: 10 additions & 1 deletion srcs/Webserv/ServerLocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ ServerLocation::ServerLocation(ServerLocation const &other) { *this = other; }

ServerLocation &ServerLocation::operator=(ServerLocation const &other) {
if (this != &other) {
(void)other;
this->port_ = other.port_;
this->path_ = other.path_;
this->error_pages_ = other.error_pages_;
this->client_max_body_size_ = other.client_max_body_size_;
this->auto_index_ = other.auto_index_;
this->index_page_ = other.index_page_;
this->redirect_uri_ = other.redirect_uri_;
this->allow_methods_ = other.allow_methods_;
this->alias_ = other.alias_;
this->cgi_extension_ = other.cgi_extension_;
}
return *this;
}
Expand Down
2 changes: 1 addition & 1 deletion srcs/Webserv/ServerLocationFacade.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "ServerLocationFacade.hpp"

ServerLocationFacade::ServerLocationFacade() { (void)server_locations_; }
ServerLocationFacade::ServerLocationFacade() {}

ServerLocationFacade::ServerLocationFacade(std::vector<ServerLocation> *vec)
: server_locations_(vec) {}
Expand Down
2 changes: 1 addition & 1 deletion srcs/Webserv/SuperVisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void SuperVisor::Watch() {
iomul.Accept(*(*itr));
} else {
Worker worker(this->facade_);
worker.Exec(*(*itr));
worker.Exec(*itr);
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions srcs/Webserv/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ Worker &Worker::operator=(Worker const &other) {

Worker::~Worker() {}

void Worker::Exec(Socket const &socket) {
void Worker::Exec(Socket *socket) {
this->logging_.Debug("start exec");

this->request_facade_ = RequestFacade::GetInstance();
HTTPRequest *request = this->request_facade_->SelectRequest(socket);
HTTPRequest *request = this->request_facade_->SelectRequest(*socket);
try {
std::string str = socket.Recv();
std::string str = socket->Recv();
request->Parse(str);
if (request->IsReady()) {
ServerLocation *sl =
this->server_location_facade_.Choose("port", "host", "path");
// TODO(ahayashi): port番号をソケットから取れるように
ServerLocation *sl = this->server_location_facade_.Choose(
"port", request->host(), request->absolute_path());
Transaction transaction;
HTTPResponse *response = transaction.Exec(request, sl);
response->Write(socket);
response->Write(*socket);
this->request_facade_->Finish(socket);
}
} catch (std::exception &e) {
Expand Down
2 changes: 1 addition & 1 deletion srcs/Webserv/Worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Worker {
Worker &operator=(Worker const &other);
~Worker();

void Exec(Socket const &socket);
void Exec(Socket *socket);

private:
Logging logging_;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_test/ConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TEST_F(ConfigParserTest, LocationContextInServerContext) {

ASSERT_EQ(conf.contexts().size(), 1);
ASSERT_EQ(conf.error_pages().empty(), true);
ASSERT_EQ(conf.client_max_body_size(), -1);
ASSERT_EQ(conf.client_max_body_size(), 1024);
ASSERT_EQ(conf.auto_index(), false);
ASSERT_EQ(conf.index_page(), "");

Expand All @@ -32,7 +32,7 @@ TEST_F(ConfigParserTest, LocationContextInServerContext) {

ASSERT_EQ(serv_context.contexts().size(), 1);
ASSERT_EQ(serv_context.error_pages().empty(), true);
ASSERT_EQ(serv_context.client_max_body_size(), -1);
ASSERT_EQ(serv_context.client_max_body_size(), 1024);
ASSERT_EQ(serv_context.auto_index(), false);
ASSERT_EQ(serv_context.index_page(), "");
ASSERT_EQ(serv_context.redirect_url(), "");
Expand All @@ -45,7 +45,7 @@ TEST_F(ConfigParserTest, LocationContextInServerContext) {
LocationContext locate_context = locate_contexts.at(0);

ASSERT_EQ(locate_context.error_pages().empty(), true);
ASSERT_EQ(locate_context.client_max_body_size(), -1);
ASSERT_EQ(locate_context.client_max_body_size(), 1024);
ASSERT_EQ(locate_context.auto_index(), false);
ASSERT_EQ(locate_context.index_page(), "");
ASSERT_EQ(locate_context.allow_methods().empty(), true);
Expand Down

0 comments on commit 2e3cd6f

Please sign in to comment.