forked from w8tcha/ripper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieManager.cs
217 lines (178 loc) · 5.68 KB
/
CookieManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CookieManager.cs" company="The Watcher">
// Copyright (c) The Watcher Partial Rights Reserved.
// This software is licensed under the MIT license. See license.txt for details.
// </copyright>
// <summary>
// Code Named: Ripper
// Function : Extracts Images posted on forums and attempts to fetch them to disk.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Ripper.Core.Components
{
#region
using System.Collections;
using Ripper.Core.Objects;
#endregion
/// <summary>
/// The cookie manager.
/// </summary>
public class CookieManager
{
#region Constants and Fields
/// <summary>
/// The Cookie Manager instance.
/// </summary>
private static CookieManager _instance;
/// <summary>
/// The cookie table.
/// </summary>
private readonly Hashtable cookieTable;
/// <summary>
/// The cookie enumerator
/// </summary>
private IDictionaryEnumerator cookieEnumerator;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="CookieManager"/> class.
/// </summary>
public CookieManager()
{
this.cookieTable = new Hashtable();
}
#endregion
#region Public Methods
/// <summary>
/// Gets the CookieManager instance.
/// </summary>
/// <returns>
/// Returns the CookieManager instance
/// </returns>
public static CookieManager GetInstance()
{
return _instance ?? (_instance = new CookieManager());
}
/// <summary>
/// The delete all cookies.
/// </summary>
public void DeleteAllCookies()
{
this.ResetCookiePos();
var cookieKeys = new ArrayList();
var cookiePair = this.NextCookie();
while (cookiePair != null)
{
cookieKeys.Add(cookiePair.Key);
cookiePair = this.NextCookie();
}
if (cookieKeys.Count <= 0)
{
return;
}
foreach (object t in cookieKeys)
{
this.DeleteCookie((string)t);
}
}
/// <summary>
/// Deletes the cookie.
/// </summary>
/// <param name="key">The key.</param>
public void DeleteCookie(string key)
{
if (this.cookieTable.ContainsKey(key))
{
this.cookieTable.Remove(key);
}
}
/// <summary>
/// Gets the cookie.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>
/// Returns the cookie
/// </returns>
public string GetCookie(string key)
{
if (this.cookieTable.ContainsKey(key))
{
return (string)this.cookieTable[key];
}
return string.Empty;
}
/// <summary>
/// Gets the cookie string.
/// </summary>
/// <returns>
/// Returns the cookie string.
/// </returns>
public string GetCookieString()
{
this.ResetCookiePos();
var cookies = string.Empty;
var cookiePair = this.NextCookie();
while (cookiePair != null)
{
cookies += string.Format("{0}={1};", cookiePair.Key, cookiePair.Value);
cookiePair = this.NextCookie();
}
if (cookies.Substring(cookies.Length - 1, 1) == ";")
{
cookies = cookies.Substring(0, cookies.Length - 1);
}
return cookies;
}
/// <summary>
/// Gets the next the cookie.
/// </summary>
/// <returns>
/// Returns the next cookie.
/// </returns>
public CookiePair NextCookie()
{
if (this.cookieEnumerator == null)
{
this.ResetCookiePos();
}
CookiePair newpair = null;
if (this.cookieEnumerator.MoveNext())
{
newpair = new CookiePair
{
Key = (string)this.cookieEnumerator.Key, Value = (string)this.cookieEnumerator.Value
};
}
return newpair;
}
/// <summary>
/// Resets the cookie position.
/// </summary>
public void ResetCookiePos()
{
if (this.cookieEnumerator == null)
{
this.cookieEnumerator = this.cookieTable.GetEnumerator();
}
this.cookieEnumerator.Reset();
}
/// <summary>
/// Sets the cookie.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void SetCookie(string key, string value)
{
if (this.cookieTable.ContainsKey(key))
{
// Just Update
this.cookieTable[key] = value;
}
else
{
this.cookieTable.Add(key, value);
}
}
#endregion
}
}