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.Threading;
7 using System.Threading.Tasks;
8 
9 namespace System.Net
10 {
11     internal partial class HttpRequestStream : Stream
12     {
13         public override bool CanSeek => false;
14         public override bool CanWrite => false;
15         public override bool CanRead => true;
16 
Read(byte[] buffer, int offset, int size)17         public override int Read(byte[] buffer, int offset, int size)
18         {
19             if (NetEventSource.IsEnabled)
20             {
21                 NetEventSource.Enter(this);
22                 NetEventSource.Info(this, "size:" + size + " offset:" + offset);
23             }
24             if (buffer == null)
25             {
26                 throw new ArgumentNullException(nameof(buffer));
27             }
28             if (offset < 0 || offset > buffer.Length)
29             {
30                 throw new ArgumentOutOfRangeException(nameof(offset));
31             }
32             if (size < 0 || size > buffer.Length - offset)
33             {
34                 throw new ArgumentOutOfRangeException(nameof(size));
35             }
36             if (size == 0 || _closed)
37             {
38                 if (NetEventSource.IsEnabled)
39                     NetEventSource.Exit(this, "dataRead:0");
40                 return 0;
41             }
42 
43             return ReadCore(buffer, offset, size);
44         }
45 
BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)46         public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
47         {
48             if (NetEventSource.IsEnabled)
49             {
50                 NetEventSource.Enter(this);
51                 NetEventSource.Info(this, "buffer.Length:" + buffer.Length + " size:" + size + " offset:" + offset);
52             }
53             if (buffer == null)
54             {
55                 throw new ArgumentNullException(nameof(buffer));
56             }
57             if (offset < 0 || offset > buffer.Length)
58             {
59                 throw new ArgumentOutOfRangeException(nameof(offset));
60             }
61             if (size < 0 || size > buffer.Length - offset)
62             {
63                 throw new ArgumentOutOfRangeException(nameof(size));
64             }
65 
66             return BeginReadCore(buffer, offset, size, callback, state);
67         }
68 
Flush()69         public override void Flush() { }
70         public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
71 
72         public override long Length => throw new NotSupportedException(SR.net_noseek);
73 
74         public override long Position
75         {
76             get => throw new NotSupportedException(SR.net_noseek);
77             set => throw new NotSupportedException(SR.net_noseek);
78         }
79 
Seek(long offset, SeekOrigin origin)80         public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(SR.net_noseek);
81 
SetLength(long value)82         public override void SetLength(long value) => throw new NotSupportedException(SR.net_noseek);
83 
Write(byte[] buffer, int offset, int size)84         public override void Write(byte[] buffer, int offset, int size) => throw new InvalidOperationException(SR.net_readonlystream);
85 
BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)86         public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
87         {
88             throw new InvalidOperationException(SR.net_readonlystream);
89         }
90 
91         public override void EndWrite(IAsyncResult asyncResult) => throw new InvalidOperationException(SR.net_readonlystream);
92 
93         internal bool Closed => _closed;
94 
Dispose(bool disposing)95         protected override void Dispose(bool disposing)
96         {
97             if (NetEventSource.IsEnabled)
98             {
99                 NetEventSource.Enter(this);
100                 NetEventSource.Info(this, "_closed:" + _closed);
101             }
102 
103             _closed = true;
104             base.Dispose(disposing);
105 
106             if (NetEventSource.IsEnabled)
107                 NetEventSource.Exit(this);
108         }
109     }
110 }
111