Fixes for loading saved cookie problems

This commit is contained in:
unknown 2015-07-11 12:54:15 -06:00
parent f76ec51c62
commit 469712c027
19 changed files with 164 additions and 81 deletions

View File

@ -10,8 +10,6 @@
<RootNamespace>CurlSharp</RootNamespace>
<AssemblyName>CurlSharp</AssemblyName>
<FileAlignment>512</FileAlignment>
<ProductVersion>12.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -86,4 +84,14 @@
</Target>
-->
<ItemGroup />
<ProjectExtensions>
<MonoDevelop>
<Properties>
<Policies>
<TextStylePolicy inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" />
<CSharpFormattingPolicy IndentSwitchBody="True" IndentBlocksInsideExpressions="True" AnonymousMethodBraceStyle="NextLine" PropertyBraceStyle="NextLine" PropertyGetBraceStyle="NextLine" PropertySetBraceStyle="NextLine" EventBraceStyle="NextLine" EventAddBraceStyle="NextLine" EventRemoveBraceStyle="NextLine" StatementBraceStyle="NextLine" ElseNewLinePlacement="NewLine" CatchNewLinePlacement="NewLine" FinallyNewLinePlacement="NewLine" WhileNewLinePlacement="DoNotCare" ArrayInitializerWrapping="DoNotChange" ArrayInitializerBraceStyle="NextLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" />
</Policies>
</Properties>
</MonoDevelop>
</ProjectExtensions>
</Project>

View File

