jellyfin/Jellyfin.Server/SocketSharp/RequestMono.cs

875 lines
27 KiB
C#
Raw Normal View History

using System;
2016-11-11 01:58:20 +00:00
using System.Collections.Generic;
2014-07-19 01:28:40 +00:00
using System.Globalization;
using System.IO;
2016-11-08 18:44:23 +00:00
using System.Net;
2014-07-19 01:28:40 +00:00
using System.Text;
2016-07-21 18:40:49 +00:00
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
2014-07-19 01:28:40 +00:00
2019-01-01 15:27:11 +00:00
namespace Jellyfin.SocketSharp
2014-07-19 01:28:40 +00:00
{
public partial class WebSocketSharpRequest : IHttpRequest
{
static internal string GetParameter(string header, string attr)
{
int ap = header.IndexOf(attr);
if (ap == -1)
{
2014-07-19 01:28:40 +00:00
return null;
}
2014-07-19 01:28:40 +00:00
ap += attr.Length;
if (ap >= header.Length)
{
2014-07-19 01:28:40 +00:00
return null;
}
2014-07-19 01:28:40 +00:00
char ending = header[ap];
if (ending != '"')
{
2014-07-19 01:28:40 +00:00
ending = ' ';
}
2014-07-19 01:28:40 +00:00
int end = header.IndexOf(ending, ap + 1);
if (end == -1)
{
2016-03-27 21:11:27 +00:00
return ending == '"' ? null : header.Substring(ap);
}
2014-07-19 01:28:40 +00:00
return header.Substring(ap + 1, end - ap - 1);
}
2018-09-12 17:26:21 +00:00
async Task LoadMultiPart(WebROCollection form)
2014-07-19 01:28:40 +00:00
{
string boundary = GetParameter(ContentType, "; boundary=");
if (boundary == null)
{
2014-07-19 01:28:40 +00:00
return;
}
2014-07-19 01:28:40 +00:00
2018-09-12 17:26:21 +00:00
using (var requestStream = InputStream)
2016-07-21 18:40:49 +00:00
{
//DB: 30/01/11 - Hack to get around non-seekable stream and received HTTP request
//Not ending with \r\n?
2018-09-12 17:26:21 +00:00
var ms = new MemoryStream(32 * 1024);
2016-07-21 18:40:49 +00:00
await requestStream.CopyToAsync(ms).ConfigureAwait(false);
2014-07-19 01:28:40 +00:00
2016-07-21 18:40:49 +00:00
var input = ms;
ms.WriteByte((byte)'\r');
ms.WriteByte((byte)'\n');
2014-07-19 01:28:40 +00:00
2016-07-21 18:40:49 +00:00
input.Position = 0;
2014-07-19 01:28:40 +00:00
2019-01-01 15:27:11 +00:00
// Uncomment to debug
2016-07-21 18:40:49 +00:00
//var content = new StreamReader(ms).ReadToEnd();
//Console.WriteLine(boundary + "::" + content);
//input.Position = 0;
2014-07-19 01:28:40 +00:00
2016-07-21 18:40:49 +00:00
var multi_part = new HttpMultipart(input, boundary, ContentEncoding);
2014-07-19 01:28:40 +00:00
2016-07-21 18:40:49 +00:00
HttpMultipart.Element e;
while ((e = multi_part.ReadNextElement()) != null)
2014-07-19 01:28:40 +00:00
{
2016-07-21 18:40:49 +00:00
if (e.Filename == null)
{
byte[] copy = new byte[e.Length];
2014-07-19 01:28:40 +00:00
2016-07-21 18:40:49 +00:00
input.Position = e.Start;
input.Read(copy, 0, (int)e.Length);
2014-07-19 01:28:40 +00:00
2016-11-11 01:58:20 +00:00
form.Add(e.Name, (e.Encoding ?? ContentEncoding).GetString(copy, 0, copy.Length));
2016-07-21 18:40:49 +00:00
}
else
{
//
// We use a substream, as in 2.x we will support large uploads streamed to disk,
//
2019-01-13 20:37:13 +00:00
var sub = new HttpPostedFile(e.Filename, e.ContentType, input, e.Start, e.Length);
2016-11-11 01:58:20 +00:00
files[e.Name] = sub;
2016-07-21 18:40:49 +00:00
}
2014-07-19 01:28:40 +00:00
}
}
}
2018-09-12 17:26:21 +00:00
public async Task<QueryParamCollection> GetFormData()
2014-07-19 01:28:40 +00:00
{
2018-09-12 17:26:21 +00:00
var form = new WebROCollection();
files = new Dictionary<string, HttpPostedFile>();
2014-07-19 01:28:40 +00:00
2018-09-12 17:26:21 +00:00
if (IsContentType("multipart/form-data", true))
{
await LoadMultiPart(form).ConfigureAwait(false);
}
else if (IsContentType("application/x-www-form-urlencoded", true))
{
await LoadWwwForm(form).ConfigureAwait(false);
}
2014-07-19 01:28:40 +00:00
#if NET_4_0
2019-01-07 23:24:34 +00:00
if (validateRequestNewMode && !checked_form) {
// Setting this before calling the validator prevents
// possible endless recursion
checked_form = true;
ValidateNameValueCollection ("Form", query_string_nvc, RequestValidationSource.Form);
} else
2014-07-19 01:28:40 +00:00
#endif
2018-09-12 17:26:21 +00:00
if (validate_form && !checked_form)
{
checked_form = true;
ValidateNameValueCollection("Form", form);
2014-07-19 01:28:40 +00:00
}
2018-09-12 17:26:21 +00:00
return form;
2014-07-19 01:28:40 +00:00
}
public string Accept => string.IsNullOrEmpty(request.Headers["Accept"]) ? null : request.Headers["Accept"];
2016-06-19 16:53:43 +00:00
public string Authorization => string.IsNullOrEmpty(request.Headers["Authorization"]) ? null : request.Headers["Authorization"];
2014-07-19 01:28:40 +00:00
protected bool validate_cookies, validate_query_string, validate_form;
protected bool checked_cookies, checked_query_string, checked_form;
static void ThrowValidationException(string name, string key, string value)
{
string v = "\"" + value + "\"";
if (v.Length > 20)
{
2014-07-19 01:28:40 +00:00
v = v.Substring(0, 16) + "...\"";
}
2014-07-19 01:28:40 +00:00
string msg = string.Format("A potentially dangerous Request.{0} value was " +
2014-07-19 01:28:40 +00:00
"detected from the client ({1}={2}).", name, key, v);
2016-11-08 18:44:23 +00:00
throw new Exception(msg);
2014-07-19 01:28:40 +00:00
}
2016-10-25 19:02:04 +00:00
static void ValidateNameValueCollection(string name, QueryParamCollection coll)
2014-07-19 01:28:40 +00:00
{
if (coll == null)
{
2014-07-19 01:28:40 +00:00
return;
}
2014-07-19 01:28:40 +00:00
2016-10-25 19:02:04 +00:00
foreach (var pair in coll)
2014-07-19 01:28:40 +00:00
{
2016-10-25 19:02:04 +00:00
var key = pair.Name;
var val = pair.Value;
2014-07-19 01:28:40 +00:00
if (val != null && val.Length > 0 && IsInvalidString(val))
{
2014-07-19 01:28:40 +00:00
ThrowValidationException(name, key, val);
}
2014-07-19 01:28:40 +00:00
}
}
internal static bool IsInvalidString(string val)
=> IsInvalidString(val, out var validationFailureIndex);
2014-07-19 01:28:40 +00:00
internal static bool IsInvalidString(string val, out int validationFailureIndex)
{
validationFailureIndex = 0;
int len = val.Length;
if (len < 2)
{
2014-07-19 01:28:40 +00:00
return false;
}
2014-07-19 01:28:40 +00:00
char current = val[0];
for (int idx = 1; idx < len; idx++)
{
char next = val[idx];
// See http://secunia.com/advisories/14325
if (current == '<' || current == '\xff1c')
{
if (next == '!' || next < ' '
|| (next >= 'a' && next <= 'z')
|| (next >= 'A' && next <= 'Z'))
{
validationFailureIndex = idx - 1;
return true;
}
}
else if (current == '&' && next == '#')
{
validationFailureIndex = idx - 1;
return true;
}
current = next;
}
return false;
}
public void ValidateInput()
{
validate_cookies = true;
validate_query_string = true;
validate_form = true;
}
bool IsContentType(string ct, bool starts_with)
{
if (ct == null || ContentType == null)
{
return false;
}
2014-07-19 01:28:40 +00:00
if (starts_with)
{
2014-07-19 01:28:40 +00:00
return StrUtils.StartsWith(ContentType, ct, true);
}
2014-07-19 01:28:40 +00:00
2016-11-11 01:58:20 +00:00
return string.Equals(ContentType, ct, StringComparison.OrdinalIgnoreCase);
2014-07-19 01:28:40 +00:00
}
2018-09-12 17:26:21 +00:00
async Task LoadWwwForm(WebROCollection form)
2014-07-19 01:28:40 +00:00
{
2019-01-13 20:37:13 +00:00
using (var input = InputStream)
2014-07-19 01:28:40 +00:00
{
2018-09-12 17:26:21 +00:00
using (var ms = new MemoryStream())
2014-07-19 01:28:40 +00:00
{
2016-07-21 18:40:49 +00:00
await input.CopyToAsync(ms).ConfigureAwait(false);
ms.Position = 0;
2014-07-19 01:28:40 +00:00
2019-01-13 20:37:13 +00:00
using (var s = new StreamReader(ms, ContentEncoding))
2014-07-19 01:28:40 +00:00
{
2019-01-13 20:37:13 +00:00
var key = new StringBuilder();
var value = new StringBuilder();
2016-07-21 18:40:49 +00:00
int c;
while ((c = s.Read()) != -1)
2014-07-19 01:28:40 +00:00
{
2016-07-21 18:40:49 +00:00
if (c == '=')
2014-07-19 01:28:40 +00:00
{
2016-07-21 18:40:49 +00:00
value.Length = 0;
while ((c = s.Read()) != -1)
{
if (c == '&')
{
2018-09-12 17:26:21 +00:00
AddRawKeyValue(form, key, value);
2016-07-21 18:40:49 +00:00
break;
}
else
{
2016-07-21 18:40:49 +00:00
value.Append((char)c);
}
2016-07-21 18:40:49 +00:00
}
if (c == -1)
2014-07-19 01:28:40 +00:00
{
2018-09-12 17:26:21 +00:00
AddRawKeyValue(form, key, value);
2016-07-21 18:40:49 +00:00
return;
2014-07-19 01:28:40 +00:00
}
}
2016-07-21 18:40:49 +00:00
else if (c == '&')
{
2018-09-12 17:26:21 +00:00
AddRawKeyValue(form, key, value);
}
2016-07-21 18:40:49 +00:00
else
{
2016-07-21 18:40:49 +00:00
key.Append((char)c);
}
2014-07-19 01:28:40 +00:00
}
2016-07-21 18:40:49 +00:00
if (c == -1)
{
2018-09-12 17:26:21 +00:00
AddRawKeyValue(form, key, value);
}
2014-07-19 01:28:40 +00:00
}
}
}
}
static void AddRawKeyValue(WebROCollection form, StringBuilder key, StringBuilder value)
2014-07-19 01:28:40 +00:00
{
form.Add(WebUtility.UrlDecode(key.ToString()), WebUtility.UrlDecode(value.ToString()));
2014-07-19 01:28:40 +00:00
key.Length = 0;
value.Length = 0;
}
2016-11-11 01:58:20 +00:00
Dictionary<string, HttpPostedFile> files;
2014-07-19 01:28:40 +00:00
2016-10-25 19:02:04 +00:00
class WebROCollection : QueryParamCollection
2014-07-19 01:28:40 +00:00
{
public override string ToString()
{
2019-01-13 20:37:13 +00:00
var result = new StringBuilder();
2016-10-25 19:02:04 +00:00
foreach (var pair in this)
2014-07-19 01:28:40 +00:00
{
if (result.Length > 0)
{
2014-07-19 01:28:40 +00:00
result.Append('&');
}
2014-07-19 01:28:40 +00:00
2016-10-25 19:02:04 +00:00
var key = pair.Name;
2014-07-19 01:28:40 +00:00
if (key != null && key.Length > 0)
{
result.Append(key);
result.Append('=');
}
2016-10-25 19:02:04 +00:00
result.Append(pair.Value);
2014-07-19 01:28:40 +00:00
}
return result.ToString();
}
}
public sealed class HttpPostedFile
{
string name;
string content_type;
Stream stream;
class ReadSubStream : Stream
{
Stream s;
long offset;
long end;
long position;
public ReadSubStream(Stream s, long offset, long length)
{
this.s = s;
this.offset = offset;
this.end = offset + length;
position = offset;
}
public override void Flush()
{
}
public override int Read(byte[] buffer, int dest_offset, int count)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
2014-07-19 01:28:40 +00:00
if (dest_offset < 0)
{
throw new ArgumentOutOfRangeException(nameof(dest_offset), "< 0");
}
2014-07-19 01:28:40 +00:00
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count), "< 0");
}
2014-07-19 01:28:40 +00:00
int len = buffer.Length;
if (dest_offset > len)
{
2014-07-19 01:28:40 +00:00
throw new ArgumentException("destination offset is beyond array size");
}
2014-07-19 01:28:40 +00:00
// reordered to avoid possible integer overflow
if (dest_offset > len - count)
{
2014-07-19 01:28:40 +00:00
throw new ArgumentException("Reading would overrun buffer");
}
2014-07-19 01:28:40 +00:00
if (count > end - position)
{
2014-07-19 01:28:40 +00:00
count = (int)(end - position);
}
2014-07-19 01:28:40 +00:00
if (count <= 0)
{
2014-07-19 01:28:40 +00:00
return 0;
}
2014-07-19 01:28:40 +00:00
s.Position = position;
int result = s.Read(buffer, dest_offset, count);
if (result > 0)
{
2014-07-19 01:28:40 +00:00
position += result;
}
2014-07-19 01:28:40 +00:00
else
{
2014-07-19 01:28:40 +00:00
position = end;
}
2014-07-19 01:28:40 +00:00
return result;
}
public override int ReadByte()
{
if (position >= end)
{
2014-07-19 01:28:40 +00:00
return -1;
}
2014-07-19 01:28:40 +00:00
s.Position = position;
int result = s.ReadByte();
if (result < 0)
{
2014-07-19 01:28:40 +00:00
position = end;
}
2014-07-19 01:28:40 +00:00
else
{
2014-07-19 01:28:40 +00:00
position++;
}
2014-07-19 01:28:40 +00:00
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();
}
long virt = real - offset;
if (virt < 0 || virt > Length)
{
2014-07-19 01:28:40 +00:00
throw new ArgumentException();
}
2014-07-19 01:28:40 +00:00
position = s.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();
}
public override bool CanRead => true;
2014-07-19 01:28:40 +00:00
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length => end - offset;
2014-07-19 01:28:40 +00:00
public override long Position
{
get => position - offset;
2014-07-19 01:28:40 +00:00
set
{
if (value > Length)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
2014-07-19 01:28:40 +00:00
position = Seek(value, SeekOrigin.Begin);
}
}
}
internal HttpPostedFile(string name, string content_type, Stream base_stream, long offset, long length)
{
this.name = name;
this.content_type = content_type;
this.stream = new ReadSubStream(base_stream, offset, length);
}
public string ContentType => content_type;
2014-07-19 01:28:40 +00:00
public int ContentLength => (int)stream.Length;
2014-07-19 01:28:40 +00:00
public string FileName => name;
2014-07-19 01:28:40 +00:00
public Stream InputStream => stream;
2014-07-19 01:28:40 +00:00
}
class Helpers
{
public static readonly CultureInfo InvariantCulture = CultureInfo.InvariantCulture;
}
internal static class StrUtils
2014-07-19 01:28:40 +00:00
{
public static bool StartsWith(string str1, string str2, bool ignore_case)
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(str1))
2016-11-11 01:58:20 +00:00
{
2014-07-19 01:28:40 +00:00
return false;
2016-11-11 01:58:20 +00:00
}
2014-07-19 01:28:40 +00:00
2016-11-11 01:58:20 +00:00
var comparison = ignore_case ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
return str1.IndexOf(str2, comparison) == 0;
2014-07-19 01:28:40 +00:00
}
public static bool EndsWith(string str1, string str2, bool ignore_case)
{
int l2 = str2.Length;
if (l2 == 0)
{
2014-07-19 01:28:40 +00:00
return true;
}
2014-07-19 01:28:40 +00:00
int l1 = str1.Length;
if (l2 > l1)
{
2014-07-19 01:28:40 +00:00
return false;
}
2014-07-19 01:28:40 +00:00
2016-11-11 01:58:20 +00:00
var comparison = ignore_case ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
return str1.IndexOf(str2, comparison) == str1.Length - str2.Length - 1;
2014-07-19 01:28:40 +00:00
}
}
class HttpMultipart
{
public class Element
{
public string ContentType;
public string Name;
public string Filename;
public Encoding Encoding;
public long Start;
public long Length;
public override string ToString()
{
return "ContentType " + ContentType + ", Name " + Name + ", Filename " + Filename + ", Start " +
Start.ToString() + ", Length " + Length.ToString();
}
}
Stream data;
string boundary;
byte[] boundary_bytes;
byte[] buffer;
bool at_eof;
Encoding encoding;
StringBuilder sb;
const byte LF = (byte)'\n', CR = (byte)'\r';
2014-07-19 01:28:40 +00:00
2019-01-07 23:24:34 +00:00
// See RFC 2046
2014-07-19 01:28:40 +00:00
// In the case of multipart entities, in which one or more different
// sets of data are combined in a single body, a "multipart" media type
// field must appear in the entity's header. The body must then contain
// one or more body parts, each preceded by a boundary delimiter line,
// and the last one followed by a closing boundary delimiter line.
// After its boundary delimiter line, each body part then consists of a
// header area, a blank line, and a body area. Thus a body part is
// similar to an RFC 822 message in syntax, but different in meaning.
public HttpMultipart(Stream data, string b, Encoding encoding)
{
this.data = data;
//DB: 30/01/11: cannot set or read the Position in HttpListener in Win.NET
//var ms = new MemoryStream(32 * 1024);
//data.CopyTo(ms);
//this.data = ms;
boundary = b;
boundary_bytes = encoding.GetBytes(b);
buffer = new byte[boundary_bytes.Length + 2]; // CRLF or '--'
this.encoding = encoding;
sb = new StringBuilder();
}
private string ReadLine()
2014-07-19 01:28:40 +00:00
{
// CRLF or LF are ok as line endings.
bool got_cr = false;
int b = 0;
sb.Length = 0;
while (true)
{
b = data.ReadByte();
if (b == -1)
{
return null;
}
if (b == LF)
{
break;
}
2016-03-27 21:11:27 +00:00
got_cr = b == CR;
2014-07-19 01:28:40 +00:00
sb.Append((char)b);
}
if (got_cr)
{
2014-07-19 01:28:40 +00:00
sb.Length--;
}
2014-07-19 01:28:40 +00:00
return sb.ToString();
}
private static string GetContentDispositionAttribute(string l, string name)
2014-07-19 01:28:40 +00:00
{
int idx = l.IndexOf(name + "=\"");
if (idx < 0)
{
2014-07-19 01:28:40 +00:00
return null;
}
2014-07-19 01:28:40 +00:00
int begin = idx + name.Length + "=\"".Length;
int end = l.IndexOf('"', begin);
if (end < 0)
{
2014-07-19 01:28:40 +00:00
return null;
}
2014-07-19 01:28:40 +00:00
if (begin == end)
{
return string.Empty;
}
2014-07-19 01:28:40 +00:00
return l.Substring(begin, end - begin);
}
private string GetContentDispositionAttributeWithEncoding(string l, string name)
2014-07-19 01:28:40 +00:00
{
int idx = l.IndexOf(name + "=\"");
if (idx < 0)
{
2014-07-19 01:28:40 +00:00
return null;
}
2014-07-19 01:28:40 +00:00
int begin = idx + name.Length + "=\"".Length;
int end = l.IndexOf('"', begin);
if (end < 0)
{
2014-07-19 01:28:40 +00:00
return null;
}
2014-07-19 01:28:40 +00:00
if (begin == end)
{
return string.Empty;
}
2014-07-19 01:28:40 +00:00
string temp = l.Substring(begin, end - begin);
byte[] source = new byte[temp.Length];
for (int i = temp.Length - 1; i >= 0; i--)
{
2014-07-19 01:28:40 +00:00
source[i] = (byte)temp[i];
}
2014-07-19 01:28:40 +00:00
2016-11-11 01:58:20 +00:00
return encoding.GetString(source, 0, source.Length);
2014-07-19 01:28:40 +00:00
}
private bool ReadBoundary()
2014-07-19 01:28:40 +00:00
{
try
{
string line = ReadLine();
while (line == string.Empty)
{
2014-07-19 01:28:40 +00:00
line = ReadLine();
}
2014-07-19 01:28:40 +00:00
if (line[0] != '-' || line[1] != '-')
{
2014-07-19 01:28:40 +00:00
return false;
}
2014-07-19 01:28:40 +00:00
if (!StrUtils.EndsWith(line, boundary, false))
{
2014-07-19 01:28:40 +00:00
return true;
}
2014-07-19 01:28:40 +00:00
}
catch
{
}
return false;
}
private string ReadHeaders()
2014-07-19 01:28:40 +00:00
{
string s = ReadLine();
if (s.Length == 0)
{
2014-07-19 01:28:40 +00:00
return null;
}
2014-07-19 01:28:40 +00:00
return s;
}
private static bool CompareBytes(byte[] orig, byte[] other)
2014-07-19 01:28:40 +00:00
{
for (int i = orig.Length - 1; i >= 0; i--)
{
2014-07-19 01:28:40 +00:00
if (orig[i] != other[i])
{
2014-07-19 01:28:40 +00:00
return false;
}
}
2014-07-19 01:28:40 +00:00
return true;
}
private long MoveToNextBoundary()
2014-07-19 01:28:40 +00:00
{
long retval = 0;
bool got_cr = false;
int state = 0;
int c = data.ReadByte();
while (true)
{
if (c == -1)
{
2014-07-19 01:28:40 +00:00
return -1;
}
2014-07-19 01:28:40 +00:00
if (state == 0 && c == LF)
{
retval = data.Position - 1;
if (got_cr)
{
2014-07-19 01:28:40 +00:00
retval--;
}
2014-07-19 01:28:40 +00:00
state = 1;
c = data.ReadByte();
}
else if (state == 0)
{
2016-03-27 21:11:27 +00:00
got_cr = c == CR;
2014-07-19 01:28:40 +00:00
c = data.ReadByte();
}
else if (state == 1 && c == '-')
{
c = data.ReadByte();
if (c == -1)
{
2014-07-19 01:28:40 +00:00
return -1;
}
2014-07-19 01:28:40 +00:00
if (c != '-')
{
state = 0;
got_cr = false;
continue; // no ReadByte() here
}
int nread = data.Read(buffer, 0, buffer.Length);
int bl = buffer.Length;
if (nread != bl)
{
2014-07-19 01:28:40 +00:00
return -1;
}
2014-07-19 01:28:40 +00:00
if (!CompareBytes(boundary_bytes, buffer))
{
state = 0;
data.Position = retval + 2;
if (got_cr)
{
data.Position++;
got_cr = false;
}
2014-07-19 01:28:40 +00:00
c = data.ReadByte();
continue;
}
if (buffer[bl - 2] == '-' && buffer[bl - 1] == '-')
{
at_eof = true;
}
else if (buffer[bl - 2] != CR || buffer[bl - 1] != LF)
{
state = 0;
data.Position = retval + 2;
if (got_cr)
{
data.Position++;
got_cr = false;
}
2014-07-19 01:28:40 +00:00
c = data.ReadByte();
continue;
}
data.Position = retval + 2;
if (got_cr)
{
2014-07-19 01:28:40 +00:00
data.Position++;
}
2014-07-19 01:28:40 +00:00
break;
}
else
{
// state == 1
state = 0; // no ReadByte() here
}
}
return retval;
}
public Element ReadNextElement()
{
if (at_eof || ReadBoundary())
{
2014-07-19 01:28:40 +00:00
return null;
}
2014-07-19 01:28:40 +00:00
2019-01-13 20:37:13 +00:00
var elem = new Element();
2014-07-19 01:28:40 +00:00
string header;
while ((header = ReadHeaders()) != null)
{
if (StrUtils.StartsWith(header, "Content-Disposition:", true))
{
elem.Name = GetContentDispositionAttribute(header, "name");
elem.Filename = StripPath(GetContentDispositionAttributeWithEncoding(header, "filename"));
}
else if (StrUtils.StartsWith(header, "Content-Type:", true))
{
elem.ContentType = header.Substring("Content-Type:".Length).Trim();
elem.Encoding = GetEncoding(elem.ContentType);
}
}
long start = 0;
start = data.Position;
elem.Start = start;
long pos = MoveToNextBoundary();
if (pos == -1)
{
2014-07-19 01:28:40 +00:00
return null;
}
2014-07-19 01:28:40 +00:00
elem.Length = pos - start;
return elem;
}
private static string StripPath(string path)
2014-07-19 01:28:40 +00:00
{
if (path == null || path.Length == 0)
{
2014-07-19 01:28:40 +00:00
return path;
}
2014-07-19 01:28:40 +00:00
if (path.IndexOf(":\\", StringComparison.Ordinal) != 1
&& !path.StartsWith("\\\\", StringComparison.Ordinal))
{
2014-07-19 01:28:40 +00:00
return path;
}
2014-07-19 01:28:40 +00:00
return path.Substring(path.LastIndexOf('\\') + 1);
}
}
}
}