-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhonySession.cs
More file actions
220 lines (188 loc) · 5.35 KB
/
PhonySession.cs
File metadata and controls
220 lines (188 loc) · 5.35 KB
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
218
219
220
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
/// <summary>
/// The FonySession allows a developer to fake up a HTTP context, so that controllers can operate against something that looks like a real session.
/// </summary>
namespace TitaniumBunker.PhonySession
{
public class PhonyUploadFile : HttpPostedFileBase
{
private System.IO.MemoryStream _inputStream = new System.IO.MemoryStream();
private string _fileName;
private string _contentType;
public PhonyUploadFile(string Filename, System.IO.Stream ContentStream, string MIMEtype)
{
_fileName = Filename;
ContentStream.CopyTo( _inputStream );
_contentType = MIMEtype;
}
public override string ContentType
{
get
{
return _contentType;
}
}
public override string FileName
{
get
{
return _fileName;
}
}
public override System.IO.Stream InputStream
{
get
{
return _inputStream;
}
}
public override int ContentLength
{
get
{
return (int)_inputStream.Length;
}
}
public static explicit operator HttpPostedFile (PhonyUploadFile o)
{
var constructorInfo = typeof(HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
var obj = (HttpPostedFile)constructorInfo
.Invoke(new object[] { o.FileName, o.ContentType, o.InputStream });
return obj;
}
}
public class FonyHttpContext : HttpContextBase
{
private FonyRequest _request = new FonyRequest();
public override HttpRequestBase Request
{
get
{
return _request;
}
}
}
public class FonyRequest : HttpRequestBase
{
public override Uri Url
{
get
{
return new Uri("http://fake");
}
}
public override string ApplicationPath
{
get
{
return "/";
}
}
private FonyFileCollection __files = new FonyFileCollection ();
public override HttpFileCollectionBase Files
{
get
{
return __files;
}
}
public void AddFile(PhonyUploadFile file)
{
__files.AddFonyFile(file);
}
}
public class FonyFileCollection : HttpFileCollectionBase
{
private NameValueCollection keyCollection = new NameValueCollection();
List<HttpPostedFileBase> _files = new List<HttpPostedFileBase>();
//Dictionary <string,HttpPostedFileBase> _files = new Dictionary<string,HttpPostedFileBase>();
public override void CopyTo(Array dest, int index)
{
var x = _files.ToArray();
x.CopyTo(dest, index);
}
public override int Count
{
get
{
return _files.Count();
}
}
public override System.Collections.IEnumerator GetEnumerator()
{
return _files.GetEnumerator();
}
public override HttpPostedFileBase Get(int index)
{
return _files[index];
}
public override KeysCollection Keys
{
get { return keyCollection.Keys; }
}
public override HttpPostedFileBase this[int index]
{
get
{
return _files[index];
}
}
public void AddFonyFile(PhonyUploadFile file)
{
_files.Add(file );
}
}
public class FonyKeyCollection : ICollection, IEnumerable
{
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
public int Count
{
get { throw new NotImplementedException(); }
}
public bool IsSynchronized
{
get { throw new NotImplementedException(); }
}
public object SyncRoot
{
get { throw new NotImplementedException(); }
}
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
public class FonySession
{
public FonySession()
{
httpContext = new FonyHttpContext();
}
public FonyHttpContext httpContext { get; set; }
public void AddFileUpload(PhonyUploadFile FileToUpload)
{
var f = (FonyRequest)this.httpContext.Request ;
f.AddFile(FileToUpload);
}
public RequestContext BuildRequestContext()
{
return new RequestContext(httpContext, new RouteData());
}
public ControllerContext BuildControllerContext(ControllerBase Controller)
{
ControllerContext c = new ControllerContext(httpContext, new RouteData() ,Controller);
return c;
}
}
}