@ -1,5 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
# Visual Studio 2012
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jackett", "Jackett\Jackett.csproj", "{E636D5F8-68B4-4903-B4ED-CCFD9C9E899F}"
@ -12,14 +13,49 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E636D5F8-68B4-4903-B4ED-CCFD9C9E899F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E636D5F8-68B4-4903-B4ED-CCFD9C9E899F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E636D5F8-68B4-4903-B4ED-CCFD9C9E899F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E636D5F8-68B4-4903-B4ED-CCFD9C9E899F}.Release|Any CPU.Build.0 = Release|Any CPU
{74420A79-CC16-442C-8B1E-7C1B913844F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{74420A79-CC16-442C-8B1E-7C1B913844F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74420A79-CC16-442C-8B1E-7C1B913844F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74420A79-CC16-442C-8B1E-7C1B913844F0}.Release|Any CPU.Build.0 = Release|Any CPU
{E636D5F8-68B4-4903-B4ED-CCFD9C9E899F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E636D5F8-68B4-4903-B4ED-CCFD9C9E899F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E636D5F8-68B4-4903-B4ED-CCFD9C9E899F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E636D5F8-68B4-4903-B4ED-CCFD9C9E899F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
$0.TextStylePolicy = $1
$1.inheritsSet = VisualStudio
$1.inheritsScope = text/plain
$1.scope = text/x-csharp
$0.CSharpFormattingPolicy = $2
$2.IndentSwitchBody = True
$2.IndentBlocksInsideExpressions = True
$2.AnonymousMethodBraceStyle = NextLine
$2.PropertyBraceStyle = NextLine
$2.PropertyGetBraceStyle = NextLine
$2.PropertySetBraceStyle = NextLine
$2.EventBraceStyle = NextLine
$2.EventAddBraceStyle = NextLine
$2.EventRemoveBraceStyle = NextLine
$2.StatementBraceStyle = NextLine
$2.ElseNewLinePlacement = NewLine
$2.CatchNewLinePlacement = NewLine
$2.FinallyNewLinePlacement = NewLine
$2.WhileNewLinePlacement = DoNotCare
$2.ArrayInitializerWrapping = DoNotChange
$2.ArrayInitializerBraceStyle = NextLine
$2.BeforeMethodDeclarationParentheses = False
$2.BeforeMethodCallParentheses = False
$2.BeforeConstructorDeclarationParentheses = False
$2.NewLineBeforeConstructorInitializerColon = NewLine
$2.NewLineAfterConstructorInitializerColon = SameLine
$2.BeforeDelegateDeclarationParentheses = False
$2.NewParentheses = False
$2.SpacesBeforeBrackets = False
$2.inheritsSet = Mono
$2.inheritsScope = text/x-csharp
$2.scope = text/x-csharp
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -5,29 +5,53 @@ using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Jackett
{
public static class CookieContainerExtensions
{
public static void FillFromJson (this CookieContainer cookies, Uri uri, JArray json)
{
foreach (string cookie in json) {
public static class CookieContainerExtensions
{
var w = cookie.Split ('=');
if (w.Length == 1)
cookies.Add (uri, new Cookie{ Name = cookie.Trim () });
else
cookies.Add (uri, new Cookie (w [0].Trim (), w [1].Trim ()));
}
}
public static void FillFromJson(this CookieContainer cookies, Uri uri, JToken json)
{
if (json["cookies"] != null)
{
var cookieArray = (JArray)json["cookies"];
foreach (string cookie in cookieArray)
{
var w = cookie.Split('=');
if (w.Length == 1)
{
cookies.Add(uri, new Cookie { Name = cookie.Trim() });
}
else
{
cookies.Add(uri, new Cookie(w[0].Trim(), w[1].Trim()));
}
}
}
public static JArray ToJson (this CookieContainer cookies, Uri baseUrl)
{
return new JArray ((
from cookie in cookies.GetCookies (baseUrl).Cast<Cookie> ()
select cookie.Name.Trim () + "=" + cookie.Value.Trim ()
).ToArray ());
}
}
if (json["cookie_header"] != null)
{
var cfh = (string)json["cookie_header"];
var cookieHeaders = ((string)json["cookie_header"]).Split(';');
foreach (var c in cookieHeaders)
{
try
{
cookies.SetCookies(uri, c);
}
catch (CookieException ex)
{
Program.LoggerInstance.Info("(Non-critical) Problem loading cookie {0}, {1}, {2}", uri, c, ex.Message);
}
}
}
}
public static void DumpToJson(this CookieContainer cookies, Uri uri, JToken json)
{
json["cookie_header"] = cookies.GetCookieHeader(uri);
}
}
}

View File

@ -75,16 +75,13 @@ namespace Jackett.Indexers
public async Task ApplyConfiguration(JToken configJson)
{
cookies = new CookieContainer();
client = new HttpClient(handler);
var configSaveData = new JObject();
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
var config = new ConfigurationDataBasicLogin();
config.LoadValuesFromJson(configJson);
var pairs = new Dictionary<string, string> {
{ "username", config.Username.Value },
{ "password", @config.Password.Value },
@ -95,17 +92,18 @@ namespace Jackett.Indexers
var content = new FormUrlEncodedContent(pairs);
var message = CreateHttpRequest(new Uri(LoginUrl));
message.Content = content;
//message.Headers.Referrer = new Uri(LoginUrl);
//message.Headers.Referrer = new Uri(LoginUrl);
string responseContent;
JArray cookieJArray;
configSaveData = new JObject();
if (Program.IsWindows)
{
// If Windows use .net http
var response = await client.SendAsync(message);
responseContent = await response.Content.ReadAsStringAsync();
cookieJArray = cookies.ToJson(SiteLink);
cookies.DumpToJson(SiteLink, configSaveData);
}
else
{
@ -113,7 +111,7 @@ namespace Jackett.Indexers
var response = await CurlHelper.PostAsync(LoginUrl, pairs);
responseContent = Encoding.UTF8.GetString(response.Content);
cookieHeader = response.CookieHeader;
cookieJArray = new JArray(response.CookiesFlat);
configSaveData["cookie_header"] = cookieHeader;
}
if (!responseContent.Contains("logout.php?"))
@ -126,8 +124,6 @@ namespace Jackett.Indexers
}
else
{
configSaveData = new JObject();
configSaveData["cookies"] = cookieJArray;
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
@ -146,7 +142,7 @@ namespace Jackett.Indexers
public void LoadFromSavedConfiguration(JToken jsonConfig)
{
cookies.FillFromJson(SiteLink, (JArray)jsonConfig["cookies"]);
cookies.FillFromJson(SiteLink, jsonConfig);
cookieHeader = cookies.GetCookieHeader(SiteLink);
IsConfigured = true;
}

View File

@ -131,7 +131,7 @@ namespace Jackett.Indexers
{
AllowRaws = config.IncludeRaw.Value;
var configSaveData = new JObject();
configSaveData["cookies"] = cookieContainer.ToJson(SiteLink);
cookieContainer.DumpToJson(SiteLink, configSaveData);
configSaveData["raws"] = AllowRaws;
if (OnSaveConfigurationRequested != null)
@ -143,7 +143,7 @@ namespace Jackett.Indexers
public void LoadFromSavedConfiguration(JToken jsonConfig)
{
cookieContainer.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]);
cookieContainer.FillFromJson(new Uri(BaseUrl), jsonConfig);
IsConfigured = true;
AllowRaws = jsonConfig["raws"].Value<bool>();
}

View File

@ -86,7 +86,7 @@ namespace Jackett.Indexers
else
{
var configSaveData = new JObject();
configSaveData["cookies"] = cookies.ToJson(SiteLink);
cookies.DumpToJson(SiteLink, configSaveData);
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
@ -101,7 +101,7 @@ namespace Jackett.Indexers
public void LoadFromSavedConfiguration(JToken jsonConfig)
{
cookies.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]);
cookies.FillFromJson(new Uri(BaseUrl), jsonConfig);
IsConfigured = true;
}

View File

@ -111,7 +111,7 @@ namespace Jackett
else
{
var configSaveData = new JObject();
configSaveData["cookies"] = cookies.ToJson(SiteLink);
cookies.DumpToJson(SiteLink, configSaveData);
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
@ -122,7 +122,7 @@ namespace Jackett
public void LoadFromSavedConfiguration(JToken jsonConfig)
{
cookies.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]);
cookies.FillFromJson(new Uri(BaseUrl), jsonConfig);
IsConfigured = true;
}

View File

@ -90,7 +90,7 @@ namespace Jackett
else
{
var configSaveData = new JObject();
configSaveData["cookies"] = cookies.ToJson(SiteLink);
cookies.DumpToJson(SiteLink, configSaveData);
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
@ -101,7 +101,7 @@ namespace Jackett
public void LoadFromSavedConfiguration(JToken jsonConfig)
{
cookies.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]);
cookies.FillFromJson(new Uri(BaseUrl), jsonConfig);
IsConfigured = true;
}

