Merge pull request #687 from Bond-009/stylecop

Fix some analyzer warnings
pull/702/head
Andrew Rabert 6 years ago committed by GitHub
commit 68a7ed6b7c

@ -224,7 +224,7 @@ namespace Jellyfin.Server
.GetManifestResourceStream("Jellyfin.Server.Resources.Configuration.logging.json")) .GetManifestResourceStream("Jellyfin.Server.Resources.Configuration.logging.json"))
using (Stream fstr = File.Open(configPath, FileMode.CreateNew)) using (Stream fstr = File.Open(configPath, FileMode.CreateNew))
{ {
await rscstr.CopyToAsync(fstr); await rscstr.CopyToAsync(fstr).ConfigureAwait(false);
} }
} }
var configuration = new ConfigurationBuilder() var configuration = new ConfigurationBuilder()
@ -334,11 +334,9 @@ namespace Jellyfin.Server
} }
else else
{ {
commandLineArgsString = string.Join(" ", commandLineArgsString = string.Join(
Environment.GetCommandLineArgs() " ",
.Skip(1) Environment.GetCommandLineArgs().Skip(1).Select(NormalizeCommandLineArgument));
.Select(NormalizeCommandLineArgument)
);
} }
_logger.LogInformation("Executable: {0}", module); _logger.LogInformation("Executable: {0}", module);

