Skip to content

Commit

Permalink
New cookie domain parsing (Fixed some exceptions when unable to parse…
Browse files Browse the repository at this point in the history
… cookie). Fixed Property: DontTrackCookies (Disabled in parsing headers also).
  • Loading branch information
grandsilence committed Aug 24, 2018
1 parent 28e3669 commit 24a7df3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
15 changes: 14 additions & 1 deletion Leaf.xNet.Tests/~Http/HttpRequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Leaf.xNet.Tests
Expand Down Expand Up @@ -55,6 +56,18 @@ public void GetTest()
}
}

[TestMethod]
public void GetInconsistantCoookie()
{
const string url = "https://tinybuildgames.us7.list-manage.com/subscribe?u=7c5ba06b8b6710be2a32d1afc&id=5211b170b7";

using (var req = new HttpRequest())
{
req.UserAgentRandomize();
req.Get(url);
}
}

[TestMethod]
public void PostTestFormEncoded()
{
Expand Down
61 changes: 58 additions & 3 deletions Leaf.xNet/~Http/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -781,7 +782,7 @@ public Dictionary<string, string>.Enumerator EnumerateHeaders()


// Загружает ответ и возвращает размер ответа в байтах.
internal long LoadResponse(HttpMethod method, bool includeRedirectHeaders)
internal long LoadResponse(HttpMethod method, bool trackMiddleHeaders)
{
Method = method;
Address = _request.Address;
Expand All @@ -791,7 +792,7 @@ internal long LoadResponse(HttpMethod method, bool includeRedirectHeaders)
KeepAliveTimeout = null;
MaximumKeepAliveRequests = null;

if (includeRedirectHeaders && _headers.Count > 0)
if (trackMiddleHeaders && _headers.Count > 0)
{
foreach (var key in _headers.Keys)
MiddleHeaders[key] = _headers[key];
Expand Down Expand Up @@ -916,7 +917,61 @@ private void ReceiveHeaders()
string headerValue = header.Substring(separatorPos + 1).Trim(' ', '\t', '\r', '\n');

if (headerName.Equals("Set-Cookie", StringComparison.OrdinalIgnoreCase))
Cookies.Set(_request.Address, headerValue);
{
if (_request.DontTrackCookies)
continue;

// Отделяем Cross-domain cookie - если не делать, будет исключение.
// Родной парсинг raw-cookie плохо работает.
if (!headerValue.Contains("domain="))
Cookies.Set(_request.Address, headerValue);
else
{
// Разделяем все key=value
var arguments = headerValue.Split(new [] {';'}, StringSplitOptions.RemoveEmptyEntries);
if (arguments.Length == 0)
continue;

// Получаем ключ и значение самой Cookie
var keyValue = arguments[0].Split(new[] {'='}, 2);
var cookie = new Cookie(keyValue[0], keyValue.Length < 2 ? string.Empty : keyValue[1]);

// Обрабатываем дополнительные ключи Cookie
for (int i = 1; i < arguments.Length; i++)
{
keyValue = arguments[i].Split(new[] {'='}, 2);

// Обрабатываем ключи нерегистрозависимо
string key = keyValue[0].Trim().ToLower();
string value = keyValue.Length < 2 ? null : keyValue[1].Trim();

switch (key)
{
case "expires":
if (!DateTime.TryParse(value, out var expires))
continue;

cookie.Expires = expires;
break;

case "path":
cookie.Path = value;
break;
case "domain":
cookie.Domain = value;
break;
case "secure":
cookie.Secure = true;
break;
case "httponly":
cookie.HttpOnly = true;
break;
}
}

Cookies.Set(cookie);
}
}
else
_headers[headerName] = headerValue;
}
Expand Down

0 comments on commit 24a7df3

Please sign in to comment.