From 1534858928312c5d299c8453194512ba77e711c8 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sun, 11 Oct 2020 19:37:04 +0200 Subject: [PATCH] Add basic ParameterParser --- src/Multipart.h | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/Multipart.h b/src/Multipart.h index 3539ca7..093254b 100644 --- a/src/Multipart.h +++ b/src/Multipart.h @@ -26,9 +26,91 @@ #include #include #include +#include namespace uWS { + /* This one could possibly be shared with ExtensionsParser to some degree */ + struct ParameterParser { + + /* Takes the line, commonly given as content-disposition header in the multipart */ + ParameterParser(std::string_view line) { + remainingLine = line; + } + + /* Common parameter */ + std::string_view getName() { + return name; + } + + /* Common parameter */ + std::string_view getFilename() { + return filename; + } + + /* Parses common values */ + void parseCommonValues() { + + } + + /* Returns next key/value where value can simply be empty. + * If key (first) is empty then we are at the end */ + std::pair getKeyValue() { + auto key = getToken(); + auto op = getToken(); + + if (!op.length()) { + return {key, ""}; + } + + if (op[0] != ';') { + return {key, getToken()}; + } + + return {key, ""}; + } + + private: + std::string_view remainingLine; + std::string_view filename, name; + + /* Consumes a token from the line. Will "unquote" strings */ + std::string_view getToken() { + /* Strip whitespace */ + while (remainingLine.length() && isspace(remainingLine[0])) { + remainingLine.remove_prefix(1); + } + + if (!remainingLine.length()) { + /* All we had was space */ + return {}; + } else { + /* Are we at an operator? */ + if (remainingLine[0] == ';' || remainingLine[0] == '=') { + auto op = remainingLine.substr(0, 1); + remainingLine.remove_prefix(1); + return op; + } else { + /* This part is not entirely correct */ + + /* Read the whole alpha word */ + std::string_view token = remainingLine; + + int tokenLength = 0; + while (remainingLine.length() && remainingLine[0] != ';' && remainingLine[0] != '=' && !isspace(remainingLine[0])) { + remainingLine.remove_prefix(1); + tokenLength++; + } + + return token.substr(0, tokenLength); + } + } + + /* Nothing */ + return ""; + } + }; + struct MultipartParser { /* 2 chars of hyphen + 1 - 70 chars of boundary */