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.

166 lines
5.1 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. using Microsoft.Fusion.ADF;
  17. public class RequestState
  18. {
  19. const int BufferSize = 0x4000;
  20. public byte[] BufferRead;
  21. public WebRequest Request;
  22. public Stream ResponseStream;
  23. public RequestState()
  24. {
  25. BufferRead = new byte[BufferSize];
  26. Request = null;
  27. ResponseStream = null;
  28. }
  29. }
  30. class ClientGetAsync
  31. {
  32. public static ManualResetEvent allDone = new ManualResetEvent(false);
  33. const int BUFFER_SIZE = 1024;
  34. public static void Main()
  35. {
  36. // You can start with only a url to a manifest file.
  37. string manifestUrl = "http://fusiontest/clickonce/test/adriaanc/AVPad/avpad_0.0.3010.0/microsoft.avalon.avpad.manifest";
  38. // Or start with a locally cached manifest file and provide the appbase.
  39. //string manifestUrl = "file://c:/Documents and Settings/adriaanc/Local Settings/My Programs/__temp__/x86_Microsoft.Avalon.AvPad_0.0.3110.0_332CDF2FABC_en/microsoft.avalon.avpad.manifest";
  40. //string appBase = "http://fusiontest/clickonce/test/adriaanc/AVPad/avpad_0.0.3010.0/";
  41. // Construct the moniker given a url to the manifest.
  42. ApplicationMoniker am = new ApplicationMoniker(manifestUrl, null);
  43. // If we had a locally cached manifest we would specify the local manifest url and
  44. // url for the application base (appbase) instead.
  45. // ApplicationMoniker am = new ApplicationMoniker(manifestUrl, appBase);
  46. // Monolithic download of app bits the easy way.
  47. // Walk through the manifest and download files, dependent
  48. // assemblies. note - modules not yet supported, coming soon.
  49. am.DownloadManifestAndDependencies();
  50. // You can delete all application files cached locally by calling this.
  51. //am.PurgeFiles();
  52. // Progressive rendering download - register the application base as prefix
  53. // for the application moniker and download bits using standard
  54. // system.net apis. If you have not called PurgeFiles above, all requests
  55. // will be satisfied from cache.
  56. WebRequest.RegisterPrefix(am.AppBase, am);
  57. DoManualDownload(am, manifestUrl);
  58. }
  59. //----------------------------------------------------------
  60. // DoManualDownload
  61. //----------------------------------------------------------
  62. static void DoManualDownload(ApplicationMoniker am, string manifestUrl)
  63. {
  64. DoDownload(new Uri(manifestUrl));
  65. DependentFileInfo dfi;
  66. DependentAssemblyInfo dai;
  67. ApplicationManifestImport ami = am.GetApplicationManifestImport();
  68. while ((dfi = ami.GetNextDependentFileInfo()) != null)
  69. {
  70. Uri httpSite = new Uri(new Uri(am.AppBase), (string) dfi["name"]);
  71. Console.WriteLine((string) dfi["name"]);
  72. DoDownload(httpSite);
  73. }
  74. while ((dai = ami.GetNextDependentAssemblyInfo()) != null)
  75. {
  76. Uri httpSite = new Uri(new Uri(am.AppBase), (string) dai["codeBase"]);
  77. Console.WriteLine(dai.assemblyIdentity.GetDirectoryName() + " " + (string) dai["codeBase"]);
  78. DoDownload(httpSite);
  79. }
  80. // If you want to re-iterate, you need to reset these. This sucks and I need
  81. // to find a way to control the xpath iterator class better.
  82. // am.ResetIterators();
  83. }
  84. //----------------------------------------------------------
  85. // DoDownload
  86. //----------------------------------------------------------
  87. private static void DoDownload(Uri httpSite)
  88. {
  89. allDone.Reset();
  90. WebRequest wreq = WebRequest.Create(httpSite);
  91. RequestState rs = new RequestState();
  92. rs.Request = wreq;
  93. IAsyncResult r = (IAsyncResult) wreq.BeginGetResponse(
  94. new AsyncCallback(RespCallback), rs);
  95. allDone.WaitOne();
  96. }
  97. //----------------------------------------------------------
  98. // ResponseCallback
  99. //----------------------------------------------------------
  100. private static void RespCallback(IAsyncResult ar)
  101. {
  102. RequestState rs = (RequestState) ar.AsyncState;
  103. WebRequest req = rs.Request;
  104. WebResponse resp = req.EndGetResponse(ar);
  105. Stream ResponseStream = resp.GetResponseStream();
  106. rs.ResponseStream = ResponseStream;
  107. IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0,
  108. BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
  109. }
  110. //----------------------------------------------------------
  111. // ReadCallback
  112. //----------------------------------------------------------
  113. private static void ReadCallBack(IAsyncResult asyncResult)
  114. {
  115. RequestState rs = (RequestState)asyncResult.AsyncState;
  116. Stream responseStream = rs.ResponseStream;
  117. int read = responseStream.EndRead( asyncResult );
  118. if (read > 0)
  119. {
  120. IAsyncResult ar = responseStream.BeginRead(
  121. rs.BufferRead, 0, BUFFER_SIZE,
  122. new AsyncCallback(ReadCallBack), rs);
  123. }
  124. else
  125. {
  126. responseStream.Close();
  127. allDone.Set();
  128. }
  129. return;
  130. }
  131. }