SP-150 Decode http multipart form data in WebServer.cpp
This commit is contained in:
1 parent
637d7384b1
commit
38964f4d10
4 files changed
+140
-34
No files matched your search
@@ -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<std::mutex> 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<std::mutex> 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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user