I have a program to parse HTTP digest's components like this:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <regex>
#include <unordered_map>
int main()
{
std::string nsInput = R"(Digest realm = "http-auth@example.org",
qop= " auth, auth-int ", algorithm = MD5 ,
nonce ="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v" ,
opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"
)";
// Spaces are inserted into some places of the input intentionally
std::smatch mat_opt, mat_val;
std::unordered_map<std::string, std::string> mapDigest;
try {
std::regex rex_opt(R"(\s*([A-Za-z]{3,})\s*=)");
std::regex rex_val(R"(\s*\"\s*(.{3,})\s*\"|\s*(.{3,})\s*,)");
auto& str = nsInput;
while (std::regex_search(nsInput, mat_opt, rex_opt))
{
if (mat_opt.size() >= 2) {
auto& field = mat_opt[1].str();
std::string& next = mat_opt.suffix().str();
if (std::regex_search(next, mat_val, rex_val) && mat_val.size() >= 2) {
auto& value = mat_val[1].str();
mapDigest[field] = value;
}
str = mat_opt.suffix().str();
}
}
for (auto& itr : mapDigest) {
std::cout << itr.first << ":" << itr.second << ".\n";
}
}
catch (std::regex_error& e) {
std::cout << "regex_search failed" << e.what() << "\n";
}
return 0;
}
The output:
nonce:7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v.
realm:http-auth@example.org.
qop:auth, auth-int .
algorithm:.
opaque:FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS.
What I am trying to solve are:
1) The spaces are still appeared at the end of "qop"'s value.
2) The value of "algorithm" can't be matched.
May someone shine the obscure cause and how to fix it?
Thanks