forked from X-rus/xNet
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cookie Filters. Fixed Cookie invalid path. Fixed cookie values ending…
… by comma.
- Loading branch information
1 parent
a4d4e7c
commit 35e7f2c
Showing
3 changed files
with
167 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
|
||
namespace Leaf.xNet | ||
{ | ||
public static class CookieFilters | ||
{ | ||
public static bool Enabled = true; | ||
|
||
public static bool Path = true; | ||
public static bool CommaEndingValue = true; | ||
|
||
/// <summary> | ||
/// Фильтруем Cookie для дальнейшего использования в нативном хранилище. | ||
/// </summary> | ||
/// <param name="rawCookie">Запись Cookie как строка со всеми параметрами</param> | ||
/// <returns>Отфильтрованная Cookie в виде строки со всеми отфильтрованными параметрами</returns> | ||
public static string Filter(string rawCookie) | ||
{ | ||
return !Enabled ? rawCookie | ||
: rawCookie | ||
.FilterPath() | ||
.FilterCommaEndingValue(); | ||
} | ||
|
||
/// <summary>Заменяем все значения path на "/"</summary> | ||
private static string FilterPath(this string rawCookie) | ||
{ | ||
if (!Path) | ||
return rawCookie; | ||
|
||
const string path = "path=/"; | ||
int pathIndex = rawCookie.IndexOf(path, 0, StringComparison.OrdinalIgnoreCase); | ||
if (pathIndex == -1) | ||
return rawCookie; | ||
|
||
pathIndex += path.Length; | ||
if (pathIndex >= rawCookie.Length - 1 || rawCookie[pathIndex] == ';') | ||
return rawCookie; | ||
|
||
int endPathIndex = rawCookie.IndexOf(';', pathIndex); | ||
if (endPathIndex == -1) | ||
endPathIndex = rawCookie.Length; | ||
|
||
return rawCookie.Remove(pathIndex, endPathIndex - pathIndex); | ||
} | ||
|
||
|
||
/// <summary>Заменяем значения кук завершающиеся запятой (escape)</summary> | ||
private static string FilterCommaEndingValue(this string rawCookie) | ||
{ | ||
if (!CommaEndingValue) | ||
return rawCookie; | ||
|
||
int equalIndex = rawCookie.IndexOf('='); | ||
if (equalIndex == -1 || equalIndex >= rawCookie.Length - 1) | ||
return rawCookie; | ||
|
||
int endValueIndex = rawCookie.IndexOf(';', equalIndex + 1); | ||
if (endValueIndex == -1) | ||
endValueIndex = rawCookie.Length - 1; | ||
|
||
int lastCharIndex = endValueIndex - 1; | ||
return rawCookie[lastCharIndex] != ',' | ||
? rawCookie | ||
: rawCookie.Remove(lastCharIndex, 1).Insert(lastCharIndex, "%2C"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters