Skip to content

Commit

Permalink
add: client
Browse files Browse the repository at this point in the history
  • Loading branch information
ak0327 committed Mar 8, 2024
1 parent 5b920d7 commit 6afb5ca
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
24 changes: 21 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ CGI_DIR = $(RESPONSE_DIR)/CgiHandler
SRCS += $(CGI_DIR)/CgiHandler.cpp


# CLIENT -----------------------------------------------------------------------
#CLIENT_DIR = Client
#CLIENT_SRC = $(CLIENT_DIR)/Client.cpp \
# $(CLIENT_DIR)/client_main.cpp
#CLIENT_OBJ = $(CLIENT_SRC:%.cpp=%.o)
#CLIENT_OBJS = $(addprefix $(OBJS_DIR)/, $(CLIENT_OBJ)) \
# $(filter-out $(OBJS_DIR)/main.o, $(OBJS))


INCLUDES = $(addprefix -I, $(INCLUDES_DIR))



# OBJS -------------------------------------------------------------------------
OBJS_DIR = objs
OBJS = $(SRCS:%.cpp=$(OBJS_DIR)/%.o)
Expand Down Expand Up @@ -169,7 +182,8 @@ INCLUDES_DIR = includes \
$(SRCS_DIR)/$(CONFIG_DIR)/Tokenizer \
$(SRCS_DIR)/$(CONFIG_DIR) \
$(SRCS_DIR)/$(EVENT_DIR) \
$(SRCS_DIR)/$(CGI_DIR)
$(SRCS_DIR)/$(CGI_DIR) \
$(SRCS_DIR)/$(CLIENT_DIR)

REQUEST_INCLUDES = $(SRCS_DIR)/$(REQUEST_DIR) \
$(SRCS_DIR)/$(DATE_DIR) \
Expand All @@ -189,8 +203,6 @@ RESPONSE_INCLUDES = $(SRCS_DIR)/$(RESPONSE_DIR) \
$(SRCS_DIR)/$(RESPONSE_DIR)/POST \
$(SRCS_DIR)/$(RESPONSE_DIR)/DELETE

INCLUDES = $(addprefix -I, $(INCLUDES_DIR))


# RULES ------------------------------------------------------------------------
.PHONY : all
Expand Down Expand Up @@ -398,5 +410,11 @@ run_post_test :
./build/unit_test --gtest_filter=HttpResponsePOST*
@. test/integration/prepare_test_file.sh; clear_test_files


#.PHONY : client
#client : $(CLIENT_OBJS)
# $(CXX) $(CXXFLAGS) -o $@ $^


# include DEPS -----------------------------------------------------------------
-include $(DEPS)
4 changes: 2 additions & 2 deletions conf/webserv.conf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# webserv conf

http {
recv_timeout 10s; # original; combined client_header_timeout and client_body_timeout
recv_timeout 30s; # original; combined client_header_timeout and client_body_timeout
send_timeout 10s;
keepalive_timeout 10s; # 0: disable keep-alive
keepalive_timeout 30s; # 0: disable keep-alive

# static file server -------------------------------------------------------
server {
Expand Down
20 changes: 12 additions & 8 deletions srcs/Client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void Client::send_msg(const std::string &send_msg) const {
DEBUG_CLIENT_PRINT("client msg[%s], size:[%zu]", send_msg.c_str(), send_msg.size());

errno = 0;
send_size = send(this->connect_fd_, send_msg.c_str(), send_msg.size(), FLAG_NONE);
send_size = send(this->connect_fd_, send_msg.c_str(), send_msg.size(), MSG_NOSIGNAL);
if (send_size == SEND_ERROR) {
std::string error_msg = CREATE_ERROR_INFO_ERRNO(errno);
std::string err_str = "[Client Error] send: " + error_msg;
Expand All @@ -65,38 +65,42 @@ void Client::send_msg(const std::string &send_msg) const {
DEBUG_CLIENT_PRINT("client send end");
}

void Client::recv_msg() {
void Client::recv_msg(std::size_t bufsize = BUFSIZ) {
ssize_t recv_size;
char buf[BUFSIZ + 1];
char* buf = new char[bufsize + 1];
std::string recv_msg;
std::size_t total_size = 0;

DEBUG_CLIENT_PRINT("client recv start");
while (true) {
errno = 0;

recv_size = recv(this->connect_fd_, buf, BUFSIZ, FLAG_NONE);
recv_size = recv(this->connect_fd_, buf, bufsize, FLAG_NONE);
DEBUG_CLIENT_PRINT(" client recv_size:%zu", recv_size);
if (recv_size == 0) {
break;
}
if (recv_size == RECV_ERROR) {
std::string error_msg = CREATE_ERROR_INFO_ERRNO(errno);
std::string err_str = "[Client Error] recv: " + error_msg;
delete[] buf;
throw std::runtime_error(RED + err_str + RESET);
}
buf[recv_size] = '\0';
DEBUG_CLIENT_PRINT(" client: recv[%s]", std::string(buf, recv_size).c_str());
total_size += recv_size;
DEBUG_CLIENT_PRINT(" client: recv[%s], total:%zu",
std::string(buf, recv_size).c_str(), total_size);

recv_msg += buf;

if (recv_size < BUFSIZ) {
if (static_cast<std::size_t>(recv_size) < bufsize) {
break;
}
sleep(1);
}
DEBUG_CLIENT_PRINT(" client: recv_message[%s]", recv_msg.c_str());
this->recv_message_ = recv_msg;

DEBUG_CLIENT_PRINT("client recv end");
delete[] buf;
}

std::string Client::get_recv_message() const { return this->recv_message_; }
2 changes: 1 addition & 1 deletion srcs/Client/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Client {

std::string get_recv_message() const;
void send_msg(const std::string &send_msg) const;
void recv_msg();
void recv_msg(std::size_t bufsize);

private:
// struct sockaddr_in addr_;
Expand Down
19 changes: 19 additions & 0 deletions srcs/Client/client_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include "Client.hpp"

int main() {
try {
Client client("127.0.0.1", "4242");

std::string request_msg = "GET / HTTP/1.1\r\n"
"Host: a\r\n"
"\r\n";
client.send_msg(request_msg);
client.recv_msg(10);
}
catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}

0 comments on commit 6afb5ca

Please sign in to comment.