mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
add: download files
This commit is contained in:
@@ -71,6 +71,11 @@ void Client::setRawData(char *str)
|
|||||||
this->_buff = str;
|
this->_buff = str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Client::setRawData(std::string &buf)
|
||||||
|
{
|
||||||
|
_stringBUF = buf;
|
||||||
|
}
|
||||||
|
|
||||||
void Client::setFd(int fd)
|
void Client::setFd(int fd)
|
||||||
{
|
{
|
||||||
this->_fd = fd;
|
this->_fd = fd;
|
||||||
@@ -80,7 +85,8 @@ void Client::setFd(int fd)
|
|||||||
|
|
||||||
int Client::parseRequest(void)
|
int Client::parseRequest(void)
|
||||||
{
|
{
|
||||||
_request.setData(_buff);
|
// _request.setData(_buff);
|
||||||
|
_request.setData(_stringBUF);
|
||||||
_ret = _request.parseRequest();
|
_ret = _request.parseRequest();
|
||||||
|
|
||||||
return (_ret);
|
return (_ret);
|
||||||
@@ -154,6 +160,9 @@ std::string Client::generateRespons(void)
|
|||||||
_to_send_char = new char[len + 1];
|
_to_send_char = new char[len + 1];
|
||||||
std::memcpy(_to_send_char, _toSend.c_str(), len + 1);
|
std::memcpy(_to_send_char, _toSend.c_str(), len + 1);
|
||||||
|
|
||||||
|
// DBOUT << "len = " << len << ENDL;
|
||||||
|
// DBOUT << "strlen = " << strlen(_to_send_char) << ENDL;
|
||||||
|
// DBOUT << "content_lenth = " << _request.getContentLength() << ENDL;
|
||||||
return (_toSend);
|
return (_toSend);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ private:
|
|||||||
unsigned int _sended;
|
unsigned int _sended;
|
||||||
char *_buff;
|
char *_buff;
|
||||||
|
|
||||||
|
std::string _stringBUF;
|
||||||
std::string _bodyToSend;
|
std::string _bodyToSend;
|
||||||
std::string _headerToSend;
|
std::string _headerToSend;
|
||||||
std::string _toSend;
|
std::string _toSend;
|
||||||
@@ -51,6 +51,7 @@ public:
|
|||||||
char *getStrToSend(void);
|
char *getStrToSend(void);
|
||||||
unsigned int getCounter(void) const;
|
unsigned int getCounter(void) const;
|
||||||
void setRawData(char *);
|
void setRawData(char *);
|
||||||
|
void setRawData(std::string &);
|
||||||
void setFd(int);
|
void setFd(int);
|
||||||
int getFd(void);
|
int getFd(void);
|
||||||
unsigned int getRecvCounter(void) const;
|
unsigned int getRecvCounter(void) const;
|
||||||
|
|||||||
@@ -105,6 +105,12 @@ void Request::setData(char *str)
|
|||||||
{
|
{
|
||||||
this->_data = str;
|
this->_data = str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Request::setData(std::string &str)
|
||||||
|
{
|
||||||
|
_stringBUF = str;
|
||||||
|
}
|
||||||
|
|
||||||
void Request::setData(char *str, ServerConfig *config)
|
void Request::setData(char *str, ServerConfig *config)
|
||||||
{
|
{
|
||||||
_data = str;
|
_data = str;
|
||||||
@@ -152,16 +158,12 @@ int Request::parseStartLine(std::string str)
|
|||||||
|
|
||||||
if (_version != "HTTP/1.1")
|
if (_version != "HTTP/1.1")
|
||||||
_ret = 505;
|
_ret = 505;
|
||||||
else if (_method != "GET" && _method != "POST"
|
|
||||||
&& _method != "DELETE")
|
|
||||||
_ret = 405;
|
|
||||||
return (_ret);
|
return (_ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::splitData(char *data)
|
void Request::splitData(std::string &data)
|
||||||
{
|
{
|
||||||
int pos;
|
int pos;
|
||||||
std::stringstream ss;
|
|
||||||
std::string str;
|
std::string str;
|
||||||
|
|
||||||
str = std::string(data);
|
str = std::string(data);
|
||||||
@@ -175,7 +177,7 @@ void Request::splitData(char *data)
|
|||||||
}
|
}
|
||||||
_head = str.substr(0, pos) + "\n";
|
_head = str.substr(0, pos) + "\n";
|
||||||
_headerSize = _head.size() + 3;
|
_headerSize = _head.size() + 3;
|
||||||
str.erase(0, pos + 4);
|
data.erase(0, pos + 4);
|
||||||
_head_ok = true;
|
_head_ok = true;
|
||||||
parseHeader();
|
parseHeader();
|
||||||
if (_contentLength == 0)
|
if (_contentLength == 0)
|
||||||
@@ -185,7 +187,8 @@ void Request::splitData(char *data)
|
|||||||
return ;
|
return ;
|
||||||
else if (!_body_ok)
|
else if (!_body_ok)
|
||||||
{
|
{
|
||||||
_body += str;
|
|
||||||
|
_body.insert(_body.end(), data.begin(), data.end());
|
||||||
if ((_received - _headerSize) == _contentLength)
|
if ((_received - _headerSize) == _contentLength)
|
||||||
{
|
{
|
||||||
_body_ok = true;
|
_body_ok = true;
|
||||||
@@ -241,7 +244,7 @@ int Request::parseHeader(void)
|
|||||||
int Request::parseRequest(void)
|
int Request::parseRequest(void)
|
||||||
{
|
{
|
||||||
if (!_head_ok || !_body_ok)
|
if (!_head_ok || !_body_ok)
|
||||||
splitData(_data);
|
splitData(_stringBUF);
|
||||||
|
|
||||||
return (_ret);
|
return (_ret);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class Request
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
char *_data;
|
char *_data;
|
||||||
|
char *_pointerBody;
|
||||||
|
|
||||||
int _ret;
|
int _ret;
|
||||||
int _row;
|
int _row;
|
||||||
@@ -29,6 +30,7 @@ private:
|
|||||||
std::string _connection;
|
std::string _connection;
|
||||||
std::map<std::string, std::string> _headerField;
|
std::map<std::string, std::string> _headerField;
|
||||||
|
|
||||||
|
std::string _stringBUF;
|
||||||
ServerConfig *_config;
|
ServerConfig *_config;
|
||||||
bool _head_ok;
|
bool _head_ok;
|
||||||
bool _body_ok;
|
bool _body_ok;
|
||||||
@@ -49,11 +51,14 @@ public:
|
|||||||
unsigned int getContentLength(void) const;
|
unsigned int getContentLength(void) const;
|
||||||
unsigned int getHeaderSize(void) const;
|
unsigned int getHeaderSize(void) const;
|
||||||
unsigned int getRecved(void)const;
|
unsigned int getRecved(void)const;
|
||||||
|
char *getPointerBody(void)const;
|
||||||
|
|
||||||
std::map<std::string, std::string> getClientFields(void);
|
std::map<std::string, std::string> getClientFields(void);
|
||||||
bool getChunked(void);
|
bool getChunked(void);
|
||||||
|
|
||||||
void setConfig(ServerConfig *config);
|
void setConfig(ServerConfig *config);
|
||||||
void setData(char *);
|
void setData(char *);
|
||||||
|
void setData(std::string &);
|
||||||
void setData(char *, ServerConfig *);
|
void setData(char *, ServerConfig *);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -75,7 +80,7 @@ public:
|
|||||||
bool autoindexOn(void);
|
bool autoindexOn(void);
|
||||||
void copyFromMap(void);
|
void copyFromMap(void);
|
||||||
void clear(void);
|
void clear(void);
|
||||||
void splitData(char *);
|
void splitData(std::string &);
|
||||||
|
|
||||||
void increaseRecvCounter(unsigned int n);
|
void increaseRecvCounter(unsigned int n);
|
||||||
~Request();
|
~Request();
|
||||||
|
|||||||
@@ -49,7 +49,9 @@ void Response::setHeaderBlocks(void)
|
|||||||
}
|
}
|
||||||
void Response::setContentType(void)
|
void Response::setContentType(void)
|
||||||
{
|
{
|
||||||
if (_request.badCode(_code))
|
if (_code == 204)
|
||||||
|
return ;
|
||||||
|
else if (_request.badCode(_code))
|
||||||
_contentType = "text/html";
|
_contentType = "text/html";
|
||||||
else
|
else
|
||||||
_contentType = getContentType();
|
_contentType = getContentType();
|
||||||
@@ -254,6 +256,7 @@ void Response::generateHeader(void)
|
|||||||
std::string tmp;
|
std::string tmp;
|
||||||
|
|
||||||
ss << "HTTP/1.1" << " " << _code << " " << getReasonPhrase(_code) << "\r\n";
|
ss << "HTTP/1.1" << " " << _code << " " << getReasonPhrase(_code) << "\r\n";
|
||||||
|
if (!_contentType.empty())
|
||||||
ss << "Content-Type: " << _contentType << "\r\n";
|
ss << "Content-Type: " << _contentType << "\r\n";
|
||||||
ss << "Content-Length: " << _contentLength << "\r\n";
|
ss << "Content-Length: " << _contentLength << "\r\n";
|
||||||
ss << "Server: " << _server << "\r\n";
|
ss << "Server: " << _server << "\r\n";
|
||||||
@@ -273,10 +276,10 @@ void Response::generate()
|
|||||||
invalidClient();
|
invalidClient();
|
||||||
else if (_request.getMethod() == "GET")
|
else if (_request.getMethod() == "GET")
|
||||||
methodGet();
|
methodGet();
|
||||||
// else if (_request.getMethod() == "POST")
|
else if (_request.getMethod() == "DELETE")
|
||||||
// methodPost();
|
methodDelete();
|
||||||
// else
|
else
|
||||||
// methodDelete();
|
methodPost();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::generate2(void)
|
void Response::generate2(void)
|
||||||
@@ -325,6 +328,9 @@ void Response::methodGet(void)
|
|||||||
}
|
}
|
||||||
void Response::methodPost(void)
|
void Response::methodPost(void)
|
||||||
{
|
{
|
||||||
|
std::ofstream outfile(_fullURI.c_str(), std::ios::out | std::ios::binary);
|
||||||
|
|
||||||
|
outfile.write(_request.getBody().data(), _request.getBody().size());
|
||||||
_code = 204;
|
_code = 204;
|
||||||
setHeaderBlocks();
|
setHeaderBlocks();
|
||||||
generateHeader();
|
generateHeader();
|
||||||
|
|||||||
@@ -87,18 +87,21 @@ void Server::readSocket(Client &client, int fd)
|
|||||||
|
|
||||||
int status;
|
int status;
|
||||||
int bytes_read;
|
int bytes_read;
|
||||||
char buf[BUFFSIZE + 1];
|
// char buf[BUFFSIZE + 1];
|
||||||
|
std::string stringBUF(BUFFSIZE, 0);
|
||||||
|
|
||||||
DBOUT << TURQ << "IN readSocket" << ENDL;
|
DBOUT << TURQ << "IN readSocket" << ENDL;
|
||||||
DBOUT << "client in readSocket "<< &client << ENDL;
|
DBOUT << "client in readSocket "<< &client << ENDL;
|
||||||
bytes_read = recv(fd, buf, BUFFSIZE, 0);
|
bytes_read = recv(fd, &stringBUF[0], BUFFSIZE, 0);
|
||||||
if (bytes_read == 0)
|
if (bytes_read == 0)
|
||||||
{
|
{
|
||||||
client.allRead = true;
|
client.allRead = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
buf[bytes_read + 1] = '\0';
|
// buf[bytes_read + 1] = '\0';
|
||||||
client.setRawData(buf);
|
stringBUF.erase(bytes_read, stringBUF.size());
|
||||||
|
client.setRawData(stringBUF);
|
||||||
|
// client.setRawData(buf);
|
||||||
client.increaseRecvCounter(bytes_read);
|
client.increaseRecvCounter(bytes_read);
|
||||||
status = client.parseRequest();
|
status = client.parseRequest();
|
||||||
// client_map[fd].printClientInfo();
|
// client_map[fd].printClientInfo();
|
||||||
|
|||||||
Reference in New Issue
Block a user