aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/SocketSharp/HttpPostedFile.cs
blob: 7479d81045cd4a7afd89b4eb93f1d87381c9e5dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.IO;

public sealed class HttpPostedFile : IDisposable
{
    private string _name;
    private string _contentType;
    private Stream _stream;
    private bool _disposed = false;

    internal HttpPostedFile(string name, string content_type, Stream base_stream, long offset, long length)
    {
        _name = name;
        _contentType = content_type;
        _stream = new ReadSubStream(base_stream, offset, length);
    }

    public string ContentType => _contentType;

    public int ContentLength => (int)_stream.Length;

    public string FileName => _name;

    public Stream InputStream => _stream;

    /// <summary>
    /// Releases the unmanaged resources and disposes of the managed resources used.
    /// </summary>
    public void Dispose()
    {
        if (_disposed)
        {
            return;
        }

        _stream.Dispose();
        _stream = null;

        _name = null;
        _contentType = null;

        _disposed = true;
    }

    private class ReadSubStream : Stream
    {
        private Stream _stream;
        private long _offset;
        private long _end;
        private long _position;

        public ReadSubStream(Stream s, long offset, long length)
        {
            _stream = s;
            _offset = offset;
            _end = offset + length;
            _position = offset;
        }

        public override bool CanRead => true;

        public override bool CanSeek => true;

        public override bool CanWrite => false;

        public override long Length => _end - _offset;

        public override long Position
        {
            get => _position - _offset;
            set
            {
                if (value > Length)
                {
                    throw new ArgumentOutOfRangeException(nameof(value));
                }

                _position = Seek(value, SeekOrigin.Begin);
            }
        }

        public override void Flush()
        {
        }

        public override int Read(byte[] buffer, int dest_offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            if (dest_offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(dest_offset), "< 0");
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), "< 0");
            }

            int len = buffer.Length;
            if (dest_offset > len)
            {
                throw new ArgumentException("destination offset is beyond array size", nameof(dest_offset));
            }

            // reordered to avoid possible integer overflow
            if (dest_offset > len - count)
            {
                throw new ArgumentException("Reading would overrun buffer", nameof(count));
            }

            if (count > _end - _position)
            {
                count = (int)(_end - _position);
            }

            if (count <= 0)
            {
                return 0;
            }

            _stream.Position = _position;
            int result = _stream.Read(buffer, dest_offset, count);
            if (result > 0)
            {
                _position += result;
            }
            else
            {
                _position = _end;
            }

            return result;
        }

        public override int ReadByte()
        {
            if (_position >= _end)
            {
                return -1;
            }

            _stream.Position = _position;
            int result = _stream.ReadByte();
            if (result < 0)
            {
                _position = _end;
            }
            else
            {
                _position++;
            }

            return result;
        }

        public override long Seek(long d, SeekOrigin origin)
        {
            long real;
            switch (origin)
            {
                case SeekOrigin.Begin:
                    real = _offset + d;
                    break;
                case SeekOrigin.End:
                    real = _end + d;
                    break;
                case SeekOrigin.Current:
                    real = _position + d;
                    break;
                default:
                    throw new ArgumentException("Unknown SeekOrigin value", nameof(origin));
            }

            long virt = real - _offset;
            if (virt < 0 || virt > Length)
            {
                throw new ArgumentException("Invalid position", nameof(d));
            }

            _position = _stream.Seek(real, SeekOrigin.Begin);
            return _position;
        }

        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }
    }
}