[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/etr/libhttpserver/master/examples/file_upload.cpp [Back]  [Original]

/* This file is part of libhttpserver Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include // HTML-escape user-controlled text before placing it in a response body // to prevent reflected XSS (CWE-79). Never echo unfiltered client input. static std::string html_escape(std::string_view in) { std::string out; out.reserve(in.size()); for (char c : in) { switch (c) { case '&': out += "&"; break; case '': out += ">"; break; case '"': out += """; break; case '\'': out += "'"; break; default: out += c; break; } } return out; } class file_upload_resource : public httpserver::http_resource { public: httpserver::http_response render_get(const httpserver::http_request&) override { return httpserver::http_response::string(R"html( [Upload] )html", "text/html"); } httpserver::http_response render_post(const httpserver::http_request& req) override { // Static header built from a raw string literal to avoid per-append // reallocations. The dynamic rows are appended after an upfront reserve. std::string post_response = R"html( Uploaded files:

)html"; post_response.reserve(post_response.size() + 512); for (auto &file_key : req.get_files()) { for (auto &files : file_key.second) { post_response += " \n"; } } post_response += "
Key Uploaded filename File system path File size Content type Transfer encoding
"; post_response += html_escape(file_key.first); post_response += ""; post_response += html_escape(files.first); post_response += ""; post_response += html_escape(files.second.get_file_system_file_name()); post_response += ""; post_response += std::to_string(files.second.get_file_size()); post_response += ""; post_response += html_escape(files.second.get_content_type()); post_response += ""; post_response += html_escape(files.second.get_transfer_encoding()); post_response += "


\n"; post_response += " back\n"; post_response += "\n"; return httpserver::http_response::string(post_response, "text/html").with_status(201); } }; int main(int argc, char** argv) { // this example needs a directory as parameter if (2 != argc) { std::cout
Web Proxy Viewer  |  New URL  |  Original Page