diff --git a/src/server/sparkd/http/MultipartFormDecoder.cpp b/src/server/sparkd/http/MultipartFormDecoder.cpp index 199c59efd..8914df4e4 100644 --- a/src/server/sparkd/http/MultipartFormDecoder.cpp +++ b/src/server/sparkd/http/MultipartFormDecoder.cpp @@ -1,5 +1,71 @@ #include "MultipartFormDecoder.h" +#include "lib/logging/logging.h" +#include +#include + namespace bts { namespace http { +using namespace std; + +vector split(const string& contents, const string& deliniator) { + vector result; + boost::iter_split(result, contents, boost::first_finder(deliniator)); + return result; +} + +std::tuple> decode_header(const std::string& header) { + auto tokens = split(header, ";"); + std::string header_name = *tokens.begin(); + map props; + for (const auto& token : tokens) { + auto equals = token.find('='); + if (equals != std::string::npos) { + auto key = token.substr(1,equals-1); + auto value = token.substr(equals+1); + boost::algorithm::trim_if(value, + [](char c) { return c == ' ' || c == '"' || c == '\r' || c == '\n'; }); + props[key] = value; + } + } + return { header_name, props }; +} + +std::optional decode_form_data(const string content_type, const string& body) { + auto [_,props] = decode_header(content_type); + (void)_; + auto boundary = props["boundary"]; + if (boundary.empty()) { + LOG(Http,Warn,"Failed to decode boundary in content type '{}'", content_type); + return std::nullopt; + } + + MultiPartFormData result; + auto parts = split(body, "--" + boundary); + for (const auto& part : parts) { + auto end_headers = part.find("\r\n\r\n"); + if (end_headers == std::string::npos) continue; + if (part == "--\r\n") continue; + auto headers = split(part.substr(0,end_headers), "\r\n"); + + FormPart form; + form.content_type = part.substr(end_headers+2); + for (const auto& header : headers) { + const char* content_type = "Content-Type: "; + if (boost::starts_with(header, content_type)) { + form.content_type = header.substr(sizeof(content_type)); + } + else { + auto [_, props] = decode_header(header); (void)_; + if (!props["filename"].empty()) { + form.filename = props["filename"]; + } + } + } + result.parts.push_back(form); + } + + return {result}; +} + }} //namespace diff --git a/src/server/sparkd/http/MultipartFormDecoder.h b/src/server/sparkd/http/MultipartFormDecoder.h index 8379a7310..4b85ff557 100644 --- a/src/server/sparkd/http/MultipartFormDecoder.h +++ b/src/server/sparkd/http/MultipartFormDecoder.h @@ -1,9 +1,22 @@ #pragma once +#include +#include +#include +#include namespace bts { namespace http { -class MultipartFormDecoder { +struct FormPart { + std::string content_type; + std::string filename; + std::string contents; +}; +struct MultiPartFormData { + std::vector parts; }; +std::tuple> decode_header(const std::string& header); +std::optional decode_form_data(const std::string content_type, const std::string& body); + }} //namespace diff --git a/src/server/sparkd/http/WebServer.cpp b/src/server/sparkd/http/WebServer.cpp index d28ea4eae..11a6cea27 100644 --- a/src/server/sparkd/http/WebServer.cpp +++ b/src/server/sparkd/http/WebServer.cpp @@ -1,5 +1,6 @@ #include "WebServer.h" #include "ApiGateway.h" +#include "MultipartFormDecoder.h" #include "lib/utils/Dispatcher.h" #include "lib/api/core/ApiClientInterop.h" #include "lib/api/apigateway.pb.h" @@ -96,7 +97,7 @@ namespace http { void on_open(connection_hdl hdl) { auto con = _server.get_con_from_hdl(hdl); if (con) { - LOG(Api,Info, "Web client connection from {}", con->get_remote_endpoint()); + LOG(Http,Info, "Web client connection from {}", con->get_remote_endpoint()); std::lock_guard lock(_m); _session[hdl] = ApiGatewaySession(_ctx); } @@ -105,7 +106,7 @@ namespace http { void on_close(connection_hdl hdl) { auto con = _server.get_con_from_hdl(hdl); if (con) { - LOG(Api,Info, "Web client disconnected: {}", con->get_remote_endpoint()); + LOG(Http,Info, "Web client disconnected: {}", con->get_remote_endpoint()); std::lock_guard lock(_m); _session.erase(hdl); } @@ -121,12 +122,12 @@ namespace http { websocketpp::lib::error_code ec; _server.send(hdl, response.SerializeAsString(), websocketpp::frame::opcode::binary, ec); if (ec) { - LOG(Api,Warn, "Error sending from websocket: '{}'", ec.message()); + LOG(Http,Warn, "Error sending from websocket: '{}'", ec.message()); } }); } else { - LOG(Api,Info, "Could not parse request as GatewayRequest: {}", msg->get_payload()); + LOG(Http,Info, "Could not parse request as GatewayRequest: {}", msg->get_payload()); } } } @@ -193,35 +194,16 @@ namespace http { void handle_file_upload(connection_ptr& con) { auto content_type = con->get_request_header("Content-Type"); - // Check that we have multipart/form-data - // parse out the boundary - // get the body - // split the body up by the boundary - // strip headers from the content - // parse out filename? (optional) - // send body on its way - - // multipart/form-data; boundary=---------------------------20830301425829 - const std::string mpfd = "multipart/form-data; boundary="; - auto pos = content_type.find(mpfd); - if (pos == std::string::npos) { - return bad_request(con, "Invalid form encoding. Please use multipart/form-data"); + auto data = decode_form_data(content_type, con->get_request_body()); + if (data) { + for(const auto& part : data.value().parts) { + if (!part.filename.empty()) { + LOG(Http,Info,"Received file upload: {}", part.filename); + } + } } - auto boundary = content_type.substr(pos + mpfd.size()); - std::cout << boundary; - - auto body = con->get_request_body(); - - std::ofstream output; - output.open("/tmp/uplog"); - - auto parts = split_string(body, boundary); - size_t count = 0; - for(const auto& t : parts) { - output << "Part " << count << std::endl; - ++count; - output << t << std::endl; - output << "========================" << std::endl; + else { + LOG(Http,Warn, "Failed to decode form data. Ensure you are using 'enctype=\"multipart/form-data\"'"); } con->set_body("got it"); @@ -357,7 +339,7 @@ namespace http { void on_http(connection_hdl hdl) { auto con = _server.get_con_from_hdl(hdl); auto resource = strip_query_str(con->get_resource()); - LOG(Api,Diag, "HTTP GET: {}", resource); + LOG(Http,Diag, "HTTP GET: {}", resource); if (boost::iequals(resource, "/config/.all")) { return_dotall(con); } diff --git a/src/server/sparkd/http/tests/MultiPartFormTests.cpp b/src/server/sparkd/http/tests/MultiPartFormTests.cpp new file mode 100644 index 000000000..784241280 --- /dev/null +++ b/src/server/sparkd/http/tests/MultiPartFormTests.cpp @@ -0,0 +1,45 @@ +#include "catch/catch.hpp" +#include "../MultipartFormDecoder.h" + +#include + +namespace bts { +namespace http { +namespace test { + +TEST_CASE("Form header decoder", "[http]") { + auto header1 = decode_header("multipart/form-data; boundary=---------------------------123631036720284"); + REQUIRE(std::get(header1) == "multipart/form-data"); + REQUIRE(std::get<1>(header1)["boundary"] == "---------------------------123631036720284"); + auto header2 = decode_header( "Content-Disposition: form-data; name=\"file\"; filename=\"file_a.txt\"\n"); + REQUIRE(std::get(header2) == "Content-Disposition: form-data"); + REQUIRE(std::get<1>(header2)["name"] == "file"); + REQUIRE(std::get<1>(header2)["filename"] == "file_a.txt"); +} + +TEST_CASE("MultiPart form decoder", "[http]") { + std::string header = "multipart/form-data; boundary=---------------------------123631036720284"; + std::string body = + "-----------------------------123631036720284\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"file_a.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "\r\n" + " a\r\n" + " aaa\r\n" + " aaaaa\r\n" + "aaaaaaaa\r\n" + "\r\n" + "\r\n" + "-----------------------------123631036720284--\r\n"; + + auto maybe_data = decode_form_data(header, body); + REQUIRE((bool)maybe_data); + const auto& data = maybe_data.value(); + REQUIRE(data.parts.size() == 1); + auto& part = data.parts[0]; + REQUIRE(part.filename == "file_a.txt"); + +} + +}}} // ns \ No newline at end of file