|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <iostream> |
| 22 | +#include <httpserver.hpp> |
| 23 | + |
| 24 | +class file_upload_resource : public httpserver::http_resource { |
| 25 | + public: |
| 26 | + const std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) { |
| 27 | + std::string get_response = "<html>\n"; |
| 28 | + get_response += " <body>\n"; |
| 29 | + get_response += " <form method=\"POST\" enctype=\"multipart/form-data\">\n"; |
| 30 | + get_response += " <h1>Upload 1 (key is 'files', multiple files can be selected)</h1><br>\n"; |
| 31 | + get_response += " <input type=\"file\" name=\"files\" multiple>\n"; |
| 32 | + get_response += " <br><br>\n"; |
| 33 | + get_response += " <h1>Upload 2 (key is 'files2', multiple files can be selected)</h1><br>\n"; |
| 34 | + get_response += " <input type=\"file\" name=\"files2\" multiple><br><br>\n"; |
| 35 | + get_response += " <input type=\"submit\" value=\"Upload\">\n"; |
| 36 | + get_response += " </form>\n"; |
| 37 | + get_response += " </body>\n"; |
| 38 | + get_response += "</html>\n"; |
| 39 | + |
| 40 | + return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(get_response, 200, "text/html")); |
| 41 | + } |
| 42 | + |
| 43 | + const std::shared_ptr<httpserver::http_response> render_POST(const httpserver::http_request& req) { |
| 44 | + std::string post_response = "<html>\n"; |
| 45 | + post_response += "<head>\n"; |
| 46 | + post_response += " <style>\n"; |
| 47 | + post_response += " table, th, td {\n"; |
| 48 | + post_response += " border: 1px solid black;\n"; |
| 49 | + post_response += " border-collapse: collapse;\n"; |
| 50 | + post_response += " }\n"; |
| 51 | + post_response += " </style>\n"; |
| 52 | + post_response += "</head>\n"; |
| 53 | + post_response += "<body>\n"; |
| 54 | + post_response += " Uploaded files:\n"; |
| 55 | + post_response += " <br><br>\n"; |
| 56 | + post_response += " <table>\n"; |
| 57 | + post_response += " <tr>\n"; |
| 58 | + post_response += " <th>Key</th>\n"; |
| 59 | + post_response += " <th>Uploaded filename</th>\n"; |
| 60 | + post_response += " <th>File system path</th>\n"; |
| 61 | + post_response += " <th>File size</th>\n"; |
| 62 | + post_response += " <th>Content type</th>\n"; |
| 63 | + post_response += " <th>Transfer encoding</th>\n"; |
| 64 | + post_response += " </tr>\n"; |
| 65 | + |
| 66 | + for (auto &file_key : req.get_files()) { |
| 67 | + for (auto &files : file_key.second) { |
| 68 | + post_response += " <tr><td>"; |
| 69 | + post_response += file_key.first; |
| 70 | + post_response += "</td><td>"; |
| 71 | + post_response += files.first; |
| 72 | + post_response += "</td><td>"; |
| 73 | + post_response += files.second.get_file_system_file_name(); |
| 74 | + post_response += "</td><td>"; |
| 75 | + post_response += std::to_string(files.second.get_file_size()); |
| 76 | + post_response += "</td><td>"; |
| 77 | + post_response += files.second.get_content_type(); |
| 78 | + post_response += "</td><td>"; |
| 79 | + post_response += files.second.get_transfer_encoding(); |
| 80 | + post_response += "</td></tr>\n"; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + post_response += " </table><br><br>\n"; |
| 85 | + post_response += " <a href=\"/\">back</a>\n"; |
| 86 | + post_response += "</body>\n</html>"; |
| 87 | + return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(post_response, 201, "text/html")); |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +int main(int argc, char** argv) { |
| 92 | + // this example needs a directory as parameter |
| 93 | + if (2 != argc) { |
| 94 | + std::cout << "Usage: file_upload <upload_dir>" << std::endl; |
| 95 | + std::cout << std::endl; |
| 96 | + std::cout << " file_upload: writeable directory where uploaded files will be stored" << std::endl; |
| 97 | + return -1; |
| 98 | + } |
| 99 | + |
| 100 | + std::cout << "CAUTION: this example will create files in the directory " << std::string(argv[1]) << std::endl; |
| 101 | + std::cout << "These files won't be deleted at termination" << std::endl; |
| 102 | + std::cout << "Please make sure, that the given directory exists and is writeable" << std::endl; |
| 103 | + |
| 104 | + httpserver::webserver ws = httpserver::create_webserver(8080) |
| 105 | + .no_put_processed_data_to_content() |
| 106 | + .file_upload_dir(std::string(argv[1])) |
| 107 | + .generate_random_filename_on_upload() |
| 108 | + .file_upload_target(httpserver::FILE_UPLOAD_DISK_ONLY); |
| 109 | + |
| 110 | + file_upload_resource fur; |
| 111 | + ws.register_resource("/", &fur); |
| 112 | + ws.start(true); |
| 113 | + |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
0 commit comments