View File

@ -89,7 +89,7 @@ namespace Jackett.Indexers
else
{
var configSaveData = new JObject();
configSaveData["cookies"] = cookies.ToJson(SiteLink);
cookies.DumpToJson(SiteLink, configSaveData);
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
@ -110,7 +110,7 @@ namespace Jackett.Indexers
public void LoadFromSavedConfiguration(Newtonsoft.Json.Linq.JToken jsonConfig)
{
cookies.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]);
cookies.FillFromJson(new Uri(BaseUrl), jsonConfig);
IsConfigured = true;
}

View File

@ -86,14 +86,16 @@ namespace Jackett.Indexers
var content = new FormUrlEncodedContent(pairs);
string responseContent;
JArray cookieJArray;
var configSaveData = new JObject();
if (Program.IsWindows)
{
// If Windows use .net http
var response = await client.PostAsync(LoginUrl, content);
responseContent = await response.Content.ReadAsStringAsync();
cookieJArray = cookies.ToJson(SiteLink);
cookies.DumpToJson(SiteLink, configSaveData);
}
else
{
@ -101,7 +103,7 @@ namespace Jackett.Indexers
var response = await CurlHelper.PostAsync(LoginUrl, pairs);
responseContent = Encoding.UTF8.GetString(response.Content);
cookieHeader = response.CookieHeader;
cookieJArray = new JArray(response.CookiesFlat);
configSaveData["cookie_header"] = cookieHeader;
}
if (!responseContent.Contains("logout.php?"))
@ -114,9 +116,6 @@ namespace Jackett.Indexers
}
else
{
var configSaveData = new JObject();
configSaveData["cookies"] = cookieJArray;
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
@ -126,7 +125,7 @@ namespace Jackett.Indexers
public void LoadFromSavedConfiguration(JToken jsonConfig)
{
cookies.FillFromJson(SiteLink, (JArray)jsonConfig["cookies"]);
cookies.FillFromJson(SiteLink, jsonConfig);
cookieHeader = cookies.GetCookieHeader(SiteLink);
IsConfigured = true;
}

View File

@ -80,14 +80,14 @@ namespace Jackett.Indexers
var content = new FormUrlEncodedContent(pairs);
string responseContent;
JArray cookieJArray;
var configSaveData = new JObject();
if (Program.IsWindows)
{
// If Windows use .net http
var response = await client.PostAsync(LoginUrl, content);
responseContent = await response.Content.ReadAsStringAsync();
cookieJArray = cookies.ToJson(SiteLink);
cookies.DumpToJson(SiteLink, configSaveData);
}
else
{
@ -95,7 +95,7 @@ namespace Jackett.Indexers
var response = await CurlHelper.PostAsync(LoginUrl, pairs);
responseContent = Encoding.UTF8.GetString(response.Content);
cookieHeader = response.CookieHeader;
cookieJArray = new JArray(response.CookiesFlat);
configSaveData["cookie_header"] = cookieHeader;
}
if (!responseContent.Contains("nav_profile"))
@ -107,9 +107,6 @@ namespace Jackett.Indexers
}
else
{
var configSaveData = new JObject();
configSaveData["cookies"] = cookieJArray;
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
@ -119,7 +116,7 @@ namespace Jackett.Indexers
public void LoadFromSavedConfiguration(JToken jsonConfig)
{
cookies.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]);
cookies.FillFromJson(new Uri(BaseUrl), jsonConfig);
cookieHeader = cookies.GetCookieHeader(SiteLink);
IsConfigured = true;
}

View File

@ -107,7 +107,7 @@ namespace Jackett.Indexers
else
{
var configSaveData = new JObject();
configSaveData["cookies"] = cookies.ToJson(SiteLink);
cookies.DumpToJson(SiteLink, configSaveData);
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
@ -118,7 +118,7 @@ namespace Jackett.Indexers
public void LoadFromSavedConfiguration(JToken jsonConfig)
{
cookies.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]);
cookies.FillFromJson(new Uri(BaseUrl), jsonConfig);
IsConfigured = true;
}

