diff --git a/src/CGI/CgiHandle.cpp b/src/CGI/CgiHandle.cpp index 916a262..ce923e5 100644 --- a/src/CGI/CgiHandle.cpp +++ b/src/CGI/CgiHandle.cpp @@ -99,7 +99,9 @@ std::string CgiHandle::executeCgi() FILE *fOt = tmpfile(); long fdin = fileno(fIn); long fdOut = fileno(fOt); - write(fdin, _request.getBody()->data(), _request.getBody()->size()); + if (_request.getBody() != NULL) + write(fdin, _request.getBody()->data(), _request.getBody()->size()); + lseek(fdin, 0, SEEK_SET); pid = fork(); if (pid == -1) @@ -151,7 +153,7 @@ void CgiHandle::initEnvVariables() _variable["AUTH TYPE"] = it->second; else _variable["AUTH TYPE"] = ""; - _variable["CONTENT_LENGTH"] = toString(_request.getBody()->size()); + _variable["CONTENT_LENGTH"] = (_request.getBody() == NULL) ? "0" : toString(_request.getBody()->size()); it = _request.getClientFields().find("content-type"); _variable["CONTENT_TYPE"] = ""; _variable["GATEWAY_INTERFACE"] = std::string("CGI/1.1"); diff --git a/src/Client/Client.cpp b/src/Client/Client.cpp index 7f5d0df..7ab8fb2 100644 --- a/src/Client/Client.cpp +++ b/src/Client/Client.cpp @@ -184,8 +184,10 @@ std::string Client::generateRespons(std::vector &configs) _to_send_char = new char[len + 1]; std::memcpy(_to_send_char, _toSend->c_str(), len + 1); - delete _request.getBody(); + delete _toSend; + if (_request.getBody() != NULL) + _request.freeData(); _response.freeData(); return (_headerToSend); } diff --git a/src/Client/Request.cpp b/src/Client/Request.cpp index 56c5c88..7005019 100644 --- a/src/Client/Request.cpp +++ b/src/Client/Request.cpp @@ -13,7 +13,7 @@ Request::Request() _received = 0; _headerSize = 0; _lifeTime = 5; - _body = new std::string; + _body = NULL; } Request::Request(char *str) @@ -28,12 +28,17 @@ Request::Request(char *str) _contentLength = 0; _headerSize = 0; _lifeTime = 5; - _body = new std::string; + _body = NULL; } //-------------------------------------------------Get/Set--------------------------------------- +void Request::freeData(void) +{ + delete _body; +} + std::string &Request::getURI(void) { return (_URI); @@ -226,11 +231,13 @@ void Request::splitData(std::string &data) if ((_contentLength == 0 && !_chunked) || (_method == "GET" || _method == "DELETE" || _method == "HEAD")) _body_ok = true; + else + _body = new std::string; } } if (badCode(_ret)) return ; - else if (_chunked) + else if (_chunked && !_body_ok) { _body->insert(_body->end(), str.begin(), str.end()); if (checkEnd(*_body, "0\r\n\r\n") == 0) diff --git a/src/Client/Request.hpp b/src/Client/Request.hpp index 18bdbbb..45f4160 100644 --- a/src/Client/Request.hpp +++ b/src/Client/Request.hpp @@ -81,6 +81,7 @@ public: void copyFromMap(void); void clear(void); void splitData(std::string &); + void freeData(void); void increaseRecvCounter(unsigned int n); ~Request(); diff --git a/src/Client/Response.cpp b/src/Client/Response.cpp index 7ae7b02..9ef9737 100644 --- a/src/Client/Response.cpp +++ b/src/Client/Response.cpp @@ -333,7 +333,7 @@ void Response::generate2(serverListen &l) _fullURI = getFullURI(); _method = _request.getMethod(); _maxBodySize = (_location->clientBodySize > 0) ? _location->clientBodySize : _config->getClientBodySize(); - if (_maxBodySize > 0) + if (_maxBodySize > 0 && _request.getBody() != NULL) _code = (_request.getBody()->size() > (unsigned long)_maxBodySize) ? 413 : _code; }