public AuthenticationInfo(String authHeader)
{
cnonce = "0a4f113b";
nc = 1;
// Example Digest header: WWW-Authenticate: Digest realm="[email protected]", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"
// get method from first word
int pos = authHeader.IndexOf(" ");
if (pos < 0)
throw new ApplicationException("Authentication header not supported: {0}".Fmt(authHeader));
method = authHeader.Substring(0, pos).ToLower();
string remainder = authHeader.Substring(pos + 1);
// split the rest by comma, then =
string[] pars = remainder.Split(',');
string[] newpars = new string[pars.Length];
int maxnewpars = 0;
// test possibility that a comma is mid value for a split (as in above example)
for (int i = 0; i < pars.Length; i++)
{
if (pars[i].EndsWith("\""))
{
newpars[maxnewpars] = pars[i];
maxnewpars++;
}
else
{
// merge with next one
newpars[maxnewpars] = pars[i] + "," + pars[i + 1];
maxnewpars++;
i++; // skips next value
}
}
// now go through each part, splitting on first = character, and removing leading and trailing spaces and " quotes
for (int i = 0; i < maxnewpars; i++)
{
int pos2 = newpars[i].IndexOf("=");
string name = newpars[i].Substring(0, pos2).Trim();
string value = newpars[i].Substring(pos2 + 1).Trim();
if (value.StartsWith("\""))
{
value = value.Substring(1);
}
if (value.EndsWith("\""))
{
value = value.Substring(0, value.Length - 1);
}
if ("qop".Equals(name))
{
qop = value;
}
else if ("realm".Equals(name))
{
realm = value;
}
else if ("nonce".Equals(name))
{
nonce = value;
}
else if ("opaque".Equals(name))
{
opaque = value;
}
}
}