private bool AddFieldToCookie(ref Cookie ck, PairItem pairInfo) { bool added = false; if (pairInfo.Key == "") { return(false); } string lowerKey = pairInfo.Key.ToLower(); switch (lowerKey) { case "expires": bool parseDatetimeOk; parseDatetimeOk = DateTime.TryParse(pairInfo.Value, out DateTime expireDatetime); if (parseDatetimeOk) { ck.Expires = expireDatetime; if (DateTime.Now.Ticks > ck.Expires.Ticks) { ck.Expired = true; } added = true; } break; case "domain": ck.Domain = pairInfo.Value; added = true; break; case "secure": ck.Secure = true; added = true; break; case "path": ck.Path = pairInfo.Value; added = true; break; case "httponly": ck.HttpOnly = true; added = true; break; case "version": if (int.TryParse(pairInfo.Value, out int versionValue)) { ck.Version = versionValue; added = true; } break; } return(added); }
private bool ParseSingleCookie(string cookieStr, ref Cookie ck) { bool parsedOk = true; string[] expressions = cookieStr.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); PairItem pair = new PairItem(); if (ParseCookieNameValue(expressions[0], out pair)) { ck.Name = pair.Key; ck.Value = pair.Value; string[] fieldExpressions = GetSubStrArr(expressions, 1, expressions.Length - 1); bool noDeisignateExpires = true; foreach (string eachExpression in fieldExpressions) { //parse key and value if (ParseCookieField(eachExpression, out pair)) { // add to cookie field if possible bool addedOk = false; addedOk = AddFieldToCookie(ref ck, pair); if (addedOk && string.Equals(pair.Key, StrExpires)) { noDeisignateExpires = false; } } else { parsedOk = false; break; } } if (noDeisignateExpires) { ck.Expires = DateTime.MaxValue; } } else { parsedOk = false; } return(parsedOk); }
private bool ParseCookieNameValue(string ckNameValueExpr, out PairItem pair) { bool parsedOK = false; if (ckNameValueExpr == "") { pair.Key = ""; pair.Value = ""; parsedOK = false; } else { ckNameValueExpr = ckNameValueExpr.Trim(); int equalPos = ckNameValueExpr.IndexOf('='); if (equalPos > 0) // is valid expression { pair.Key = ckNameValueExpr.Substring(0, equalPos); pair.Key = pair.Key.Trim(); if (IsValidCookieName(pair.Key)) { // only process while is valid cookie field pair.Value = ckNameValueExpr.Substring(equalPos + 1); pair.Value = pair.Value.Trim(); parsedOK = true; } else { pair.Key = ""; pair.Value = ""; parsedOK = false; } } else { pair.Key = ""; pair.Value = ""; parsedOK = false; } } return(parsedOK); }
private bool ParseCookieField(string ckFieldExpr, out PairItem pair) { bool parsedOK = false; if (ckFieldExpr == "") { pair.Key = ""; pair.Value = ""; parsedOK = false; } else { ckFieldExpr = ckFieldExpr.Trim(); switch (ckFieldExpr.ToLower()) { case "httponly": pair.Key = "httponly"; //pair.value = ""; pair.Value = "true"; parsedOK = true; break; case "secure": pair.Key = "secure"; //pair.value = ""; pair.Value = "true"; parsedOK = true; break; // normal cookie field default: { int equalPos = ckFieldExpr.IndexOf('='); if (equalPos > 0) { pair.Key = ckFieldExpr.Substring(0, equalPos); pair.Key = pair.Key.Trim(); if (IsValidCookieField(pair.Key)) { pair.Value = ckFieldExpr.Substring(equalPos + 1); pair.Value = pair.Value.Trim(); parsedOK = true; } else { pair.Key = ""; pair.Value = ""; parsedOK = false; } } else { pair.Key = ""; pair.Value = ""; parsedOK = false; } break; } } } return(parsedOK); }