1 //------------------------------------------------------------------------------
2 // <copyright file="filewebresponse.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
8 using System.Runtime.Serialization;
10 using System.Globalization;
11 using System.Security.Permissions;
12 using System.Diagnostics.CodeAnalysis;
15 public class FileWebResponse : WebResponse, ISerializable, ICloseEx
17 const int DefaultFileStreamBufferSize = 8192;
18 const string DefaultFileContentType = "application/octet-stream";
24 FileAccess m_fileAccess;
25 WebHeaderCollection m_headers;
31 internal FileWebResponse(FileWebRequest request, Uri uri, FileAccess access, bool asyncHint) {
32 GlobalLog.Enter("FileWebResponse::FileWebResponse", "uri="+uri+", access="+access+", asyncHint="+asyncHint);
34 m_fileAccess = access;
35 if (access == FileAccess.Write) {
36 m_stream = Stream.Null;
40 // apparently, specifying async when the stream will be read
41 // synchronously, or vice versa, can lead to a 10x perf hit.
42 // While we don't know how the app will read the stream, we
43 // use the hint from whether the app called BeginGetResponse
44 // or GetResponse to supply the async flag to the stream ctor
47 m_stream = new FileWebStream(request,
52 DefaultFileStreamBufferSize,
55 m_contentLength = m_stream.Length;
57 m_headers = new WebHeaderCollection(WebHeaderCollectionType.FileWebResponse);
58 m_headers.AddInternal(HttpKnownHeaderNames.ContentLength, m_contentLength.ToString(NumberFormatInfo.InvariantInfo));
59 m_headers.AddInternal(HttpKnownHeaderNames.ContentType, DefaultFileContentType);
61 } catch (Exception e) {
62 Exception ex = new WebException(e.Message, e, WebExceptionStatus.ConnectFailure, null);
63 GlobalLog.LeaveException("FileWebResponse::FileWebResponse", ex);
66 GlobalLog.Leave("FileWebResponse::FileWebResponse");
70 // ISerializable constructor
72 [Obsolete("Serialization is obsoleted for this type. http://go.microsoft.com/fwlink/?linkid=14202")]
73 protected FileWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext):base(serializationInfo, streamingContext) {
74 m_headers = (WebHeaderCollection)serializationInfo.GetValue("headers", typeof(WebHeaderCollection));
75 m_uri = (Uri)serializationInfo.GetValue("uri", typeof(Uri));
76 m_contentLength = serializationInfo.GetInt64("contentLength");
77 m_fileAccess = (FileAccess )serializationInfo.GetInt32("fileAccess");
81 // ISerializable method
84 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Justification = "System.dll is still using pre-v4 security model and needs this demand")]
85 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter, SerializationFormatter=true)]
86 void ISerializable.GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
88 GetObjectData(serializationInfo, streamingContext);
92 // FxCop: provide some way for derived classes to access GetObjectData even if the derived class
93 // explicitly re-inherits ISerializable.
95 [SecurityPermission(SecurityAction.Demand, SerializationFormatter=true)]
96 protected override void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
98 serializationInfo.AddValue("headers", m_headers, typeof(WebHeaderCollection));
99 serializationInfo.AddValue("uri", m_uri, typeof(Uri));
100 serializationInfo.AddValue("contentLength", m_contentLength);
101 serializationInfo.AddValue("fileAccess", m_fileAccess);
102 base.GetObjectData(serializationInfo, streamingContext);
107 public override long ContentLength {
110 return m_contentLength;
114 public override string ContentType {
117 return DefaultFileContentType;
121 public override WebHeaderCollection Headers {
128 // For portability only
129 public override bool SupportsHeaders {
135 public override Uri ResponseUri {
144 private void CheckDisposed() {
146 throw new ObjectDisposedException(this.GetType().FullName);
150 public override void Close() {
151 ((ICloseEx)this).CloseEx(CloseExState.Normal);
154 void ICloseEx.CloseEx(CloseExState closeState) {
155 GlobalLog.Enter("FileWebResponse::Close()");
160 Stream chkStream = m_stream;
161 if (chkStream!=null) {
162 if (chkStream is ICloseEx)
163 ((ICloseEx)chkStream).CloseEx(closeState);
171 GlobalLog.Leave("FileWebResponse::Close()");
175 public override Stream GetResponseStream() {
176 GlobalLog.Enter("FileWebResponse::GetResponseStream()");
181 GlobalLog.Leave("FileWebResponse::GetResponseStream()");