SP-150 file upload decoder
This commit is contained in:
1 parent
9a21d03311
commit
469914c6c9
3 files changed
+60
-5
No files matched your search
@@ -0,0 +1,5 @@
|
||||
#include "MultipartFormDecoder.h"
|
||||
namespace bts {
|
||||
namespace http {
|
||||
|
||||
}} //namespace
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
namespace bts {
|
||||
namespace http {
|
||||
|
||||
class MultipartFormDecoder {
|
||||
};
|
||||
|
||||
}} //namespace
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
@@ -174,14 +175,54 @@ namespace http {
|
||||
_mtimer->observe((utils::TimeStamp::now() - start).microseconds() / 1000.0);
|
||||
}
|
||||
|
||||
void bad_request(connection_ptr& con, const std::string& message) {
|
||||
con->set_body(message);
|
||||
con->set_status(websocketpp::http::status_code::bad_request);
|
||||
}
|
||||
|
||||
std::vector<std::string> split_string(std::string body, std::string& deliniator) {
|
||||
std::vector<std::string> result;
|
||||
size_t i = body.find(deliniator);
|
||||
while(i != std::string::npos) {
|
||||
result.push_back(body.substr(0,i));
|
||||
body = body.substr(i + deliniator.size());
|
||||
i = body.find(deliniator);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void handle_file_upload(connection_ptr& con) {
|
||||
// NOTE: to work this requires enctype="multipart/form-data"
|
||||
// otherwise all these strings turn out empty
|
||||
auto content_type = con->get_request_header("Content-Type");
|
||||
auto content_length = con->get_request_header("Content-Length");
|
||||
auto request = con->get_request().get_body();
|
||||
// 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 boundary = content_type.substr(pos + mpfd.size());
|
||||
std::cout << boundary;
|
||||
|
||||
auto body = con->get_request_body();
|
||||
std::cout << body << std::endl;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
con->set_body("got it");
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
|
||||
Reference in new issue
Block a user