Skip to content

Commit

Permalink
Fixed Cookie unescape value before send
Browse files Browse the repository at this point in the history
  • Loading branch information
grandsilence committed Feb 4, 2019
1 parent 8a725e1 commit c88cb3b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
23 changes: 22 additions & 1 deletion Leaf.xNet/~Http/CookieStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Leaf.xNet
{
Expand Down Expand Up @@ -295,7 +296,27 @@ public void Remove(Uri uri, string name)
/// <returns>Вернет строку содержащую все куки для адреса.</returns>
public string GetCookieHeader(Uri uri)
{
return Container.GetCookieHeader(uri);
string header = Container.GetCookieHeader(uri);
if (!UnescapeValuesOnSend)
return header;

// Unescape cookies values
var sb = new StringBuilder();
var cookies = header.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries);

foreach (string cookie in cookies)
{
var kv = cookie.Split(new []{'='}, 2);
sb.Append(kv[0].Trim());
sb.Append('=');
sb.Append(Uri.UnescapeDataString(kv[1].Trim()));
sb.Append("; ");
}

if (sb.Length > 0)
sb.Remove(sb.Length - 2, 2);

return sb.ToString();
}

/// <inheritdoc cref="GetCookieHeader(System.Uri)"/>
Expand Down
6 changes: 1 addition & 5 deletions Leaf.xNet/~Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2172,12 +2172,8 @@ private string ToHeadersString(Dictionary<string, string> headers)

// Каждую Cookie в отдельный заголовок
var cookies = header.Value.Split(new[] {"; "}, StringSplitOptions.None);
// ReSharper disable once ForCanBeConvertedToForeach
for (int i = 0; i < cookies.Length; i++)
{
string cookie = Cookies.UnescapeValuesOnSend ? Uri.UnescapeDataString(cookies[i]) : cookies[i];
foreach (string cookie in cookies)
headersBuilder.AppendFormat("Cookie: {0}\r\n", cookie);
}
}

headersBuilder.AppendLine();
Expand Down

0 comments on commit c88cb3b

Please sign in to comment.