Skip to content

Commit

Permalink
add(Result): add result and update Server()
Browse files Browse the repository at this point in the history
  • Loading branch information
ak0327 committed Aug 28, 2023
1 parent 715612a commit fe52c7d
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 36 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
!config

!srcs
!srcs/Socket
!srcs/Server
!srcs/Client
!srcs/Result
!srcs/Server
!srcs/Socket
!www
!includes
!test
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(webserv)

set(CMAKE_CXX_STANDARD 98)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "-std=c++98 -Wall -Wextra -Werror -pedantic -g -fsanitize=address,undefined -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -pedantic -g -fsanitize=address,undefined -fno-omit-frame-pointer")

# google test ------------------------------------------------------------------
include(FetchContent)
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ run_server_test :
#rm -rf build
cmake -S . -B build
cmake --build build
./build/unit_test --gtest_filter=Server* 2>/dev/null
# ./build/unit_test --gtest_filter=Server* 2>/dev/null
./build/unit_test --gtest_filter=Server*

.PHONY : run_socket_test
run_socket_test :
Expand Down
42 changes: 42 additions & 0 deletions includes/Result.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <iostream>

template <typename OkType, typename ErrType>
class Result {
public:
static Result Ok(OkType value) {
Result res;
res._is_ok = true;
res._ok_value = value;
return res;
}

static Result Err(ErrType value) {
Result res;
res._is_ok = false;
res._err_value = value;
return res;
}

bool is_ok() const { return _is_ok; }

OkType get_ok_value() const {
if (_is_ok) {
return _ok_value;
}
throw std::runtime_error("Result is not Ok");
}

ErrType get_err_value() const {
if (!_is_ok) {
return _err_value;
}
throw std::runtime_error("Result is not Err");
}

private:
bool _is_ok;
OkType _ok_value;
ErrType _err_value;
};
56 changes: 27 additions & 29 deletions srcs/Server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include "Server.hpp"
#include "webserv.hpp"
#include "Color.hpp"
#include "Result.hpp"
#include "Server.hpp"

Server::Server(const char *server_ip,
const char *server_port)
: _socket(server_ip, server_port),
_connect_fd(ERROR),
_recv_message() {
if (this->_socket.get_status() == ERROR) {
if (this->_socket.get_status() == ERROR) { // todo: use result
throw std::runtime_error("[Error] server initialization error");
}
}
Expand All @@ -31,75 +32,73 @@ void Server::process_client_connection() {
// todo

// accept
this->_connect_fd = accept_connection(this->_socket.get_socket_fd());
if (this->_connect_fd == ERROR) {
throw std::runtime_error("[Error] accept");
Result<int, std::string> accept_result = accept_connection(this->_socket.get_socket_fd());
if (!accept_result.is_ok()) {
throw std::runtime_error("[Error] accept:" + accept_result.get_err_value());
}
this->_connect_fd = accept_result.get_ok_value();

// recv
std::string recv_msg;
if (recv_request(this->_connect_fd, &recv_msg) == ERROR) {
throw std::runtime_error("[Error] recv");
Result<std::string, std::string>recv_result = recv_request(this->_connect_fd);
if (!recv_result.is_ok()) {
throw std::runtime_error("[Error] recv:" + recv_result.get_err_value());
}

this->_recv_message = recv_msg;
this->_recv_message = recv_result.get_err_value();
// std::cout << YELLOW "recv_msg(string):[" << this->_recv_message << "]" RESET << std::endl;

// request, response
HttpRequest request = HttpRequest(this->_recv_message);
HttpResponse response = HttpResponse(request);

// send
if (send_response(this->_connect_fd, response) == ERROR) {
throw std::runtime_error("[Error] send");
Result<int, std::string> send_result = send_response(this->_connect_fd, response);
if (!send_result.is_ok()) {
throw std::runtime_error("[Error] send:" + send_result.get_err_value());
}
}

std::string Server::get_recv_message() const { return this->_recv_message; }

int Server::accept_connection(int socket_fd) {
Result<int, std::string> Server::accept_connection(int socket_fd) {
int connect_fd;

errno = 0;
connect_fd = accept(socket_fd, NULL, NULL); // NULL:peer addr not needed
if (connect_fd == ERROR) {
std::cerr << strerror(errno) << std::endl;
return ERROR;
return Result<int, std::string>::Err(strerror(errno));
}
return connect_fd;
return Result<int, std::string>::Ok(connect_fd);
}

int Server::recv_request(int connect_fd, std::string *recv_msg) {
ssize_t recv_size;
char buf[BUFSIZ + 1];
Result<std::string, std::string> Server::recv_request(int connect_fd) {
char buf[BUFSIZ + 1];
ssize_t recv_size;
std::string recv_msg;

*recv_msg = "";
while (true) {
errno = 0;
recv_size = recv(connect_fd, buf, BUFSIZ, FLAG_NONE);
if (recv_size == ERROR || recv_size > BUFSIZ) {
std::cerr << strerror(errno) << std::endl;
return ERROR;
return Result<std::string, std::string>::Err(strerror(errno));
}
buf[recv_size] = '\0';
*recv_msg += buf;
recv_msg += buf;
if (recv_size < BUFSIZ) {
break;
}
}
return OK;
return Result<std::string, std::string>::Ok(recv_msg);
}

int Server::send_response(int connect_fd, const HttpResponse &response) {
Result<int, std::string> Server::send_response(int connect_fd, const HttpResponse &response) {
char *response_message = response.get_response_message();
size_t message_len = response.get_response_size();

errno = 0;
if (send(connect_fd, response_message, message_len, FLAG_NONE) == ERROR) {
std::cerr << strerror(errno) << std::endl;
return ERROR;
return Result<int, std::string>::Err(strerror(errno));
}
return OK;
return Result<int, std::string>::Ok(OK);
}

void Server::close_connection(int connect_fd) {
Expand All @@ -108,4 +107,3 @@ void Server::close_connection(int connect_fd) {
std::cerr << strerror(errno) << std::endl;
}
}

7 changes: 4 additions & 3 deletions srcs/Server/Server.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

# include <string>
# include "Result.hpp"
# include "Socket.hpp"

# define FLAG_NONE 0
Expand Down Expand Up @@ -37,8 +38,8 @@ class Server {
int _connect_fd; // close in destructor
std::string _recv_message;

static int accept_connection(int socket_fd);
static int recv_request(int connect_fd, std::string *recv_msg);
static int send_response(int connect_fd, const HttpResponse &response);
static Result<int, std::string> accept_connection(int socket_fd);
static Result<std::string, std::string> recv_request(int connect_fd);
static Result<int, std::string> send_response(int connect_fd, const HttpResponse &response);
static void close_connection(int connect_fd);
};
2 changes: 2 additions & 0 deletions test/unit_test/TestServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ static void *run_server(void *server_info) {
}
catch (std::exception const &e) {
is_server_success = false;
std::cerr << e.what() << std::endl;
}
return (void *)(is_server_success);
}
Expand All @@ -183,6 +184,7 @@ static void *run_client(void *client_info) {
}
catch (std::exception const &e) {
is_client_success = false;
std::cerr << e.what() << std::endl;
}
return (void *)(is_client_success);
}
Expand Down

0 comments on commit fe52c7d

Please sign in to comment.