@ -15,19 +15,27 @@ namespace Jellyfin.SocketSharp
{ {
int ap = header.IndexOf(attr); int ap = header.IndexOf(attr);
if (ap == -1) if (ap == -1)
{
return null; return null;
}
ap += attr.Length; ap += attr.Length;
if (ap >= header.Length) if (ap >= header.Length)
{
return null; return null;
}
char ending = header[ap]; char ending = header[ap];
if (ending != '"') if (ending != '"')
{
ending = ' '; ending = ' ';
}
int end = header.IndexOf(ending, ap + 1); int end = header.IndexOf(ending, ap + 1);
if (end == -1) if (end == -1)
{
return ending == '"' ? null : header.Substring(ap); return ending == '"' ? null : header.Substring(ap);
}
return header.Substring(ap + 1, end - ap - 1); return header.Substring(ap + 1, end - ap - 1);
} }
@ -36,7 +44,9 @@ namespace Jellyfin.SocketSharp
{ {
string boundary = GetParameter(ContentType, "; boundary="); string boundary = GetParameter(ContentType, "; boundary=");
if (boundary == null) if (boundary == null)
{
return; return;
}
using (var requestStream = InputStream) using (var requestStream = InputStream)
{ {
@ -124,7 +134,9 @@ namespace Jellyfin.SocketSharp
{ {
string v = "\"" + value + "\""; string v = "\"" + value + "\"";
if (v.Length > 20) if (v.Length > 20)
{
v = v.Substring(0, 16) + "...\""; v = v.Substring(0, 16) + "...\"";
}
string msg = string.Format("A potentially dangerous Request.{0} value was " + string msg = string.Format("A potentially dangerous Request.{0} value was " +
"detected from the client ({1}={2}).", name, key, v); "detected from the client ({1}={2}).", name, key, v);
@ -135,21 +147,23 @@ namespace Jellyfin.SocketSharp
static void ValidateNameValueCollection(string name, QueryParamCollection coll) static void ValidateNameValueCollection(string name, QueryParamCollection coll)
{ {
if (coll == null) if (coll == null)
{
return; return;
}
foreach (var pair in coll) foreach (var pair in coll)
{ {
var key = pair.Name; var key = pair.Name;
var val = pair.Value; var val = pair.Value;
if (val != null && val.Length > 0 && IsInvalidString(val)) if (val != null && val.Length > 0 && IsInvalidString(val))
{
ThrowValidationException(name, key, val); ThrowValidationException(name, key, val);
}
} }
} }
internal static bool IsInvalidString(string val) internal static bool IsInvalidString(string val)
{ => IsInvalidString(val, out var validationFailureIndex);
return IsInvalidString(val, out var validationFailureIndex);
}
internal static bool IsInvalidString(string val, out int validationFailureIndex) internal static bool IsInvalidString(string val, out int validationFailureIndex)
{ {
@ -157,7 +171,9 @@ namespace Jellyfin.SocketSharp
int len = val.Length; int len = val.Length;
if (len < 2) if (len < 2)
{
return false; return false;
}
char current = val[0]; char current = val[0];
for (int idx = 1; idx < len; idx++) for (int idx = 1; idx < len; idx++)
@ -195,10 +211,15 @@ namespace Jellyfin.SocketSharp
bool IsContentType(string ct, bool starts_with) bool IsContentType(string ct, bool starts_with)
{ {
if (ct == null || ContentType == null) return false; if (ct == null || ContentType == null)
{
return false;
}
if (starts_with) if (starts_with)
{
return StrUtils.StartsWith(ContentType, ct, true); return StrUtils.StartsWith(ContentType, ct, true);
}
return string.Equals(ContentType, ct, StringComparison.OrdinalIgnoreCase); return string.Equals(ContentType, ct, StringComparison.OrdinalIgnoreCase);
} }
@ -231,7 +252,9 @@ namespace Jellyfin.SocketSharp
break; break;
} }
else else
{
value.Append((char)c); value.Append((char)c);
}
} }
if (c == -1) if (c == -1)
{ {
@ -240,22 +263,26 @@ namespace Jellyfin.SocketSharp
} }
} }
else if (c == '&') else if (c == '&')
{
AddRawKeyValue(form, key, value); AddRawKeyValue(form, key, value);
}
else else
{
key.Append((char)c); key.Append((char)c);
}
} }
if (c == -1) if (c == -1)
{
AddRawKeyValue(form, key, value); AddRawKeyValue(form, key, value);
}
} }
} }
} }
} }
void AddRawKeyValue(WebROCollection form, StringBuilder key, StringBuilder value) static void AddRawKeyValue(WebROCollection form, StringBuilder key, StringBuilder value)
{ {
string decodedKey = WebUtility.UrlDecode(key.ToString()); form.Add(WebUtility.UrlDecode(key.ToString()), WebUtility.UrlDecode(value.ToString()));
form.Add(decodedKey,
WebUtility.UrlDecode(value.ToString()));
key.Length = 0; key.Length = 0;
value.Length = 0; value.Length = 0;
@ -271,7 +298,9 @@ namespace Jellyfin.SocketSharp
foreach (var pair in this) foreach (var pair in this)
{ {
if (result.Length > 0) if (result.Length > 0)
{
result.Append('&'); result.Append('&');
}
var key = pair.Name; var key = pair.Name;
if (key != null && key.Length > 0) if (key != null && key.Length > 0)
@ -314,33 +343,52 @@ namespace Jellyfin.SocketSharp
public override int Read(byte[] buffer, int dest_offset, int count) public override int Read(byte[] buffer, int dest_offset, int count)
{ {
if (buffer == null) if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer)); throw new ArgumentNullException(nameof(buffer));
}
if (dest_offset < 0) if (dest_offset < 0)
{
throw new ArgumentOutOfRangeException(nameof(dest_offset), "< 0"); throw new ArgumentOutOfRangeException(nameof(dest_offset), "< 0");
}
if (count < 0) if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count), "< 0"); throw new ArgumentOutOfRangeException(nameof(count), "< 0");
}
int len = buffer.Length; int len = buffer.Length;
if (dest_offset > len) if (dest_offset > len)
{
throw new ArgumentException("destination offset is beyond array size"); throw new ArgumentException("destination offset is beyond array size");
}
// reordered to avoid possible integer overflow // reordered to avoid possible integer overflow
if (dest_offset > len - count) if (dest_offset > len - count)
{
throw new ArgumentException("Reading would overrun buffer"); throw new ArgumentException("Reading would overrun buffer");
}
if (count > end - position) if (count > end - position)
{
count = (int)(end - position); count = (int)(end - position);
}
if (count <= 0) if (count <= 0)
{
return 0; return 0;
}
s.Position = position; s.Position = position;
int result = s.Read(buffer, dest_offset, count); int result = s.Read(buffer, dest_offset, count);
if (result > 0) if (result > 0)
{
position += result; position += result;
}
else else
{
position = end; position = end;
}
return result; return result;
} }
@ -348,14 +396,20 @@ namespace Jellyfin.SocketSharp
public override int ReadByte() public override int ReadByte()
{ {
if (position >= end) if (position >= end)
{
return -1; return -1;
}
s.Position = position; s.Position = position;
int result = s.ReadByte(); int result = s.ReadByte();
if (result < 0) if (result < 0)
{
position = end; position = end;
}
else else
{
position++; position++;
}
return result; return result;
} }
@ -380,7 +434,9 @@ namespace Jellyfin.SocketSharp
long virt = real - offset; long virt = real - offset;
if (virt < 0 || virt > Length) if (virt < 0 || virt > Length)
{
throw new ArgumentException(); throw new ArgumentException();
}
position = s.Seek(real, SeekOrigin.Begin); position = s.Seek(real, SeekOrigin.Begin);
return position; return position;
@ -410,7 +466,9 @@ namespace Jellyfin.SocketSharp
set set
{ {
if (value > Length) if (value > Length)
throw new ArgumentOutOfRangeException(); {
throw new ArgumentOutOfRangeException(nameof(value));
}
position = Seek(value, SeekOrigin.Begin); position = Seek(value, SeekOrigin.Begin);
} }
@ -438,7 +496,7 @@ namespace Jellyfin.SocketSharp
public static readonly CultureInfo InvariantCulture = CultureInfo.InvariantCulture; public static readonly CultureInfo InvariantCulture = CultureInfo.InvariantCulture;
} }
internal sealed class StrUtils internal static class StrUtils
{ {
public static bool StartsWith(string str1, string str2, bool ignore_case) public static bool StartsWith(string str1, string str2, bool ignore_case)
{ {
@ -455,11 +513,15 @@ namespace Jellyfin.SocketSharp
{ {
int l2 = str2.Length; int l2 = str2.Length;
if (l2 == 0) if (l2 == 0)
{
return true; return true;
}
int l1 = str1.Length; int l1 = str1.Length;
if (l2 > l1) if (l2 > l1)
{
return false; return false;
}
var comparison = ignore_case ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; var comparison = ignore_case ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
return str1.IndexOf(str2, comparison) == str1.Length - str2.Length - 1; return str1.IndexOf(str2, comparison) == str1.Length - str2.Length - 1;
@ -493,7 +555,7 @@ namespace Jellyfin.SocketSharp
Encoding encoding; Encoding encoding;
StringBuilder sb; StringBuilder sb;
const byte HYPHEN = (byte)'-', LF = (byte)'\n', CR = (byte)'\r'; const byte LF = (byte)'\n', CR = (byte)'\r';
// 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
@ -520,7 +582,7 @@ namespace Jellyfin.SocketSharp
sb = new StringBuilder(); sb = new StringBuilder();
} }
string ReadLine() private string ReadLine()
{ {
// CRLF or LF are ok as line endings. // CRLF or LF are ok as line endings.
bool got_cr = false; bool got_cr = false;
@ -543,58 +605,86 @@ namespace Jellyfin.SocketSharp
} }
if (got_cr) if (got_cr)
{
sb.Length--; sb.Length--;
}
return sb.ToString(); return sb.ToString();
} }
static string GetContentDispositionAttribute(string l, string name) private static string GetContentDispositionAttribute(string l, string name)
{ {
int idx = l.IndexOf(name + "=\""); int idx = l.IndexOf(name + "=\"");
if (idx < 0) if (idx < 0)
{
return null; return null;
}
int begin = idx + name.Length + "=\"".Length; int begin = idx + name.Length + "=\"".Length;
int end = l.IndexOf('"', begin); int end = l.IndexOf('"', begin);
if (end < 0) if (end < 0)
{
return null; return null;
}
if (begin == end) if (begin == end)
return ""; {
return string.Empty;
}
return l.Substring(begin, end - begin); return l.Substring(begin, end - begin);
} }
string GetContentDispositionAttributeWithEncoding(string l, string name) private string GetContentDispositionAttributeWithEncoding(string l, string name)
{ {
int idx = l.IndexOf(name + "=\""); int idx = l.IndexOf(name + "=\"");
if (idx < 0) if (idx < 0)
{
return null; return null;
}
int begin = idx + name.Length + "=\"".Length; int begin = idx + name.Length + "=\"".Length;
int end = l.IndexOf('"', begin); int end = l.IndexOf('"', begin);
if (end < 0) if (end < 0)
{
return null; return null;
}
if (begin == end) if (begin == end)
return ""; {
return string.Empty;
}
string temp = l.Substring(begin, end - begin); string temp = l.Substring(begin, end - begin);
byte[] source = new byte[temp.Length]; byte[] source = new byte[temp.Length];
for (int i = temp.Length - 1; i >= 0; i--) for (int i = temp.Length - 1; i >= 0; i--)
{
source[i] = (byte)temp[i]; source[i] = (byte)temp[i];
}
return encoding.GetString(source, 0, source.Length); return encoding.GetString(source, 0, source.Length);
} }
bool ReadBoundary() private bool ReadBoundary()
{ {
try try
{ {
string line = ReadLine(); string line = ReadLine();
while (line == "") while (line == string.Empty)
{
line = ReadLine(); line = ReadLine();
}
if (line[0] != '-' || line[1] != '-') if (line[0] != '-' || line[1] != '-')
{
return false; return false;
}
if (!StrUtils.EndsWith(line, boundary, false)) if (!StrUtils.EndsWith(line, boundary, false))
{
return true; return true;
}
} }
catch catch
{ {
@ -603,25 +693,31 @@ namespace Jellyfin.SocketSharp
return false; return false;
} }
string ReadHeaders() private string ReadHeaders()
{ {
string s = ReadLine(); string s = ReadLine();
if (s == "") if (s.Length == 0)
{
return null; return null;
}
return s; return s;
} }
bool CompareBytes(byte[] orig, byte[] other) private static bool CompareBytes(byte[] orig, byte[] other)
{ {
for (int i = orig.Length - 1; i >= 0; i--) for (int i = orig.Length - 1; i >= 0; i--)
{
if (orig[i] != other[i]) if (orig[i] != other[i])
{
return false; return false;
}
}
return true; return true;
} }
long MoveToNextBoundary() private long MoveToNextBoundary()
{ {
long retval = 0; long retval = 0;
bool got_cr = false; bool got_cr = false;
@ -631,13 +727,18 @@ namespace Jellyfin.SocketSharp
while (true) while (true)
{ {
if (c == -1) if (c == -1)
{
return -1; return -1;
}
if (state == 0 && c == LF) if (state == 0 && c == LF)
{ {
retval = data.Position - 1; retval = data.Position - 1;
if (got_cr) if (got_cr)
{
retval--; retval--;
}
state = 1; state = 1;
c = data.ReadByte(); c = data.ReadByte();
} }
@ -650,7 +751,9 @@ namespace Jellyfin.SocketSharp
{ {
c = data.ReadByte(); c = data.ReadByte();
if (c == -1) if (c == -1)
{
return -1; return -1;
}
if (c != '-') if (c != '-')
{ {
@ -662,7 +765,9 @@ namespace Jellyfin.SocketSharp
int nread = data.Read(buffer, 0, buffer.Length); int nread = data.Read(buffer, 0, buffer.Length);
int bl = buffer.Length; int bl = buffer.Length;
if (nread != bl) if (nread != bl)
{
return -1; return -1;
}
if (!CompareBytes(boundary_bytes, buffer)) if (!CompareBytes(boundary_bytes, buffer))
{ {
@ -673,6 +778,7 @@ namespace Jellyfin.SocketSharp
data.Position++; data.Position++;
got_cr = false; got_cr = false;
} }
c = data.ReadByte(); c = data.ReadByte();
continue; continue;
} }
@ -690,12 +796,16 @@ namespace Jellyfin.SocketSharp
data.Position++; data.Position++;
got_cr = false; got_cr = false;
} }
c = data.ReadByte(); c = data.ReadByte();
continue; continue;
} }
data.Position = retval + 2; data.Position = retval + 2;
if (got_cr) if (got_cr)
{
data.Position++; data.Position++;
}
break; break;
} }
else else
@ -711,7 +821,9 @@ namespace Jellyfin.SocketSharp
public Element ReadNextElement() public Element ReadNextElement()
{ {
if (at_eof || ReadBoundary()) if (at_eof || ReadBoundary())
{
return null; return null;
}
var elem = new Element(); var elem = new Element();
string header; string header;
@ -734,19 +846,27 @@ namespace Jellyfin.SocketSharp
elem.Start = start; elem.Start = start;
long pos = MoveToNextBoundary(); long pos = MoveToNextBoundary();
if (pos == -1) if (pos == -1)
{
return null; return null;
}
elem.Length = pos - start; elem.Length = pos - start;
return elem; return elem;
} }
static string StripPath(string path) private static string StripPath(string path)
{ {
if (path == null || path.Length == 0) if (path == null || path.Length == 0)
{
return path; return path;
}
if (path.IndexOf(":\\") != 1 && !path.StartsWith("\\\\")) if (path.IndexOf(":\\", StringComparison.Ordinal) != 1
&& !path.StartsWith("\\\\", StringComparison.Ordinal))
{
return path; return path;
}
return path.Substring(path.LastIndexOf('\\') + 1); return path.Substring(path.LastIndexOf('\\') + 1);
} }
} }

@ -83,15 +83,15 @@ namespace Jellyfin.SocketSharp
private void ProcessContext(HttpListenerContext context) private void ProcessContext(HttpListenerContext context)
{ {
//InitTask(context, _disposeCancellationToken); var _ = Task.Run(async () => await InitTask(context, _disposeCancellationToken));
Task.Run(() => InitTask(context, _disposeCancellationToken));
} }
private void LogRequest(ILogger logger, HttpListenerRequest request) private static void LogRequest(ILogger logger, HttpListenerRequest request)
{ {
var url = request.Url.ToString(); var url = request.Url.ToString();
logger.LogInformation("{0} {1}. UserAgent: {2}", request.IsWebSocketRequest ? "WS" : "HTTP " + request.HttpMethod, url, request.UserAgent ?? string.Empty); logger.LogInformation("{0} {1}. UserAgent: {2}",
request.IsWebSocketRequest ? "WS" : "HTTP " + request.HttpMethod, url, request.UserAgent ?? string.Empty);
} }
private Task InitTask(HttpListenerContext context, CancellationToken cancellationToken) private Task InitTask(HttpListenerContext context, CancellationToken cancellationToken)
@ -196,7 +196,7 @@ namespace Jellyfin.SocketSharp
{ {
try try
{ {
ctx.Response.StatusCode = 200; ctx.Response.StatusCode = statusCode;
ctx.Response.Close(); ctx.Response.Close();
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)

@ -242,7 +242,6 @@ namespace Jellyfin.SocketSharp
return request.ContentType.StartsWith(contentType, StringComparison.OrdinalIgnoreCase); return request.ContentType.StartsWith(contentType, StringComparison.OrdinalIgnoreCase);
} }
public const string Xml = "application/xml";
private static string GetQueryStringContentType(IRequest httpReq) private static string GetQueryStringContentType(IRequest httpReq)
{ {
var format = httpReq.QueryString["format"]; var format = httpReq.QueryString["format"];
@ -250,22 +249,40 @@ namespace Jellyfin.SocketSharp
{ {
const int formatMaxLength = 4; const int formatMaxLength = 4;
var pi = httpReq.PathInfo; var pi = httpReq.PathInfo;
if (pi == null || pi.Length <= formatMaxLength) return null; if (pi == null || pi.Length <= formatMaxLength)
if (pi[0] == '/') pi = pi.Substring(1); {
return null;
}
if (pi[0] == '/')
{
pi = pi.Substring(1);
}
format = LeftPart(pi, '/'); format = LeftPart(pi, '/');
if (format.Length > formatMaxLength) return null; if (format.Length > formatMaxLength)
{
return null;
}
} }
format = LeftPart(format, '.').ToLower(); format = LeftPart(format, '.').ToLower();
if (format.Contains("json")) return "application/json"; if (format.Contains("json", StringComparison.OrdinalIgnoreCase))
if (format.Contains("xml")) return Xml; {
return "application/json";
}
if (format.Contains("xml", StringComparison.OrdinalIgnoreCase))
{
return "application/xml";
}
return null; return null;
} }
public static string LeftPart(string strVal, char needle) public static string LeftPart(string strVal, char needle)
{ {
if (strVal == null) return null; if (strVal == null)
{
return null;
}
var pos = strVal.IndexOf(needle); var pos = strVal.IndexOf(needle);
return pos == -1 return pos == -1
? strVal ? strVal
@ -283,14 +300,14 @@ namespace Jellyfin.SocketSharp
{ {
var mode = HandlerFactoryPath; var mode = HandlerFactoryPath;
var pos = request.RawUrl.IndexOf("?"); var pos = request.RawUrl.IndexOf("?", StringComparison.Ordinal);
if (pos != -1) if (pos != -1)
{ {
var path = request.RawUrl.Substring(0, pos); var path = request.RawUrl.Substring(0, pos);
this.pathInfo = GetPathInfo( this.pathInfo = GetPathInfo(
path, path,
mode, mode,
mode ?? ""); mode ?? string.Empty);
} }
else else
{ {
@ -307,18 +324,27 @@ namespace Jellyfin.SocketSharp
private static string GetPathInfo(string fullPath, string mode, string appPath) private static string GetPathInfo(string fullPath, string mode, string appPath)
{ {
var pathInfo = ResolvePathInfoFromMappedPath(fullPath, mode); var pathInfo = ResolvePathInfoFromMappedPath(fullPath, mode);
if (!string.IsNullOrEmpty(pathInfo)) return pathInfo; if (!string.IsNullOrEmpty(pathInfo))
{
return pathInfo;
}
//Wildcard mode relies on this to work out the handlerPath //Wildcard mode relies on this to work out the handlerPath
pathInfo = ResolvePathInfoFromMappedPath(fullPath, appPath); pathInfo = ResolvePathInfoFromMappedPath(fullPath, appPath);
if (!string.IsNullOrEmpty(pathInfo)) return pathInfo; if (!string.IsNullOrEmpty(pathInfo))
{
return pathInfo;
}
return fullPath; return fullPath;
} }
private static string ResolvePathInfoFromMappedPath(string fullPath, string mappedPathRoot) private static string ResolvePathInfoFromMappedPath(string fullPath, string mappedPathRoot)
{ {
if (mappedPathRoot == null) return null; if (mappedPathRoot == null)
{
return null;
}
var sbPathInfo = new StringBuilder(); var sbPathInfo = new StringBuilder();
var fullPathParts = fullPath.Split('/'); var fullPathParts = fullPath.Split('/');
@ -345,7 +371,10 @@ namespace Jellyfin.SocketSharp
} }
} }
} }
if (!pathRootFound) return null; if (!pathRootFound)
{
return null;
}
var path = sbPathInfo.ToString(); var path = sbPathInfo.ToString();
return path.Length > 1 ? path.TrimEnd('/') : "/"; return path.Length > 1 ? path.TrimEnd('/') : "/";
@ -400,7 +429,10 @@ namespace Jellyfin.SocketSharp
public static Encoding GetEncoding(string contentTypeHeader) public static Encoding GetEncoding(string contentTypeHeader)
{ {
var param = GetParameter(contentTypeHeader, "charset="); var param = GetParameter(contentTypeHeader, "charset=");
if (param == null) return null; if (param == null)
{
return null;
}
try try
{ {
return Encoding.GetEncoding(param); return Encoding.GetEncoding(param);
@ -423,7 +455,9 @@ namespace Jellyfin.SocketSharp
if (httpFiles == null) if (httpFiles == null)
{ {
if (files == null) if (files == null)
return httpFiles = new IHttpFile[0]; {
return httpFiles = Array.Empty<IHttpFile>();
}
httpFiles = new IHttpFile[files.Count]; httpFiles = new IHttpFile[files.Count];
var i = 0; var i = 0;

Loading…
Cancel
Save