-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add(server): add recv_request() and test recv message from client
- Loading branch information
Showing
9 changed files
with
268 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,179 @@ | ||
#include <netdb.h> | ||
#include <errno.h> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
#include <cstdlib> | ||
#include <cstring> | ||
#include <iostream> | ||
#include "Server.hpp" | ||
#include "webserv.hpp" | ||
#include "Color.hpp" | ||
|
||
Server::Server(const char *server_ip, | ||
const char *server_port) | ||
: _socket(server_ip, server_port), | ||
_connect_fd(ERROR) { | ||
_connect_fd(ERROR), | ||
_recv_message() { | ||
if (this->_socket.get_status() == ERROR) { | ||
throw std::runtime_error("[Error] server initialization error"); | ||
} | ||
} | ||
|
||
Server::Server() {} | ||
|
||
Server::~Server() { close_connection(); } | ||
Server::~Server() { | ||
if (this->_connect_fd != ERROR) { | ||
close_connection(this->_connect_fd); | ||
this->_connect_fd = ERROR; | ||
} | ||
} | ||
|
||
void Server::process_client_connection() { | ||
char *buf = NULL; | ||
|
||
void Server::run() { | ||
char buf[BUF_SIZE]; // tmp | ||
// select | ||
// todo | ||
|
||
// accept | ||
if (accept_connection() == ERROR) { | ||
this->_connect_fd = accept_connection(this->_socket.get_socket_fd(), this->_socket.get_addr_info()); | ||
if (this->_connect_fd == ERROR) { | ||
throw std::runtime_error("[Error] accept"); | ||
} | ||
|
||
// recv | ||
if (recv_request(buf) == ERROR) { | ||
if (recv_request(this->_connect_fd, &buf) == ERROR) { | ||
throw std::runtime_error("[Error] recv"); | ||
} | ||
|
||
this->_recv_message = std::string(buf); | ||
// std::cout << YELLOW "recv_msg(string):[" << this->_recv_message << "]" RESET << std::endl; | ||
// printf(YELLOW "recv_msg(buf):[%s]\n" RESET , buf); | ||
|
||
delete buf; | ||
|
||
return; | ||
|
||
// request, response | ||
HttpRequest request = HttpRequest(buf); | ||
HttpResponse response = HttpResponse(request); | ||
|
||
// send | ||
if (send_response(response) == ERROR) { | ||
if (send_response(this->_connect_fd, response) == ERROR) { | ||
throw std::runtime_error("[Error] send"); | ||
} | ||
} | ||
|
||
int Server::accept_connection() { | ||
int Server::accept_connection(int socket_fd, struct addrinfo *addr_info) { | ||
int connect_fd; | ||
struct sockaddr *ai_addr = addr_info->ai_addr; | ||
socklen_t ai_addrlen = addr_info->ai_addrlen; | ||
|
||
errno = 0; | ||
this->_connect_fd = accept(this->_socket.get_socket_fd(), NULL, NULL); // todo: arg | ||
if (this->_connect_fd == ERROR) { | ||
connect_fd = accept(socket_fd, ai_addr, &ai_addrlen); // todo: arg | ||
// connect_fd = accept(socket_fd, NULL, NULL); // todo: arg | ||
if (connect_fd == ERROR) { | ||
std::cerr << strerror(errno) << std::endl; | ||
return ERROR; | ||
} | ||
return OK; | ||
return connect_fd; | ||
} | ||
|
||
static size_t ft_strlen(const char *str) { | ||
size_t i; | ||
|
||
i = 0; | ||
while (str && str[i]) | ||
i++; | ||
return (i); | ||
} | ||
|
||
static char *ft_strjoin_dst2src(char **dst, const char *src) | ||
{ | ||
char *newdst; | ||
size_t i, j; | ||
size_t len; | ||
|
||
len = ft_strlen(*dst) + ft_strlen(src); | ||
// newdst = (char *)malloc(sizeof(char) * (len + 1)); | ||
try { | ||
newdst = new char[len + 1]; | ||
} | ||
catch (std::bad_alloc const &e) { | ||
std::cerr << e.what() << std::endl; | ||
return NULL; | ||
} | ||
|
||
i = 0; | ||
while (*dst && (*dst)[i]) | ||
{ | ||
newdst[i] = (*dst)[i]; | ||
i++; | ||
} | ||
|
||
j = 0; | ||
while (src && src[j]) | ||
{ | ||
newdst[i + j] = src[j]; | ||
j++; | ||
} | ||
newdst[i + j] = '\0'; | ||
|
||
delete *dst; | ||
*dst = newdst; | ||
return (*dst); | ||
} | ||
|
||
// todo: while (size) | ||
// use read? | ||
int Server::recv_request(char *buf) const { | ||
int Server::recv_request(int connect_fd, char **received_request) { | ||
ssize_t recv_size; | ||
char *buf; | ||
char *save = NULL; | ||
|
||
errno = 0; | ||
recv_size = recv(this->_connect_fd, buf, BUF_SIZE, FLAG_NONE); | ||
if (recv_size == ERROR) { | ||
std::cerr << strerror(errno) << std::endl; | ||
try { | ||
buf = new char[BUFSIZ + 1]; | ||
} | ||
catch (std::bad_alloc const &e) { | ||
std::cerr << e.what() << std::endl; | ||
return ERROR; | ||
} | ||
buf[recv_size] = '\0'; | ||
|
||
while (true) { | ||
errno = 0; | ||
recv_size = recv(connect_fd, buf, BUFSIZ, FLAG_NONE); | ||
if (recv_size == ERROR) { | ||
std::cerr << strerror(errno) << std::endl; | ||
return ERROR; | ||
} | ||
buf[recv_size] = '\0'; | ||
if (ft_strjoin_dst2src(&save, buf) == NULL) { | ||
return ERROR; | ||
} | ||
if (recv_size == 0) { | ||
break; | ||
} | ||
} | ||
*received_request = save; | ||
return OK; | ||
} | ||
|
||
// todo: while | ||
// use write(fd)? | ||
int Server::send_response(const HttpResponse &response) const { | ||
// todo: while? | ||
int 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(this->_connect_fd, response_message, message_len, FLAG_NONE) == ERROR) { | ||
if (send(connect_fd, response_message, message_len, FLAG_NONE) == ERROR) { | ||
std::cerr << strerror(errno) << std::endl; | ||
return ERROR; | ||
} | ||
return OK; | ||
} | ||
|
||
void Server::close_connection() { | ||
if (this->_connect_fd != ERROR) { // todo: check here? | ||
errno = 0; | ||
if (close(this->_connect_fd) == ERROR) { | ||
std::cerr << strerror(errno) << std::endl; | ||
this->_connect_fd = ERROR; | ||
} | ||
void Server::close_connection(int connect_fd) { | ||
errno = 0; | ||
if (close(connect_fd) == ERROR) { | ||
std::cerr << strerror(errno) << std::endl; | ||
} | ||
} | ||
|
||
std::string Server::get_recv_message() const { return this->_recv_message; } | ||
struct addrinfo *Server::get_addr() const { return this->_socket.get_addr_info(); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.