Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
9.9 KiB

  1. using System;
  2. using System.Text;
  3. using System.Net;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using System.Runtime.Remoting;
  7. using System.Globalization;
  8. using System.Security;
  9. using System.Security.Policy;
  10. using System.Security.Permissions;
  11. using System.Collections;
  12. using System.Runtime.InteropServices;
  13. using System.Reflection;
  14. using System.Configuration.Assemblies;
  15. using System.Threading;
  16. namespace Microsoft.Fusion.ADF
  17. {
  18. //----------------------------------------------------------
  19. // ApplicationMonikerStream
  20. //----------------------------------------------------------
  21. public class ApplicationMonikerStream : Stream
  22. {
  23. HttpWebRequest _webRequest;
  24. WebResponse _webResponse;
  25. Stream _responseStream;
  26. Stream _stream;
  27. Uri _appBase;
  28. byte[] _buffer;
  29. int _offset;
  30. int _count;
  31. AsyncCallback _callback;
  32. object _state;
  33. Uri _uri;
  34. string _filePath;
  35. int _writePos;
  36. int _readPos;
  37. int _read;
  38. long _contentLength;
  39. int _syncBytesRead;
  40. ManualResetEvent _syncEvent;
  41. bool _cachedCopyExists;
  42. public string path;
  43. //----------------------------------------------------------
  44. // ctor
  45. //----------------------------------------------------------
  46. public ApplicationMonikerStream(Uri uri, Uri appBase, string appStorePath)
  47. {
  48. _uri = uri;
  49. _appBase = appBase;
  50. path = _appBase.MakeRelative(_uri);
  51. _filePath = appStorePath + '/' + path;
  52. string fileDir = System.IO.Path.GetDirectoryName(_filePath);
  53. FileInfo fi = new FileInfo(_filePath);
  54. if (fi.Exists)
  55. {
  56. _stream = new FileStream(_filePath, FileMode.Open, FileAccess.ReadWrite);
  57. _contentLength = fi.Length;
  58. _cachedCopyExists = true;
  59. _writePos = (int) fi.Length;
  60. _readPos = 0;
  61. }
  62. else
  63. {
  64. _writePos = _readPos = 0;
  65. _stream = new MemoryStream();
  66. _cachedCopyExists = false;
  67. }
  68. _syncBytesRead = 0;
  69. _syncEvent = new ManualResetEvent(false);
  70. }
  71. //----------------------------------------------------------
  72. // CanRead
  73. //----------------------------------------------------------
  74. public override bool CanRead
  75. {
  76. get { return true;}
  77. }
  78. //----------------------------------------------------------
  79. // CanSeek
  80. //----------------------------------------------------------
  81. public override bool CanSeek
  82. {
  83. get { return false;}
  84. }
  85. //----------------------------------------------------------
  86. // CanWrite
  87. //----------------------------------------------------------
  88. public override bool CanWrite
  89. {
  90. get { return false;}
  91. }
  92. //----------------------------------------------------------
  93. // Length
  94. //----------------------------------------------------------
  95. public override long Length
  96. {
  97. get { return 0;}
  98. }
  99. //----------------------------------------------------------
  100. // Position
  101. //----------------------------------------------------------
  102. public override long Position
  103. {
  104. get { return 0;}
  105. set {}
  106. }
  107. //----------------------------------------------------------
  108. // BeginRead
  109. //----------------------------------------------------------
  110. public override IAsyncResult BeginRead(
  111. byte[] buffer,
  112. int offset,
  113. int count,
  114. AsyncCallback callback,
  115. object state
  116. )
  117. {
  118. _buffer = buffer;
  119. _offset = offset;
  120. _count = count;
  121. _callback = callback;
  122. _state = state;
  123. if (_cachedCopyExists)
  124. {
  125. return _stream.BeginRead(_buffer, _offset, _count, _callback, _state);
  126. }
  127. else
  128. {
  129. if (_responseStream != null)
  130. {
  131. return _responseStream.BeginRead(_buffer, _offset, _count, new AsyncCallback(ReadCallback), state);
  132. }
  133. _webRequest = (HttpWebRequest) WebRequest.CreateDefault(_uri);
  134. return _webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), null);
  135. }
  136. }
  137. //----------------------------------------------------------
  138. // BeginWrite
  139. //----------------------------------------------------------
  140. public override IAsyncResult BeginWrite(
  141. byte[] buffer,
  142. int offset,
  143. int count,
  144. AsyncCallback callback,
  145. object state
  146. )
  147. {
  148. return null;
  149. }
  150. //----------------------------------------------------------
  151. // Close
  152. //----------------------------------------------------------
  153. public override void Close()
  154. {
  155. if (_responseStream != null)
  156. _responseStream.Close();
  157. // _cachedCopyExists -> _stream is response stream from webResponse
  158. if (_cachedCopyExists == false)
  159. {
  160. if (_writePos == _contentLength)
  161. {
  162. string fileDir = System.IO.Path.GetDirectoryName(_filePath);
  163. DirectoryInfo di = new DirectoryInfo(fileDir);
  164. if (di.Exists == false)
  165. di.Create();
  166. byte[] buffer = new Byte[0x4000];
  167. int read;
  168. FileStream fileStream = new FileStream(_filePath, FileMode.Create, FileAccess.Write);
  169. _stream.Seek(0, SeekOrigin.Begin);
  170. while ((read = _stream.Read(buffer, 0, 0x4000)) != 0)
  171. fileStream.Write(buffer, 0, read);
  172. fileStream.Close();
  173. }
  174. }
  175. _stream.Close();
  176. }
  177. //----------------------------------------------------------
  178. // EndRead
  179. //----------------------------------------------------------
  180. public override int EndRead(
  181. IAsyncResult asyncResult
  182. )
  183. {
  184. return _stream.EndRead(asyncResult);
  185. }
  186. //----------------------------------------------------------
  187. // EndWrite
  188. //----------------------------------------------------------
  189. public override void EndWrite(
  190. IAsyncResult asyncResult
  191. )
  192. {
  193. }
  194. //----------------------------------------------------------
  195. // Flush
  196. //----------------------------------------------------------
  197. public override void Flush()
  198. {
  199. if (_responseStream != null)
  200. _responseStream.Flush();
  201. _stream.Flush();
  202. }
  203. //----------------------------------------------------------
  204. // Read
  205. //----------------------------------------------------------
  206. public override int Read(
  207. byte[] buffer,
  208. int offset,
  209. int count)
  210. {
  211. _syncEvent.Reset();
  212. BeginRead(buffer, offset, count, new AsyncCallback(SyncReadCallback), null);
  213. _syncEvent.WaitOne();
  214. return _syncBytesRead;
  215. }
  216. //----------------------------------------------------------
  217. // ReadByte
  218. //----------------------------------------------------------
  219. public override int ReadByte()
  220. {
  221. return 0;
  222. }
  223. //----------------------------------------------------------
  224. // Seek
  225. //----------------------------------------------------------
  226. public override long Seek(
  227. long offset,
  228. SeekOrigin origin)
  229. {
  230. return 0;
  231. }
  232. //----------------------------------------------------------
  233. // SetLength
  234. //----------------------------------------------------------
  235. public override void SetLength(
  236. long value)
  237. {
  238. }
  239. //----------------------------------------------------------
  240. // Write
  241. //----------------------------------------------------------
  242. public override void Write(
  243. byte[] buffer,
  244. int offset,
  245. int count)
  246. {
  247. }
  248. //----------------------------------------------------------
  249. // WriteByte
  250. //----------------------------------------------------------
  251. public override void WriteByte(
  252. byte value)
  253. {
  254. }
  255. //----------------------------------------------------------
  256. // CreateWaitHandle
  257. //----------------------------------------------------------
  258. protected override WaitHandle CreateWaitHandle()
  259. {
  260. return null;
  261. }
  262. //----------------------------------------------------------
  263. // ResponseCallback
  264. //----------------------------------------------------------
  265. private void ResponseCallback(IAsyncResult asyncResult)
  266. {
  267. _webResponse = _webRequest.EndGetResponse(asyncResult);
  268. _contentLength = _webResponse.ContentLength;
  269. _responseStream = _webResponse.GetResponseStream();
  270. _responseStream.BeginRead(_buffer, _offset, _count, new AsyncCallback(ReadCallback), _state);
  271. }
  272. //----------------------------------------------------------
  273. // ReadCallback
  274. //----------------------------------------------------------
  275. private void ReadCallback(IAsyncResult asyncResult)
  276. {
  277. _read = _responseStream.EndRead(asyncResult);
  278. if (_read > 0)
  279. {
  280. _stream.Seek(_writePos, SeekOrigin.Begin);
  281. _stream.Write(_buffer, 0, _read);
  282. _writePos += _read;
  283. _stream.Seek(_readPos, SeekOrigin.Begin);
  284. _stream.BeginRead(_buffer, 0, _read, _callback, _state);
  285. _readPos += _read;
  286. }
  287. else
  288. {
  289. _responseStream.Close();
  290. _responseStream = null;
  291. _stream.BeginRead(_buffer, 0, _read, _callback, _state);
  292. }
  293. }
  294. //----------------------------------------------------------
  295. // SyncReadCallback
  296. //----------------------------------------------------------
  297. private void SyncReadCallback(IAsyncResult asyncResult)
  298. {
  299. _syncBytesRead = EndRead( asyncResult );
  300. _syncEvent.Set();
  301. return;
  302. }
  303. //----------------------------------------------------------
  304. // GetCachePath
  305. //----------------------------------------------------------
  306. public Uri GetCachePath()
  307. {
  308. return new Uri(_filePath);
  309. }
  310. //----------------------------------------------------------
  311. // CachedCopyExists
  312. //----------------------------------------------------------
  313. public bool CachedCopyExists()
  314. {
  315. return _cachedCopyExists;
  316. }
  317. //----------------------------------------------------------
  318. // GetCacheFileSize
  319. //----------------------------------------------------------
  320. public long GetCacheFileSize()
  321. {
  322. return _stream.Length;
  323. }
  324. //----------------------------------------------------------
  325. // Dispose
  326. //----------------------------------------------------------
  327. public void Dispose()
  328. {
  329. if (_responseStream != null)
  330. {
  331. _responseStream.Close();
  332. _responseStream = null;
  333. }
  334. if (_stream != null)
  335. {
  336. _stream.Close();
  337. _stream = null;
  338. }
  339. }
  340. }
  341. }