Changed ContentDisposition::parse to HttpHeader::FieldValue::SemicolonSeparated::parse. This function can now also parse Set-Cookie header field values
This commit is contained in:
parent
29f8cc5669
commit
f55eb4af65
2 changed files with 134 additions and 50 deletions
|
|
@ -195,19 +195,89 @@ int main() {
|
||||||
|
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
SimpleWeb::CaseInsensitiveMultimap solution = {{"form-data", ""}};
|
SimpleWeb::CaseInsensitiveMultimap solution;
|
||||||
auto parsed = SimpleWeb::ContentDisposition::parse("form-data");
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("");
|
||||||
assert(parsed == solution);
|
assert(parsed == solution);
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"a", ""}};
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("a");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"a", ""}, {"b", ""}};
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("a; b");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("a;b");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"a", ""}, {"b", "c"}};
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("a; b=c");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("a;b=c");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"form-data", ""}};
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("form-data");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"form-data", ""}, {"test", ""}};
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("form-data; test");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
}
|
||||||
{
|
{
|
||||||
SimpleWeb::CaseInsensitiveMultimap solution = {{"form-data", ""}, {"name", "file"}};
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"form-data", ""}, {"name", "file"}};
|
||||||
auto parsed = SimpleWeb::ContentDisposition::parse("form-data; name=\"file\"");
|
{
|
||||||
assert(parsed == solution);
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("form-data; name=\"file\"");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("form-data; name=file");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
SimpleWeb::CaseInsensitiveMultimap solution = {{"form-data", ""}, {"name", "file"}, {"filename", "filename.png"}};
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"form-data", ""}, {"name", "file"}, {"filename", "filename.png"}};
|
||||||
auto parsed = SimpleWeb::ContentDisposition::parse("form-data; name=\"file\"; filename=\"filename.png\"");
|
{
|
||||||
assert(parsed == solution);
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("form-data; name=\"file\"; filename=\"filename.png\"");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("form-data;name=\"file\";filename=\"filename.png\"");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("form-data; name=file; filename=filename.png");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("form-data;name=file;filename=filename.png");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SimpleWeb::CaseInsensitiveMultimap solution = {{"form-data", ""}, {"name", "fi le"}, {"filename", "file name.png"}};
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("form-data; name=\"fi le\"; filename=\"file name.png\"");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto parsed = SimpleWeb::HttpHeader::FieldValue::SemicolonSeparated::parse("form-data; name=fi le; filename=file name.png");
|
||||||
|
assert(parsed == solution);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
102
utility.hpp
102
utility.hpp
|
|
@ -155,7 +155,64 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
class FieldValue {
|
||||||
|
public:
|
||||||
|
class SemicolonSeparated {
|
||||||
|
public:
|
||||||
|
/// Parse Set-Cookie or Content-Disposition header field value
|
||||||
|
static CaseInsensitiveMultimap parse(const std::string &line) {
|
||||||
|
CaseInsensitiveMultimap result;
|
||||||
|
|
||||||
|
std::size_t para_start_pos = std::string::npos;
|
||||||
|
std::size_t para_end_pos = std::string::npos;
|
||||||
|
std::size_t value_start_pos = std::string::npos;
|
||||||
|
for(std::size_t c = 0; c < line.size(); ++c) {
|
||||||
|
if(para_start_pos == std::string::npos) {
|
||||||
|
if(line[c] != ' ' && line[c] != ';')
|
||||||
|
para_start_pos = c;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(para_end_pos == std::string::npos) {
|
||||||
|
if(line[c] == ';') {
|
||||||
|
result.emplace(line.substr(para_start_pos, c - para_start_pos), std::string());
|
||||||
|
para_start_pos = std::string::npos;
|
||||||
|
}
|
||||||
|
else if(line[c] == '=')
|
||||||
|
para_end_pos = c;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(value_start_pos == std::string::npos) {
|
||||||
|
if(line[c] == '"' && c + 1 < line.size())
|
||||||
|
value_start_pos = c + 1;
|
||||||
|
else
|
||||||
|
value_start_pos = c;
|
||||||
|
}
|
||||||
|
else if(line[c] == '"' || line[c] == ';') {
|
||||||
|
result.emplace(line.substr(para_start_pos, para_end_pos - para_start_pos), line.substr(value_start_pos, c - value_start_pos));
|
||||||
|
para_start_pos = std::string::npos;
|
||||||
|
para_end_pos = std::string::npos;
|
||||||
|
value_start_pos = std::string::npos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(para_start_pos != std::string::npos) {
|
||||||
|
if(para_end_pos == std::string::npos)
|
||||||
|
result.emplace(line.substr(para_start_pos), std::string());
|
||||||
|
else if(value_start_pos != std::string::npos) {
|
||||||
|
if(line.back() == '"')
|
||||||
|
result.emplace(line.substr(para_start_pos, para_end_pos - para_start_pos), line.substr(value_start_pos, line.size() - 1));
|
||||||
|
else
|
||||||
|
result.emplace(line.substr(para_start_pos, para_end_pos - para_start_pos), line.substr(value_start_pos));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}; // namespace SimpleWeb
|
||||||
|
|
||||||
class RequestMessage {
|
class RequestMessage {
|
||||||
public:
|
public:
|
||||||
|
|
@ -231,49 +288,6 @@ namespace SimpleWeb {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ContentDisposition {
|
|
||||||
public:
|
|
||||||
/// Can be used to parse the Content-Disposition header field value when
|
|
||||||
/// clients are posting requests with enctype="multipart/form-data"
|
|
||||||
static CaseInsensitiveMultimap parse(const std::string &line) {
|
|
||||||
CaseInsensitiveMultimap result;
|
|
||||||
|
|
||||||
std::size_t para_start_pos = 0;
|
|
||||||
std::size_t para_end_pos = std::string::npos;
|
|
||||||
std::size_t value_start_pos = std::string::npos;
|
|
||||||
for(std::size_t c = 0; c < line.size(); ++c) {
|
|
||||||
if(para_start_pos != std::string::npos) {
|
|
||||||
if(para_end_pos == std::string::npos) {
|
|
||||||
if(line[c] == ';') {
|
|
||||||
result.emplace(line.substr(para_start_pos, c - para_start_pos), std::string());
|
|
||||||
para_start_pos = std::string::npos;
|
|
||||||
}
|
|
||||||
else if(line[c] == '=')
|
|
||||||
para_end_pos = c;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if(value_start_pos == std::string::npos) {
|
|
||||||
if(line[c] == '"' && c + 1 < line.size())
|
|
||||||
value_start_pos = c + 1;
|
|
||||||
}
|
|
||||||
else if(line[c] == '"') {
|
|
||||||
result.emplace(line.substr(para_start_pos, para_end_pos - para_start_pos), line.substr(value_start_pos, c - value_start_pos));
|
|
||||||
para_start_pos = std::string::npos;
|
|
||||||
para_end_pos = std::string::npos;
|
|
||||||
value_start_pos = std::string::npos;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(line[c] != ' ' && line[c] != ';')
|
|
||||||
para_start_pos = c;
|
|
||||||
}
|
|
||||||
if(para_start_pos != std::string::npos && para_end_pos == std::string::npos)
|
|
||||||
result.emplace(line.substr(para_start_pos), std::string());
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} // namespace SimpleWeb
|
} // namespace SimpleWeb
|
||||||
|
|
||||||
#ifdef __SSE2__
|
#ifdef __SSE2__
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue