diff --git a/includes/requests/HttpIMessage.hpp b/includes/requests/HttpIMessage.hpp index d2c1a79..9ed38b2 100644 --- a/includes/requests/HttpIMessage.hpp +++ b/includes/requests/HttpIMessage.hpp @@ -6,7 +6,7 @@ /* By: mmoussou getHeaders(void) const; + virtual std::map getHeaders(void) const; virtual std::string getBody(void) const; - virtual void setHeaders(std::multimap const headers); + virtual void setHeaders(std::map const headers); virtual void setBody(std::string const body); virtual void addHeader(std::string const key, std::string const value); @@ -34,7 +34,7 @@ public: virtual std::string str(void) const = 0; protected: - std::multimap _headers; + std::map _headers; std::string _body; static const std::map _response_status_codes; diff --git a/src/main.cpp b/src/main.cpp index 8c0507c..c4fb958 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ /* By: mmoussou http::IMessage::getHeaders(void) const +std::map http::IMessage::getHeaders(void) const { return (this->_headers); } @@ -24,7 +24,7 @@ std::string http::IMessage::getBody(void) const return (this->_body); } -void http::IMessage::setHeaders(std::multimap const headers) +void http::IMessage::setHeaders(std::map const headers) { this->_headers = headers; } diff --git a/src/requests_handling/HttpRequests.cpp b/src/requests_handling/HttpRequests.cpp index 7b205fe..0e37862 100644 --- a/src/requests_handling/HttpRequests.cpp +++ b/src/requests_handling/HttpRequests.cpp @@ -6,7 +6,7 @@ /* By: mmoussou _method << " " << this->_target << " " << this->_protocol; response << "\r\n"; - for (std::multimap::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) + for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) response << it->first << ": " << it->second << "\r\n"; response << "\r\n"; @@ -187,7 +187,6 @@ http::Response http::Get::execute(void) response.setBody(std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator())); std::stringstream length; length << (file.tellg() - file_start); - std::cout << length.str() << std::endl; response.addHeader("Content-Length", length.str()); response.setProtocol(this->_protocol); @@ -345,12 +344,64 @@ void http::Post::parse(std::string const &data) //*/ } +std::string extractFilename(const std::string &header) +{ + size_t start = header.find("filename=\"") + 10; + size_t end = header.find("\"", start); + return header.substr(start, end - start); +} + +void handleMultipartData(const std::string &body, const std::string &boundary) +{ + size_t i = 0; + std::string delim = "--" + boundary; + delim.erase(delim.size() - 1); + + while ((i = body.find(delim, i)) != std::string::npos) + { + size_t start = i + delim.length(); + size_t end = body.find("\r\n\r\n", start); + + if (end != std::string::npos) + { + std::string part_header = body.substr(start, end - start); + std::cout << std::endl<< std::endl<< std::endl<< std::endl<< std::endl; + std::string part_content = body.substr(end + 4, body.find(delim, end) - end - 4); + + std::ofstream outfile(extractFilename(part_header).c_str(), std::ios::binary); + if (outfile.is_open()) + { + outfile.write(part_content.c_str(), part_content.length()); + outfile.close(); + } + else + { + std::cerr << "open failed" << std::endl; + } + } + + i += delim.length(); + } +} + http::Response http::Post::execute(void) { http::Response response; - // uh idk anymore - + try + { + handleMultipartData(this->_body, this->getHeaders()["Content-Type"].substr(this->getHeaders()["Content-Type"].find("=", this->getHeaders()["Content-Type"].find(";")) + 1)); + + response.setProtocol(this->_protocol); + response.setStatusCode(204); + } + catch (...) + { + response.setProtocol(this->_protocol); + response.setStatusCode(500); + response.addHeader("Content-Type", "text/html"); + response.setBody(http::Errors::getResponseBody(response.getStatusCode())); + } return (response); } diff --git a/src/requests_handling/HttpResponse.cpp b/src/requests_handling/HttpResponse.cpp index 7810f18..c0d91dd 100644 --- a/src/requests_handling/HttpResponse.cpp +++ b/src/requests_handling/HttpResponse.cpp @@ -6,7 +6,7 @@ /* By: mmoussou _protocol << " " << this->_status_code << " " << this->_status_text; response << "\r\n"; - for (std::multimap::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) + for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) response << it->first << ": " << it->second << "\r\n"; response << "\r\n"; diff --git a/upload.html b/upload.html new file mode 100644 index 0000000..a9e575f --- /dev/null +++ b/upload.html @@ -0,0 +1,90 @@ + + + + upload + + + + + +
+
+ +

+

+
+ + + + +