feat: done boolean + response to char *

This commit is contained in:
3lswear
2022-02-05 15:06:28 +03:00
parent d37f013069
commit b243ca893b
3 changed files with 50 additions and 28 deletions

View File

@@ -5,6 +5,7 @@
Client::Client()
{
allRead = false;
done = false;
this->_fd = -1;
this->_sended = 0;
}
@@ -12,6 +13,7 @@ Client::Client()
Client::Client(char *str)
{
allRead = false;
done = false;
this->_fd = -1;
this->_buff = str;
this->_sended = 0;
@@ -21,6 +23,7 @@ Client::Client(char *str)
Client::Client(char *str, ServerConfig *config)
{
allRead = false;
done = false;
this->_fd = -1;
this->_config = config;
this->_buff = str;
@@ -40,9 +43,9 @@ Response Client::getResponse(void)
return (_response);
}
std::string Client::getStrToSend(void)
char *Client::getStrToSend(void)
{
return (_toSend);
return (_to_send_char);
}
int Client::getFd(void)
@@ -98,8 +101,8 @@ bool Client::isChunked(void)
bool Client::allSended(void)
{
if (_toSend.size() <= _sended)
return (true);
return (false);
done = true;
return (done);
}
bool Client::allRecved(void)
@@ -134,12 +137,21 @@ int Client::sendResponse(int fd)
std::string Client::generateRespons(void)
{
size_t len;
_response.setData(_request, _config);
_response.generate();
_headerToSend = _response.getHeader();
_bodyToSend = _response.getBody();
_toSend = _headerToSend + _bodyToSend;
len = _toSend.size();
response_len = len;
/* _to_send_char = new char[len + 1]; */
_to_send_char = (char *)malloc(sizeof(char) * (len + 1));
std::memcpy(_to_send_char, _toSend.c_str(), len + 1);
return (_toSend);
}
@@ -222,8 +234,10 @@ void Client::clear(void)
_bodyToSend = "";
_headerToSend = "";
_toSend = "";
free(_to_send_char);
}
Client::~Client()
{
DBOUT << "client destructor called" << ENDL;
}

View File

@@ -6,6 +6,7 @@
#include "ServerConfig.hpp"
#include "Request.hpp"
#include "Response.hpp"
#include <cstring>
class Client
{
@@ -25,6 +26,7 @@ private:
std::string _bodyToSend;
std::string _headerToSend;
std::string _toSend;
char *_to_send_char;
std::map<std::string, std::string> _errorCode;
public:
@@ -39,11 +41,13 @@ public:
int serverfd;
size_t readn;
size_t left;
bool done;
size_t response_len;
public:
Request getRequest(void);
Response getResponse(void);
std::string getStrToSend(void);
char *getStrToSend(void);
unsigned int getCounter(void) const;
void setRawData(char *);
void setFd(int);