Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
128 lines
5.2 KiB
C++
128 lines
5.2 KiB
C++
#pragma once
|
|
// Spec: molo::app::forms multipart binding. Parses multipart/form-data field
|
|
// parts into an Object; file parts become FileUpload records (bytes held in
|
|
// memory under the standalone caps; quota policy and scanning belong to
|
|
// 03-security, wired through Application on integration).
|
|
|
|
#include <cctype>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
#include "../caps.h"
|
|
#include "../validate/value.h"
|
|
|
|
namespace molo::app::forms {
|
|
|
|
struct FileUpload {
|
|
std::string field;
|
|
std::string filename;
|
|
std::string content_type = "application/octet-stream";
|
|
std::string data;
|
|
};
|
|
|
|
struct MultipartBody {
|
|
validate::Object fields;
|
|
std::vector<FileUpload> files;
|
|
};
|
|
|
|
inline std::string multipart_boundary(std::string_view content_type_header) {
|
|
std::string lower(content_type_header);
|
|
for (char& c : lower) c = (char)std::tolower((unsigned char)c);
|
|
std::string::size_type pos = lower.find("boundary=");
|
|
if (pos == std::string::npos) return "";
|
|
std::string b(content_type_header.substr(pos + 9));
|
|
while (!b.empty() && (b.back() == ' ' || b.back() == '\t' || b.back() == '"')) b.pop_back();
|
|
while (!b.empty() && (b.front() == ' ' || b.front() == '\t' || b.front() == '"'))
|
|
b.erase(b.begin());
|
|
return b;
|
|
}
|
|
|
|
inline std::string part_header_attr(std::string_view headers, std::string_view attr) {
|
|
std::string lower(headers);
|
|
for (char& c : lower) c = (char)std::tolower((unsigned char)c);
|
|
std::string key(attr);
|
|
for (char& c : key) c = (char)std::tolower((unsigned char)c);
|
|
key += "=\"";
|
|
std::string::size_type pos = lower.find(key);
|
|
if (pos == std::string::npos) {
|
|
key = std::string(attr) + "=";
|
|
for (char& c : key) c = (char)std::tolower((unsigned char)c);
|
|
pos = lower.find(key);
|
|
if (pos == std::string::npos) return "";
|
|
std::string::size_type start = pos + key.size();
|
|
std::string::size_type end = lower.find_first_of("; \t\r\n", start);
|
|
return std::string(headers.substr(start, end - start));
|
|
}
|
|
std::string::size_type start = pos + key.size();
|
|
std::string::size_type end = headers.find('"', start);
|
|
if (end == std::string_view::npos) return "";
|
|
return std::string(headers.substr(start, end - start));
|
|
}
|
|
|
|
inline MultipartBody parse_multipart(std::string_view content_type_header, std::string_view body) {
|
|
std::string boundary = multipart_boundary(content_type_header);
|
|
if (boundary.empty()) throw std::runtime_error("multipart body without boundary");
|
|
if (body.size() > caps::kMaxMultipartTotal) throw std::length_error("multipart body too large");
|
|
MultipartBody out;
|
|
std::string delim = "--" + boundary;
|
|
std::string end_delim = delim + "--";
|
|
std::size_t pos = 0;
|
|
std::size_t field_count = 0;
|
|
while (true) {
|
|
std::size_t start = body.find(delim, pos);
|
|
if (start == std::string_view::npos) break;
|
|
start += delim.size();
|
|
if (body.substr(start, 2) == "--") break;
|
|
if (body.substr(start, 2) == "\r\n") start += 2;
|
|
std::size_t head_end = body.find("\r\n\r\n", start);
|
|
if (head_end == std::string_view::npos) break;
|
|
std::string_view headers = body.substr(start, head_end - start);
|
|
std::size_t data_start = head_end + 4;
|
|
std::size_t next = body.find(delim, data_start);
|
|
if (next == std::string_view::npos) break;
|
|
std::size_t data_end = next;
|
|
if (data_end >= 2 && body.substr(data_end - 2, 2) == "\r\n") data_end -= 2;
|
|
std::string data(body.substr(data_start, data_end - data_start));
|
|
std::string name = part_header_attr(headers, "name");
|
|
std::string filename = part_header_attr(headers, "filename");
|
|
if (name.size() > caps::kMaxFieldName) throw std::length_error("form field name too long");
|
|
if (filename.empty()) {
|
|
if (field_count >= caps::kMaxFormFields) throw std::length_error("too many form fields");
|
|
if (data.size() > caps::kMaxNonFileValue) throw std::length_error("form value too large");
|
|
out.fields[std::move(name)] = validate::Value(std::move(data));
|
|
++field_count;
|
|
} else {
|
|
if (out.files.size() >= caps::kMaxMultipartFiles) {
|
|
throw std::length_error("too many uploaded files");
|
|
}
|
|
if (data.size() > caps::kMaxFileBytes) throw std::length_error("uploaded file too large");
|
|
FileUpload upload;
|
|
upload.field = std::move(name);
|
|
upload.filename = std::move(filename);
|
|
std::string ctype = part_header_attr(headers, "content-type");
|
|
// Content-Type of a part is a full header line, not an attr; parse it.
|
|
std::string lower(headers);
|
|
for (char& c : lower) c = (char)std::tolower((unsigned char)c);
|
|
std::string::size_type cpos = lower.find("content-type:");
|
|
if (cpos != std::string::npos) {
|
|
std::string::size_type vstart = cpos + 13;
|
|
while (vstart < headers.size() && (headers[vstart] == ' ' || headers[vstart] == '\t'))
|
|
++vstart;
|
|
std::string::size_type vend = headers.find("\r", vstart);
|
|
ctype = std::string(headers.substr(vstart, vend - vstart));
|
|
std::string::size_type semi = ctype.find(';');
|
|
if (semi != std::string::npos) ctype = ctype.substr(0, semi);
|
|
}
|
|
if (!ctype.empty()) upload.content_type = ctype;
|
|
upload.data = std::move(data);
|
|
out.files.push_back(std::move(upload));
|
|
}
|
|
pos = next;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
} // namespace molo::app::forms
|