FileWebRequest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // System.Net.FileWebRequest
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.IO;
  10. using System.Runtime.Serialization;
  11. using System.Runtime.Remoting.Messaging;
  12. namespace System.Net
  13. {
  14. [Serializable]
  15. public class FileWebRequest : WebRequest, ISerializable
  16. {
  17. private Uri uri;
  18. private WebHeaderCollection webHeaders;
  19. private ICredentials credentials;
  20. private string connectionGroup;
  21. private string method;
  22. private int timeout;
  23. private bool open = false;
  24. // Constructors
  25. internal FileWebRequest (Uri uri)
  26. {
  27. this.uri = uri;
  28. this.webHeaders = new WebHeaderCollection ();
  29. this.method = "GET";
  30. this.timeout = System.Threading.Timeout.Infinite;
  31. }
  32. [MonoTODO]
  33. protected FileWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
  34. {
  35. throw new NotImplementedException ();
  36. }
  37. // Properties
  38. // currently not used according to spec
  39. public override string ConnectionGroupName {
  40. get { return connectionGroup; }
  41. set { connectionGroup = value; }
  42. }
  43. public override long ContentLength {
  44. get {
  45. try {
  46. return Int64.Parse (webHeaders ["Content-Length"]);
  47. } catch (Exception) {
  48. return 0;
  49. }
  50. }
  51. set {
  52. if (value < 0)
  53. throw new ArgumentException ("value");
  54. webHeaders ["Content-Length"] = Convert.ToString (value);
  55. }
  56. }
  57. public override string ContentType {
  58. get { return webHeaders ["Content-Type"]; }
  59. set { webHeaders ["Content-Type"] = value; }
  60. }
  61. public override ICredentials Credentials {
  62. get { return credentials; }
  63. set { credentials = value; }
  64. }
  65. public override WebHeaderCollection Headers {
  66. get { return webHeaders; }
  67. }
  68. // currently not used according to spec
  69. public override string Method {
  70. get { return this.method; }
  71. set { this.method = value; }
  72. }
  73. // currently not used according to spec
  74. public override bool PreAuthenticate {
  75. get { throw new NotSupportedException (); }
  76. set { throw new NotSupportedException (); }
  77. }
  78. // currently not used according to spec
  79. public override IWebProxy Proxy {
  80. get { throw new NotSupportedException (); }
  81. set { throw new NotSupportedException (); }
  82. }
  83. public override Uri RequestUri {
  84. get { return this.uri; }
  85. }
  86. public override int Timeout {
  87. get { return timeout; }
  88. set {
  89. if (value < 0)
  90. throw new ArgumentException ("value");
  91. timeout = value;
  92. }
  93. }
  94. // Methods
  95. private delegate Stream GetRequestStreamCallback ();
  96. private delegate WebResponse GetResponseCallback ();
  97. // TODO: bit simplistic this, need to add code to check for exceptions..
  98. // TODO: use Timeout value
  99. // TODO: as the spec says GetResponse can throw an exception on timeout,
  100. // and because in theory one could start an async GetResponse, change
  101. // properties, and start a sync GetResponse it seems best that GetResponse
  102. // actually works via the async methods and the async delegates delegate
  103. // to a private get response method. timeout value probably isn't an
  104. // issue here, but it is in HttpWebRequest.
  105. public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
  106. {
  107. GetRequestStreamCallback c = new GetRequestStreamCallback (this.GetRequestStream);
  108. return c.BeginInvoke (callback, state);
  109. }
  110. public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
  111. {
  112. GetResponseCallback c = new GetResponseCallback (this.GetResponse);
  113. return c.BeginInvoke (callback, state);
  114. }
  115. public override Stream EndGetRequestStream (IAsyncResult asyncResult)
  116. {
  117. if (asyncResult == null)
  118. throw new ArgumentNullException ("asyncResult");
  119. AsyncResult async = (AsyncResult) asyncResult;
  120. GetRequestStreamCallback cb = (GetRequestStreamCallback) async.AsyncDelegate;
  121. asyncResult.AsyncWaitHandle.WaitOne ();
  122. return cb.EndInvoke(asyncResult);
  123. }
  124. public override WebResponse EndGetResponse (IAsyncResult asyncResult)
  125. {
  126. if (asyncResult == null)
  127. throw new ArgumentNullException ("asyncResult");
  128. AsyncResult async = (AsyncResult) asyncResult;
  129. GetResponseCallback cb = (GetResponseCallback) async.AsyncDelegate;
  130. asyncResult.AsyncWaitHandle.WaitOne ();
  131. return cb.EndInvoke(asyncResult);
  132. }
  133. public override Stream GetRequestStream()
  134. {
  135. if (method == null || (!method.Equals ("PUT") && !method.Equals ("POST")))
  136. throw new ProtocolViolationException ("Cannot send file when method is: " + this.method + ". Method must be PUT.");
  137. if (open)
  138. throw new WebException ("Stream already open");
  139. open = true;
  140. FileStream fileStream = new FileWebStream (
  141. this,
  142. FileMode.CreateNew,
  143. FileAccess.Write,
  144. FileShare.Read,
  145. 4096,
  146. false);
  147. return fileStream;
  148. }
  149. public override WebResponse GetResponse()
  150. {
  151. if (method == null || !method.Equals ("GET"))
  152. throw new ProtocolViolationException ("Cannot retrieve file when method is: " + this.method + ". Method must be GET.");
  153. if (open)
  154. throw new WebException ("Stream already open");
  155. FileStream fileStream = new FileWebStream (
  156. this,
  157. FileMode.Open,
  158. FileAccess.Read,
  159. FileShare.Read,
  160. 4096,
  161. false);
  162. return new FileWebResponse (this, fileStream);
  163. }
  164. [MonoTODO]
  165. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  166. StreamingContext streamingContext)
  167. {
  168. throw new NotImplementedException ();
  169. }
  170. internal void Close ()
  171. {
  172. open = false;
  173. }
  174. internal bool IsClosed ()
  175. {
  176. return !open;
  177. }
  178. // to catch the Close called on the FileStream
  179. internal class FileWebStream : FileStream
  180. {
  181. FileWebRequest webRequest;
  182. internal FileWebStream (FileWebRequest webRequest,
  183. FileMode mode,
  184. FileAccess access,
  185. FileShare share,
  186. int bufferSize,
  187. bool useAsync)
  188. : base (webRequest.RequestUri.LocalPath,
  189. mode, access, share, bufferSize, useAsync)
  190. {
  191. this.webRequest = webRequest;
  192. }
  193. public override void Close()
  194. {
  195. base.Close ();
  196. webRequest.Close ();
  197. }
  198. }
  199. }
  200. }