Skip to content

Commit

Permalink
Merge branch 'main' into feat/74_createserverlocations
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamada-Ika committed Jul 22, 2022
2 parents 80f1026 + 2e3cd6f commit 40000cf
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 53 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
1 change: 0 additions & 1 deletion srcs/Config/ServerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ static T strtonum(const std::string& s) {
return num;
}

// TODO(iyamada) 初期値、どんな値を入れたら良いかわからないので適当に入れている
ServerContext::ServerContext()
: client_max_body_size_(InitialValues::kClientMaxBodySize),
auto_index_(InitialValues::kAutoIndex),
Expand Down
3 changes: 1 addition & 2 deletions srcs/Config/WebservConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ static std::string numtostr(T num) {
return ss.str();
}

// 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
6 changes: 5 additions & 1 deletion srcs/Http/HTTPRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ void HTTPRequest::ParseBodyByContentLength(std::string str) {
}
}

int HTTPRequest::CalcBodySize() const { return 0; }
// TODO(takkatao): chunked requestのbody size計算の実装が必要。
int HTTPRequest::CalcBodySize() const {
// Transactionの動作確認のための暫定的な実装。
return this->request_body_.size();
}

bool HTTPRequest::IsReady() const {
return this->is_finish_to_read_header_ && this->is_finish_to_read_body_;
Expand Down
16 changes: 9 additions & 7 deletions 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 All @@ -25,20 +26,20 @@ std::string HTTPResponse::location() const { return this->location_; }
int HTTPResponse::content_length() const { return this->content_length_; }
std::string HTTPResponse::response_body() const { return this->response_body_; }

void HTTPResponse::status_code(int status_code) {
void HTTPResponse::set_status_code(int status_code) {
this->status_code_ = status_code;
}
void HTTPResponse::connection(std::string connection) {
void HTTPResponse::set_connection(std::string connection) {
this->connection_ = connection;
}
void HTTPResponse::allow(std::string allow) { this->allow_ = allow; }
void HTTPResponse::location(std::string location) {
void HTTPResponse::set_allow(std::string allow) { this->allow_ = allow; }
void HTTPResponse::set_location(std::string location) {
this->location_ = location;
}
void HTTPResponse::content_length(int content_length) {
void HTTPResponse::set_content_length(int content_length) {
this->content_length_ = content_length;
}
void HTTPResponse::response_body(std::string response_body) {
void HTTPResponse::set_response_body(std::string response_body) {
this->response_body_ = response_body;
}

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
12 changes: 6 additions & 6 deletions srcs/Http/HTTPResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class HTTPResponse {
int content_length() const;
std::string response_body() const;

void status_code(int);
void connection(std::string);
void allow(std::string);
void location(std::string);
void content_length(int);
void response_body(std::string);
void set_status_code(int);
void set_connection(std::string);
void set_allow(std::string);
void set_location(std::string);
void set_content_length(int);
void set_response_body(std::string);

private:
Logging logging_;
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
34 changes: 26 additions & 8 deletions srcs/Http/ResponseBuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "ResponseBuilder.hpp"

#include <set>
#include <sstream>
ResponseBuilder::ResponseBuilder() : logging_(Logging(__FUNCTION__)) {}

ResponseBuilder::ResponseBuilder(ResponseBuilder const &other) {
Expand All @@ -18,19 +20,35 @@ ResponseBuilder::~ResponseBuilder() {}
HTTPResponse *ResponseBuilder::Build(std::string body) {
HTTPResponse *res = new HTTPResponse();

res->status_code(200);
res->content_length(body.size());
res->response_body(body);
res->set_status_code(200);
res->set_content_length(body.size());
res->set_response_body(body);
return res;
}

HTTPResponse *ResponseBuilder::BuildError(int status_code, ServerLocation *sl) {
(void)status_code;
(void)sl;
return new HTTPResponse();
HTTPResponse *res = new HTTPResponse();

res->set_status_code(status_code);
if (status_code == 403) {
std::ostringstream oss;
std::set<std::string>::iterator iter;

iter = sl->allow_methods().begin();
while (iter != sl->allow_methods().end()) {
oss << *iter;
iter++;
if (iter != sl->allow_methods().end()) oss << ", ";
}
res->set_allow(oss.str());
}
return res;
}

HTTPResponse *ResponseBuilder::BuildRedirect(std::string redirect_url) {
(void)redirect_url;
return new HTTPResponse();
HTTPResponse *res = new HTTPResponse();

res->set_status_code(302);
res->set_location(redirect_url);
return res;
}
11 changes: 11 additions & 0 deletions srcs/Transaction/HTTPException.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "HTTPException.hpp"

#include <stdio.h>

#include <sstream>
#include <string>

HTTPException::HTTPException(int status_code) throw()
: status_code_(status_code) {}

int HTTPException::status_code() const { return this->status_code_; }
16 changes: 16 additions & 0 deletions srcs/Transaction/HTTPException.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef SRCS_TRANSACTION_HTTPEXCEPTION_HPP_
#define SRCS_TRANSACTION_HTTPEXCEPTION_HPP_

#include <exception>

class HTTPException : public std::exception {
public:
HTTPException() throw();
explicit HTTPException(int) throw();
int status_code() const;

private:
int status_code_;
};

#endif // SRCS_TRANSACTION_HTTPEXCEPTION_HPP_
40 changes: 35 additions & 5 deletions srcs/Transaction/Transaction.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Transaction.hpp"

#include "FileReadExecutor.hpp"
#include "HTTPException.hpp"
#include "ResponseBuilder.hpp"

Transaction::Transaction() : logging_(Logging(__FUNCTION__)) {}
Expand All @@ -17,10 +18,39 @@ Transaction &Transaction::operator=(Transaction const &other) {
Transaction::~Transaction() {}

HTTPResponse *Transaction::Exec(HTTPRequest *request, ServerLocation *sl) {
if (request->method() == "GET") {
FileReadExecutor fre;
return fre.Exec(*request, *sl);
try {
if (!sl->IsAllowedMethod(request->method())) {
throw HTTPException(403); // ステータスコードを設定。
}
if (!sl->IsValidBodySize(request->CalcBodySize())) {
throw HTTPException(413);
}
if (sl->IsRedirect()) {
return ResponseBuilder::BuildRedirect(sl->redirect_uri());
}
// TODO(takkatao): CGIの処理を実装。
/*
string alias_resolved_uri = ServerLocation.ResolveAlias(request->uri());
if (sl->IsCGI()) {
return FileExecExecutor(req, sl);
}
*/
if (request->method() == "GET") {
FileReadExecutor fre;
return fre.Exec(*request, *sl);
}
// TODO(takkatao): CGIプログラム以外にPOST, DELETEが来た場合はどうなる?
/*
if (request->method() == "POST" || request->method() == "DELETE") {
throw HTTPException(XXX);
//もしくは return FileWriteExecutor(req, sl);
}
*/
return ResponseBuilder::BuildError(400, sl);
} catch (HTTPException &e) {
return ResponseBuilder::BuildError(e.status_code(), sl);
} catch (...) {
return ResponseBuilder::BuildError(500,
sl); // その他エラーは500にする。
}
logging_.Debug("*** TBD not implemented***");
return ResponseBuilder::Build(request->request_body());
}
14 changes: 10 additions & 4 deletions srcs/Webserv/ServerLocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ ServerLocation &ServerLocation::operator=(ServerLocation const &other) {
this->auto_index_ = other.auto_index_;
this->index_page_ = other.index_page_;
this->redirect_url_ = other.redirect_url_;
this->allow_methods_ = other.allow_methods_;
this->alias_ = other.alias_;
this->cgi_extension_ = other.cgi_extension_;
}
return *this;
}

ServerLocation::~ServerLocation() {}

bool ServerLocation::IsRedirect() const { return !this->redirect_url_.empty(); }
bool ServerLocation::IsAllowedMethod(std::string method) const {
return this->allow_methods().count(method) != 0;
}

bool ServerLocation::IsValidBodySize(int body_size) const {
if (body_size > this->client_max_body_size()) return false;
return true;
}

bool ServerLocation::IsRedirect() const { return !this->redirect_uri_.empty(); }

// TODO(ahayashi): 実装する。utilsに移してもいいかもしれない。
static std::string GetExtension(std::string path) {
Expand Down
2 changes: 2 additions & 0 deletions srcs/Webserv/ServerLocation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ServerLocation {
void InsertErrorPages(const std::map<int, std::string> &error_pages);
void SetDefaultAllowMethods();

bool IsAllowedMethod(std::string) const;
bool IsValidBodySize(int) const;
bool IsRedirect() const;
bool IsCGI(std::string path) const;
std::string ResolveAlias(std::string request_uri) const;
Expand Down
2 changes: 1 addition & 1 deletion srcs/Webserv/ServerLocationFacade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "ServerLocationKey.hpp"

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

ServerLocationFacade::ServerLocationFacade(
std::map<ServerLocationKey, ServerLocation> server_locations)
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
Loading

0 comments on commit 40000cf

Please sign in to comment.