1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.IO;
6 using System.Runtime.Serialization;
7 using System.Globalization;
8 
9 namespace System.Net
10 {
11     [Serializable]
12     public class FileWebResponse : WebResponse, ISerializable
13     {
14         private const int DefaultFileStreamBufferSize = 8192;
15         private const string DefaultFileContentType = "application/octet-stream";
16 
17         private readonly long _contentLength;
18         private readonly FileAccess _fileAccess;
19         private readonly WebHeaderCollection _headers;
20         private readonly Uri _uri;
21 
22         private Stream _stream;
23         private bool _closed;
24 
FileWebResponse(FileWebRequest request, Uri uri, FileAccess access, bool useAsync)25         internal FileWebResponse(FileWebRequest request, Uri uri, FileAccess access, bool useAsync)
26         {
27             try
28             {
29                 _fileAccess = access;
30                 if (access == FileAccess.Write)
31                 {
32                     _stream = Stream.Null;
33                 }
34                 else
35                 {
36                     _stream = new WebFileStream(request, uri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultFileStreamBufferSize, useAsync);
37                     _contentLength = _stream.Length;
38                 }
39                 _headers = new WebHeaderCollection();
40                 _headers[HttpKnownHeaderNames.ContentLength] = _contentLength.ToString(NumberFormatInfo.InvariantInfo);
41                 _headers[HttpKnownHeaderNames.ContentType] = DefaultFileContentType;
42                 _uri = uri;
43             }
44             catch (Exception e)
45             {
46                 throw new WebException(e.Message, e, WebExceptionStatus.ConnectFailure, null);
47             }
48         }
49 
50         [Obsolete("Serialization is obsoleted for this type. http://go.microsoft.com/fwlink/?linkid=14202")]
FileWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext)51         protected FileWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext)
52         {
53             throw new PlatformNotSupportedException();
54         }
55 
ISerializable.GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)56         void ISerializable.GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
57         {
58             throw new PlatformNotSupportedException();
59         }
60 
GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)61         protected override void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
62         {
63             throw new PlatformNotSupportedException();
64         }
65 
66         public override long ContentLength
67         {
68             get
69             {
70                 CheckDisposed();
71                 return _contentLength;
72             }
73         }
74 
75         public override string ContentType
76         {
77             get
78             {
79                 CheckDisposed();
80                 return DefaultFileContentType;
81             }
82         }
83 
84         public override WebHeaderCollection Headers
85         {
86             get
87             {
88                 CheckDisposed();
89                 return _headers;
90             }
91         }
92 
93         public override bool SupportsHeaders => true;
94 
95         public override Uri ResponseUri
96         {
97             get
98             {
99                 CheckDisposed();
100                 return _uri;
101             }
102         }
103 
CheckDisposed()104         private void CheckDisposed()
105         {
106             if (_closed)
107             {
108                 throw new ObjectDisposedException(this.GetType().FullName);
109             }
110         }
111 
Close()112         public override void Close()
113         {
114             if (!_closed)
115             {
116                 _closed = true;
117 
118                 Stream chkStream = _stream;
119                 if (chkStream != null)
120                 {
121                     chkStream.Close();
122                     _stream = null;
123                 }
124             }
125         }
126 
GetResponseStream()127         public override Stream GetResponseStream()
128         {
129             CheckDisposed();
130             return _stream;
131         }
132     }
133 }
134