Reorder elements

pull/885/head
Bond-009 6 years ago
parent e620bb9512
commit cb9e50b2ea

@ -185,6 +185,7 @@ namespace Jellyfin.Server.SocketSharp
for (int idx = 1; idx < len; idx++) for (int idx = 1; idx < len; idx++)
{ {
char next = val[idx]; char next = val[idx];
// See http://secunia.com/advisories/14325 // See http://secunia.com/advisories/14325
if (current == '<' || current == '\xff1c') if (current == '<' || current == '\xff1c')
{ {
@ -556,15 +557,23 @@ namespace Jellyfin.Server.SocketSharp
} }
} }
private const byte LF = (byte)'\n';
private const byte CR = (byte)'\r';
private Stream data; private Stream data;
private string boundary; private string boundary;
private byte[] boundary_bytes;
private byte[] boundaryBytes;
private byte[] buffer; private byte[] buffer;
private bool at_eof;
private bool atEof;
private Encoding encoding; private Encoding encoding;
private StringBuilder sb;
private const byte LF = (byte)'\n', CR = (byte)'\r'; private StringBuilder sb;
// See RFC 2046 // See RFC 2046
// In the case of multipart entities, in which one or more different // In the case of multipart entities, in which one or more different
@ -580,12 +589,48 @@ namespace Jellyfin.Server.SocketSharp
{ {
this.data = data; this.data = data;
boundary = b; boundary = b;
boundary_bytes = encoding.GetBytes(b); boundaryBytes = encoding.GetBytes(b);
buffer = new byte[boundary_bytes.Length + 2]; // CRLF or '--' buffer = new byte[boundaryBytes.Length + 2]; // CRLF or '--'
this.encoding = encoding; this.encoding = encoding;
sb = new StringBuilder(); sb = new StringBuilder();
} }
public Element ReadNextElement()
{
if (atEof || ReadBoundary())
{
return null;
}
var elem = new Element();
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)
{
return null;
}
elem.Length = pos - start;
return elem;
}
private string ReadLine() private string ReadLine()
{ {
// CRLF or LF are ok as line endings. // CRLF or LF are ok as line endings.
@ -774,7 +819,7 @@ namespace Jellyfin.Server.SocketSharp
return -1; return -1;
} }
if (!CompareBytes(boundary_bytes, buffer)) if (!CompareBytes(boundaryBytes, buffer))
{ {
state = 0; state = 0;
data.Position = retval + 2; data.Position = retval + 2;
@ -790,7 +835,7 @@ namespace Jellyfin.Server.SocketSharp
if (buffer[bl - 2] == '-' && buffer[bl - 1] == '-') if (buffer[bl - 2] == '-' && buffer[bl - 1] == '-')
{ {
at_eof = true; atEof = true;
} }
else if (buffer[bl - 2] != CR || buffer[bl - 1] != LF) else if (buffer[bl - 2] != CR || buffer[bl - 1] != LF)
{ {
@ -824,42 +869,6 @@ namespace Jellyfin.Server.SocketSharp
return retval; return retval;
} }
public Element ReadNextElement()
{
if (at_eof || ReadBoundary())
{
return null;
}
var elem = new Element();
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)
{
return null;
}
elem.Length = pos - start;
return elem;
}
private static string StripPath(string path) private static string StripPath(string path)
{ {
if (path == null || path.Length == 0) if (path == null || path.Length == 0)

@ -18,6 +18,7 @@ namespace Jellyfin.Server.SocketSharp
public class WebSocketSharpResponse : IHttpResponse public class WebSocketSharpResponse : IHttpResponse
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly HttpListenerResponse _response; private readonly HttpListenerResponse _response;
public WebSocketSharpResponse(ILogger logger, HttpListenerResponse response, IRequest request) public WebSocketSharpResponse(ILogger logger, HttpListenerResponse response, IRequest request)
@ -29,7 +30,9 @@ namespace Jellyfin.Server.SocketSharp
} }
public IRequest Request { get; private set; } public IRequest Request { get; private set; }
public Dictionary<string, object> Items { get; private set; } public Dictionary<string, object> Items { get; private set; }
public object OriginalResponse => _response; public object OriginalResponse => _response;
public int StatusCode public int StatusCode
@ -50,7 +53,7 @@ namespace Jellyfin.Server.SocketSharp
set => _response.ContentType = value; set => _response.ContentType = value;
} }
// public ICookies Cookies { get; set; } public QueryParamCollection Headers => _response.Headers;
public void AddHeader(string name, string value) public void AddHeader(string name, string value)
{ {
@ -63,8 +66,6 @@ namespace Jellyfin.Server.SocketSharp
_response.AddHeader(name, value); _response.AddHeader(name, value);
} }
public QueryParamCollection Headers => _response.Headers;
public string GetHeader(string name) public string GetHeader(string name)
{ {
return _response.Headers[name]; return _response.Headers[name];

Loading…
Cancel
Save