1
using System;
2
using System.Collections;
3
using System.IO;
4
using System.Reflection;
5
using System.Text;
6
using System.Web;
7
using System.Xml;
8
9
namespace Webb.WAVE.Controls.Upload
10
...{
11
/**//// <summary>
12
///
13
/// </summary>
14
internal class WebbHttpModule : IHttpModule
15
...{
16
DateTime m_beginTime = DateTime.Now;
17
18
public WebbHttpModule()
19
...{
20
}
21
22
Init#region Init
23
/**//// <summary>
24
///
25
/// </summary>
26
/// <param name="m_application"></param>
27
public void Init(HttpApplication m_application)
28
...{
29
m_application.BeginRequest += new EventHandler(WebbUpload_BeginRequest);
30
m_application.EndRequest += new EventHandler(WebbUpload_EndRequest);
31
m_application.Error += new EventHandler(WebbUpload_Error);
32
}
33
#endregion
34
35
/**//// <summary>
36
///
37
/// </summary>
38
/// <param name="sender"></param>
39
/// <param name="e"></param>
40
private void WebbUpload_BeginRequest(object sender, EventArgs e)
41
...{
42
HttpApplication m_application = sender as HttpApplication;
43
HttpWorkerRequest m_workRequest = GetWorkerRequest();
44
try
45
...{
46
function WebbUpload_BeginRequest#region function WebbUpload_BeginRequest
47
string m_contentType = m_application.Request.ContentType.ToLower();
48
//
49
if(!m_contentType.StartsWith("multipart/form-data")) return;
50
if(!m_workRequest.HasEntityBody()) return;
51
TimeSpan m_timeSpan = DateTime.Now.Subtract(m_beginTime);
52
//
53
int m_currentPoint = 0; //A flag to signal the current data possition.
54
string m_boundaryStr = "\r\n--"+m_contentType.Substring(m_contentType.IndexOf("boundary=")+9);
55
byte[] m_boundaryData = Encoding.ASCII.GetBytes(m_boundaryStr);
56
long m_requestTotalSize = Convert.ToInt64(m_workRequest.GetKnownRequestHeader(11));
57
long m_MaxSize = this.GetUpLoadFileLength();
58
if(m_requestTotalSize>m_MaxSize)
59
...{
60
return;
61
}
62
byte[] m_preloadedEntityBody = m_workRequest.GetPreloadedEntityBody();
63
m_currentPoint += m_preloadedEntityBody.Length;
64
//WebbTextTrace.TraceMsg(m_currentPoint.ToString());
65
string m_uploadGUID = this.AnalysePreLoadedData(m_preloadedEntityBody, "Webb_Upload_GUID");
66
string m_tempPath = this.AnalysePreLoadedData(m_preloadedEntityBody, "Webb_Upload_TempPath");
67
string m_enamgeUpload = this.AnalysePreLoadedData(m_preloadedEntityBody, "Webb_Upload_Enable");
68
if(m_enamgeUpload!=null&&m_enamgeUpload.ToLower()=="false")
69
...{
70
// m_isEnable = false;
71
return;
72
}
73
//WebbTextTrace.TraceMsg(m_uploadGUID);
74
UploadStatus m_uploadStatus = new UploadStatus();
75
WebbRequestStream m_stream = new WebbRequestStream();
76
if(m_uploadGUID!=string.Empty)
77
...{
78
m_uploadStatus.GetUploadStatus(m_uploadGUID);
79
}
80
else
81
...{
82
m_uploadGUID = m_application.Application["Webb_Upload_GUID"].ToString();
83
m_uploadStatus.GetUploadStatus(m_uploadGUID);
84
}
85
if(m_uploadStatus.IsActive)
86
...{
87
m_uploadStatus.ResetBeginTime();
88
m_uploadStatus.FileName = m_stream.m_rawFileName;
89
m_uploadStatus.AllDataLength = m_requestTotalSize;
90
m_uploadStatus.ReadData = m_preloadedEntityBody.Length;
91
//WebbTextTrace.TraceMsg(m_uploadStatus.ReadData.ToString());
92
m_uploadStatus.Status = UploadStatus.UploadState.Uploading;
93
HttpContext.Current.Application[("Upload_Status_"+m_uploadStatus.UploadGUID)] = m_uploadStatus;
94
}
95
if(m_tempPath!=null&&Directory.Exists(m_tempPath))
96
...{
97
m_stream.SetTempPath(m_tempPath);
98
}
99
m_stream.SetBoundaryFlag(m_boundaryData);
100
m_stream.SetHttpContent(WebbHelper.GetContext());
101
m_stream.TransactReadData(m_preloadedEntityBody);
102
//Is all of the data have uploaded.
103
if (!m_workRequest.IsEntireEntityBodyIsPreloaded())
104
...{
105
...#region
106
int m_bufferSize = 655350; //640K for test.
107
int m_readSize = 0;
108
byte[] m_readBuffer = new byte[m_bufferSize];
109
while(m_requestTotalSize-m_currentPoint>m_bufferSize)
110
...{
111
if (!m_application.Context.Response.IsClientConnected)
112
...{
113
this.ReleaseResource(m_application);
114
this.ClearApplication(m_application);
115
break;
116
}
117
...#region
118
m_readSize = m_workRequest.ReadEntityBody(m_readBuffer,m_bufferSize);
119
m_currentPoint += m_readSize;
120
if(m_uploadStatus.IsActive)
121
...{
122
m_uploadStatus.GetUploadStatus(m_uploadGUID);
123
m_uploadStatus.FileName = m_stream.m_rawFileName;
124
m_uploadStatus.ReadData = m_currentPoint;
125
//m_uploadStatus.AllDataLength = m_requestTotalSize;
126
//WebbTextTrace.TraceMsg(m_uploadStatus.ReadData.ToString());
127
m_uploadStatus.Status = UploadStatus.UploadState.Uploading;
128
HttpContext.Current.Application[("Upload_Status_"+m_uploadGUID)] = m_uploadStatus;
129
}
130
if(m_stream!=null)
131
...{
132
m_stream.TransactReadData(m_readBuffer);
133
}
134
#endregion
135
}
136
int m_leftBufferSize = (int)m_requestTotalSize-m_currentPoint;
137
byte[] m_leftBuffer = new byte[m_leftBufferSize];
138
if (!m_application.Context.Response.IsClientConnected)
139
...{
140
this.ReleaseResource(m_application);
141
this.ClearApplication(m_application);
142
return;
143
}
144
m_readSize = m_workRequest.ReadEntityBody(m_leftBuffer,m_leftBufferSize);
145
//WebbTextTrace.TraceMsg(m_readSize.ToString());
146
m_currentPoint += m_readSize;
147
if(m_uploadStatus.IsActive)
148
...{
149
// WebbTextTrace.TraceMsg(m_uploadDtatus.LeftTime.ToString());
150
m_uploadStatus.GetUploadStatus(m_uploadGUID);
151
m_uploadStatus.ReadData = m_currentPoint;
152
// WebbTextTrace.TraceMsg(m_uploadStatus.ReadData.ToString());
153
m_uploadStatus.Status = UploadStatus.UploadState.Uploaded;
154
HttpContext.Current.Application[("Upload_Status_"+m_uploadGUID)] = m_uploadStatus;
155
}
156
if(m_stream!=null)
157
...{
158
m_stream.TransactReadData(m_leftBuffer);
159
}
160
#endregion
161
}
162
#endregion
163
byte[] readedBodyBuffer = new byte[m_stream.ContentTextBody.Count];
164
m_stream.ContentTextBody.CopyTo(readedBodyBuffer);
165
m_stream.Dispose();
166
this.AddTextPartToRequest(m_workRequest, readedBodyBuffer);
167
// WebbTextTrace.TraceMsg(HttpContext.Current.Request.ContentEncoding.GetString(readedBodyBuffer));
168
}
169
catch (Exception exception)
170
...{
171
this.ReleaseResource(m_application);
172
this.ClearApplication(m_application);
173
throw exception;
174
}
175
}
176
/**//// <summary>
177
///
178
/// </summary>
179
/// <param name="sender"></param>
180
/// <param name="e"></param>
181
private void WebbUpload_EndRequest(object sender, EventArgs e)
182
...{
183
function WebbUpload_EndRequest#region function WebbUpload_EndRequest
184
#endregion
185
186
}
187
/**//// <summary>
188
///
189
/// </summary>
190
/// <param name="sender"></param>
191
/// <param name="e"></param>
192
private void WebbUpload_Error(object sender, EventArgs e)
193
...{
194
function WebbUpload_Error#region function WebbUpload_Error
195
this.ReleaseResource(sender as HttpApplication);
196
this.ClearApplication(sender as HttpApplication);
197
#endregion
198
this.Dispose();
199
}
200
201
Assistant functions#region Assistant functions
202
/**//// <summary>
203
///
204
/// </summary>
205
public void Dispose()
206
...{
207
208
}
209
/**//// <summary>
210
///
211
/// </summary>
212
/// <param name="application"></param>
213
private void ReleaseResource(HttpApplication application)
214
...{
215
if(application.Context.Items["Webb_Upload_Stream"]!=null)
216
...{
217
FileStream m_fs = ((FileStream) application.Context.Items["Webb_Upload_Stream"]);
218
m_fs.Close();
219
m_fs = null;
220
}
221
if (application.Context.Items["Webb_Upload_FileList"] != null)
222
...{
223
Hashtable fileList = ((Hashtable) application.Context.Items["Webb_Upload_FileList"]);
224
225
foreach (object obj in fileList.Values)
226
...{
227
if (!File.Exists(obj.ToString()))
228
...{
229
continue;
230
}
231
File.Delete(obj.ToString());
232
}
233
}
234
application.Context.Items.Clear();
235
}
236
private void ClearApplication(HttpApplication m_application)
237
...{
238
//Webb_Upload_Stream;
239
if(m_application.Context.Items["Webb_Upload_Stream"]!=null)
240
...{
241
FileStream m_fs = ((FileStream) m_application.Context.Items["Webb_Upload_Stream"]);
242
m_fs.Close();
243
m_fs = null;
244
}
245
if (m_application.Context.Items["Webb_Upload_GUID"] != null)
246
...{
247
string uploadGuid = ((string) m_application.Context.Items["Web_Upload_UploadGUID"]);
248
m_application.Application.Remove(("Upload_Status_" + uploadGuid));
249
}
250
}
251
/**//// <summary>
252
/// Get the upload file max size from web.config.
253
/// </summary>
254
/// <returns></returns>
255
private long GetUpLoadFileLength()
256
...{
257
int m_MaxLength = 0;
258
XmlDocument xmlDocument = new XmlDocument();
259
xmlDocument.Load(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath,"web.config"));
260
XmlNode node = xmlDocument.SelectSingleNode("configuration/system.web/httpRuntime/@maxRequestLength");
261
if (node!= null)
262
...{
263
m_MaxLength = Convert.ToInt32(node.Value);
264
}
265
else
266
...{
267
m_MaxLength = 1024;//1MB for default;
268