diff --git a/Makefile b/Makefile index 4e5583bc..0a86620f 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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) \ @@ -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 @@ -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) diff --git a/conf/webserv.conf b/conf/webserv.conf index 59e48a1a..23a549b4 100644 --- a/conf/webserv.conf +++ b/conf/webserv.conf @@ -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 { diff --git a/srcs/Client/Client.cpp b/srcs/Client/Client.cpp index 04d4f763..83d2e3c6 100644 --- a/srcs/Client/Client.cpp +++ b/srcs/Client/Client.cpp @@ -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; @@ -65,16 +65,16 @@ 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; @@ -82,21 +82,25 @@ void Client::recv_msg() { 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(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_; } diff --git a/srcs/Client/Client.hpp b/srcs/Client/Client.hpp index c48aa861..942f2797 100644 --- a/srcs/Client/Client.hpp +++ b/srcs/Client/Client.hpp @@ -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_; diff --git a/srcs/Client/client_main.cpp b/srcs/Client/client_main.cpp new file mode 100644 index 00000000..e0415f75 --- /dev/null +++ b/srcs/Client/client_main.cpp @@ -0,0 +1,19 @@ +#include +#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; +}