View File

@ -90,7 +90,7 @@ namespace Jackett.Indexers
else
{
var configSaveData = new JObject();
configSaveData["cookies"] = cookies.ToJson(SiteLink);
cookies.DumpToJson(SiteLink, configSaveData);
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
@ -101,7 +101,7 @@ namespace Jackett.Indexers
public void LoadFromSavedConfiguration(JToken jsonConfig)
{
cookies.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]);
cookies.FillFromJson(new Uri(BaseUrl), jsonConfig);
IsConfigured = true;
}

View File

@ -91,7 +91,7 @@ namespace Jackett.Indexers
else
{
var configSaveData = new JObject();
configSaveData["cookies"] = cookies.ToJson(SiteLink);
cookies.DumpToJson(SiteLink, configSaveData);
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
@ -103,7 +103,7 @@ namespace Jackett.Indexers
public void LoadFromSavedConfiguration(JToken jsonConfig)
{
cookies.FillFromJson(new Uri(BaseUrl), (JArray)jsonConfig["cookies"]);
cookies.FillFromJson(new Uri(BaseUrl), jsonConfig);
IsConfigured = true;
}

View File

@ -57,11 +57,9 @@
<Reference Include="CsQuery">
<HintPath>..\packages\CsQuery.1.3.4\lib\net40\CsQuery.dll</HintPath>
</Reference>
<Reference Include="ModernHttpClient">
<HintPath>..\packages\modernhttpclient.2.3.0\lib\Portable-Net45+WinRT45+WP8+WPA81\ModernHttpClient.dll</HintPath>
</Reference>
<Reference Include="NLog">
<HintPath>..\packages\NLog.3.2.0.0\lib\net45\NLog.dll</HintPath>
<Reference Include="NLog.Windows.Forms, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.Windows.Forms.2.0.0.0\lib\net35\NLog.Windows.Forms.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -76,7 +74,10 @@
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog">
<HintPath>..\packages\NLog.4.0.1\lib\net45\NLog.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
@ -293,4 +294,14 @@
<Target Name="AfterBuild">
</Target>
-->
<ProjectExtensions>
<MonoDevelop>
<Properties>
<Policies>
<TextStylePolicy inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" />
<CSharpFormattingPolicy IndentSwitchBody="True" IndentBlocksInsideExpressions="True" AnonymousMethodBraceStyle="NextLine" PropertyBraceStyle="NextLine" PropertyGetBraceStyle="NextLine" PropertySetBraceStyle="NextLine" EventBraceStyle="NextLine" EventAddBraceStyle="NextLine" EventRemoveBraceStyle="NextLine" StatementBraceStyle="NextLine" ElseNewLinePlacement="NewLine" CatchNewLinePlacement="NewLine" FinallyNewLinePlacement="NewLine" WhileNewLinePlacement="DoNotCare" ArrayInitializerWrapping="DoNotChange" ArrayInitializerBraceStyle="NextLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" />
</Policies>
</Properties>
</MonoDevelop>
</ProjectExtensions>
</Project>

View File

@ -1,4 +1,6 @@
namespace Jackett
#if !__MonoCS__
namespace Jackett
{
partial class Main
{
@ -97,4 +99,5 @@
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemShutdown;
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using Microsoft.Win32;
#if !__MonoCS__
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -92,3 +93,4 @@ namespace Jackett
}
}
}
#endif

View File

@ -3,6 +3,7 @@ using Newtonsoft.Json.Linq;
using NLog;
using NLog.Config;
using NLog.Targets;
using NLog.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -70,12 +71,14 @@ namespace Jackett
if (Program.IsWindows)
{
#if !__MonoCS__
var logAlert = new MessageBoxTarget();
logConfig.AddTarget("alert", logAlert);
logAlert.Layout = "${message}";
logAlert.Caption = "Alert";
var logAlertRule = new LoggingRule("*", LogLevel.Fatal, logAlert);
logConfig.LoggingRules.Add(logAlertRule);
#endif
}
var logConsole = new ConsoleTarget();
@ -98,7 +101,11 @@ namespace Jackett
try
{
if (Program.IsWindows)
{
#if !__MonoCS__
Application.Run(new Main());
#endif
}
}
catch (Exception)
{

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CsQuery" version="1.3.4" targetFramework="net451" />
<package id="modernhttpclient" version="2.3.0" targetFramework="net451" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
<package id="NLog" version="3.2.0.0" targetFramework="net451" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net451" />
<package id="NLog" version="4.0.1" targetFramework="net451" />
<package id="NLog.Windows.Forms" version="2.0.0.0" targetFramework="net451" />
</packages>