From b1a14048639824c210b4582e4ca9dbba54d3be5f Mon Sep 17 00:00:00 2001 From: Talyx Date: Mon, 14 Feb 2022 17:34:01 +0300 Subject: [PATCH] add: url decode, and change client_body_size var to ssize_t --- src/CGI/CgiHandle.cpp | 2 +- src/Client/Request.cpp | 27 +++++++++++++++++++++++++-- src/Client/Request.hpp | 8 ++++---- src/Client/Response.cpp | 2 +- src/Client/Response.hpp | 6 +++--- src/Server/ServerConfig.cpp | 6 +++--- src/Server/ServerConfig.hpp | 6 +++--- 7 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/CGI/CgiHandle.cpp b/src/CGI/CgiHandle.cpp index 1f29e66..9f522e5 100644 --- a/src/CGI/CgiHandle.cpp +++ b/src/CGI/CgiHandle.cpp @@ -153,7 +153,7 @@ void CgiHandle::initEnvVariables() _variable["AUTH TYPE"] = it->second; else _variable["AUTH TYPE"] = ""; - _variable["CONTENT_LENGTH"] = toString(_request.getContentLength()); + _variable["CONTENT_LENGTH"] = 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/Request.cpp b/src/Client/Request.cpp index 96ec965..ae553a9 100644 --- a/src/Client/Request.cpp +++ b/src/Client/Request.cpp @@ -89,7 +89,7 @@ bool Request::getChunked(void) return (_chunked); } -unsigned int Request::getContentLength(void) const +ssize_t Request::getContentLength(void) const { return (_contentLength); } @@ -97,7 +97,7 @@ unsigned int Request::getHeaderSize(void) const { return (_headerSize); } -unsigned int Request::getRecved(void) const +ssize_t Request::getRecved(void) const { return (_received); } @@ -127,6 +127,25 @@ void Request::increaseRecvCounter(unsigned int n) { _received += n; } + + +std::string urlDecode(std::string &url) { + std::string ret; + char ch; + size_t i, ii; + for (i=0; i < url.length(); i++) { + if (size_t(url[i])==37) { + sscanf(url.substr(i+1,2).c_str(), "%lx", &ii); + ch=static_cast(ii); + ret+=ch; + i=i+2; + } else { + ret+=url[i]; + } + } + return (ret); +} + void Request::parseURI(std::string str) { std::string tmp; @@ -141,6 +160,10 @@ void Request::parseURI(std::string str) } else _URI = str; + std::string sIn = _URI; + + _URI = urlDecode(sIn); + _fullURI = HOME + _URI; } diff --git a/src/Client/Request.hpp b/src/Client/Request.hpp index 241f5df..61a740a 100644 --- a/src/Client/Request.hpp +++ b/src/Client/Request.hpp @@ -14,8 +14,8 @@ private: int _ret; int _row; int _lifeTime; - unsigned int _contentLength; - unsigned int _received; + ssize_t _contentLength; + ssize_t _received; unsigned int _headerSize; std::string _URI; @@ -48,9 +48,9 @@ public: ServerConfig *getConfig(void); int getCode(void); int getLifeTime(void); - unsigned int getContentLength(void) const; + ssize_t getContentLength(void) const; unsigned int getHeaderSize(void) const; - unsigned int getRecved(void)const; + ssize_t getRecved(void)const; char *getPointerBody(void)const; std::map getClientFields(void); diff --git a/src/Client/Response.cpp b/src/Client/Response.cpp index 25f75fe..97ff0ed 100644 --- a/src/Client/Response.cpp +++ b/src/Client/Response.cpp @@ -103,7 +103,7 @@ std::string Response::getCgiPass(void) return (_location->cgi_pass); } -unsigned int Response::getMaxBodySize(void) +ssize_t Response::getMaxBodySize(void) { return (_maxBodySize); } diff --git a/src/Client/Response.hpp b/src/Client/Response.hpp index 3221795..281d789 100644 --- a/src/Client/Response.hpp +++ b/src/Client/Response.hpp @@ -25,8 +25,8 @@ private: private: std::string _contentType; - unsigned int _contentLength; - unsigned int _maxBodySize; + ssize_t _contentLength; + ssize_t _maxBodySize; std::string _server; std::string _keepAlive; std::string _date; @@ -67,7 +67,7 @@ public: static std::string getReasonPhrase(int); std::string getErrorPage(int code); std::string getFullURI(); - unsigned int getMaxBodySize(void); + ssize_t getMaxBodySize(void); bool isRedirect(void); bool allowedMethod(std::string &); void setData(Request, ServerConfig *); diff --git a/src/Server/ServerConfig.cpp b/src/Server/ServerConfig.cpp index bafbd62..e888d90 100644 --- a/src/Server/ServerConfig.cpp +++ b/src/Server/ServerConfig.cpp @@ -33,7 +33,7 @@ int &ServerConfig::getPort(void) return (_port); } -int &ServerConfig::getClientBodySize(void) +ssize_t &ServerConfig::getClientBodySize(void) { return (_clientBodySize); } @@ -206,7 +206,7 @@ int ServerConfig::putLocation(toml_node *node) else if (it1->first == "body_size_limit") { DBOUT << "BodySize in locaton" << ENDL; - if (node->get_type() != toml_node::NUM) + if (it1->second->get_type() != toml_node::NUM) continue; tmp->clientBodySize = it1->second->getNum(); } @@ -241,7 +241,7 @@ int ServerConfig::putLocation(toml_node *node) tmp->redirect.insert(std::make_pair(atoi(str.c_str()), *(*it2)->getString())); } else - std::cout << RED << it1->first << ZERO_C << std::endl; + std::cerr << RED << "Warning: unknown parameter: "<< it1->first << ZERO_C << std::endl; } _locations.push_back(tmp); it++; diff --git a/src/Server/ServerConfig.hpp b/src/Server/ServerConfig.hpp index 748be9d..139062e 100644 --- a/src/Server/ServerConfig.hpp +++ b/src/Server/ServerConfig.hpp @@ -15,7 +15,7 @@ struct location std::vector methods; std::map redirect; std::string cgi_pass; - unsigned int clientBodySize; + ssize_t clientBodySize; }; struct serverListen @@ -33,7 +33,7 @@ private: std::string _serverName; std::string _host; int _port; - int _clientBodySize; + ssize_t _clientBodySize; std::map _errorPages; std::vector _locations; @@ -51,7 +51,7 @@ public: std::string &getServerName(void); std::string &getHost(void); int &getPort(void); - int &getClientBodySize(void); + ssize_t &getClientBodySize(void); std::vector &getLocations(void); std::map &getErrorPages(void); TOMLMap *getRoot(void);